You are on page 1of 373

Chapter 10:

Mathematical Topics from 3D Graphics


Fletcher Dunn
Valve Software

Ian Parberry
University of North Texas

3D Math Primer for Graphics & Game Development

What ou!ll "ee in This Chapter


This chapter #iscusses a number of mathematical issues that arise $hen creatin% 3D %raphics on a computer& It is #i'i#e# into ele'en sections& "ection 10&1 #iscusses the ren#erin% e(uation& "ection 10&) intro#uces the math of 'ie$in% in 3D& "ection 10&3 re'ie$s coor#inate spaces an# transforms& "ection 10&* loo+s at representin% %eometry& "ection 10&, is about material properties& "ection 10&- #iscusses the stan#ar# li%htin% mo#el& "ection 10&. loo+s at some common li%ht sources& "ection 10&/ e0amines s+eletal animation& "ection 10&1 intro#uces bump mappin%& "ection 10&10 is on the %raphics pipeline& "ection 10&11 %i'es some e0amples of pi0el an# 'erte0 sha#ers that implement some of the techni(ues co'ere# in this chapter&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

Wor# Clou#

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

"ection 10&1:

4o$ Graphics Wor+s

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

The 5en#erin% 6(uation


5en#erin% ans$ers the (uestion: 7For each pi0el on the screen8 $hat is its color9: That is8 7What color of li%ht is approachin% the camera from the #irection correspon#in% to this pi0el9: There are t$o cases to consi#er&
1& 6ither $e are loo+in% #irectly at a li%ht source8 an# li%ht tra'ele# #irectly from it to our eye8 or ;more commonly< )& =i%ht #eparte# from a li%ht source in some other #irection8 bounce# one or more times8 an# then entere# our eye&

The ren#erin% al%orithm also consists of t$o steps:


1. Visible surface determination & Fin# the surface that is closest to the eye in the #irection correspon#in% to the current pi0el& 2. Lighting& Determine $hat li%ht is emitte# an#>or reflecte# off of this surface in the #irection of the eye&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

?isible "urface Determination


There are ) common solutions to this problem&
1 !ay tracin": Follo$ li%ht rays bac+$ar#s& # Depth $%fferin": @lso +no$n as &'$%fferin"

More ne0tA

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

5ay Tracin%
5ather than follo$in% li%ht rays in the #irection that they tra'el from the emissi'e surfaces8 $e trace the rays bac+$ar#s8 so that $e can #eal only $ith the li%ht rays that matter: the ones that enter our eye from the %i'en #irection& We sen# a ray out from the eye in the #irection throu%h the center of each pi0el to see the first obBect in the scene this ray stri+es& Then $e compute the color that is bein% emitte#>reflecte# from that surface bac+ in the #irection of the ray&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

Depth Cuffer
Depth buffer is a solution to the occl%sion pro$lem: fi%urin% $hich trian%les are in front of $hich& Depth buffer has the same resolution as the screen& For each pi0el D x y E8 the correspon#in% #epth buffer entry FDxED yE stores the #epth of the pi0el& Gsually the ( coor#inate in clip space is use# H hence the name ('$%ffer&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' /

Depth Cuffer Gsa%e


@t the start of each frame8 initialiIe the #epth buffer to some far #istance ;in clip space this is 1&0<& 6ach time a pi0el D x y ( w E from clip space is to be #ra$n8 only #ra$ it if ( J FDxEDyEK if this happens8 then set FDxEDyE L ( Depth buffers come in #ifferent resolutions H for e0ample /Mbit or 1-Mbit&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 1

For$ar# an# Deferre# 5en#erin%


@t this point $e mi%ht also procee# to step ) of the ren#erin% al%orithm ;at least for this pi0el< an# up#ate the frame buffer $ith the color of the li%ht bein% emitte# or reflecte# from the surface that point& This is +no$n as forwar) ren)erin"& Deferre) ren)erin" is an ol# techni(ue that is becomin% popular a%ain #ue to the current location of bottlenec+s in the types of ima%es $e are pro#ucin% an# the har#$are $e are usin% to pro#uce them& @ #eferre# ren#erer uses8 in a##ition to the frame buffer an# the #epth buffer8 a##itional buffers8 collecti'ely +no$n as the G'$%ffer ;short for "eometry $%ffer<8 $hich hol#s e0tra information about the surface closest to the eye at that location& Compare# to a for$ar# ren#erer8 a #eferre# ren#erer follo$s our t$o step ren#erin% al%orithm a bit more literally&
1& First $e ren#er the scene into the GMbuffer8 essentially performin% only 'isibility #etermination8 fetchin% the material properties of the point that is seen by each pi0el8 but not yet performin% li%htin% calculations& )& 2o$ the %eometry of the scene can be #iscar#e#8 since $e ha'e e'erythin% $e nee# to perform the li%htin% calculations an# fill in the color 'alues in the frame buffer&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

10

=i%htin%
What Color is That NbBect9 It #epen#s on the color of the reflecte# li%ht& More e0actly8 it #epen#s on the fre(uency #istribution of the reflecte# li%ht& It is calle# the $i)irectional reflectance )istri$%tion f%nction8 or C5DF for short8 %i'en by:

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

11

C5DF
The 'alue of this function tells $hat fraction of the li%ht inci#ent at the point x from #irection is reflecte# in the out%oin% #irection @s in#icate# by the boldface an# hat8 OP mi%ht be a unit 'ector8 but more %enerally it can be any $ay of specifyin% a #irectionK polar an%les mi%ht be another choice& Different colors of li%ht are usually reflecte# #ifferently8 hence the #epen#ence on Q8 $hich is the color ;actually8 the $a'elen%th R more on this in a moment< of the li%ht&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1)

60ample: @ Green Call


@ %reen obBect is %reen an# not blue because it reflects inci#ent li%ht that is %reen more stron%ly than that of any other color& Perhaps 1,S of the blue li%ht is absorbe#8 an# only ,S reflecte# in 'arious #irections& White li%ht is actually a combination of all the #ifferent colors of li%ht8 so a %reen obBect essentially filters out colors other than %reen& If a #ifferent obBect reflecte# %reen li%ht in the same $ay as this ball8 but ,0S of the blue li%ht $ere absorbe# an# ,0S reflecte#8 $e mi%ht percei'e the obBect as teal& Nr if most of the li%ht $as absorbe#8 e0cept for a small amount of %reen li%ht8 then $e $oul# percei'e it as a #ar+er sha#e of %reen&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 13

C""DF
2e0t8 consi#er the #ifference bet$een shiny re# plastic an# #iffuse re# construction paper&

@ shiny surface reflects inci#ent li%ht much more stron%ly in one particular #irection compare# to others8 $hereas a #iffuse surface scatters li%ht more e'enly across all out%oin% #irections& @ perfect reflector $oul# reflect all the li%ht from one incomin% #irection in a sin%le out%oin% #irection8 $hereas a perfectly #iffuse surface $oul# reflect li%ht e(ually in all out%oin% #irections8 re%ar#less of the #irection of inci#ence& More complicate# phenomena can be e0presse# by %eneraliIin% the C5DF sli%htly& Translucence an# li%ht refraction can be easily incorporate#8 simply by allo$in% the #irection 'ectors to point bac+ into the surface& We mi%ht call this mathematical %eneraliIation a bi#irectional s%rface scatterin% #istribution function ;C""DF<& 6'en 'olumetric effects such as fo% an# subsurface scatterin% can be e0presse# by #roppin% the $or# 7surface: an# #efinin% a C"DF at any point in space& Ta+en at face 'alue8 these are ob'iously impractical theoretical tools8 but they can be useful in un#erstan#in% ho$ to #esi%n practical ones&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1*

The 2ormaliIation Constraint an# 4elmholtI 5eciprocity


There are criteria that a C5DF must satisfy in or#er to be physically plausible& For e0ample8 it #oesn!t ma+e sense for a ne%ati'e amount of li%ht to be reflecte# in any #irection& @lso8 it!s not possible for more li%ht to be reflecte# than is inci#ent8 althou%h the surface may absorb some ener%y so less li%ht may be reflecte# than is inci#ent& This is usually calle# the normali(ation constraint& @ less ob'ious principle obeye# by physical surfaces is *elmholt( reciprocity: if $e pic+ t$o arbitrary #irections8 the same fraction of li%ht shoul# be reflecte#8 no matter $hich is the inci#ent #irection an# $hich is the out%oin% #irection& In other $or#s8

In some boo+s an# papers you may see C5DF!s $here the t$o #irections are not labele# as 7in: an# 7out:8 assumin% 4elmholtI reciprocity&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1,

The =i%htin% 6(uation

More #etail on the ne0t sli#eA

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1-

The =i%htin% 6(uation

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1.

More on the Tric+y Inte%ral

"ymbol appears $here $e normally $oul# $rite the limits of inte%ration& This is inten#e# to mean 7sum o'er the hemisphere of possible incomin% #irections&: For each incomin% #irection 8 $e #etermine ho$ much ra#iance $as inci#ent in this incomin% #irection an# %ot scattere# in the out%oin% #irection The sum of all these contributions from all the #ifferent inci#ent #irections %i'es the total ra#iance reflecte# in the #irection In the real ;continuous< $orl# this is an inte%ral& In the 'irtual ;#iscrete< $orl#8 this $ill be a summation&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1/

More on the Tric+y Inte%ral

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

11

More on the Tric+y Inte%ral

The first term #enotes the ra#iance inci#ent from the #irection of The next term is simply the C5DF8 $hich tells us ho$ much of the ra#iance inci#ent from this particular #irection $ill be reflecte# in the out%oin% #irection $e care about& Finally8 $e ha'e the Lambert term8 $hich accounts for the fact that more inci#ent li%ht is a'ailable to be reflecte#8 per unit surface area8 $hen is perpen#icular to the surface than $hen at a %lancin% an%le to the surface& The 'ector is the out$ar# facin% surface normal8 an# the #ot pro#uct pea+s at 1 in the perpen#icular #irection8 an# trails off to Iero as the an%le of inci#ence becomes more %lancin%& More about this term later&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)0

5en#erin% as an Inte%ral 6(uation


The #ifficulty of ren#erin% comes from the fact the ren#erin% e(uation is an inte%ral e(uation: it states a relationship bet$een some un+no$n function +out the #istribution of li%ht on the surfaces in the scene8 in terms of its o$n inte%ral& It mi%ht not be apparent that the ren#erin% e(uation is recursi'e8 but +out actually appears on both si#es of the TL!& It appears in the e'aluation of +in8 $hich is precisely the e0pression $e set out to sol'e for each pi0el: What is the ra#iance inci#ent on a point8 from a %i'en #irection9 Thus to fin# the ra#iance e0itin% a point x8 $e nee# to +no$ all the ra#iance inci#ent at 0 from all #irections& Cut the ra#iance inci#ent on x is the same as the ra#iance lea'in% from all other surfaces 'isible to x8 in the #irection pointin% from the other surface to$ar#s x&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' )1

5en#erin% as an @ppro0imation
To ren#er a scene realistically8 $e must sol'e the ren#erin% e(uation8 $hich re(uires us to +no$ ;in theory< not only the ra#iance arri'in% at the camera8 but the entire #istribution of ra#iance in the scene in e'ery #irection at e'ery point& Clearly this is too much to as+ for a finite8 #i%ital computer8 since both the set of surface locations an# the set of potential inci#ent an# e0itin% #irections are infinite& The real art in creatin% soft$are for #i%ital ima%e synthesis is to allocate the limite# processor time an# memory most efficiently8 in or#er to ma+e the best possible appro0imation&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

))

Global Illumination
The basic li%htin% mo#el that $e $ill learn only accounts for #irect li%ht& It #oesn!t account for in#irect li%ht that bounce# off of one surface an# arri'e# at another& In other $or#s8 it only #oes one le'el of recursion in the ren#erin% e(uation& @ hu%e component of realistic ima%es is accountin% for the in#irect li%htK that is8 to more fully sol'e the ren#erin% e(uation& The 'arious metho#s for accomplishin% this are +no$n as "lo$al ill%mination techni(ues&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' )3

"ection 10&):

?ie$in% in 3D

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)*

"pecifyin% the Nutput Win#o$


We #on!t ha'e to ren#er to the entire screen& For e0ample8 in splitMscreen multiplayer %ames8 each player is %i'en a portion of the screen& Can ren#er to a file or to a te0ture& The o%tp%t win)ow is the portion of the output #e'ice $here our ima%e $ill be ren#ere#&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

),

"creen Coor#inates
5ecall that coor#inates start $ith ;08 0< at the topMleft of the screen8 Uy %oes #o$n8 Ux %oes across& ;winPosx8 winPosy< is the top left corner of the output screen in screen coor#inates& )ev!esx an# )ev!esy are the #imensions of the #e'ice in pi0els& win!esx an# win!esy are the #imensions of the $in#o$ in pi0els&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' )-

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

).

Pi0el @spect 5atio


The ratio of a pi0el!s hei%ht to its $i#th& Function of the #e'ice physical siIe 3 resolution& "iIe #oesn!t matter H it!s the ratio that matters&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)/

Physical "iIe of the De'ice


The 'ie$able area on most monitors has a ratio of *:3& @nother common ratio is 1-:1 on hi%hM #efinition tele'isions an# D?Ds& For e0ample8 on a stan#ar# *:3 monitor8 many common resolutions ha'e s(uare pi0el ratiosK 3)0 0 )*08 -*0 0 */08 /00 0 -008 10)* 0 .-/8 an# 1-00 0 1)00&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' )1

The Frustum
The view fr%st%m is the 'olume of space that is 'isible to the camera& "hape# li+e pyrami# $ith tip snippe# off& The 'ie$ frustum is boun#e# by si0 planes8 +no$n as the clip planes&
top8 left8 $ottom8 an# ri"ht planes near clip plane: pre'ents obBects from %ettin% too close to the camera far clip plane: far obBects e'entually %et too small to ren#er effecti'ely an# can be clippe# entirely&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 30

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

31

Camera Properties
Position Nrientation Fiel# of 'ie$ Foom @ctually8 the last t$o are interrelate#&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3)

Fiel# of ?ie$
The fiel# of 'ie$ is the an%le intercepte# by the 'ie$ frustum& @bbre'iation: fov& @ctually8 $e nee# both a horiIontal an# a 'ertical fo' an%le& 6%& 4oriIontal fo' is sho$n here&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 33

Foom
&oom measures the ratio of the apparent siIe of the obBect relati'e to a 10 fo'& =ar%er numbers Ioom in& "maller numbers Ioom out& For e0ample8 a Ioom of )&0 means that the obBect $ill appear t$ice as bi% on screen as $ith a 10 fo'& @ Ioom of 1&0 means a fo' of 10 & Fo'MIoom e(uation:
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 3*

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3,

3D Fooms
In 3D8 $e nee# t$o #ifferent Ioom 'alues8 one for the horiIontal an# one for the 'ertical& We can choose any t$o arbitrary 'alues8 but if $e #o not maintain a proper relationship bet$een the horiIontal an# 'ertical Ioom8 then the ren#ere# ima%e $ill appear stretche#&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 3-

Foom 5atio
In the formula on the ne0t sli#e: (oom refers to the cameraVs Ioom 'alues& winPhys refers to the physical $in#o$ siIe& win!es refers to the resolution of the $in#o$8 in pi0els& pixPhys refers to the physical #imensions of a pi0el& )evPhys refers to the physical #imensions of the output #e'ice& 5emember that $e usually #onVt +no$ the in#i'i#ual siIes8 but $e #o +no$ the ratio& )ev!es refers to the resolution of the output #e'ice&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 3.

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3/

"pecifyin% FN? or Fooms


Many ren#erin% pac+a%es allo$ you to specify the camera usin% only one fiel# of 'ie$ an%le ;or Ioom 'alue<& When you #o this8 they automatically compute the other 'alue for you8 assumin% you $ant uniform #isplay proportions& For e0ample8 you may specify the horiIontal fiel# of 'ie$8 an# they compute the 'ertical fiel# of 'ie$ for you& Nr 'iceM'ersa&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 31

Nrtho%raphic ProBection
We intro#uce# ortho%raphic proBection in Chapter ,8 but to briefly re'ie$8 in ortho%raphic proBection8 the lines of proBection are parallel8 rather than intersectin% at a sin%le point& There is no perspecti'e foreshortenin% in ortho%raphic proBectionK an obBect $ill appear the same siIe on the screen no matter ho$ far a$ay it is8 an# mo'in% the camera for$ar# or bac+$ar#s alon% the 'ie$in% #irection has no apparent effect so lon% as the obBects remain in front of the near clip plane&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

*0

Perspecti'e ProBection
2otice that $ith perspecti'e proBection8 parallel lines #o not remain parallel8 an# the closer %ri# s(uares are lar%er than the ones in the #istance&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' *1

Nrtho%raphic ProBection
With ortho%raphic proBection8 the %ri# lines remain parallel an# the %ri# s(uares are all the same siIe &

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

*)

Nrtho%raphic Fo'
For an ortho%raphic proBection8 it ma+es no sense to spea+ of the fiel# of 'ie$ as an an%le8 since the 'ie$ frustum is shape# li+e a bo08 not a pyrami#& 5ather than #efinin% the x an# y #imensions of the 'ie$ frustum usin% t$o an%les8 $e instea# %i'e t$o siIes: the physical $i#th an# hei%ht of the bo0& The Ioom 'alue has a #ifferent meanin% in ortho%raphic proBection compare# to perspecti'e& It is relate# to the physical siIe of the frustum bo0: (oom L )>si(e si(e L )>(oom
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' *3

Nrtho%raphic Foom 5atio


