Post on 23-Aug-2020
18/04/26
1
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
CS380:IntroductiontoComputerGraphicsVaryingVariables
Chapter13
MinH.KimKAISTSchoolofComputing
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
SUMMARYRasterization
2
18/04/26
2
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Pathfromvertextopixel
3
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Clippingcoordinates• Eyecoordinates(projected)àclipcoordinatesànormalizeddevicecoordinates(NDCs)
• (reminder)Dividingclipcoordinatesbythecomponent(thefourthcomponentinthehomogeneouscoordinates)yieldsnormalizeddevicecoordinates(NDCs).
4
(xc , yc , zc ,wc )wc(wc = wn )
xnwn
ynwn
znwn
wn
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
=
xcyczcwc
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
=
sx 0 −cx 00 sy −cy 0
0 0 f + nf − n
− 2 fnf − n
0 0 −1 0
⎡
⎣
⎢⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥⎥
xeyeze1
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
18/04/26
3
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Viewportmatrix
5
• Weneedatransformthatmapsthelowerleftcornertoandupperrightcornerto
• Theappropriatescaleandshiftcanbedoneusingtheviewportmatrix:
[−0.5,−0.5]t
[W − 0.5,H − 0.5]t
xwywzw1
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
=
W / 2 0 0 (W −1) / 20 H / 2 0 (H −1) / 20 0 1/ 2 1 / 20 0 0 1
⎡
⎣
⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥
xnynzn1
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
TextureViewport• Theabstractdomainfortexturesisnotthecanonicalsquare,butinsteadistheunitsquare.
• Inthiscasethecoordinatetransformationmatrixis:
6
xwyw−1
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
=
W 0 0 −1/ 20 H 0 −1/ 2− − − −0 0 0 1
⎡
⎣
⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥
xtyt−1
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
18/04/26
4
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
VARYINGVARIABLESChapter13andAppendixB
7
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Interpolationofvaryingvariables• Inbetweenthevertexandfragmentshader,weneedtointerpolatethevaluesofthevaryingvariables.
• Whatisthedesiredinterpolant,andhowshouldwecomputeit.
• Thisissurprisinglysubtle.
8
18/04/26
5
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Texturemapping
9
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Wrongrepresentationoftexture
10
Whentexturecoordinatesarelinearlyinterpolatedinwindowcoordinates,anincorrectimageresults.
(xt , yt )
18/04/26
6
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Correctrepresentationoftexture
11
Howcanweinterpolatetexturecoordinatesproperlylikethis?(xt , yt )
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Motivation:texturecoordinates• Letusmapasquarecheckerboardimageontoaquad
• Webreakupthequadandtheimageeachintotwotriangles.
• Wewillassociatetexturecoordinatesforeachvertex.
• Wedesirethatintheinteriorofatriangle,shouldbedeterminedastheuniqueinterpolantfunctionsoverthetrianglethatareaffinein
12
[xt , yt ]t
[xt , yt ]t
(xo , yo ,zo)
18/04/26
7
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Motivation:texturecoordinates• Evenstepsonthegeometryshouldbeevenstepsinthetexture.
• Ifweusethisinterpolationandfetchthetexturevalues,weshouldgetanexpectedforeshorteningeffect.
13
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Linearinterpolation• Supposewesimplyusedlinearinterpolationonthefunctionsontexture.
• Then,aswemovebysomefixed2Dvectordisplacementonthescreen,thetexturecoordinateswillbeupdatedbysomefixed2Dvectordisplacementintexturecoordinates.
• Inthiscase,allofthesquaresofthetexturewillmaptoequalsizeparallelograms.
• Wewillgetanoddseamwherethetwotrianglesmeet.
14
xt and yt
18/04/26
8
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Affinefunctionsin2D• Wesayafunctionisanaffinefunctioninthevariablesifitisoftheform:forsomeconstants
• Thiscanalsobewrittenas:
15
v(x , y)=ax+by+c
v( )
a,b and c
v = a b c⎡⎣
⎤⎦
xy1
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
x and y
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Affinefunctionsin2D• Alsocalled‘linear’butnotetheadditiveconstantterm.
• Anaffinefunctioncanbeevaluatedbyplugginginorbyincrementalevaluationalongalineinspace.
• Wealreadysawforexamplethatisanaffinefunctionofandsowasa3D“edgefunction”:
16
x, yx, y
znxn , yn
zn = axn + byn + c
18/04/26
9
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Affine/linearinterpolationin2D• Ifwearegiven,thevaluesofforthree(non-collinear)pointsinthe2Dplane,saytheverticesofatriangle,
• Thisdeterminesanaffinefunctionovertheentireplane.
17
vi v(x, y)
v
vi
x1
x2
x3
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Affine/linearinterpolationin2D• Inthiscase,wesaythatisthelinearinterpolantofthevaluesatthethreevertices.
• Theprocessofevaluatingthelinearinterpolantofthreeverticesiscalledlinearinterpolation.
• Giventheonecanusesomesimplematrixoperationstosolvefortheconstants.
18
v
vi
[a,b,c]vi
x1
x2
x3
18/04/26
10
Barycentric Interpolation How do you interpolate values defined at vertices across the entire triangle?
Barycentric Interpolation How do you interpolate values defined at vertices across the entire triangle?
Solve a simpler problem first:
x 1
x 2
1DInterpolation
18/04/26
11
x 2
Barycentric Interpolation How do you interpolate values defined at vertices across the entire triangle? Solve a simpler problem first:
0
x 1 t
Want to define a value for every
0
1
1 t
t ∈ [0,1]
1DInterpolation
Barycentric Interpolation How do we come up with this equation? Look at the picture!
t
1DInterpolation
18/04/26
12
Barycentric Interpolation
The further t is from the red point, the more blue we want.
t
1DInterpolation
Barycentric Interpolation
The further t is from the red point, the more blue we want. The further t is from the blue point, the more red we want.
t
1DInterpolation
18/04/26
13
t
Barycentric Interpolation
The further t is from the red point, the more blue we want. The further t is from the blue point, the more red we want. Percent blue = (length of blue segment)/(total length)
1DInterpolation
Barycentric Interpolation
The further t is from the red point, the more blue we want. The further t is from the blue point, the more red we want. Percent blue = (length of blue segment)/(total length) Percent red = (length of red segment)/(total length)
t
1DInterpolation
18/04/26
14
Barycentric Interpolation
The further t is from the red point, the more blue we want. The further t is from the blue point, the more red we want. Percent blue = (length of blue segment)/(total length) Percent red = (length of red segment)/(total length)
Value at t = (% blue)(value at blue) + (% red)(value at red)
t
1DInterpolation
The further t is from the red point, the more blue we want. The further t is from the blue point, the more red we want.
Percent blue = t Percent red = 1-t Value at t = tx + (1-t)x
1 2
x 1
x 2
Barycentric Interpolation 0
t 1
1DInterpolation
18/04/26
15
Barycentric Interpolation Now what about triangles?
2DInterpolation
Barycentric Interpolation
Now what about triangles?
Just consider the geometry:
What’s the interpolated value at the point p?
x
p
x 3
1
x 2
2DInterpolation
18/04/26
16
Barycentric Interpolation
Last time (in 1D) we used ratios of lengths.
t
p
x 1
x 2
x 3
2DInterpolation
Barycentric Interpolation Now what about triangles?
Just consider the geometry:
What about ratios of areas (2D)?
p
2DInterpolation
18/04/26
17
Barycentric Interpolation Now what about triangles?
Just consider the geometry:
p
If we color the areas carefully, the red area (for example) covers more of the triangle as p approaches the red point.
2DInterpolation
p
Barycentric Interpolation
Just like before: percent red = area of red triangle
total area
percent green = area of green triangle total area
percent blue = area of blue triangle total area
2DInterpolation
18/04/26
18
p
Barycentric Interpolation
Just like before: percent red
Value at p: (% red)(value at red) + (% green)(value at green) + (% blue)(value at blue)
= area of red triangle total area
percent green = area of green triangle total area
percent blue = area of blue triangle total area
2DInterpolation
p
x 3
A 3
A 1
A 1
A 2
x 1
x 2
Barycentric Interpolation
“barycentric interpolation” a.k.a.“convex combination” a.k.a.“affine linear extension”
Just like before: percent red =
Σ λ =1 i i
Why? Look at the picture!
“barycentric coordinates”
= λ 1
= λ 2
= λ3
Value at p: (A x + A x + A x )/A
1 1 2 2 3 3
A A 2
A A 3
A
percent green =
percent blue =
Now convert this to a bunch of ugly symbols if you want... just don’t think about it that way!
2DInterpolation
18/04/26
19
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Affinefunctionsin3D• Wesayafunctionisaffineinvariableifitisoftheform:
• Suchafunctioncanbeuniquelydeterminedbyitsvaluesatthefourverticesofatetrahedronsittingin3D.
37
vx, y and z
v(x , y ,z)=ax+by+cz+d
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Trianglesin3D• Givenatrianglein3D,supposewespecifythevalueofafunctionatitsthreevertices.
• Theremaybemanyfunctionsthatareaffineinthatagreewiththesethreevalues.
• Butallofthesefunctionswillagreewhenrestrictedtotheplaneofthetrianglein2D.
• Assuch,wecanrefertothisrestrictedfunctionoverthetriangleasthelinearinterplantofthevertexvalues.
38
(x, y, z)
18/04/26
20
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Interpolatingcolorsofanobject• Whenweassociatecolorswithvertices,itisnaturaltointerpretourdesiredcolorfieldtobetheuniqueinterpolatingfunctionoverthetrianglethatisaffineintheobjectcoordinates,,notinscreencoordinates!
• Duringtexturemapping,itisnaturaltointerpreteachofthetexturecoordinates,,astheuniqueinterpolatingfunctionsoverthetrianglethataffinein.
39
(xo , yo ,zo)
(xt , yt )
(xo, yo, zo )
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Interpolatingcolorsofanobject• Asaratherselfreferentialexample,wecaneventhinkofeachofthethreeobjectcoordinatesofapointonsometrianglein3Dasaffinefunctionsin.Forexample,
• Forthisreason,thedefaultsemanticsofOpenGListointerpolateallvaryingvariablesasfunctionsovertrianglesthatareaffinein.
40
(xo, yo, zo )
v(xo , yo ,zo)= xo (whena=1andb= c =d =0)
(xo, yo, zo )
18/04/26
21
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Interpolatingcolorsofanobject• Aswewillsee,thisisequivalenttoafunctionbeingaffineovereyecoordinates,,butitisnotequivalenttoafunctionbeingaffineovernormalizeddevicecoordinates,,orwindowcoordinates.
41
(xe, ye, ze )
(xn , yn , zn )
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Goingdown• Ifwehaveafunctionthatisaffineinwhenrestrictedtoatrianglein3D,
• Thenwecanusethefactthatthetriangleisflattowriteasafunctionthatisaffineinonlytwovariables.
• Idea:writeasanaffinefunctionofplugthisintotheaffineexpressionfor.
42
v (x, y, z)
v
z (x, y)v
z =ex+ fy+ gv =ax+by+c(ex+ fy+ g)+dv =hx+ iy+ j
18/04/26
22
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Goingsideways• Supposewehavesomematrixexpressionoftheform:forsome4-by-4matrixM(whereMdoesnotevenhavetobeanaffinematrix).
• Then,justlookingatthefourrowsindependently,weseethatareallaffinefunctionsof.
43
x 'y 'z 'w '
⎡
⎣
⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥
= M
xyz1
⎡
⎣
⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥
x ', y ',z ', andw'(x , y ,z)
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Moresideways• Ifwehaveafunctionvwhichisaffinein,then,giventherelationofthepreviousequation,wecanseethatvisalsoaffine.Toseethis,notethat:
44
(x, y, z)
(x ', y ', z ',w ')
v = a b c d⎡⎣
⎤⎦
xyz1
⎡
⎣
⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥
v = a b c d⎡⎣
⎤⎦M
−1
x 'y 'z 'w'
⎡
⎣
⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥
∵
x 'y 'z 'w'
⎡
⎣
⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥
=M
xyz1
⎡
⎣
⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥
18/04/26
23
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Moresideways
• Sothepropertyofaffine-nessisinagreement– betweenobjectoreyeorclipcoordinates,andalso– betweennormalizedandwindowcoordinates.
45
v = e f g h⎡⎣
⎤⎦
x 'y 'z 'w'
⎡
⎣
⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Notsideways• Theonlytimewehavetobecarefuliswhendivisionisdone.
• Forexample,giventherelation:
vs.
• Itwillgenerallynotbethecasethatafunctionv,whichwasaffineinwillbeaffineinor.
• OurvaryingvariablesarenotaffineinNDCorwindowscoordinates.
46
x 'w 'y 'w 'z 'w 'w '
⎡
⎣
⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥
= M
xyz1
⎡
⎣
⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥
(x, y, z)(x ', y ', z ') (x ', y ', z ',w ')
x 'y 'z 'w '
⎡
⎣
⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥
= M
xyz1
⎡
⎣
⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥
18/04/26
24
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Howtoevaluatethevaryingvariables
• Recall
• Invertingourmatrices,thisimpliesthatateachpointonthetriangle,wehavetherelation:
47
xnwn
ynwn
znwn
wn
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
= PM
xoyozo1
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
xoyozo1
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
= M −1P−1
xnwn
ynwn
znwn
wn
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Howtoevaluatethevaryingvariables
• Nowsupposethatvisanaffinefunctionof.
• Wealsomakeuseoftheobviousfactthattheconstantfunction1isalsoaffinein
• Thusforsome,wehave
48
(xo, yo, zo )
(xo, yo, zo )(a,b,c,d)
v1
⎡
⎣⎢
⎤
⎦⎥ =
a b c d
0 0 0 1
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
xoyozo1
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
18/04/26
25
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Howtoevaluatethevaryingvariables
• Andtherefore:
• Forsomeappropriatevaluese…l49
v1
⎡
⎣⎢
⎤
⎦⎥=
a b c d
0 0 0 1
⎡
⎣
⎢⎢
⎤
⎦
⎥⎥M−1P−1
xnwn
ynwn
znwn
wn
⎡
⎣
⎢⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥⎥
v1
⎡
⎣⎢
⎤
⎦⎥=
e f g h
i j k l
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
xnwn
ynwn
znwn
wn
⎡
⎣
⎢⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥⎥
∵
xoyozo1
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
=M−1P−1
xnwn
ynwn
znwn
wn
⎡
⎣
⎢⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥⎥
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Howtoevaluatethevaryingvariables
• Nowdividebothsidesbyandweget
• Conclusion:areaffinefunctionsofnormalizeddevicecoordinates.
50
wn
vwn
1wn
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
=e f g h
i j k l
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
xnynzn1
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
vwn
and 1wn
18/04/26
26
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Howtoevaluatethevaryingvariables• “goingsideways”wededucethatareaffinefunctionsofwindowcoordinates.
• “goingdown”wecanconclude:arebothaffinefunctionof.
• Meaningthatwecancalculatetheirvaluesateachpixel,justgiventheirvaluesattheverticesandlinearinterpolation.
• Theabovederivationcannowbethrownaway.51
vwn
and 1wn
(xw , yw ,zw )
vwn
and 1wn
(xw , yw )
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
VVinOpenGL• NowwecanseehowOpenGLcanperformthecorrect“rationallinearinterpolation”tocalculatevateachpixel.– Thevertexshaderisrunoneachvertex,calculatingclipcoordinatesandvaryingvariablesforeachvertex.
– Clippingisrunoneachtriangle;thismaycreatenewvertices.Linearinterpolationinclipcoordinatesspaceisruntodeterminetheclipcoordinatesandvaryingvariablevaluesforeachsuchnewvertex.
52
18/04/26
27
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
VVinOpenGL
– Foreachvertex,andforeachvaryingvariablev,OpenGLcreatesaninternalvariable.Additionally,foreachvertexOpenGLcreatesoneinternalvariable.
53
vwn
1wn
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
VVinOpenGL
– Foreachvertex,divisionisdonetoobtainthenormalizeddevicecoordinates.
54
xn =xcwc
, yn =ycwc
, zn =zcwc
,
18/04/26
28
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
VVinOpenGL
– Foreachvertex,thenormalizeddevicecoordinatesaretransformedtowindowcoordinates.
– Thecoordinatesareusedtopositionthetriangleonthescreen.
55
[xw , yw ]t
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
– Foreveryinteriorpixelofthetriangle,linearinterpolationisusedtoobtaintheinterpolatedvaluesof.
– Ateachpixel,theinterpolatedvalueisusedforz-buffering.
56
zw ,
vwn
(forallv)and 1wn
zw
18/04/26
29
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
VVinOpenGL
• Ateachpixel,andforallvaryingvariables,divisionisdoneontheinterpolatedinternalvariablestoobtainthecorrectanswer:
57v = v
wn
⎛
⎝⎜⎜
⎞
⎠⎟⎟/
1wn
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
VVinOpenGL
• Thevaryingvariablesvispassedintothefragmentshader.
58