@s $ith perspecti'e proBections8 there are t$o #ifferent Ioom 'alues8 one for x an# one for y8 an# their ratio must be coor#inate# $ith the aspect ratio of the ren#erin% $in#o$& We #e'elope# the Ioom e(uation about , sli#es a%o $ith perspecti'e proBection in min#8 but it $or+s for ortho%raphic proBection too& 4ere it is a%ain:

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

**

"ection 10&3:

Coor#inate "paces

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

*,

Coor#inate "paces
NbBect 'ertices specifie# in o$,ect space& First transform these into worl) space
Weep trac+ of the obBect!s orientation an# position in $orl# space Calle# the mo)el transform or o$,ect transform&

Then transform into camera space


Gse in'erse of the camera!s obBect transform
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' *-

@ 2e$ Coor#inate "pace


Clip space& @W@ canonical view vol%me space& The matri0 that transforms 'ertices from camera space into clip space is +no$n as the clip matrix& The clip matri0 ser'es t$o primary functions:
Copy ( into w so that the homo%enous #i'ision by w performs a perspecti'e proBection&& "cale x8 y8 an# ( so that they can be compare# a%ainst w for clippin%&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' *.

4omo%enous ?ectors @%ain


5ecall that a *D homo%enous 'ector is mappe# to the correspon#in% physical 3D 'ector by #i'i#in% by w:

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

*/

ProBection 5e'isite#
Nne %oal of the clip matri0 is to %et the ri%ht 'alue into w so that the proper proBection occurs& We +no$ ho$ to proBect onto a plane perpen#icular to the (Ma0is an# ) units a$ay from the ori%in ;the plane ( L )&< The rectan%ular portion of the proBection plane insi#e the 'ie$ frustum %ets mappe# onto the screen&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

*1

?aryin% Focal Distance


If $e 'ary the focal #istance )8 $e mo'e the proBection plane for$ar# or bac+$ar#& In a real camera8 'aryin% the focal #istance causes the camera to Ioom in or out& 4o$e'er8 this #oes not occur $ith the proBection plane in a computer&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' ,0

Why 2ot9
In a real camera8 the ima%e proBection %ets bi%%er as $e increase the focal #istance8 an# the film stays the same siIe&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

,1

Why 2ot9 ;Continue#<


Insi#e a computer8 as $e increase the focal #istance ) the ima%e proBection %ets bi%%er the 7film: ;that is8 the rectan%ular portion of the proBection plane that is insi#e the 'ie$ frustum< $ill also %et bi%%er CutA

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

,)

Why 2ot9 ;Continue#<


The film has to be scale# to the screen& Cecause the film an# proBection increase by the same proportion8 there is no chan%e in the ren#ere# ima%e on the screen& The Ioom is controlle# completely by the shape of the 'ie$ frustum8 an# the 'alue of ) is not really si%nificant& "o $e can pic+ any 'alue for )& The most con'enient 'alue is ) L 1&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' ,3

Clip Matri0: First Cut


If this $as the only purpose of the clip matri08 it $oul# simply be:

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

,*

Perspecti'e Clip Matri0


Multiplyin% 'ector Dx8 y8 (8 1E by this matri08 then performin% the perspecti'e #i'i#e %i'es:

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

,,

Nrtho%raphic Clip Matri0


For ortho%raphic proBection $e #on!t $ant the perspecti'e #i'i#e& Ta+e ) L 08 so the clip matri0 is the i#entity matri0& "et w L 1&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

,-

Clip Planes
The secon# %oal of the clip matri0 is to scale the x8 y8 an# ( components such that the si0 clip planes ha'e a tri'ial form& Points are outsi#e the 'ie$ frustum planes accor#in% to the ine(ualities belo$ left& Points insi#e the 'ie$ frustum satisfy the ine(ualities belo$ ri%ht&

Cottom Top =eft 5i%ht 2ear Far


Chapter 10 2otes

y J Hw yXw x J Hw xXw ( J Hw (Xw

Hw Y x Y w Hw Y y Y w Hw Y ( Y w

3D Math Primer for Graphics 3 Game De'

,.

Foom in the Clip Matri0


To stretch thin%s to put the top8 left8 ri%ht8 an# bottom clip planes in place8 $e scale the x an# y 'alues by the Ioom 'alues of the camera& We #iscusse# ho$ to compute these 'alues in earlier& For the near an# far clip planes8 the ( coor#inate is biase# an# scale# such that at the near clip plane8 (>w L H18 an# at the far clip plane8 (>w L 1& =et (oomx an# (oomy be the horiIontal an# 'ertical Ioom 'alues8 an# let n an# f be the #istances to the near an# far clippin% planes&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

,/

Foomin% Clip Matri0


Then the follo$in% matri0 scales x8 y8 an# ( appropriately8 $hile simultaneously outputtin% the ( coor#inate into w&

This clip matri0 assumes a leftMhan#e# coor#inate system8 ro$ 'ectors on the left8 an# ( 'alues ran%in% from Hw to w from the near to far clip plane&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' ,1

DirectZ
DirectZ $ants the proBection matri0 such that ( ran%es from 0 to w& In other $or#s8 the clip space ine(ualities become:
Outside : y J Hw Inside: Hw Y x Y w Hw Y y Y w 0Y(Yw

Cottom Top =eft 5i%ht Near Chapter 10 2otes Far

yXw x J Hw xXw z<0 3D Math Primer for ( X w 3 Game De' Graphics

-0

DirectZ Clip Matri0


The clip matri0 for DirectZ becomes:

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

-1

Nrtho%raphic ?ersion
We can easily tell that the t$o matrices Bust %i'en are perspecti'e proBection matrices because the ri%htMhan# column is D08 08 18 0ET& What about ortho%raphic proBection9 The first an# secon# columns #onVt chan%e8 an# $e +no$ the fourth column $ill become D08 08 08 1ET& The thir# column8 $hich controls the output ( 'alue8 nee# to chan%e& "tart by assumin% the first set of con'entions8 that is $e $ish the output I 'alue to be scale# such that (>w ta+es on the 'alues H1 an# U1 at the near an# far clip planes8 respecti'ely&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

-)

Nrtho%raphic Clip Matrices


The clip matri0 is as follo$s8 $ith the DirectZ 'ersion on the ri%ht:

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

-3

NpenG= ?ersions
NpenG= has a leftMhan#e# coor#inate system an# column 'ectors& 4ence the clip matrices are a follo$s8 perspecti'e on the left an# ortho%raphic on the ri%ht&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

-*

The 2ear Clip Plane


2o$ that $e +no$ a bit about clip space8 $e can un#erstan# the nee# for the near clip plane& There is a sin%ularity precisely at the ori%in8 $here a perspecti'e proBection is not #efine#8 correspon#in% to a #i'ision by Iero in the perspecti'e #i'i#e& In practice8 this sin%ularity $oul# be e0tremely rare an# $e can +lu%e it by arbitrarily proBectin% the point to the center of the screen8 since puttin% the camera #irectly in a poly%on isnVt often nee#e# in practice&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

-,

N'erflo$ 5is+ $ith w


Cut Bust proBectin% poly%ons onto pi0els isnVt the only issue& @llo$in% for arbitrarily small ;but positi'e< 'alues of ( $ill result in arbitrarily lar%e 'alues for w& Depen#in% on the har#$are8 this can cause problems $ith perspecti'e correct rasteriIation& @nother potential problem area is #epth bufferin%& For practical reasons it is necessary to restrict the ran%e of the ( 'alues so that there is a +no$n minimum 'alue8 an# $e must accept the rather unpleasant necessity of a near clip plane&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

--

"creen "pace
Nnce $e ha'e clippe# the %eometry to the 'ie$ frustum8 it is proBecte# into screen space8 $hich correspon#s to actual pi0els in the frame buffer& 5emember that $e are ren#erin% into an output $in#o$ that #oes not necessarily occupy the entire #isplay #e'ice& 4o$e'er8 $e usually $ant our screen space coor#inates to be specifie# usin% coor#inates that are absolute to the ren#erin% #e'ice&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' -.

ProBectin% to "creen "pace


We must proBect the points from clip space to screen space to %enerate the correct )D coor#inates& The first thin% that happens is the homo%enous #i'ision by w& ;NpenG= calls the result of this #i'ision the normali(e) )evice coor)inates&< Then8 the x an# y coor#inates must be scale# to map into the output $in#o$ as follo$s ;see ne0t sli#e for #efinitions&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

-/

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

-1

Cut screeny has a Minus "i%n


@ (uic+ $or# about the ne%ation of the y component in the math ) sli#es a%o& This reflects a DirectZMstyle coor#inate con'ention $here ;080< is in the upper left corner& Gn#er these con'entions8 Uy points up in clip space8 but #o$n in screen space& In fact8 if $e continue to thin+ about U( pointin% into the screen8 then screen space actually becomes a ri%htMhan#e# coor#inate space8 e'en thou%h itVs leftMhan#e# e'ery$here else in DirectZ& In NpenG=8 the ori%in is in the lo$er left corner8 an# the ne%ation of the y coor#inate #oes not occur& @s $as alrea#y #iscusse#8 in NpenG=8 they choose a #ifferent place to intro#uce confusion8 by flippin% the (Ma0is bet$een eye space $here HI points into the screen8 to clip space $here U ( points into the screen&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

.0

wMbufferin%
"pea+in% of (8 $hat happens to clip(9 The precise #etails can 'ary8 but in %eneral itVs use# in some $ay for #epth bufferin%& @ tra#itional metho# is to ta+e the normaliIe# #epth 'alue clip(>clipw an# store this 'alue in the #epth buffer& @n alternati'e strate%y8 +no$n as wMbufferin% is to use clipw as the #epth 'alue& In most situations clipw is simply a scale# 'ersion of the cameraMspace ( 'alue8 an# thus by usin% clipw in the #epth buffer8 each 'alue has a linear relationship to the 'ie$in% #epth of the correspon#in% pi0el& This metho# can be attracti'e if the #epth buffer is fi0e#Mpoint $ith limite# precision8 because it sprea#s out the a'ailable precision more e'enly&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

.1

wMbufferin%
The tra#itional metho# of storin% clip(>clipw in the #epth buffer results in %reatly increase# precision up close8 at the e0pense of ;sometimes #rastically< re#uce# precision near the far clip plane& The precise #etails $ill #epen# on e0actly $hat sort of clip 'alues are use# for clippin%8 an# $hat sort of #epth 'alues %o into the #epth buffer& For e0ample8 in NpenG=8 the conceptual con'ention is for the 'ie$ frustum to contain H1 Y clip(>clipw Y 18 but this mi%ht not be optimal for #epth bufferin%& Dri'er 'en#ors must con'ert from the @PIVs conceptual con'entions to $hate'er is optimal for the har#$are& If the #epth buffer 'alues are store# in floatin% point8 this issue is much less important& @lso note that this #oesnVt $or+ for ortho%raphic proBection8 since an ortho%raphic proBection matri0 al$ays outputs w L 1&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

.)

Nther Gses for clipw


The clipw 'alue is usually not #iscar#e#& It ser'es the important purpose as the #enominator in the homo%enous #i'ision to normaliIe# #e'ice coor#inates& It is also nee#e# for proper perspecti'eMcorrect interpolation of te0ture coor#inates8 colors8 an# other 'erte0Mle'el 'alues #urin% rasteriIation& Nn mo#ern %raphics @PIs at the time of this $ritin%8 the con'ersion of 'erte0 coor#inates from clip space to screen space is #one for you& our 'erte0 sha#er $ill output coor#inates in clip space& The @PI $ill clip the trian%les to the 'ie$ frustum8 an# then proBect the coor#inates to screen space& Cut that #oesnVt mean that you $ill ne'er use the e(uations in your co#e& [uite often $e nee# to perform these calculations in soft$are for 'isibility testin%8 le'elMofM#etail selection8 etc&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' .3

More Coor#inate "paces


There are more spaces that are use# in computer %raphics& For e0ample8 a proBecte# li%ht mi%ht ha'e its o$n space8 $hich is essentially the same as camera space8 only it is from the perspecti'e that the li%ht loo+s onto the scene& This space is important $hen the li%ht proBects an ima%e ;sometimes calle# a "o$o8 $e!ll return to this later<8 an# also for sha#o$ mappin%8 to #etermine if a li%ht can see a %i'en point&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

.*

Tan%ent "pace
Tan"ent space is a local space on the surface of an obBect8 $ith one basis 'ector the surface normal8 an# the other t$o basis 'ectors locally tan%ent to the surface& This establishes a )D coor#inate space that is flat on the surface at a %i'en spot& Cy far the most common reason to establish such a coor#inate space is for bump mappin% an# relate# techni(ues& More on this $hen $e co'er te0ture mappin% later& Tan%ent space is sometimes calle# s%rface'local space&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

.,

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

.-

"ection 10&*:

Trian%le Meshes

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

..

C"G
Constructi'e soli# %eometry ;C"G< is a system for #escribin% an obBects shape usin% Coolean operators ;union8 intersection8 subtraction< on primiti'es& Within 'i#eo %ames8 C"G can be especially useful for rapi# prototypin% tools8 $ith the Gnreal en%ine bein% a notable e0ample&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' ./

Metaballs
Meta$alls are use# to mo#el or%anic shapes an# flui#s& @ three #imensional scalar #ensity function is #efine# surroun#in% each ball base# on the #istance to the center& Cy summin% the #ensity o'er all the balls8 $e can #efine an a%%re%ate #ensity function for any arbitrary point in space& The tric+ to metaballs is that the 'olume of the obBect is #efine# to be the re%ion $here the #ensity e0cee#s some nonIero threshol#& Thus balls ha'e a fuIIy re%ion aroun# them that e0ten#s outsi#e of the 'olume $hen the ball is in isolation& When t$o or more balls come to%ether8 the fuIIy re%ions interfere constructi'ely8 causin% a %raceful bon# of soli# 'olume to materialiIe in the re%ion in bet$een the balls8 $here no such soli# $oul# e0ist if either ball $ere there alone&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' .1

Trian%le Meshes
C"G8 metaballs8 an# other 'olumetric #escriptions are 'ery useful in particular realms8 but for ren#erin% ;especially realMtime ren#erin%< $e are intereste# in a #escription of the surface of the obBect8 an# sel#om nee# to #etermine $hether a %i'en point is insi#e or outsi#e this surface& In#ee#8 the surface nee# not be close# or e'en #efine a coherent 'olume& The most common surface #escription is the poly"on mesh& In certain circumstances itVs useful to allo$ the poly%ons that form the surface of the obBect to ha'e an arbitrary number of 'erticesK this is often the case in importin% an# e#itin% tools& For realMtime ren#erin%8 ho$e'er8 mo#ern har#$are is optimiIe# for trian"le meshes8 $hich are poly%on meshes $here e'ery poly%on is a trian%le&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

/0

Tri'ial Mesh 5epresentation


Nne 'ery strai%htfor$ar# $ay to store a trian%le mesh $oul# be to use an array of trian%les&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

/1

Trian%le Mesh Information


For some applications this tri'ial representation mi%ht be a#e(uate& 4o$e'er8 the term mesh implies connecti'ity bet$een a#Bacent trian%les8 an# this connecti'ity is not e0presse# in our tri'ial representation& There are three basic types of information in a trian%le mesh:
1& ?ertices )& 6#%es 3& Faces
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' /)

Trian%le Mesh Information


1. Vertices& 6ach trian%le has e0actly three 'ertices& 6ach 'erte0 may be share# by multiple trian%les& The valence of a 'erte0 refers to ho$ many faces are connecte# to the 'erte0& 2. Edges& @n e#%e connects t$o 'ertices& 6ach trian%le has three e#%es& In many cases8 each e#%e is share# by e0actly t$o faces8 but there are certainly e0ceptions& If the obBect is not close#8 an open e#%e $ith only one nei%hborin% face can e0ist& 3. aces& These are the surfaces of the trian%les& We can store a face as either a list of three 'ertices8 or a list of three e#%es&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

/3

Trian%le Mesh
@n in)exe) trian"le mesh consists of t$o lists: a list of 'ertices8 an# a list of trian%les& 6ach 'erte0 contains a position in 3D& We may also store other information at the 'erte0 le'el8 such te0ture mappin% coor#inates8 surface normals8 or li%htin% 'alues& @ trian%le is represente# usin% three in#ices into the 'erte0 list& Gsually the or#er that these 'ertices are liste# is si%nificant8 since $e may consi#er faces to ha'e a front an# a bac+ si#es& We $ill a#opt the con'ention that the 'ertices are liste# in cloc+$ise or#er $hen 'ie$e# from the front si#e& Nther information may also be store# at the trian%le le'el8 such as a precompute# normal of the plane containin% the trian%le8 surface properties ;such as a te0ture map<8 etc&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' /*

?erte0 "tructure

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

/,

Trian%le "tructure

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

/-

Trian%le Mesh "tructure

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

/.

60ample

4ere is ho$ a cube an# a pyrami# mi%ht be represente# as a poly%on mesh or a trian%le mesh& 2ote that in either case8 it is a sin%le mesh $ith 13 'ertices& The li%hter8 thic+er $ires sho$ the outlines of poly%ons8 an# the %reen $ires sho$ one $ay to a## a##itional e#%es to trian%ulate the poly%on mesh&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' //

60ample: ?erte0 Coor#inates


@ssumin% the ori%in is on the %roun# #irectly in bet$een the t$o obBects8 these mi%ht be the 'erte0 coor#inates&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

/1

60ample Mesh Table


The table on the ne0t sli#e sho$s the 'erte0 in#ices that $oul# form faces of this mesh8 either as a poly%on mesh8 or as a trian%le mesh& 2ote that the or#er of the 'ertices is si%nificant& While the 'ertices must be liste# in cloc+$ise or#er aroun# a face8 it #oesnVt matter $hich one is consi#ere# first 'erte0K they can be cycle# $ithout chan%in% the lo%ical structure of the mesh& For e0ample8 the (ua# formin% the cube top coul# e(ui'alently been %i'en as: \18 )8 38 0]8 \)8 38 08 1]8 or \38 08 18 )]&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 10

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

11

Implementation 2otes
@##itional #ata is almost al$ays store# per 'erte08 such as te0ture coor#inates8 surface normals8 basis 'ectors8 colors8 s+innin% #ata8 etc& @##itional #ata can also be store# at the trian%le le'el8 such as an in#e0 that tells $hich material to use for that face8 or the plane e(uation for the face& This is hi%hly useful for e#itin% purposes or in other tools that perform mesh manipulations& For realMtime ren#erin%8 ho$e'er8 $e sel#om store a##itional #ata at the trian%le le'el& In fact8 its 'ery common to not ha'e a struct Triangle at all8 an# simply represent the entire list of trian%les as an array li+e unsigned short []8 $here the len%th of the array is the number of trian%les times three& Trian%les $ith i#entical properties are %roupe# into batches so that an entire batch can be fe# to the GPG in this optimal format& We recommen# that you #on!t try to use the same mesh class for both ren#erin% an# e#itin%& The re(uirements are 'ery #ifferent8 an# a bul+ier #ata structure $ith more fle0ibility is best for use in tools etc&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1)

Implementation 2otes
2ote that in an in#e0e# trian%le mesh8 the e#%es are not store# e0plicitly8 but rather the a#Bacency information containe# in an in#e0e# trian%le list is store# implicitly: to locate share# e#%es bet$een trian%les8 $e must search the trian%le list& Nur ori%inal tri'ial 7array of trian%les: format #i# not ha'e any lo%ical connecti'ity information&

4o$e'er8 $hat!s surprisin% is that the 7e0tra: connecti'ity information containe# in the in#e0e# representation actually results in a re#uction of memory usa%e in most cases8 compare# to the flat metho#& The reason for this is that the information store# at the 'erte0 le'el8 $hich is #uplicate# in the tri'ial flat format8 is relati'ely lar%e compare# to a sin%le inte%er in#e0& ;@t the minimum8 $e most store a 3D 'ector position&< In meshes that arise in practice8 a typical 'erte0 has 3H- nei%hbors8 $hich means that the flat format #uplicates (uite a lot of #ata&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

13

Implementation 2otes
The simple in#e0e# trian%le mesh scheme is appropriate for many simple applications8 inclu#in% the 'ery important one of ren#erin%& 4o$e'er8 some operations on trian%le meshes re(uire a more a#'ance# #ata structure in or#er to be implemente# more efficiently& The basic problem is that the a#Bacency bet$een trian%les is not e0presse# e0plicitly an# must be e0tracte# by searchin% the trian%le list& Nther representation techni(ues ma+e this information a'ailable in constant time& Nne i#ea is to maintain an e#%e list e0plicitly&
6ach e#%e is #efine# by listin% the t$o 'ertices on the en#s& We also maintain a list of trian%les that share the e#%e& Then the trian%les can be 'ie$e# as a list of three e#%es rather than a list of three 'ertices8 so that they are store# as three in#ices into the e#%e list rather than the 'erte0 list&

@n e0tension of this i#ea is +no$n as the win"e) e)"e mo)el8 $hich also stores8 for each 'erte08 a reference to one e#%e that uses the 'erte0& The e#%es an# trian%les may be tra'erse# intelli%ently to (uic+ly locate all e#%es an# trian%les that use the 'erte0&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1*

"urface an# ?erte0 2ormals


@ s%rface normal is a unit 'ector that is perpen#icular to a surface& We mi%ht be intereste# in the normal of a %i'en face8 in $hich case the surface $e are intereste# in is the plane that contains the face& The surface normals for poly%ons can be compute# easily usin% cross pro#uct of t$o si#es& Care must be ta+en to %et the #irection ri%ht& ?erte0Mle'el normals are a bit tric+ier& First8 it shoul# be note# that strictly spea+in% there is not a true surface normal at a 'erte0 ;or an e#%e for that matter< since these locations mar+ #iscontinuities in the surface of the poly%on mesh& 5ather8 for ren#erin% purposes $e typically interpret a poly%on mesh as an appro0imation to some smooth surface&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1,

2ormals an# =i%htin%


The primary purpose of 'erte0 normals is for li%htin%& Practically e'ery li%htin% mo#el ta+es a surface normal at the spot bein% lit as an input& In#ee#8 the surface normal is part of the ren#erin% e(uation itself ;in the =ambert term< an# so it is al$ays a factor8 e'en if the C5DF #oes not #epen# e0plicitly on it& We only store normals at the 'ertices8 but yet $e nee# to compute li%htin% 'alues o'er the entire surface&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1-

What to Do9
If har#$are resources permit8 then $e can appro0imate the normal of the continuous surface correspon#in% to any point on a %i'en face by interpolatin% 'erte0 normals an# renormaliIin% the result& This is illustrate# belo$: a cross section of a cylin#er ;blac+ circle< is bein% appro0imate# by a he0a%onal prism ;blue<& Clac+ normals at the 'ertices are the true surface normals8 $hile the interior normals are bein% appro0imate# throu%h interpolation& The actual normals use# $oul# be the result of stretchin% these to unit len%th&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1.

Gourau# an# Phon% "ha#in%


Nnce $e ha'e a normal at a %i'en point8 $e can perform the full li%htin% e(uation per pi0el& This is +no$n as per'pixel sha)in"8 or Phon" sha)in"& @n alternate strate%y to perMpi0el sha#in%8 +no$n as Go%ra%) sha)in"8 is to only perform li%htin% calculations at the 'erte0 le'el8 an# then interpolate the results themsel'es8 rather than the normal8 across the face& This re(uires less computation8 an# is still #one on some systems such as the 2inten#o Wii&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1/

Phon% "ha#in%
4ere is perMpi0el li%htin% of cylin#ers $ith #ifferent numbers of si#es& @lthou%h the illusion brea+s #o$n on the en#s of the cylin#er8 this metho# of appro0imatin% a smooth surface can ma+e e'en a 'ery lo$Mresolution mesh loo+ smooth& Co'er up the en#s of the cylin#er8 an# e'en the ,Msi#e# cylin#er is remar+ably con'incin%&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

11

Cac+ to ?erte0 2ormals


If the mesh is %enerate# proce#urally8 for e0ample from a parametric cur'e# surface8 then the 'erte0 normals can be supplie#& Nr you may simply be han#e# the 'erte0 normals from the mo#elin% pac+a%e as part of the mesh& 4o$e'er8 sometimes the surface normals are not pro'i#e#8 an# $e must appro0imate them by interpretin% the only information a'ailable to us: the 'erte0 positions an# the trian%les& Nne tric+ that $or+s is to a'era%e the normals of the a#Bacent trian%les8 an# then reMnormaliIe the result8 as follo$s&&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 100

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

101

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

10)

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

103

@ Gotcha
@'era%in% face normals to compute 'erte0 normals is a trie#M an#Mtrue techni(ue that $or+s $ell in most cases& 4o$e'er8 there are a fe$ thin%s to $atch out for8 as this picture sho$s&

"ometimes the mesh is suppose# to ha'e a #iscontinuity8 an# if $e!re not careful8 this #iscontinuity coul# %et smoothe# out& Ta+e the 'ery simple e0ample of a bo0& There shoul# be a sharp li%htin% #iscontinuity at its e#%es& 4o$e'er8 if $e use 'erte0 normals compute# from the a'era%e of the surface normals8 then there is no li%htin% #iscontinuity&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

10*

Detachin% Faces
The basic problem is that the surface #iscontinuity at the bo0 e#%es cannot be properly represente# because there is only one normal store# per 'erte0& The solution to this problem is to )etach the facesK in other $or#s8 #uplicate the 'ertices alon% the e#%e $here there is a true %eometric #iscontinuity& In #oin% so8 $e create a #iscontinuity to pre'ent the 'erte0 normals from bein% a'era%e#& @t this point8 the faces are no lon%er lo%ically connecte#8 but this seam in the topolo%y of the mesh #oesn!t cause a problem for many important tas+s such as ren#erin% an# ray tracin%& 2e0t8 cubes $ith $el#e# 's& #etache# facesA

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

10,

Cube $ith Wel#e# Faces

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

10-

Cube $ith Detache# Faces

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

10.

Cillboar#s
@n e0treme 'ersion of this situation occurs $hen t$o faces are place# bac+MtoM bac+8 $hich is calle# a $ill$oar)& "uch infinitely thin #oubleMsi#e# %eometry can arise $ith folia%e an# cloth& In this case8 since the normals are e0actly opposite8 a'era%in% them pro#uces the Iero 'ector8 $hich cannot be normaliIe#& The simplest solution is to #etach the faces so that the 'erte0 normals $ill not a'era%e to%ether& Nr if the front an# bac+ si#es are mirror ima%es8 the t$o sin%leMsi#e# poly%ons can be replace# by one #oubleMsi#e# one& This re(uires special treatment #urin% ren#erin% to #isable bac+face cullin% ;more later< an# #ealin% $ith the normal intelli%ently in the li%htin% e(uation&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

10/

When Gn$ei%hte# @'era%in% is Wron%


@ more subtle problem is that un$ei%hte# a'era%in% is biase# to$ar#s lar%e numbers of trian%les $ith the same normal& For e0ample8 consi#er 'erte0 at in#e0 1 in the leftmost picture& This 'erte0 is a#Bacent to t$o trian%les on the top of the cube ;secon# picture8 re# an# yello$ trian%les<8 but only one trian%le on the ri%ht si#e ;thir# picture8 %reen trian%le < an# one trian%le on the bac+ si#e ;ri%htmost picture8 pin+ trian%le<& The 'erte0 normal compute# by a'era%in% the trian%le normals is biase# because the top face normal effecti'ely %ets t$ice as many 'otes as the si#e face normals&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

101

6#%e Turnin%
Cut this topolo%y is the result of an arbitrary #ecision as to $here to #ra$ the e#%es to trian%ulate the faces of the cube& For e0ample8 if $e $ere to trian%ulate the top face by #ra$in% an e#%e bet$een 'ertices 0 an# ) ;this is +no$n as t%rnin" the e#%e8 see pictures at ri%ht<8 all of the normals on the top face $oul# chan%e& Techni(ues e0ist to #eal $ith this problem8 such as $ei%hin% the contribution from each a#Bacent face base# on the interior an%le a#Bacent to the 'erte0& Cut it!s often i%nore# in practice& Most of the really terrible e0amples are contri'e# ones li+e this8 $here the faces shoul# be #etache#8 any$ay& Furthermore8 the normals are an appro0imation to be%in $ith8 an# ha'in% a sli%htly perturbe# normal is often #ifficult to tell 'isually&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

110

Co'ariant an# Contra'ariant ?ectors


"urface normals cannot be transforme# usin% the same matri0 that is use# to transform positions& This is an entirely separate issue from the fact that normals shoul# not be translate# li+e positions& The reason for this is that normals are covariant vectors& ?ectors such as position an# 'elocity are sai# to be contravariant: If $e scale the coor#inate space use# to #escribe the 'ector8 the coor#inates $ill respon# in the opposite #irection& If $e use a coor#inate space $ith a lar%er scale ;for e0ample8 usin% meters instea# of feet< the coor#inates of a contra'ariant 'ector respon# to the contrary by becomin% smaller& 2otice that this is all about scaleK translation an# rotation are not part of the #iscussion& 2ormals an# other types of %ra#ients8 +no$n as #ual 'ectors8 #o not beha'e li+e this&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

111

60ample of Contra'ariance
Ima%ine $e stretch a )D obBect such as a circle horiIontally& 2otice that the normals be%in to turn to point more 'erticallyRthe horiIontal coor#inates of the normals are #ecreasin% in absolute 'alue8 as the horiIontal coor#inates of the positions are increasin%& @ stretchin% of the obBect ;obBect %ettin% bi%%er $hile coor#inates space stays the same< has the same effect as scalin% #o$n the coor#inate space $hile hol#in% the obBect at the same siIe& The coor#inates of the normal chan%e in the same #irection as the scale of the coor#inate space8 $hich is $hy they are calle# co'ariant 'ectors&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

11)

Transformin% "urface 2ormals


To properly transform surface normals $e must use the in'erse transpose of the matri0 use# to transform positionsK that is8 the result of transposin% an# in'ertin% the matri0& This is sometimes #enote# MHT 8 since it #oesn!t matter if $e transpose first8 or in'ert first8 that is8 ;MH1<TL ;MT< H1 If the transform matri0 #oesn!t contain any scale ;or s+e$<8 then the matri0 must be orthonormal8 an# thus the in'erse transpose is simply the same as the ori%inal matri08 an# $e can safely transform normals usin% it& If the matri0 contains uniform scale8 then $e can still i%nore this8 but $e must reMnormaliIe the normals after transformin% them& If the matri0 contains nonMuniform scale ;or s+e$<8 then to properly transform the normals $e must use the reMnormaliIe# in'erse transpose transform matri0&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 113

"ection 10&,:

Te0ture Mappin%

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

11*

Te0ture Mappin%
@ te0ture map is a bitmap ima%e that is paste# onto the surface of an obBect& @ texel is a sin%le pi0el in a te0ture map& Planar mappin" proBects the te0ture ortho%raphically onto the mesh& Spherical8 cylin)rical8 an# c%$ic mappin% are 'arious metho#s of $rappin% the te0ture aroun# the obBect& The +ey point is that8 at each point on the surface of the mesh8 $e can obtain te0ture mappin% coor#inates8 $hich #efine the )D location in the te0ture map that correspon#s to this 3D location& Tra#itionally8 these coor#inates are assi%ne# the 'ariables ; %8 v<8 $here % is the horiIontal coor#inate an# v is the 'ertical coor#inate8 thus te0ture mappin% coor#inates are often calle# UV coor)inates or simply UV.s&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 11,

G? Coor#inates
@lthou%h bitmaps come in #ifferent siIes8 G? coor#inates are normaliIe# such that the mappin% space ran%es from 0 to 1 o'er the entire $i#th ;%< or hei%ht ;v< of the ima%e8 rather than #epen#in% on the ima%e #imensions& The ori%in of this space is either in the upperMleftM han# corner of the ima%e ;DirectZ<8 or in the lo$er left corner ;NpenG=<& We!ll use DirectZ con'entions&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

11-

60ample

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

11.

5epeatin% an# Clampin%


G? coor#inates outsi#e of the ran%e 0 to 1 are allo$e#& "uch coor#inates are interprete# in a 'ariety of $ays& The most common a##ressin% mo#es are repeat ;also +no$n as tile or wrap< an# clamp& When repeatin" is use#8 the inte%er portion is #iscar#e# an# only the fractional portion is use#8 causin% the te0ture to repeat8 as sho$n in the top ima%e& Gn#er clampin"8 $hen a coor#inate outsi#e the ran%e 0 to 1 is use# to access a bitmap8 it is clampe# in ran%e& This has the effect of strea+in% the e#%e pi0els of the bitmap out$ar#s8 as #epicte# in the bottom ima%e&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

11/

More on Te0ture Mappin%


There are other options supporte# on some har#$are8 such as mirror8 $hich is similar to repeat e0cept that e'ery other tile is mirrore#& This can be useful because it %uarantees that no seam $ill e0ist bet$een a#Bacent tiles& Nn most har#$are8 the a##ressin% mo#e can be set for the %M an# vMa0es in#epen#ently& It!s important to un#erstan# that these rules are applie# at the last moment8 $hen the coor#inates are use# to in#e0 into the te0ture& The coor#inates at the 'erte0 are not limite# or processe# in any $ayK other$ise they coul# not be interpolate# across the face properly&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 111

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1)0

"ection 10&-:

The "tan#ar# =i%htin% Mo#el

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1)1

The "tan#ar# =i%htin% Mo#el


In the ren#erin% e(uation8 the C5DF #escribes the scatterin% #istribution for li%ht of a %i'en fre(uency an# #irection of inci#ence& The #ifferences in #istributions bet$een #ifferent surfaces is precisely $hat causes those surfaces to loo+ #ifferent from one another& Most C5DF!s are e0presse# in a computer by some sort of formula8 $here certain numbers in the formula are a#Buste# to match the #esire# material properties& The formula itself is often calle# a li"htin" mo)el8 an# the particular 'alues %oin% into the formula come from the material assi%ne# to the surface& It is common for a %ame en%ine to only use a han#ful of li%htin% mo#els8 e'en thou%h the materials in the scene may be (uite #i'erse an# there may be thousan#s of #ifferent C5DF!s& In#ee#8 Bust a fe$ years a%o almost all realMtime ren#erin% $as #one $ith a sin%le li%htin% mo#el8 an# the practice is not uncommon to#ay&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 1))

=e%acy Feature8 or 5eliable "tan#ar#9


This li%htin% mo#el $as so ubi(uitous that it $as har#$ire# into the ren#erin% @PIs of NpenG= an# DirectZ& @lthou%h these ol#er parts of the @PI ha'e effecti'ely become le%acy features8 the stan#ar# mo#el is still commonly use# in the more %eneral frame$or+ of sha#ers an# %eneric constants an# interpolants& The %reat #i'ersity an# fle0ibility a'ailable is usually use# to #etermine the best $ay to fee# the parameters into the mo#el ;for e0ample8 by #oin% multiple li%hts at once8 or #oin% all the li%htin% at the en# $ith #eferre# sha#in%<8 rather than usin% #ifferent mo#els& @t the time of $ritin% the most popular 'i#eo %ame console is the 2inten#o Wii8 $hich has har#$ire# support for the stan#ar# mo#el& This is a 'ery important lesson& 5ealistic %raphics mi%ht be important to har#core %amers8 but for a more %eneral au#ience they are not nearly as important as $e once belie'e#&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 1)3

Casic Concepts of the "tan#ar# =i%htin% Mo#el


Phon% CuiMTuon% intro#uce# the basic concepts behin# the stan#ar# li%htin% mo#el in 11.,& Cac+ then the focus $as on a fast $ay to mo#el #irect reflection& While certainly researchers un#erstoo# the importance of in#irect li%ht8 it $as a lu0ury that coul# not yet be affor#e#& Thus $hile the stan#ar# ren#erin% e(uation is an e(uation for the ra#iance out%oin% from a point in any particular #irection8 the only out%oin% #irection that mattere# in those #ays $ere the #irections that pointe# to the eye& "imilarly8 $hile the ren#erin% e(uation consi#ers inci#ent li%ht from the entire hemisphere surroun#in% the surface normal8 if $e i%nore in#irect li%ht8 then $e nee# not cast about in all inci#ent #irections: We nee# only consi#er those inci#ent #irections that point to$ar#s a li%ht source&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1)*

The "tan#ar# =i%htin% 6(uation


Gi'es the color 'ector for a sin%le pi0el& It!s the sum of three components:
clit L cemis U cspec U c)iff U cam$
cemis is the emissive component cspec is the spec%lar component c)iff is the )iff%se component cam$ is the am$ient component

c is short for contri$%tion or component& There!s not much to say about cemis/ it!s a fi0e# 'alue&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1),

=i%htin% an# Dar+enin%


clit is the resultin% lit color 'alue8 as oppose# to an %nlit color 'alue8 $hich has full li%htin% intensity& Contrary to normal use of the 'erb li"ht8 in computer %raphics the term li"htin" normally refers to the process of ta+in% a color ;e%& from a te0ture< an# )ar0enin" it& Factors affectin% li%htin%:
The properties of the surface of the obBect itself ;+no$n as material properties<& The location an# orientation of the surface bein% lit& The orientation is usually #escribe# usin% a surface normal& The properties of the 'arious li%hts shinin% on the obBect& The location of the 'ie$er&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 1)-

The "pecular Component


The spec%lar component is $hat %i'es surfaces a shiny appearance& Mo#els #irect reflection of li%ht from a li%ht source to the eye& The amount of specular li%htin% #epen#s on
the obBect the li%hts the 'ie$er

T$o important 'ariants: Phon" an# 1linn&


Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 1).

The Phon% =i%htin% Mo#el


The Phon% specular component #epen#s upon these inputs:
n8 the surface normal& ! a unit 'ector that points to$ar# the 'ie$er& l a unit 'ector that points to$ar# the li%ht source& For #irectional li%hts8 l is constant& Properties of the material an# the li%ht&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 1)/

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1)1

Phon% Parameters
r: the reflection 'ector8 the result of reflectin% l about n& : the an%le bet$een r an# !& m"ls: the "lossiness of the material8 also +no$n as the Phon" exponent& mspec: the material!s spec%lar color& sspec: the specular color of the li%ht source&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 130

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

131

The 5eflection ?ector


r is %i'en by );n^l<nHl& Why is this9 It!s easier to see $ith pictures&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

13)

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

133

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

13*

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

13,

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

13-

Glossiness
m"ls controls ho$ $i#e the reflection hotspot is& @ smaller 'alue results in a lar%er8 more %ra#ual falloff from the hotspot8 an# a lar%er 'alue results in a 'ery ti%ht hotspot $ith sharp falloff& Perfectly reflecti'e surfaces8 such as %lass8 $ill ha'e a 'ery hi%h 'alue for m"lsRonly a perfectly reflecte# ray of li%ht $ill enter the eye& "hiny surfaces that are not perfect reflectors8 li+e the surface of an apple8 mi%ht ha'e lo$er 'alues8 resultin% in a lar%er hotspot&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

13.

"hininess
While m"ls controls the siIe of the hotspot8 the shininess mspec controls its intensity& mspec8 is usually a %rayscale 'alue that is constant for the entire material& 4i%hly reflecti'e surfaces $ill ha'e a hi%her 'alue for mspec8 an# more matte surfaces $ill ha'e a lo$er 'alue& If #esire#8 a "loss map may be use# to control the bri%htness of the reflections usin% a bitmap8 much as a te0ture map controls the color of an obBect&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

13/

Gobos
Furthermore8 e'en i%norin% attenuation8 li%hts #on!t ha'e to ha'e a constant color& For rectan%ular spotli%hts8 $e mi%ht #etermine the color from a "o$o8 $hich is a proBecte# bitmap ima%e& @ colore# %obo mi%ht be use# for a li%ht shinin% throu%h a staine# %lass $in#o$8 or an animate# %obo coul# be use# to fa+e sha#o$s of spinnin% ceilin% fans or trees blo$in% in the $in#& The term "o$o ori%inate# from the $orl# of theater8 $here it refers to a mas+ or filter place# o'er a spotli%ht use# to create a colore# li%ht or special effect8 an# it!s the term $e!ll use& Gobos are 'ery useful for fa+in% special li%htin% situations& If canonical li%hts are not #irectly supporte#8 they can be implemente# $ith an appropriately #esi%ne#circular %obo
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 131

"pecular =i%ht Color


sspec controls the basic color an# intensity of the li%ht& For rectan%ular spotli%hts8 this 'alue may come from a proBecte# li%ht map& sspec is often e(ual to the li%ht!s #iffuse color8 s)iff&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1*0

The Phon% "pecular 6(uation

"ince cos may be ne%ati'e8 $e clamp cspec to 0 if necessary&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1*1

Deconstructin% the Phon% "pecular 6(uation

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1*)

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1*3

NptimiIin% Phon%
If the #istance to the 'ie$er is lar%e relati'e to the siIe of an obBect8 then ! may be compute# once an# then consi#ere# constant for an entire obBect& The same is true for a li%ht source an# the 'ector l& ;In fact8 for #irectional li%hts8 l is al$ays constant&< 4o$e'er8 since n is not constant8 $e must still compute r8 a computation $hich $e $oul# li+e to a'oi# if possible.

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1**

The Clinn Mo#el


The Clinn mo#el uses h8 the 7half$ay: 'ector bet$een ! an# l8 $hich is compute# by a'era%in% ! an# l an# then normaliIin% the result:

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1*,

The Clinn "pecularity 6(uation

Why are $e #oin% this9 h is sli%htly easier to compute than r& If the 'ie$er an# li%ht source are far enou%h a$ay from the obBect to be consi#ere# a constant8 then h is a constant an# only nee#s to be compute# once&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1*-

The Diffuse Component


"pecular li%ht accounts for li%ht that reflects perfectly off the surface into the eye& Diff%se li"ht mo#els reflections that are scattere# in ran#om #irections #ue to the rou%h ;7#iffuse:< nature of the obBect!s surface&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1*.

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1*/

Diffuse =i%htin%
Diffuse li%htin% #oes not #epen# on the location of the 'ie$er because the reflections are scattere# ran#omly& 4o$e'er8 the position of the li%ht source relati'e to the surface is important& If count the photons of li%ht that hit the surface of the obBect an# ha'e a chance of reflectin% into the eye8 a surface perpen#icular to the rays of li%ht recei'es more rays per unit area than a surface oriente# at a more %lancin% an%le&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1*1

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1,0

Diffuse =i%htin% Mo#el


The #iffuse component #epen#s upon these inputs: n8 the surface normal& l a unit 'ector that points to$ar# the li%ht source& For #irectional li%hts8 l is constant& Properties of the material an# the li%ht&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1,1

Diffuse =i%htin% Parameters


m)iff: the material!s #iffuse color& This is the 'alue that most people thin+ of $hen they thin+ of the color of an obBect ;usually comes from a te0ture map<& s)iff: the #iffuse color of the li%ht source8 $hich is often e(ual to the li%ht!s specular color8 sspec&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1,)

=ambert!s =a$
Diffuse li%htin% obeys +am$ert.s law: intensity of reflecte# li%ht is proportional to the cosine of the an%le bet$een the surface normal an# the rays of li%ht& We $ill compute this cosine usin% the #ot pro#uct as usual:

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1,3

@mbient =i%htin%
"pecular an# #iffuse li%htin% mo#el li%ht rays that tra'el #irectly from the li%ht source to the surface of the obBect an# are then reflecte# to the 'ie$er& 4o$e'er8 in the real $orl#8 li%ht often bounces off of multiple interme#iate obBects before hittin% an obBect an# reflectin% to the eye& There!s li%ht e'ery$here H this is calle# am$ient li"htin"&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1,*

@mbient =i%htin% Parameters


mam$: the material!s ambient color& This is almost al$ays the same as the #iffuse color m)iff ;usually #efine# usin% a te0ture map<& gam$ is the %lobal ambient li%ht 'alue for the entire scene& The ambient li%htin% e(uation is simple:

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1,,

=i%ht @ttenuation
=i%ht intensity falls off $ith #istance from the li%ht source H in'ersely proportional to the s(uare of the #istance& This is calle# atten%ation& To sa'e the multiplication8 li%ht attention is often appro0imate# usin% linear interpolation&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 1,-

What Gets @ttenuate#9


@ttenuation can be applie# to point li%hts an# spotli%hts R#irectional li%hts are not attenuate#& @n a##itional attenuation factor is use# for spotli%hts& *otspot falloff attenuates li%ht as $e mo'e closer to the e#%e of the cone& Nnce $e ha'e compute# the intensity multiplier i8 it is applie# to the #iffuse an# specular components& @mbient li%ht is not attenuate#&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1,.

Falloff Distances
=inear interpolation of li%ht attenuation uses t$o parameters& )min is the minim%m falloff )istance& @t #istances ) )min8 the li%ht has intensity 1&0 )max is the maxim%m falloff )istance& @t #istances ) )max8 the li%ht has intensity 0&0 @t #istances )min ) )max the li%ht intensity 'aries $ith 1>)

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1,/

The Full =i%htin% 6(uation

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1,1

60ample
The ear is lit Bust as bri%ht as the nose8 e'en thou%h it is actually in the sha#o$ of the hea#& This is a conse(uence of usin% a local li%htin% mo#el& For sha#o$s8 a more a#'ance# techni(ue must be use#&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1-0

"pecular

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1-1

Diffuse

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1-)

@mbient

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1-3

@mbient
Without ambient li%ht the si#e of the hea# that is facin% a$ay from the li%ht is completely blac+& In or#er to li%ht the bac+ si#e of obBects8 you must use ambient li%ht& Nr8 you can place enou%h li%hts in your scene so that e'ery surface is lit #irectly&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 1-*

2otes
With only ambient li%htin%8 Bust the silhouette is 'isible& =i%htin% is an e0tremely po$erful 'isual cue that ma+es the obBect appear 3D& The solution to this 7cartoon: effect is8 a%ain8 to place a sufficient number of li%hts in the scene so that e'ery surface is lit #irectly&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 1-,

Multiple =i%hts
"um the specular an# #iffuse components from each li%ht source&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1--

"ha#in% Trian%les
Phon" sha)in": compute li%htin% e(uation for e'ery pi0el& Go%ra%) sha)in": compute li%htin% e(uation at 'ertices an# interpolate across trian%le& 2lat sha)in": compute a sin%le li%htin% 'alue for the $hole trian%le&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 1-.

Compare an# Contrast


Phon" sha)in" loo+s best but is slo$& 2lat sha)in" loo+s $orst but is fast& Go%ra%) sha)in" is a compromise& ;It neither loo+s %oo# nor is fast&<

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1-/

Flat "ha#in%

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1-1

Gourau# "ha#in%

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1.0

Phon% "ha#in%

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1.1

Problem $ith Gourau# "ha#in%


The specular hi%hli%hts #on!t loo+ as %oo#& The problem is that no 'alue in the mi##le of the trian%le can be lar%er than the lar%est 'alue at a 'erte0& "o hi%hli%hts can only occur at a 'erte0& Partial $or+Maroun#: use more trian%les&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 1.)

Gsin% the =i%htin% 6(uation for Gourau# "ha#in%


m)iff is not a 'erte0Mle'el material property: usually #efine# by a te0ture map& To ma+e the stan#ar# li%htin% e(uation more suitable for use in an interpolate# li%htin% scheme8 it must be manipulate# to isolate m)iff& 5easonable assumption: mam$L m)iff&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 1.3

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1.*

!spec an# !#iff

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1.,

?erte0 an# Pi0el =e'el Properties

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1.-

@pply interpolate# 'alues across pi0els&


Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 1..

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1./

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1.1

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1/0

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1/1

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1/)

"ection 10&.:

=i%ht "ources

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1/3

"tan#ar# =i%ht "ources


Point li%hts Directional li%hts "pot li%hts @mbient li%ht

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1/*

Point =i%hts
@ point li%ht source represents li%ht at a sin%le point that ra#iates in all #irections& @lso calle# omni or spherical li%hts& @ttributes:
Position Color ;combines hue an# intensity<& Falloff ra#ius8 ;siIe of the sphere illuminate# by the li%ht<

Gse# for li%ht bulbs8 lamps8 fires8 etc&


Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 1/,

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1/-

Directional =i%hts
@ )irectional li%ht represents li%ht emanatin% from a point in space sufficiently far a$ay that all the rays of li%ht point in the same #irection& @ttributes:
Color Nrientation
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 1/.

"pot =i%hts
@ spot li%ht is use# to represent li%ht from a specific source in a specific #irection& The shape of the lit area is either a cone or pyrami#& @ttributes:
Position Nrientation Color Falloff #istance
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 1//

Conical "potli%ht
@ conical spotli%ht has a circular bottom& @##itional attributes:
Falloff an%le 4otspot an%le

Gse# for flashli%hts8 hea#li%hts8 spotli%hts8 etc.

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

1/1

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

110

5ectan%ular "potli%ht
@ rectan%ular spotli%ht forms a pyrami# rather than a cone& 5ectan%ular spotli%hts are especially interestin% because they are use# to proBect an ima%e& @n ima%e proBecte# in this manner is +no$n as a pro,ecte) li"ht map&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 111

@mbient =i%htin%
@ constant li%ht le'el for the entire scene representin% in#irect li%htin%& Without ambient li%ht8 obBects that are in sha#o$ $oul# be completely blac+& @ttributes:
Color

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

11)

=i%ht @ttenuation
=i%ht intensity falls off $ith #istance from the li%ht source H in'ersely proportional to the s(uare of the #istance& This is calle# atten%ation& To sa'e the multiplication8 li%ht attention is often appro0imate# usin% linear interpolation&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 113

What Gets @ttenuate#9


@ttenuation can be applie# to point li%hts an# spotli%hts R#irectional li%hts are not attenuate#& @n a##itional attenuation factor is use# for spotli%hts& *otspot falloff attenuates li%ht as $e mo'e closer to the e#%e of the cone& Nnce $e ha'e compute# the intensity multiplier i8 it is applie# to the #iffuse an# specular components& @mbient li%ht is not attenuate#&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

11*

Falloff Distances
=inear interpolation of li%ht attenuation uses t$o parameters& )min is the minim%m falloff )istance& @t #istances ) )min8 the li%ht has intensity 1&0 )max is the maxim%m falloff )istance& @t #istances ) )max8 the li%ht has intensity 0&0 @t #istances )min ) )max the li%ht intensity 'aries $ith 1>)

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

11,

The =i%ht @ttenuation Formula


The li%ht intensity at #istance )8 #enote# i;)</ is a floatin% point 'alue bet$een 0&0 an# 1&0:

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

11-

@ =inear @ppro0imation to 5eality

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

11.

Doom "tyle ?olumetric =i%hts


Coth in the theoretical frame$or+ of the ren#erin% e(uation8 an# 4="= sha#ers #oin% li%htin% e(uations usin% the stan#ar# ClinnMPhon% mo#el8 all that is re(uire# of a li%ht source in or#er for it to be use# in sha#in% calculations at a particular point x is a li"ht intensity an# inci)ent )irection& 2o$ $e $ill #iscuss a type of 'olumetric li%ht populariIe# by the Doom 3 en%ine ;also +no$n as i) Tech 3< aroun# )0038 that specifies these 'alues in a no'el $ay& 2ot only are these types of li%hts interestin% to un#erstan# from a practical stan#point ;they are still useful to#ay<8 they are interestin% from a theoretical perspecti'e8 because they illustrate an ele%ant8 fast appro0imation& "uch appro0imations are the essence of the art of realMtime ren#erin%&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

11/

@ppro0imatin% Intensity
The most creati'e aspect of DoomMstyle 'olumetric li%hts is ho$ they #etermine the intensity at a %i'en point& It is controlle# throu%h t$o te0ture maps& Nne map is essentially a %obo8 $hich can be proBecte# usin% either ortho%raphic or perspecti'e proBection8 similar to a spot or #irectional li%ht& The other map is a one #imensional map8 +no$n as the falloff map8 $hich controls the falloff& The proce#ure for #eterminin% the li%ht intensity at point x is as follo$s: x is multiplie# by a *_* matri08 an# the resultin% coor#inates are use# to in#e0 into the t$o maps& The )D %obo is in#e0e# usin% ;x4w8 y4w<8 $hile 1D falloff map is in#e0e# $ith the (& The pro#uct of these t$o te0els #efines the li%ht intensity at x&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 111

The Nmni =i%ht

The omni li%ht proBects the circular %obo ortho%raphically across the bo08 an# places the position of the li%ht ;$hich is use# to compute the l 'ector< in the center of the bo0& The *_* matri0 use# to %enerate the te0ture coor#inates in this case $oul# be as follo$s8 $here sx8 sy8 an# s( are the #imensions of the bo0 on each a0is ;ne0t sli#e<A

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)00

The Nmni =i%ht Matri0

This matri0 operates on points in the obBect space of the li%ht8 $here the position of the li%ht is in the center of the bo08 so for the matri0 that operates on $orl# space coor#inates8 $e $oul# nee# to leftMmultiply this matri0 by a *_* $orl#MtoMobBect matri0& The ri%htmost column is D08 08 08 1ET8 since $e use an ortho%raphic proBection onto the %obo& The translation of 1>) is to a#Bust the coor#inates from the M1>) to U1>) ran%e into the 0 to 1 ran%e of the te0ture& The y a0is is flippe# since Uy points up8 but Uv points #o$n in the te0ture&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)01

The "pot =i%ht

The spot li%ht uses a perspecti'e proBection8 $here the center of proBection is at one en# of the bo0& The position of the li%ht use# for calculatin% the l 'ector is at this same location8 but that isn!t al$ays the caseP

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)0)

The "pot =i%ht Matri0


The same circular %obo is use# as the omni8 but #ue to the perspecti'e proBection it forms a cone shape& The falloff map is bri%htest at the en# of the bo0 nearest the center of proBection an# falls off linearly alon% the U( a0is8 $hich is the #irection of proBection of the %obo in all cases& 2otice that the 'ery first pi0el of the spot li%ht %obo is blac+8 to pre'ent obBects behin# the li%ht from %ettin% lit& In fact all of the %obos an# falloff maps ha'e blac+ pi0els at their e#%es8 since these pi0els $ill be use# for any %eometry outsi#e the bo0& The a##ressin% mo#e must be set to clamp to a'oi# the %obo an# falloff map tilin% across 3D space& The te0ture %eneration matri0 for perspecti'e spots is:

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)03

The Fa+e "pot

4ere8 proBection is ortho%raphic an# si#e$ays& The conical nature of the li%ht as $ell as $hat $e or#inarily thin+ of as the falloff are both enco#e# in the %obo& The falloff map use# for this li%ht is the same as the omni li%ht& It is bri%htest in the center of the bo08 an# causes the li%ht to fa#e out as $e approach the M( an# U( faces of the bo0& The te0ture coor#inate matri0 in this case is actually the same as the omni& The entire chan%e comes from usin% a #ifferent %obo8 an# orientin% the li%ht properlyP

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)0*

@#'anta%es of ?olumetric =i%hts


DoomMstyle 'olumetric li%hts can be attracti'e for realMtime %raphics for se'eral reasons& 1& They are simple an# efficient8 re(uirin% only the basic functionality of te0ture coor#inate %eneration8 an# t$o te0ture loo+ups& These are fle0ible operations that are easily har#$ire# into fi0e#Mfunction har#$are li+e the 2inten#o Wii& )& Many #ifferent li%ht types an# effects can be represente# in the same frame$or+& This can be helpful to limit the number of #ifferent sha#ers that are nee#e#& =i%htin% mo#els8 li%ht types8 material properties8 an# li%htin% passes8 can all be #imensions in the matri0 of sha#ers8 an# the siIe of this matri0 can %ro$ (uite (uic+ly& It can also be useful to re#uce the amount of s$itchin% of ren#er states&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)0,

More @#'anta%es of ?olumetric =i%hts


3& @rbitrary falloff cur'es can be create# usin% the %obo an# falloff maps& We are not restricte# to linear or realM$orl# in'erse s(uare# attenuation& ,& Due to this ability to control the falloff8 the bo0 that contains the li%htin% 'olume can usually be relati'ely ti%ht compare# to tra#itional spot an# omni li%hts& In other $or#s8 a lar%e percenta%e of the 'olume $ithin the bo0 is recei'in% si%nificant li%htin%8 an# the li%ht falls off more rapi#ly than tra#itional mo#els8 so that the 'olume is as small an# as ti%ht as possible& =oo+in% at the fi%ure belo$8 compare the siIe of the bo0 nee#e# to contain the true spot8 'ersus the fa+e spot&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)0-

?olumetric =i%hts in Doom 3


This is perhaps the most important feature behin# the intro#uction of these sorts of li%hts in Doom 38 $hich use# a accumulate# ren#erin% techni(ue $ith no li%htmaps or precompute# li%htin%K e'ery obBect $as fully lit in realMtime& 6ach li%ht $as a##e# into the scene by reMren#erin% the %eometry $ithin the 'olume of the li%ht an# a##in% the li%ht!s contribution into the frame buffer& =imitin% the amount of %eometry that ha# to be re#ra$n ;as $ell as the %eometry that ha# to be processe# for stencil sha#o$s< $as a hu%e performance $in&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)0.

"ection 10&/:

"+eletal @nimation

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)0/

Character @nimation
@rticulate# mo#els are bro+en up into a hierarchy of parts8 for e0ample8 torso8 hea#8 left arm8 ri%ht arm8 etc& In ol#Mschool Play"tation ) %ames8 these articulate# parts are ri%i#8 that is8 they #o not fle0 or ben#& "o the characters loo+ li+e robots& @ better $ay: s+eletal animation&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' )01

"+eletal @nimation
The i#ea behin# s+eletal animation is to replace the hierarchy of parts $ith an ima%inary hierarchy of bones& Then each 'erte0 of the mo#el is associate# $ith one or more bones8 each of $hich e0ert influence o'er the 'erte0 but #o not totally #etermine its position& @n artist nee#s to specify $hich bones influence $hich 'ertices8 an# by ho$ much& This process is +no$n as s0innin"& "+innin% can be 'ery labor intensi'e& @lthou%h automate# tools e0ist $hich can pro'i#e a (uic+ first pass at the s+innin%8 a $ellM s+inne# character re(uires e0pertise an# time&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)10

60ample
=etVs loo+ at an e0ample& The ne0t fi%ure sho$s t$o e0ample s+inne# 'ertices near the elbo$ of a robot& The blue an# %reen #ots sho$ $hat a 'erte0 $oul# loo+ li+e if it $ere ri%i# to the correspon#in% bone& The cyan #ot is the s+inne# 'erte0K notice that it stays attache# to the surface of the mesh&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)11

The 'erte0 on the left appears to be influence# about /0S by the forearm bone an# )0S by the upper arm bone8 an# thus it stays close to its green ri%i# 'erte0&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' )1)

The 'erte0 on the ri%ht appears to be influence# about *0S by the forearm bone an# -0S by the upper arm bone8 an# thus it stays close to its blue ri%i# 'erte0&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' )13

@ "imple "trate%y
For each 'erte08 +eep a list of bones that influence it& Typically $e set a limit on the number of bones that may influence any one 'erte0& ;Four is common&< For each bone8 $e +no$ the position of the 'erte0 relati'e to the boneVs local a0es8 an# $e ha'e a $ei%ht for that bone& To compute the s+inne# 'erte0 positions for a mo#el in an arbitrary pose8 $e $ill nee# a transform matri0 for each bone8 $hich tells ho$ to con'ert from bone coor#inate space into mo#el space& ?aryin% these transform matrices o'er time is $hat ma+es the character appear to animate& In the follo$in% t$o sli#es $e $ill see some #eclarations an# co#e that implement this simple strate%y&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' )1*

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)1,

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)1-

Where are the Cones From9


Where #o the bone space coor#inates ;member 'ariables posInCone"pace an# normalInCone"pace< come from9 7ThatVs easy8` says the pro%rammer8 $e ha'e the artist Bust e0port them #irectly from MayaP` Cut ho$ #i# Maya #etermine them9 The ans$er is they come from the $in)in" pose& The $in)in" pose ;sometimes calle# the home pose< #escribes an orientation of the bones in some #efault position& When an artist creates a character mesh8 he or she starts by buil#in% a mesh $ithout any bones or s+innin% #ata8 Bust li+e any other mo#el& Durin% this process the artist buil#s the character pose# in the bin#in% pose&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' )1.

60ample

The abo'e 3 fi%ures sho$ a s+inne# mo#el in bin#in% pose ;left<8 the s+eleton ;center<8 an# their relati'e positions ;ri%ht<& 5emember that bones are really Bust coor#inate spaces8 an# #onVt ha'e any actual %eometry& The %eometry you see abo'e is Bust so you can see it&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)1/

5i%%in%
When the mesh is #one8 it is ri""e)8 $hich means a hierarchy of bones ;a s+eleton< is create# an# the s+innin% #ata is e#ite# to associate 'ertices $ith the appropriate bones& Durin% this process the ri%%er $ill ben# bones to 'arious e0treme an%les to pre'ie$ ho$ $ell the mo#el reacts& This is $here the s+ill an# e0perience of the character mo#eler an# the ri%%er come into play& The point for us is that althou%h Maya is constantly calculatin% ne$ 'erte0 positions in response to the manipulation of the bones8 it has sa'e# the ori%inal mo#elin% space coor#inates of each 'erte08 in the bin#in% pose& 6'erythin% starts $ith that ori%inal 'erte0 position&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)11

The Ci% Picture of Mesh "+innin%


"o8 to compute the boneMspace coor#inates of a 'erte08 $e start $ith the mo#elin%Mspace coor#inates of that 'erte0 in the bin#in% pose& We also +no$ the position an# orientation of each bone in the bin#in% pose& We simply transform the 'erte0 positions from mo#elin% space into bone space base# on those positions an# orientations& ThatVs the bi% picture of mesh s+innin%8 in principle& 2aturally this can be optimiIe#&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' ))0

@n NptimiIation
"tore the position of each 'erte0 in the bin#in% pose8 rather than storin% it relati'e to each bone that e0erts influence& Then8 $hen ren#erin% a mesh8 rather than ha'in% a boneMtoMmo#el transform for each bone8 $e ha'e a matri0 that transforms coor#inates from the ori%inal bin#in% space to mo#elin% space in the current pose& In other $or#s8 this matri0 #escribes the #ifference bet$een the boneVs orientation in the $in)in" pose8 an# its orientation in the c%rrent pose& In the follo$in% t$o sli#es $e $ill see some #eclarations an# co#e that implement this optimiIe# strate%y&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

))1

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)))

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

))3

=ast Wor# on "+innin%


This pro#uces a si%nificant re#uction in ban#$i#th to the GPG ;#ue to the #ecrease in siIeof;"+inne#?erte0<8 as $ell as a re#uction in perM'erte0 computation8 especially $hen basis 'ectors are present& It Bust re(uires a bit more manipulation of the matrices before han#in% them to the GPG& We ha'e presente# the basic i#ea behin# simple s+innin%& Certainly8 in situations $here computin% resources ;an# human resourcesP< are a'ailable an# $orth e0pen#in% to pro#uce the hi%hest fi#elity characters possible8 such as fi%htin% %ames or sports %ames8 more a#'ance# techni(ues can be employe#& For e0ample8 $e mi%ht $ant to ma+e the bicep bul%e as the arm ben#s up8 or s(uish out the flesh of a #inosaur foot as $ei%ht is transferre# an# the foot is presse# har#er into the %roun#&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

))*

"ection 10&1:

Cump Mappin%

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)),

Cump Mappin%
1%mp mappin" is a %eneral term that can refer to at least t$o #ifferent metho#s of controllin% the surface normal per te0el& @ hei"ht map is a %reyscale map8 $here the intensity in#icates the local ele'ation of the surface& =i%hter colors in#icate portions of the surface that are 7bumpe# out8: an# #ar+ colors are areas $here the surface is 7bumpe# in&: 4ei%ht maps are attracti'e because they are 'ery easy to author8 but they are not i#eal for realMtime purposes8 because the normal is not #irectly a'ailableK instea# it must be calculate# from the intensity %ra#ient& We $ill focus on the techni(ue of normal mappin"8 $hich is 'ery common no$a#ays an# $hat most people usually mean $hen they say 7bump map&:

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

))-

Gsin% a 2ormal Map


In a normal map8 the coor#inates of the surface normal are #irectly enco#e# in the map& The most basic $ay is to enco#e x8 y8 an# ( in the re#8 %reen8 an# blue channels8 respecti'ely8 althou%h certain har#$are support more optimiIe# formats& The 'alues are usually scale#8 biase#8 an# (uantiIe#8 suchthat a coor#inate 'alue of M1 is enco#e# as a 08 an# U1 is enco#e# usin% the ma0imum color 'alue ;usually ),,<& 2o$ in principle8 usin% a normal map is simple& In our li%htin% calculation8 rather than usin% the result of interpolatin% the 'erte0 normals8 $e fetch a normal out of the normal map an# use it instea#& Cut it!s not that easyA

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)).

First Complication
Complications arise for t$o primary reasons& First8 normal maps are not intuiti'e to e#it& While a hei%ht map ;or true #isplacement map< can be easily painte# in Photoshop8 normal maps are not so easily 'isualiIe# an# e#ite#& CutMan#Mpaste operations on normal maps are usually safe8 but for the normal map to be 'ali#8 each pi0el shoul# enco#e a normaliIe# 'ector& The usual techni(ue for ma+in% a normal map is for an artist to actually mo#el a lo$ an# hi%h res 'ersion of the mesh& The lo$Mres mesh is the one actually use# at run time8 an# the hi%h res mesh is solely to create the bump map8 usin% an automate# tool that raytraces a%ainst the hi%her resolution mesh to #etermine the surface normal for e'ery te0el in the normal map&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

))/

"econ# Complication
The tric+ier issue is that te0ture memory is a precious resource& In some simple cases8 e'ery te0el in the normal map is use# at most once on the surface of the mesh&
In this case8 $e coul# simply enco#e the normal in obBect space8 an# our earlier #escription $oul# $or+ Bust fine& Cut realM$orl# obBects e0hibit a %reat #eal of symmetry an# selfMsimilarity8 an# patterns are often repeate#& For e0ample8 a bo0 often has similar bumps an# notches on more than one si#e&

Cecause of this8 currently it is a more efficient use of the same amount of memory ;an# artist time< to increase the resolution of the map8 an# reuse the same normal map ;or perhaps Bust portions of it< on multiple mo#els ;or perhaps Bust on multiple places in the same mo#el<& Nf course8 the same principle applies to any sort of te0ture map8 not Bust normal maps& Cut normal maps are #ifferent in that they cannot be arbitrarily rotate# or mirrore#8 because they enco#e a 'ector&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

))1

Tan%ent "pace: The Tan%ent ?ector


The most common techni(ue these #ays is for the normal enco#e# in the map to use coor#inates in tan"ent space& In tan%ent space8 U( points out from the surfaceK the U( basis 'ector is n actually Bust the surface normal & The x basis 'ector is +no$n as the tan"ent vector8 $hich $e!ll #enote 8 #irection of increasin% % in te0ture space& u an# it points in the In other $or#s8 $hen $e mo'e in the #irection of the tan%ent 'ector in 3D8 this correspon#s to mo'in% to the ri%ht in )D in the normal map& ;Nften the bump map shares the same G? coor#inates as other maps8 but if they #iffer8 it!s the coor#inates use# for bump mappin% that count&<

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)30

Tan%ent "pace: The Cinormal ?ector


"imilarly8 the y basis 'ector8 +no$n as the $inormal an# 8 correspon#s to the #irection of #enote# here as v increasin% v8 althou%h $hether this motion is 7up: or 7#o$n: in the te0ture space #epen#s on the con'entions for the ori%in in ;%8 v< space8 $hich can #iffer as $e #iscusse# earlier& Nf course the coor#inates for the tan%ent an# binormal are %i'en in mo#el space8 Bust li+e the surface normal& @s implie# by the hats o'er the 'ariables8 basis 'ectors are usually store# usin% unit 'ectors&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' )31

2ormal Map 60ample


For e0ample8 assume that a te0el in a normal map has 5GC 'alues D3.8 1)/8 )1/E8 $hich is #eco#e# to the unit 'ector DM0&.0.8 08 0&.0.E& We interpret this to mean that the local surface normal is pointin% at about a *,a an%le from a flat surface normal #efine# by the interpolate# 'erte0 normal& It points 7to the left8: $here 7left: is meanin%ful in the ima%e space of the normal map an# really means 7in the #irection of #ecreasin% %:&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)3)

Tan%ent "pace to Mo#el "pace


In summary8 the tan"ent8 $inormal8 an# normal are the a0es of a coor#inate space +no$n as tan"ent space8 an# the coor#inates of the perMte0el normal are interprete# usin% this coor#inate space& To obtain the mo#elMspace normal from a tan%entMspace normal8 $e first #eco#e the normal from the map8 an# then transform it into mo#el space Bust li+e any other 'ector&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' )33

Tan%ent to Mo#el "pace Transform


=et st L Dstx8 sty8 st(E #enote the tan%entMspace surface normal8 an# let sm L Dsmx8 smy8 sm(E #enote the mo#elMspace surface normal& sm is a linear combination of the basis 'ectors: +s v +s n s =s u
m t x t y t z

5ecall that this is the same as multiplyin% st by a matri0 $hose ro$s are basis 'ectors:
u s m = st v n
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' )3*

Tan%ent ?ectors as an @ppro0imation


5emember that the poly%on mesh is Bust an appro0imation for a potentially cur'e# surface8 an# so the surface normal $e use for li%htin% 'aries continuously o'er each face in or#er to appro0imate the true surface normal& In the same $ay8 the tan%ent an# binormal basis 'ectors $ill also 'ary continuously o'er the mesh8 since they shoul# be perpen#icular to the surface normal an# tan%ent to the surface bein% appro0imate#& Cut e'en in on a flat surface8 the basis 'ectors can chan%e o'er the surface if a te0ture is s(ueeIe#8 s(uashe#8 or t$iste# ;for e0ample8 see the ne0t sli#e<&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)3,

The left si#e sho$s an e0ample of s5%ishin"& In this case8 the tan%ent 'ector $oul# be pointin% to the ri%ht8 parallel to the horiIontal poly%on e#%es8 $hile the binormal $oul# be locally parallel to the 'ertical ;cur'in%< poly%on e#%es at each 'erte0&

Compare this $ith the te0ture mappin% on the ri%ht si#e8 $here the te0ture mappin% is planar& In this e0ample8 the binormal at e'ery 'erte0 ;an# e'ery interior point< points #irectly #o$n&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)3-

Tan%ent "pace NptimiIation 1 of )


2otice that in the te0ture mappin% use# in the left si#e of the fi%ure8 the tan%ent an# binormal 'ectors are not perpen#icular& Despite this possibility8 itVs common to assume the basis 'ectors form an orthonormal basis ;or to a#Bust them so that they #o<8 e'en if the te0ture is bein% manhan#le#& We ma+e this assumption in or#er to facilitate t$o optimiIations& The first optimiIation is that $e can perform our li%htin% calculations in tan%ent space rather than in mo#el space& If $e #o the li%htin% in mo#el space8 $e must interpolate the three basis 'ectors across the face8 an# then in the pi0el sha#er $e must transform our tan%entMspace normal into mo#el space& When $e #o the li%htin% in tan%ent space8 ho$e'er8 $e can instea# transform the li%htin% 'ectors into tan%ent space in the 'erte0 sha#er8 an# then #urin% rasteriIation the interpolation is #one in tan%ent space&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)3.

Tan%ent "pace NptimiIation 1 of )


In many circumstances this is faster& If $e ha'e an orthonormal basis8 then the in'erse of the transform matri0 is simply its transpose8 an# $e can transform from mo#el space to tan%ent space usin% #ot pro#uct& Nf course8 $e are free to rotate 'ectors into tan%ent space usin% the #ot pro#uct e'en if our basis isnVt orthonormalK in fact after interpolatin% basis 'ectors an# reMnormaliIin% them8 itVs li+ely that it $ill be sli%htly out of ortho%onality& In this case8 our transform is not completely correct8 but usually this #oesnVt cause any problems because the $hole i#ea of interpolatin% surface normals an# basis 'ectors is an appro0imation to be%in $ith&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)3/

Tan%ent "pace NptimiIation ) of )


The secon# optimiIation $e can ma+e by assumin% perpen#icular basis 'ectors is to completely a'oi# storin% one of the t$o basis 'ectors ;usually $e #rop the binormal< an# compute it on the fly& This can be faster $hen the performance bottlenec+ is shuffin% memory aroun#8 rather than perM'erte0 calculations& ThereVs Bust one complication: mirrore# bump maps& It is 'ery common on symmetrical obBects for te0ture maps8 inclu#in% the bump map8 to be use# t$iceK on one si#e the re%ular $ay8 an# mirrore# on the other si#e& 6ssentially $e nee# to +no$ if the te0ture is applie# in its re%ular orientation or mirrore#& This is #one by storin% a fla% that in#icates $hether the te0ture is mirrore#& @ 'alue of U1 in#icate# the usual orientation8 an# H1 in#icates the mirrore# state& ItVs common to con'eniently tuc+ a$ay this fla% in the w component of the one basis 'ector $e are +eepin%

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)31

Tan%ent "pace NptimiIation ) of )


2o$ $hen $e nee# to compute the #roppe# basis 'ector8 $e ta+e the appropriate cross pro#uct8 e&%&

u =n v

an# then multiply by our fla% to flip the basis 'ector if necessary& This fla% is calculate# usin% the triple pro#uct This is the same as the #eterminant of the transformation matri0 $e sa$ a fe$ sli#es a%o:
u v n
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' )*0

u v n

Calculatin% Tan%ent "pace Casis ?ectors


"uppose $e are %i'en a trian%le $ith 'ertices
"0 L ;x08 y08 (0<8 "1 L ;x18 y18 (1<8 an# ") L ;x)8 y)8 ()<8

$hose G? coor#inates are ;%08 v0<8 ;%18 v1<8 an# ;%)8 v)<8 respecti'ely& It is al$ays possible to fin# a planar mappin%8 meanin% that the mappin% %ra#ient is constant o'er the entire trian%le& =et #1 an# #) be trian%le e#%e 'ectors %i'en by
#1 L "1H "08 an# #) L ")H "0

$ith correspon#in% G? coor#inates :


;s18 t1< L ;%16%08 v16v0<8 an# ;s)8 t)< L ;%)6%08 v)6v0<&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)*1

"ee+in% u an# !
"ince $e see+ basis 'ectors u8 ! that lie in the plane of the trian%le8 $e can e0press the trian%le e#%e 'ectors #1 an# #) as a linear combination of u an# !& That is8 us1 U !t1 L #1 us) U !t) L #) We can $rite these e(uations more compactly in matri0 notation as
s1 s 2 t1 u q1 = t 2 v q 2

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)*)

Trian%le =e'el 2ormals


Multiplyin% both si#es by the in'erse of the s8 t matri0 on the left8 $e ha'e:
u s1 v = s 2 t1 q1 t2 1 = t 2 q 2 s1t 2 s2t1 s2
1

t1 q1 q s1 2

"ince $e are plannin% on normaliIin% our basis 'ectors8 $e can #rop the lea#in% constant fraction8 an# $e are left $ith: u L t)#1 H t1#) ! L Hs)#1 U s1#)
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' )*3

?erte0 =e'el 2ormals


This %i'es us basis 'ectors for each trian%le& They are not %uarantee# to be perpen#icular8 but they are usable for our main purpose: #eterminin% basis 'ectors at the 'erte0 le'el& These can be calculate# usin% a tric+ similar to computin% 'erte0 normals: for each 'erte0 $e ta+e the a'era%e of the basis 'ectors of the a#Bacent trian%les& We also usually enforce an orthonormal basis usin% GramM"chmi#t ortho%onaliIation @lso8 if $e are #roppin% one of the basis 'ectors8 then this is $here $e nee# to sa'e the #eterminant of the basis as #iscusse# earlier& 2o$ for some co#eA

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)**

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)*,

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)*-

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)*.

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)*/

@ Complication
Nne irritatin% complication that is not #ealt $ith by this co#e is that there may be a #iscontinuity in the mappin% $here the basis 'ectors shoul# not be a'era%e# to%ether8 an# the basis 'ectors must be #ifferent across a share# e#%e& Most of the time the faces $ill ha'e alrea#y be #etache# from each other ;the 'ertices $ill be #uplicate#< alon% such an e#%e8 since the G? coor#inates $ill not match& 4o$e'er8 there is unfortunately one particularly common case $here this is not true: mirrore# te0tures on symmetric obBects& For e0ample8 it is common for character mo#els an# other symmetric meshes to ha'e a line #o$n their center $here the te0ture has been mirrore#& The 'ertices alon% this seam 'ery often re(uire i#entical G?s but an opposite u or !& These 'ertices must be #etache# in or#er to a'oi# pro#ucin% in'ali# basis 'ectors alon% this seam&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)*1

Do it =ater in a "ha#er
=ater $eVll sho$ some sample sha#er co#e that uses the basis 'ectors to perform bump mappin%& The runtime co#e is surprisin%ly simple once all the #ata has been mun%e# into the ri%ht format& This illustrates a common theme of contemporary realMtime %raphics: about .,S of the co#e is in the tools that manipulate the #ata8 optimiIin%8 pac+in%8 an# other$ise manipulatin% it into Bust the ri%ht format so that the runtime co#e ;the other ),S< can run as fast as possible&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

),0

"ection 10&10:

The 5ealMTime Graphics Pipeline

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

),1

The 5eal Time Graphics Pipeline


The ren#erin% e(uation is the correct $ay to pro#uce ima%es8 assumin% you ha'e an infinite amount of computin% po$er& Cut if you $ant to pro#uce ima%es in the real $orl# on a real computer8 you nee# to un#erstan# the contemporary tra#eoffs that are bein% ma#e&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

),)

Ca'eat 1
There is no such thin% as the 7typical: mo#ern %raphics pipeline& The number of #ifferent ren#erin% strate%ies is e(ual to the number of %raphics pro%rammers& 6'eryone has his or her o$n preferences8 tric+s8 an# optimiIations& Graphics har#$are continues to e'ol'e rapi)ly& @s e'i#ence8 the use of sha#er pro%rams is no$ in $i#esprea# use in consumer har#$are li+e %amin% consoles8 an# this technolo%y $as in its infancy at the time of the $ritin% of the first e#ition of this boo+& "till8 althou%h there is %reat 'ariance in %raphics systems an# %raphics pro%rammers8 most systems #o ha'e a %reat #eal in common& WeV# li+e to reiterate that our %oal is to %i'e you a soli# o'er'ie$8 especially $here the mathematics are in'ol'e#8 from $hich you can e0pan# your +no$le#%e& This is not a sur'ey of the latest cuttin% e#%e techni(ues&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

),3

Ca'eat )
We $ill attempt to #escribe the basic proce#ure for %eneratin% a sin%le ren#ere# ima%e $ith 'ery basic li%htin%& We $ill not consi#er animation8 an# $e $ill only briefly mention techni(ues for %lobal illumination& Nur #escription $ill be of the conceptual flo$ of #ata throu%h the %raphics pipeline& In practice8 tas+s are often performe# in parallel or out of se(uence for performance reasons&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

),*

Ca'eat 3
We are intereste# in realMtime ren#erin% systems $hich8 at the time of this $ritin%8 are primarily %eare# for ren#erin% trian%le meshes& Nther means of pro#ucin% an ima%e8 such as raytracin"8 ha'e a 'ery #ifferent hi%h le'el structure than #iscusse# here& Ce $arne# that future techni(ues for realMtime an# offMline ren#erin% coul# con'er%e if parallel raytracin% pro'es to be a more economical $ay to +eep up $ith the ine0orable march of MooreVs la$&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' ),,

The Graphics Pipeline: N'er'ie$


1& "ettin% up the scene )& ?isibility #etermination 3& "ettin% obBectMle'el ren#erin% states *& Geometry %eneration an# #eli'ery ,& ?erte0 le'el operations -& Cullin%8 clippin%8 an# proBection .& 5asteriIation /& Pi0el sha#in% 1& Clen#in% an# output
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' ),-

The Graphics Pipeline 1: "ettin% up the "cene


Cefore $e can be%in ren#erin%8 $e must set se'eral options that apply to the entire scene& For e0ample8 $e nee# to set up the camera8 or more specifically8 pic+ a point of 'ie$ in the scene to ren#er it from8 an# choose $here on the screen to ren#er it to& We #iscusse# the math in'ol'e# in this process earlier in this chapter& We also nee# to select li%htin% an# fo% options8 an# prepare the #epth buffer&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' ),.

The Graphics Pipeline ): ?isibility Determination


Nnce $e ha'e a camera in place8 $e must then #eci#e $hich obBects in the scene are 'isible& This is e0tremely important for realMtime ren#erin%8 since $e #onVt $ant to $aste time ren#erin% anythin% that isnVt actually 'isible& This hi%h le'el cullin% is 'ery important for real %ames8 but is usually i%nore# for simple applications $hen youVre %ettin% starte#& We!ll i%nore it too&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' ),/

The Graphics Pipeline 3: "ettin% NbBectMle'el 5en#erin% "tates


Nnce $e +no$ that an obBect is potentially 'isible8 itVs time to actually #ra$ it& 6ach obBect may ha'e its o$n ren#erin% options& We must install these options into the ren#erin% conte0t before ren#erin% any primiti'es associate# $ith the obBect& Perhaps the most basic property associate# $ith an obBect is a material/ $hich #escribes the surface properties of the obBect& Nne of the most common material properties is the #iffuse color of the obBect8 $hich is usually controlle# usin% a te0ture map8 as $e #iscusse# alrea#y in this chapter&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

),1

The Graphics Pipeline *: Geometry Generation 3 Deli'ery


2e0t8 the %eometry is actually submitte# to the ren#erin% @PI& Typically the #ata is #eli'ere# in the form of trian%lesK either as in#i'i#ual trian%les8 or an in#e0e# trian%le mesh8 trian%le strip8 etc& @t this sta%e $e may also perform =ND ;=e'el of Detail< selection or %enerate %eometry proce#urally& We $ill #iscuss a number of issues relate# to #eli'erin% %eometry to the ren#erin% @PI soon&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)-0

The Graphics Pipeline ,: ?erte0 =e'el Nperations


Nnce the ren#erin% @PI has the %eometry in some trian%ulate# format8 a number of 'arious operations are performe# at the 'erte0 le'el& Perhaps the most important such operation is the transformation of 'erte0 positions from mo#el space into camera space& Nther 'erte0 le'el operations mi%ht inclu#e s+innin% for animation of s+eletal mo#els8 'erte0 li%htin%8 an# te0ture coor#inate %eneration& In consumer %raphics systems at the time of $ritin%8 these operations are performe# by a userMsupplie# micropro%ram calle# a vertex sha)er& WeVll %i'e se'eral e0amples of 'erte0 sha#ers soon&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)-1

The Graphics Pipeline -: Cullin%8 Clippin%8 3 ProBection


2e0t8 $e must perform three operations to %et trian%les in 3D onto the screen in )D& The e0act or#er that these steps are ta+en can 'ary& 1& First8 any portion of a trian%le outsi#e the 'ie$ frustum is remo'e#8 a process +no$n as clippin"8 $hich $ill be #iscusse# later& )& Nnce $e ha'e a clippe# poly%on in 3D clip space8 $e then proBect the 'ertices of that poly%on8 mappin% them to the )D screen space coor#inates of the output $in#o$8 as $e e0plaine# earlier& 3& Finally8 in#i'i#ual trian%les that face a$ay from the camera are remo'e# ;7culle#:<8 base# on the cloc+$ise or countercloc+$ise or#erin% of their 'ertices8 as $e $ill #iscuss later&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)-)

The Graphics Pipeline .: 5asteriIation


Nnce $e ha'e a clippe# poly%on in screen space8 it is rasteri(e)& !asteri(ation refers to the process of selectin% $hich pi0els on the screen shoul# be #ra$n for a particular trian%le8 interpolatin% te0ture coor#inates8 colors8 an# li%htin% 'alues that $ere compute# at the 'erte0 le'el across the face for each pi0el8 an# passin% these #o$n to the ne0t sta%e for pi0el sha#in%& "ince this operation is usually performe# at the har#$are le'el8 $e $ill only briefly mention it later&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)-3

The Graphics Pipeline /: Pi0el "ha#in%


2e0t $e compute a color for the trian%le8 a process +no$n as sha)in"& This innocuous phrase 7compute a color: is the heart of computer %raphicsP Nnce $e ha'e pic+e# a color8 $e then $rite that color to the screen8 possibly subBect to alpha blen#in% an# IMbufferin%& In to#ayVs consumer har#$are8 pi0el sha#in% is #one by a pixel sha)er ;%o fi%ure<8 a small piece of co#e you can $rite that ta+es 'alues from the 'erte0 sha#er ;$hich are interpolate# across the face an# supplie# perM pi0el<8 an# outputs the final color 'alue to the screen& We $ill #iscuss this process in more #etail later&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)-*

The Graphics Pipeline 1: Clen#in% an# Nutput


Finally8 at the 'ery bottom of the ren#er pipeline8 $e ha'e pro#uce# a color8 opacity8 an# #epth 'alue& The #epth 'alue is teste# a%ainst the #epth buffer for perMpi0el 'isibility #etermination8 to ensure that an obBect further a$ay from the camera #oesnVt obscure one closer to the camera& Pi0els $ith an opacity that is too lo$ are reBecte#8 an# the output color is then combine# $ith the pre'ious color in the frame buffer in a process +no$n as alpha $len)in"&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)-,

What 4as Chan%e#


It $asnVt too lon% a%o that a %raphics pro%rammer $oul# be responsible for $ritin% co#e to #o all of these steps in soft$are& 2o$a#ays8 $e #ele%ate many tas+s to a %raphics @PI ;@pplication Pro%rammin% Interface< such as DirectZ or NpenG=& The @PI may perform some of these tas+s in soft$are on the CPG8 $hile other tas+s ;hopefully as many as possible< are #ispatche# to specialiIe# %raphics har#$are& Mo#ern %raphics har#$are allo$s the %raphics pro%rammer ;thatVs us< 'ery lo$ le'el control throu%h 'erte0 sha#ers an# pi0el sha#ers8 $hich are micropro%rams $e $rite that are e0ecute# on the har#$are for each 'erte0 an# pi0el& While performance concerns in the ol# sin%le processor soft$are ren#erin% #ays $ere a##resse# $ith han#Mtune# assembly8 no$a#ays the concerns are more about usin% the GPG as efficiently as possible8 an# ensurin% that it is ne'er sittin% i#le8 $aitin% on the CPG to #o somethin%&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)--

What 4asn!t Chan%e#


Nf course8 both no$ an# then the simplest $ay to spee# up ren#erin% somethin% is simply: To a'oi# ren#erin% it at all if it isnVt 'isible8 an# To ren#er the cheapest appro0imation of it that you can %et a$ay $ith if it is&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' )-.

The Cottom =ine


In summary8 a mo#ern %raphics pipeline in'ol'es close cooperation of our co#e an# the ren#erin% @PI& When $e say 7%raphics @PI: $e mean the @PI soft$are an# the %raphics har#$are& Nn PC platforms the @PI soft$are layer is necessarily 'ery thic+8 #ue to the $i#e 'ariety of un#erlyin% har#$are that must be supporte#& Nn console platforms $here the har#$are is stan#ar#iIe#8 the layer can be si%nificantly leaner& The most notable e0ample of this $as the Play"tation )8 $hich allo$e# pro%rammers #irect access to har#$are re%isters an# 'ery lo$ le'el control o'er DM@&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' )-/

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)-1

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

).0

Cuffers
@ buffer is simply a rectan%ular re%ion of memory that stores some sort of #ata per pi0el& The most important buffers are the frame $%ffer an# the )epth $%ffer& The frame buffer hol#s the ren#ere# ima%e8 that is8 it stores one color per pi0el& This color may be store# in a 'ariety of formatsK the 'ariations are not si%nificant for the current #iscussion& In realMtime animation8 the frame buffer is normally locate# in 'i#eo 5@M& The vi)eo seriali(er on the 'i#eo car# is constantly rea#in% this area of 'i#eo 5@M8 con'ertin% the binary #ata into the appropriate si%nal to be sent to the #isplay #e'ice& Cut ho$ can the 'i#eo serialiIer rea# the frame buffer at the same time that the ren#erer is tryin% to $rite to it9

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

).1

Double Cufferin%
@ techni(ue +no$n as )o%$le $%fferin" is use# to pre'ent an ima%e from bein% #isplaye# before it is completely ren#ere#& Double bufferin% uses t$o frame buffers ;%o fi%ure<& Nne frame buffer8 the front or )isplay buffer8 hol#s the ima%e currently #isplaye# on the monitor& The bac+ buffer is the offMscreen buffer that hol#s the ima%e currently bein% ren#ere#& When $e ha'e finishe# ren#erin% an ima%e an# are rea#y for it to be #isplaye#8 $e flip the buffers ;DirectZ uses the 'erb present<& We can #o this in one of t$o $ays& 1& If $e use pa%e flippin%8 then $e instruct the 'i#eo har#$are to be%in rea#in% from the buffer that $as the offMscreen buffer& We then s$ap the roles of the t$o buffersK the #isplay buffer no$ becomes the offMscreen buffer& This can be #one (uic+ly by s$appin% t$o pointers& )& Nr $e may blit ;copy< the offMscreen buffer o'er the #isplay buffer&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

).)

Pa%e Flippin%: Dra$ from Nne8 5en#er to the Nther

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

).3

The Depth Cuffer


The secon# important buffer use# for ren#erin% is the )epth $%ffer8 also +no$n as the ('$%ffer& While the frame buffer stores a color at each pi0el8 the #epth buffer stores a #epth 'alue per pi0el& There are many 'ariations in the specifics of e0actly $hat 'alue %oes into the #epth buffer8 but the basic i#ea is that it is relate# to the pi0el!s #istance from the camera& Nften the clipspace ( coor#inate is use# as a #epth 'alue8 $hich is $hy the #epth buffer is also +no$n as the (Mbuffer&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

).*

4o$ a Depth Cuffer Wor+s


Cefore ren#erin% a pi0el8 $e compare its #epth 'alue $ith the 'alue alrea#y in the #epth buffer for this pi0el& If the ne$ #epth is further from the camera than the 'alue currently in the #epth buffer8 then the pi0el is #iscar#e#& Nther$ise8 the pi0el color is $ritten to the frame buffer8 an# the #epth buffer is up#ate# $ith the ne$8 closer #epth 'alue& Cefore $e can be%in ren#erin% an ima%e8 $e must clear the #epth buffer to a 'alue that means 7'ery far from the camera&: ;In clip space8 this 'alue is 1&0<&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

).,

The Graphics Pipeline *: More on Geometry Generation 3 Deli'ery

@fter #eci#in% $hich obBects to ren#er8 $e nee# to actually ren#er them& This is a t$oMstep process& First8 $e must set up the ren#er conte0t& This in'ol'es tellin% the ren#erer $hat 'erte0 an# pi0el sha#ers to use8 $hat te0tures to use8 an# settin% any other constants nee#e# by the sha#ers8 such as the transform matrices8 or li%htin% positions8 colors8 fo% settin%s8 etc& The #etails of this process #epen# %reatly on your hi%hMle'el ren#erin% strate%y an# tar%et platform8 so there isnVt much more specific $e can say here8 althou%h $eVll %i'e some e0amples later&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' ).-

"tep ): ?erte0 Data


Instea#8 $e $oul# li+e to focus on the secon# step8 $hich is essentially the top bo0 in that fi%ure $e sa$ earlier:

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)..

Inputs an# Nutputs


2o$a#ays a pro%rammer has (uite a bit of fle0ibility in $hat #ata to sen#8 ho$ to pac+ an# format each #ata element8 an# ho$ to arran%e thin%s in memory for ma0imum performance& What 'alues mi%ht $e nee# to supply per 'erte09 "hort ans$er: 7Whate'er properties you $ant to use to ren#er the trian%les&: There are re(uire# outputs ho$e'er& 1& The 'erte0 sha#er must output a position for each 'erte0& This position is typically specifie# in clipMspace8 $hich means the har#$are $ill #o the perspecti'e #i'i#e an# con'ersion to screen space coor#inates for you& )& The pi0el sha#er must output a color 'alue ;$hich typically inclu#es an alpha channel< for each pi0el&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' )./

"ha#er Constants
Nf course8 in or#er for you to properly #etermine the proper clipMspace coor#inates8 you $ill probably nee# the matri0 that transforms from mo#el space to clip space& ou can pass parameters li+e this that apply to all the 'ertices or pi0els in a %i'en batch of trian%les by settin% sha)er constants&

This is conceptually Bust a lar%e table of 'ector 'alues that is part of the ren#er conte0t an# for you to use $hene'er an# ho$e'er you nee#& @ctually8 there is usually one set of re%isters assi%ne# for use in the 'erte0 sha#er an# a #ifferent set of re%isters that can be accesse# in the pi0el sha#er&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

).1

Typical ?erte0 =e'el Information


4ere are some typical pieces of information that are store# at the 'erte0 le'el: 1& Position )& Te0ture mappin% coor#inates 3& "urface normal *& Color ,& Tan%ent space basis 'ectors =et!s loo+ at these one at a timeA
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' )/0

Typical ?erte0 =e'el Information: 1& Position


Position can be a 3D 'ector or a )D screenMspace position8 or it coul# be a position alrea#y transforme# into clip space that is simply passe# #irectly throu%h the 'erte0 sha#er& If a 3D 'ector is use#8 the position must be transforme# into screen space usin% the current mo#el an# 'ie$ transforms& If )D $in#o$ coor#inates ;ran%in% accor#in% to the resolution of the screen8 not normaliIe#< are use#8 then they must be con'erte# bac+ into clip space in the 'erte0 sha#er& If the mo#el is a s+inne# mo#el8 then the positional #ata must also inclu#e the in#ices an# $ei%hts of the bones that influence the 'erte0&
The animate# matrices can be #eli'ere# in a 'ariety of $ays& @ stan#ar# techni(ue is to pass them as 'erte0 sha#er constants& @ ne$er techni(ue that $or+s on some har#$are is to #eli'er them in a separate 'erte0 stream8 $hich must be accesse# throu%h special instructions since the access pattern is ran#om rather than streamin%&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)/1

Typical ?erte0 =e'el Information: )& Te0ture Mappin% Coor#inates


If $e are usin% te0ture mappe# trian%les8 then each 'erte0 must be assi%ne# a set of mappin% coor#inates& In this simplest case8 this is a )D location into the te0ture map& If $e are usin% multite0turin%8 then $e mi%ht nee# one set of mappin% coor#inates per te0ture map& Nptionally8 $e can %enerate one or more set of te0ture mappin% coor#inates proce#urally ;for e0ample8 if $e are proBectin% a %obo onto a surface<&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)/)

Typical ?erte0 =e'el Information: 3& "urface 2ormal8 an# *& Color
$urface Normal: Most li%htin% calculations nee# the surface normal& 6'en thou%h these li%htin% e(uations are often #one perMpi0el $ith the surface normal bein% #etermine# from a normal map8 $e still often store a normal at the 'erte0 le'el in or#er to establish the basis for tan%ent space& %olor: "ometimes itVs useful to assi%n a color input to each 'erte0& For e0ample8 if $e are ren#erin% particles8 the color of the particle may chan%e o'er time& Nr $e may use one channel ;such as alpha< to control the blen#in% bet$een t$o te0ture layers& @n artist can e#it the 'erte0 alpha to control this blen#in%& We mi%ht also ha'e perM'erte0 li%htin% calculations that $ere #one offline&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' )/3

Typical ?erte0 =e'el Information: ,& Tan%ent "pace Casis ?ectors


@s #iscusse# in alrea#y8 for tan%entMspace normal maps ;an# a fe$ other similar techni(ues< $e nee# basis 'ectors in or#er to #efine the local tan%ent space& The basis 'ectors an# surface normal establish this coor#inate space at each 'erte0& These 'ectors are then interpolate# across the trian%le #urin% rasteriIation to pro'i#e an appro0imate tan%ent space per pi0el&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)/*

?erte0 Format 60ample 1


@ basic trian%le mesh $ith a simple #iffuse map is store# usin% this 'erte0 type& We canVt use tan%ent space normal maps $ith this 'erte0 format8 since there are no basis 'ectors&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)/,

?erte0 Format 60ample )


If $e $ant to use a tan%entMspace normal map8 $eVll nee# to inclu#e basis 'ectors&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)/-

?erte0 Format 60ample 3


@nother common format8 use# for hea#sMupM#isplays8 te0t ren#erin%8 an# other )D items8 is a 'erte0 $ith screen space coor#inates an# preMlit 'ertices& ;2o normal nee#s to be supplie# since no li%htin% calculations $ill ta+e place&<

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)/.

?erte0 Format 60ample *


4ereVs a 'erte0 that is e0presse# in 3D8 but #oes not nee# to be lit by the %raphics @PIVs li%htin% en%ine& This format is often useful for particle effects such as e0plosions8 flames8 an# selfMilluminate# obBects8 an# for ren#erin% #ebu%%in% obBects li+e boun#in% bo0es8 $aypoints8 mar+ers8 etc&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)//

?erte0 Format 60ample ,


4ereVs a 'erte0 that is use# for li%htMmappe#8 bumpMmappe# %eometry& It has basis 'ectors for li%htmappin%8 an# t$o sets of G?Vs8 one for the re%ular #iffuse te0ture8 an# another for the li%htmap8 $hich stores ba+e#Min li%htin% that $as calculate# offline&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)/1

?erte0 Format 60ample 4ereVs a 'erte0 that mi%ht be use# for s+eletal ren#erin%& The in#ices are store# in four /M bit 'alues8 an# the $ei%hts are store# as four floats&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)10

?erte0 Format 60ample .


WeV'e %i'en e0amples of 'ertices #eclare# as structs& @s you can see8 the combinations can %ro$ (uite (uic+ly& Dealin% $ith this simply but efficiently is a challen%e& Nne i#ea is to allocate the fiel#s as a structure of arrays ;"N@< rather than an array of structures ;@N"<& If a 'alue is not present8 the array pointer $oul# simply be 2G==&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)11

The Graphics Pipeline ,: More on ?erte0 =e'el Nperations @fter mesh #ata has been submitte#8 a $i#e ran%e of 'erte0 le'el computations are performe#& In a sha#erMbase# ren#erer ;as oppose# to a fi0e#M function pipeline<8 this happens in the 'erte0 sha#er& The input to a 'erte0 sha#er is essentially one of the structs $e Bust #escribe#& @s $e #iscusse# earlier8 a 'erte0 sha#er can pro#uce many #ifferent types of output8 but there are t$o basic responsibilities it must fulfill&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)1)

?erte0 "ha#er 5esponsibilities


1& It must output8 at the 'ery minimum8 a clipMspace ;or in some circumstances screenMspace< position& )& It must pro'i#e to the pi0el sha#er any inputs that are nee#e# for the pi0el sha#er to perform the sha#in% calculations&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)13

"ome Common ?erte0 "ha#er Nperations


Transformin% mo#elMspace 'erte0 positions into clip space& Performin% s+innin% for s+eletal mo#els& Transformin% normals an# basis 'ectors into the appropriate space for li%htin%& Calculatin% 'ectors nee#e# for li%htin% ; l8 an# h< an# transformin% them into the appropriate coor#inate space& Computin% fo% #ensity 'alues from the 'erte0 position&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' )1*

More Common ?erte0 "ha#er Nperations


Generatin% te0ture mappin% coor#inates proce#urally& 60amples inclu#e proBecte# spot li%hts8 DoomMstyle 'olumetric li%ht8 reflectin% a 'ie$ 'ector about the normal for en'ironment mappin%8 'arious fa+e reflection techni(ues8 scrollin% or other$ise animate# te0tures8 etc& Passin% throu%h ra$ 'erte0 inputs $ithout mo#ification8 if they are alrea#y in the correct format an# coor#inate space& If $e are usin% Gourau# sha#in%8 $e mi%ht actually perform the li%htin% calculations here8 an# interpolate the li%htin% results&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)1,

5e'ie$: Mo#el to Clip "pace


Done $ith matri0 multiplication& Conceptually8 the 'ertices un#er%o a se(uence of transformations as follo$s:
1& The mo#el transform ta+es us from mo#el space to $orl# space& )& The 'ie$ transform ta+es us from $orl# space to camera space& 3& The clip matri0 ta+es us from camera space to clip space&

The matri0 multiplications are as follo$s:


Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' )1-

Mo#el to Clip "pace


The matri0 multiplications are as follo$s:
v clip = ( v model )(M modelworld )(M worldcamera )(M camera clip )

@s $e mentione# alrea#y8 $e #onVt actually perform three separate matri0 multiplications in practice& We ha'e one matri0 that transforms from mo#el space to clip space8 an# insi#e the 'erte0 sha#er $e perform one matri0 multiplication usin% this matri0&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' )1.

Nperations in Clip "pace


@fter 'ertices ha'e been transforme# into clip space8 t$o important tests are performe# on the trian%le: clippin% an# cullin%& Coth operations are usually performe# by the ren#erin% @PI8 so althou%h you $onVt usually ha'e to perform these operations yourself8 itVs interestin% to +no$ ho$ they $or+& The or#er that $e #iscuss these tests is not necessarily the or#er that they $ill occur on a particular piece of har#$are& Most har#$are culls in screen space8 $hereas ol#er soft$are ren#erers #i# it earlier8 in 3D8 in or#er to re#uce the number of trian%les that ha# to be clippe#&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)1/

Clippin%
Cefore $e can proBect the 'ertices onto screen space8 $e must ensure that they are completely insi#e the 'ie$ frustum& This process is +no$n as clippin"& "ince clippin% is normally performe# by the har#$are8 $e $ill only #escribe the process $ith cursory #etail& The stan#ar# al%orithm for clippin% poly%ons is the S%therlan)' *o)"man al"orithm8 $hich tac+les the #ifficult problem of poly%on clippin% by brea+in% it #o$n into a se(uence of easy subproblems&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

)11

"utherlan#M4o#%man
The input poly%on is clippe# a%ainst one plane at a time& To clip a poly%on a%ainst one plane8 $e iterate aroun# the poly%on8 clippin% each e#%e a%ainst the plane in se(uence& 6ach of the t$o 'ertices of the e#%e may be insi#e or outsi#e the plane8 thus there are four cases& 6ach case may %enerate Iero8 one8 or t$o output 'ertices8 as follo$s:
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 300

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

301

"utherlan#M4o#%man 60ample
The ne0t sli#e sho$s an e0ample of ho$ $e can apply these rules to clip a poly%on a%ainst the ri%ht clip plane& 5emember that the clipper outputs 'ertices8 not e#%es& In this fi%ure the e#%es are #ra$n only for illustration& In particular8 the final clip step appears to output t$o e#%es $hen actually only one 'erte0 $as output& The last e#%e is implicit to complete the poly%on&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

30)

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

303

Clippin% 2otes
"ome %raphics har#$are #oes not clip poly%ons to all si0 planes in 3D ;or *D<& Instea#8 only the near clip is performe#8 an# then scissorin% is #one in )D to clip to the $in#o$& This can be a performance $in because clippin% is slo$ on certain har#$are& @ 'ariation on this techni(ue is to employ a %uar# ban#& Poly%ons completely outsi#e the screen are reBecte#8 poly%ons completely insi#e the %uar# ban# are scissore# rather than clippe# in 3D8 an# poly%ons that are partially on screen but outsi#e of the %uar# ban# are clippe# in 3D&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

30*

Cac+face Cullin%
1ac0face c%llin" reBects trian%les that face a$ay from the camera& In opa(ue close# meshes $e shoul# ne'er see the bac+ si#e of a trian%le unless $e are allo$e# to %o insi#e the mesh& 5emo'al of bac+facin% trian%les isn!t strictly necessary in an opa(ue mesh8 but $e cull bac+ faces because $e #onVt $ant to $aste time #ra$in% anythin% that isnVt 'isible& For #ynamic obBects that mo'e aroun# in the $orl# freely8 rou%hly half of the faces $ill be bac+facin% on a'era%e& This isn!t true for thin%s li+e terrain&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

30,

Cac+face Cullin% in )D
Cac+facin% trian%les can be #etecte# in 3D ;before proBection< or )D ;after proBection<& Nn mo#ern %raphics har#$are8 bac+face cullin% is performe# in )D base# on the #irection of 'erte0 enumeration in screen space& In a leftMhan#e# coor#inate system8 or#er the 'ertices cloc+$ise aroun# the trian%le $hen 'ie$e# from the front&& Cac+facin% trian%les ha'e 'ertices or#ere# counterMcloc+$ise on the screen&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

30-

Cac+face Cullin% in 3D
)D bac+face cullin% is usually #one in har#$are& ou can turn it off $hen not nee#e#& Cac+face cullin% sometimes nee#s to be #one in 3D8 e&%& In soft$are ren#erin%& Ta+e the #ot pro#uct of the trian%le normal $ith the 'ector from the camera to one of its 'ertices& Cull those trian%les $hose #ot pro#uct is positi'e&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

30.

The Graphics Pipeline .M1: More on 5asteriIation8 "ha#in%8 an# Nutput


@fter clippin%8 the 'ertices are proBecte# an# mappe# into the screen coor#inates of the output $in#o$& These coor#inates are floatin%Mpoint 'alues8 $hich are continuous ;$ell8 +in# of<& Cut $e typically ren#er pi0els8 $hich are #iscrete& "o ho$ #o $e +no$ $hich pi0els actually %et #ra$n9 De'isin% an al%orithm to ans$er this (uestion is surprisin% complicate#& If $e ans$er $ron%8 then %aps can appear inMbet$een trian%les& 5en#erin% a pi0el more than once can be ba#8 too8 if $e are usin% alpha blen#in%& In other $or#s8 $e must ma+e sure that $hen $e ren#er a surface represente# usin% trian%les8 e'ery pi0el is ren#ere# e0actly once& =uc+ily the %raphics har#$are ta+es care of this for us an# $e #onVt ha'e to s$eat the #etails

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

30/

5en#erin% a Pi0el in , 6asy "teps


While $e #onVt necessarily ha'e to un#erstan# e0actly ho$ the %raphics har#$are #eci#es $hich pi0els to ren#er for a %i'en trian%le8 $e #o nee# to un#erstan# ho$ it #etermines $hat to #o $ith a sin%le pi0el& 4ere it is in , easy steps: 1& Interpolate& )& Depth test& 3& "ha#e& *& @lpha test& ,& Write& More #etails follo$A
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 301

Pi0el 5en#erin% "tep 1: Interpolate


@ny (uantities compute# at the 'erte0 le'el8 such as te0ture coor#inates8 colors8 normals8 etc& are interpolate# across the face& The interpolate# 'alues for each (uantity must be compute# for the pi0el before it can be sha#e#&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

310

Pi0el 5en#erin% "tep ): Depth Test


We reBect pi0els usin% the #epth buffer if the pi0el $e are about to sha#e $oul# be obscure# by a closer pi0el& 2ote that in some circumstances8 the pi0el sha#er is allo$e# to mo#ify the #epth 'alue8 in $hich case this test must be #eferre# until after sha#in%&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

311

Pi0el 5en#erin% "tep 3: "ha#e


Pixel sha)in" means computin% a color for a pi0el& Nn mo#ern %raphics har#$are8 this is $here your pi0el sha#er is e0ecute#& In a basic for$ar# ren#erer pass $here $e are actually ren#erin% obBects into the frame buffer ;as oppose# to $ritin% into a sha#o$ map8 or #oin% some other li%htin% pass<8 the pi0el is typically first lit8 an# then fo%%e# if necessary& The output of a pi0el sha#er consists of an 5GC color an# an alpha 'alue that is usually interprete# as the opacity of the pi0el& We $ill see se'eral e0amples of pi0el sha#ers in a moment&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

31)

Pi0el 5en#erin% "tep *: @lpha Test


This reBects pi0els base# on the alpha 'alue of the pi0el& @ll sorts of #ifferent alpha tests can be use#8 but the most common one is to reBect pi0els that are 7too transparent&: @lthou%h such in'isible pi0els may not cause any chan%e the frame buffer $ere $e to $rite them8 $e #o nee# to reBect them so that they #on!t cause us to $rite into the #epth buffer&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 313

Pi0el 5en#erin% "tep ,: Write


If the pi0el passes the #epth an# alpha tests8 then the frame buffer an# #epth buffer is up#ate#& The #epth buffer is up#ate# simply by replacin% the ol# #epth 'alue for the pi0el $ith the ne$ one& The frame buffer up#ate is more complicate#&
If blen#in% is not use#8 then the ne$ pi0el color replaces the ol# one& Nther$ise8 the ne$ pi0el color is blen#e# $ith the ol# one8 $ith the relati'e contributions of the ol# an# ne$ colors controlle# by the alpha 'alue& Nther mathematical operations such as a##ition8 subtraction8 an# multiplication are also often a'ailable8 #epen#in% on the %raphics har#$are&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 31*

"ection 10&11:

"ome 4="= 60amples

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

31,

"ome "ha#ers in 4="=


WeVll start $ith a 'ery simple e0ample to %et $arme# up an# #emonstrate the basic mechanisms of 4="= for #eclarin% constants an# passin% interpolate# ar%uments&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

31-

?erte0 "ha#er for Decal 5en#erin%


Perhaps the simplest type of sha#in% is to Bust output the color from the te0ture map #irectly8 $ithout any li%htin%& This is sometimes calle# )ecal sha)in"& The follo$in% 'erte0 sha#er illustrates se'eral of the basic mechanisms of 4="=8 as e0plaine# by the source comments&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 31.

Input
This struct #eclares the inputs $e recei'e from the mesh& 2ote that the or#er here is not important& Inputs are i#entifie# by their semantic8 $hich is the thin% on the ri%ht after the colon& When sen#in% the 'erte0 list to the ren#erer8 $e must specify the semantic of each 'erte0 element&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

31/

Nutput
This is the #ata $e $ill output from our 'erte0 sha#er& These are matche# up $ith the pi0el sha#er inputs base# on the semantic&

4ere $e #eclare a %lobal 'ariable8 a sha#er constant that hol#s the mo#elbclip transform matri0&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

311

Co#y of ?erte0 "ha#er

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3)0

Pi0el "ha#er for Decal 5en#erin%


@ 'erte0 sha#er li+e this coul# be be use# $ith the follo$in% pi0el sha#er8 $hich actually #oes the #ecal sha#in%& 4o$e'er8 to ma+e thin%s interestin% an# #emonstrate that pi0el sha#er constants $or+ the same as 'erte0 sha#er constants8 $eV'e a##e# a %lobal constant color8 $hich $e consi#er to be part of the %lobal ren#er conte0t& We ha'e foun# it 'ery useful to ha'e a constant li+e this8 $hich mo#ulates the color an# opacity of e'ery ren#ere# primiti'e&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3)1

Inputs an# Global ?ariable


This struct #eclares the interpolate# inputs $e recei'e from the rasteriIer& They $ill usually match the 'erte0 outputs e0actly8 e0cept that $e often lea'e off the clipMspace position&

4ere8 Bust to sho$ ho$ a pi0el sha#er constant $or+s8 $e #eclare a %lobal constant color& The output of our sha#er is multiplie# by this 5GC@ 'alue& Nne of the most common reasons to ha'e such a constant is to a## an opacity settin% into the ren#er conte0t8 but itVs 'ery han#y to ha'e a full 5GC@ constant color&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3))

Te0ture =oo+up ?ariable


We are %oin% to #o a te0ture loo+up& 4ere $e #eclare a 'ariable to refer to this te0ture8 an# annotate it $ith sufficient information so that our ren#erin% co#e can select the appropriate te0ture into the ren#erin% conte0t before #ra$in% primiti'es&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3)3

Co#y of Pi0el "ha#er

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3)*

Callin% Parameters
Clearly8 the hi%her le'el co#e must supply the sha#er constants an# the primiti'e #ata properly& The simplest $ay to match up a sha#er constant $ith the hi%her le'el co#e is to specifically assi%n a re%ister number to a constant usin% special 4="= 'ariable #eclaration synta0 There are subtler techni(ues8 such as locatin% constants by name&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3),

Casic PerMPi0el =i%htin%


2o$ letVs loo+ at a simple e0ample that actually #oes some li%htin% calculations& WeVll start $ith basic perMpi0el li%htin%& This e0ample illustrates the basic Phon% sha#in% techni(ue of interpolatin% the normal across the face an# e'aluatin% the full li%htin% e(uation perMpi0el& @ll of our li%htin% e0amples $ill use the stan#ar# ClinnM Phon% li%htin% e(uation& In this e0ample ;an# most of the later e0amples< the li%htin% en'ironment consists of a sin%le omni li%ht plus a constant ambient&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 3)-

Per Pi0el =i%htin% 1:

?erte0 "ha#er
For the first e0ample8 $e $ill #o all of the $or+ in the pi0el sha#er& In this case8 the 'erte0 sha#er is fairly tri'ial& It Bust nee#s to pass throu%h the inputs to the pi0el sha#er&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3).

Per Pi0el =i%htin% 1:

?erte0 "ha#er Nutput


2ote that $ith the e0ception of the output position8 $hich is output un#er the PN"ITIN2 sematic8 all others %o un#er the T6ZCNN5D0 semantic& Despite its name8 this semantic is actually use# for pretty much any interpolate# 'ector 'alue up to *D that $e $ant to pass to the pi0el sha#er8 not Bust te0ture coor#inates&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3)/

Per Pi0el =i%htin% 1:

Co#y of ?erte0 "ha#er

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3)1

Per Pi0el =i%htin% 1:

Pi0el "ha#er
4ereVs the correspon#in% pi0el sha#er8 $here all the action happens& 2otice that $e are usin% t$o #ifferent te0ture maps8 one for the #iffuse color8 an# another for the specular color& We assume that the t$o maps use the same te0ture mappin% coor#inates&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

330

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

331

Per Pi0el =i%htin% 1:

5eciprocal of Nmni =i%ht 5a#ius


The li%ht $ill falloff linearly to Iero at this ra#ius& ItVs common to tuc+ this into the w component of the position8 to re#uce the number of constants8 since each constant usually ta+es a full *D 'ector slot&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

33)

Per Pi0el =i%htin% 1:

Diffuse an# "pecular Map "amplers

2ote that $e assume that #iffuse an# spec maps are mappe# the same8 an# so they use the same G? coor#inates&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

333

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

33*

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

33,

Do it in the ?erte0 or the Pi0el "ha#er9


Nf course8 se'eral of the 'alues nee#e# in this calculation coul# be compute# in the 'erte0 sha#er8 an# $e coul# use the interpolate# results in the pi0el sha#er& This is usually a performance $in8 because there are usually fe$er 'ertices than pi0els& 4o$e'er8 a precise analysis can be complicate#8 because the number of 'ertices an# pi0els is not the only factorK the number of e0ecution units a'ailable for 'erte0 an# pi0el sha#in% is also important& Furthermore8 on some har#$are a pool of %eneric e0ecution units are share# bet$een 'erte0 an# pi0el sha#in%& There can also be performance implications for increasin% the number of interpolate# 'alues& "till8 #i'i#in% up the labor to #o more calculations per 'erte0 is a spee#up on most platforms an# in most situations&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 33-

Per Pi0el =i%htin% )8 Pi0el "ha#er:

Get =8 48 li%htColor from ?erte0 "ha#er

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

33.

Per Pi0el =i%htin% )8 Pi0el "ha#er:

Delete Globals for Nmni =i%ht8 ?ie$ Pos

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

33/

Per Pi0el =i%htin% )8 Pi0el "ha#er:

Delete Co#e for Computin% =

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

331

Per Pi0el =i%htin% )8 Pi0el "ha#er:

Delete Co#e for Computin% ?8 48 an# li%htColor

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3*0

Per Pi0el =i%htin% )8 Pi0el "ha#er:

@## Co#e for Processin% =8 48 an# li%htColor

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3*1

Per Pi0el =i%htin% )8 ?erte0 "ha#er:

"en# =8 48 li%htColor to Pi0el "ha#er

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3*)

Per Pi0el =i%htin% )8 ?erte0 "ha#er:

2e$ ;Mo'e#< Global ?ariables

2otice $e!'e mo'e# t$o %lobals from the pi0el sha#er ;at left< to the 'erte0 sha#er ;abo'e<& Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 3*3

Per Pi0el =i%htin% )8 ?erte0 "ha#er:

2e$ ;Mo'e#< Co#e for Computin% ?8 48 an# li%htColor

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3**

Per Pi0el =i%htin% 38 ?erte0 "ha#er:

=i%htin% in Camera "pace


Finally8 one last 'ariation on this e0ample& 2otice that in the pre'ious pi0el sha#er the co#e #oes not ma+e any assumption about $hat coor#inate space the li%htin% is ta+in% place in& We ha'e been performin% the li%htin% calculations in mo#el space8 but it is also common to #o it in camera space& The a#'anta%e is that $e #o not nee# to resen# sha#er constants for li%htin% #ata for each obBect that is ren#ere#8 li+e $e #o $hen those 'alues are specifie# in mo#elin% space ;$hich $ill 'ary for each obBect<&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3*,

Per Pi0el =i%htin% 38 ?erte0 "ha#er:

Global ?ariables

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3*-

Per Pi0el =i%htin% 38 ?erte0 "ha#er:

Co#e

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3*.

Per Pi0el =i%htin% 38 ?erte0 "ha#er:

Co#e

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3*/

Gourau# "ha#in%
6'en mo#est mo#ern har#$are has plenty of beef for Phon% sha#in%K in#ee# the pre'ious e0amples are relati'ely cheap sha#ers& 4o$e'er8 itVs 'ery instructi'e to consi#er ho$ to implement Gourau# sha#in%& 6'en thou%h the results are inferior to Phon% sha#in%8 an# Gourau# sha#in% preclu#es bump mappin%8 Gourau# sha#in% can still be useful on the PC to emulate the results of other har#$are& Go bac+ to our sha#ers for basic perMpi0el li%htin% an# $or+ from there&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 3*1

Gourau# "ha#in%:

?erte0 "ha#er Nutput

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3,0

Gourau# "ha#in%:

2e$ ?erte0 "ha#er Globals

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3,1

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3,)

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3,3

Gourau# "ha#in%:

Chan%e =i%htin% 6n'ironment


The pi0el sha#er from the pre'ious sli#e #oesn!t #epen# on the number of li%hts or e'en the li%htin% mo#el8 since all li%htin% calculations are #one in the 'erte0 sha#er& To chan%e the li%htin% en'ironment8 simply chan%e the 'erte0 sha#er& The pi0el sha#er stays the same& This is a useful li%htin% en'ironment in e#itors an# tools8 since itVs easy to create one li%htin% ri% that $or+s #ecently $ell for practically any obBect& 60ample: ambient plus three #irectional li%hts:

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3,*

Gourau# "ha#in%:

?erte0 "ha#er for 3 Directional U 1 @mbient =i%ht

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3,,

Gourau# "ha#in%:

?erte0 "ha#er for 3 Directional U 1 @mbient =i%ht

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3,-

Cump Mappin%
2e0t8 letVs loo+ at an e0ample of normal mappin%& We $ill be performin% the li%htin% in tan%ent space8 an# $eVll stic+ $ith the li%htin% en'ironment of a sin%le omni li%ht plus constant ambient8 to ma+e the e0amples easier to compare& In the 'erte0 sha#er8 $e ta+e the t$o basis 'ectors an# synthesiIe the binormal& Then8 $e use the three basis 'ectors to rotate = an# 4 into tan%ent space8 after first computin% them as usual in mo#el space& 2otice the use of the three #ot pro#ucts8 each of $hich is e(ui'alent to multiplication by the transpose of the matri0& We also perform the attenuation calculations in the 'erte0 sha#er8 passin% the unclampe# attenuate# li%ht color8 as in the earlier e0amples&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3,.

Cump Mappin%:

?erte0 "ha#er Global ?ariables

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3,/

Cump Mappin%:

?erte0 "ha#er Co#e

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3,1

Cump Mappin%:

?erte0 "ha#er Co#e

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3-0

Cump Mappin%:

Pi0el "ha#er
The pi0el sha#er is (uite compact8 since most of the prep $or+ has been #one in the 'erte0 sha#er& We unpac+ the normal8 an# normaliIe the interpolate# L an# & 'ectors& Then $e perform the ClinnMPhon% li%htin% e(uation Bust as in other e0amples&
Chapter 10 2otes 3D Math Primer for Graphics 3 Game De' 3-1

Cump Mappin%:

Pi0el "ha#er Globals

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3-)

Cump Mappin%:

Pi0el "ha#er Co#e

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3-3

Cump Mappin%:

Pi0el "ha#er Co#e

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3-*

"+inne# Mesh
2o$ for some e0amples of s+eletal ren#erin%& @ll of the s+innin% happens in the 'erte0 sha#ers8 an# so $e $ill not nee# to sho$ any pi0el sha#ers& This is not unusual: s+inne# an# uns+inne# %eometry can usually share the same pi0el sha#er& WeVll %i'e t$o e0amples& The first e0ample illustrates perMpi0el li%htin% of our omniUambient li%htin% ri%& Then $e!ll a## bump mappin%&

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3-,

"+inne# Mesh:

?erte0 "ha#er Input an# Nutput

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3--

"+inne# Mesh:

?erte0 "ha#er Globals

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3-.

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3-/

"+inne# 2ormalMmappe# Mesh:

?erte0 "ha#er Input an# Nutput

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3-1

"+inne# 2ormalMmappe# Mesh:

?erte0 "ha#er Globals

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3.0

"+inne# 2ormalMmappe# Mesh:

?erte0 "ha#er Co#e

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3.1

"+inne# 2ormalMmappe# Mesh:

?erte0 "ha#er Co#e

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3.)

That conclu#es Chapter 10& 2e0t8 Chapter 11:

Mechanics 1: =inear Winematics an# Calculus

Chapter 10 2otes

3D Math Primer for Graphics 3 Game De'

3.3

You might also like