Transcript
Page 1: Mid-term exam - KAIST

18/04/26

1

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

CS380:IntroductiontoComputerGraphicsRasterizationChapter12

MinH.KimKAISTSchoolofComputing

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Mid-termexam•  Resultsareavailableinthelabsession!•  TAwillexplainthequestionsandanswersinthelabsessioninthisweek.

•  Don’tworryaboutresultstoomuch.It’sjust25%!Let’sjustkeepmovingforward!J

2

Page 2: Mid-term exam - KAIST

18/04/26

2

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

SUMMARYDepth

3

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Visibility

•  Wecouldexplicitlystoreeverythinghitalongarayandthencomputetheclosest.– Makesenseinaraytracingsetting,whereweareworkingonepixelperrayatatime,butnotforOpenGL,whereweareworkingonetriangleatatime.

4

Page 3: Mid-term exam - KAIST

18/04/26

3

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Z-buffer•  Wewillusez-buffer•  Trianglearedrawninanyorder•  Eachpixelinframebufferstores‘depth’valueofclosestgeometryobservedsofar.

•  Whenanewtriangletriestosetthecolorofapixel,wefirstcompareitsdepthtothevaluestoredinthez-buffer.

•  Onlyiftheobservedpointinthistriangleiscloser,weoverwritethecoloranddepthvaluesofthispixel.

5

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Proj.Trans.:Eyecoor.àNDC

6

•  Cameraprojectiontransformation

xnwn

ynwn

znwn

wn

⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥

=

xcyczcwc

⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥

=M

xeyeze1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

M =

1atan θ

2⎛

⎝⎜⎞

⎠⎟

0 0 0

0 1tan θ

2⎛

⎝⎜⎞

⎠⎟

0 0

0 0 f +nf −n

−2 fnf −n

0 0 −1 0

⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥

or

M =

−2nr − l

0 r+ lr − l

0

0 −2nt −b

t +bt −b

0

0 0 f +nf −n

−2 fnf −n

0 0 −1 0

⎢⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥⎥

Page 4: Mid-term exam - KAIST

18/04/26

4

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Stagesofvertextransformation

7

Modelviewmatrix

Projectionmatrix

Perspectivedivision

Viewporttransforma

tion

eyecoordinates

http://www.glprogramming.com/red/chapter03.html

clipcoordinates

normalizeddevicecoordinates

windowcoordinates

vertex

E−1O

xoyozo1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

=

xeyeze1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

xoyozo1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

M

xeyeze1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

=

xcyczcwc

⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥

1wn

xcyczcwc

⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥

=

xnynzn1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

RASTERIZATIONChapter12

8

Page 5: Mid-term exam - KAIST

18/04/26

5

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Vertexshader(pervertex)•  Takestheobjectcoordinatesofeveryvertexpositionandturnsthemintoeyecoordinates,aswellasthevertex’snormalcoordinates

9

#version130uniformMatrix4uModelViewMatrix;uniformMatrix4uNormalMatrix;uniformMatrix4uProjMatrix;invec3aColor;invec4aNormal;invec4aVertex;outvec3vColor;outvec3vNormal;outvec3vPosition;

voidmain(){vColor=aColor;vPosition=uModelViewMatrix*aVertex;vec4normal=vec4(aNormal.x,aNormal.y,aNormal.z,0.0);vNormal=vec3(uNormalMatrix*normal);gl_Position=uProjMatrix*vPosition;}

E−1Oc

PE−1OcCameraprojection(3Dàà2D)

Vertexintheeyeframe(noprojectionhere!)

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Fragmentshader(pixelcolor)

10

#version130uniformvec3uLight1,uLight2;//theeyecoordinatesoflightsuniformvec3uColor;//useglVertexUniform3fcalltopassthemtotheshadersinvec3vNormal;//eyecoordinatesinvec3vPosition;//eyecoordinatesoutfragColor;//outputvariableoftheinterpolatedcolordataperpixelvoidmain(){vec3toLight=normalize(uLight–vec3(vPosition));//directiontothepointlightvec3normal=normalize(vNormal);floatdiffuse=max(0.0,dot(normal,toLight1));//negativeclampingofthecosinelawdiffuse+=max(0.0,dot(normal,toLight2));//addlight2energyvec3intensity=uColor*diffuse;//onlyapplydiffusereflectancefunctionofBRDFfragColor=vec4(intensity.x,intensity.y,intensity.z,1.0);//4thelementisopacity.}

Vertexintheeyeframe(noprojectionhere!)

Page 6: Mid-term exam - KAIST

18/04/26

6

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Pathfromvertextopixel

11

•  Threeverticeshavepassedthroughthevertexshader

•  Followitjourneytobeabunchofpixels

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Varyingvariables•  Varyingvariablesprovidesaninterfacebetweenthevertexandfragmentshader.

•  Whentheprimitives(triangles)areassembledandfragmentscomputed,foreachfragmentthereisasetofvariablesthatareinterpolatedautomaticallyandprovidedtothefragmentshader.

•  Anexampleisthecolorofthefragment.Thecolorthatarrivesatthefragmentshaderistheresultoftheinterpolationofthecolorsoftheverticesthatmakeuptheprimitive.–  varyingfloatintensity;

12

Page 7: Mid-term exam - KAIST

18/04/26

7

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Clipping•  Whatifthereisavertexbehindyou?

13

Δp1p2p3

p1 p2

p2

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Clipping•  Notonlyfrontvertexbutalsobackvertexwillbeprojected

•  Wewillbedrawingincompletelythewrongarea.

14

•  BewareinterpolationnatureofOpenGL:Projectionofverticesfollowedbyinterpolation

p1 p2

p2

Page 8: Mid-term exam - KAIST

18/04/26

8

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Clipping•  Processingfortrianglesthatarefullyorpartiallyoutofview

•  Wedon’twanttoseebehindus

•  Wewanttominimizeprocessing

•  Thetrickypartwillbetodealwitheye-spanningtriangles.

15

p1 p2

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Eyespanners•  Backvertexprojectshigherupintheimage

•  Fillinginthein-betweenpixelswillfillinthewrongregion.

•  Solution:sliceupthegeometrybythesixfacesoftheviewfrustum

16

p1 p2

Page 9: Mid-term exam - KAIST

18/04/26

9

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Clippingcoordinates•  Eyecoordinates(projected)àclipcoordinatesànormalizeddevicecoordinates(NDCs)

•  (reminder)Dividingclipcoordinatesbythecomponent(thefourthcomponentinthehomogeneouscoordinates)yieldsnormalizeddevicecoordinates(NDCs).

17

(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

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Clippingcoordinates•  Supposethatisthesameas.Whatifiszero?

•  Ifyouwaitfornormalizeddevicecoordinates(NDCs)wherethevertexhasflipped,andit’stoolatetodotheclipping(àdivided-by-zeroerror!).

•  Wecoulddointheeyespace,butthenwouldneedtousethecameraparameters

•  Thesolutionistouseclipcoordinates:post-matrix-multiplybutpre-divide.

•  Nodivide=noflipping

18

xc /wc −xc / −wc

wc

E−1O

xoyozo1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

=

xeyeze1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

M

xeyeze1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

=

xcyczcwc

⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥

1wn

xcyczcwc

⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥

=

xnynzn1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥ wn =wc = −ze

Page 10: Mid-term exam - KAIST

18/04/26

10

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Clippingcoordinates•  Recallthatwewantpointsintherange:

•  Inclipcoordinatesthisis:

19

−1< xn <1−1< yn <1−1< zn <1

−wc < xc < wc

−wc < yc < wc

−wc < zc < wc

xnwn

ynwn

znwn

wn

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

=

xcyczcwc

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Clippingcoordinates•  Primitives(triangles)totallyinsidetheclippingvolumearenotaltered.

•  Primitivesoutsidetheviewingvolumearediscarded.

•  Primitiveswhoseedgesintersecttheboundariesoftheclippingvolumeareclipped(learnlater).

20

Page 11: Mid-term exam - KAIST

18/04/26

11

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

ClippingcoordinatesàNDCs•  Clippingisdone,wecannowdividebytoobtainnormalizedevicecoordinates(NDCs).

21

wc (typically -ze )

xnynzn1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

=

xc /wc

yc /wc

zc /wc

wc /wc

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

wn =wc = −ze

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Backfaceculling(killing?)•  Whendrawingaclosedsolidobject,wewillonlyeverseeone‘front’sideofeachtriangle.

•  Forefficiencywecandropthesefromtheprocessing.

22

Page 12: Mid-term exam - KAIST

18/04/26

12

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Withoutbackfaceculling

23

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Withbackfaceculling

24

Page 13: Mid-term exam - KAIST

18/04/26

13

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Backfaceculling•  Todothis,inOpenGL,weusetheconventionoforderingthethreeverticesinthedrawcall(IBO/VBO)sothattheyarecounterclockwise(CCW)whenlookingatitsfrontside.

•  Duringsetup,wecallglEnable(GL_CULL_FACE)•  Toimplementculling,OpenGLdoesthefollowing…

25

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

•  Letbethethreeverticesofthetriangleprojecteddowntotheplane.

•  Definethevector•  Nextcomputethecrossproduct

•  Ifthethreeverticesarecounterclockwiseintheplane,thenwillbeinthedirection(frontface).

MathofBackfaceCulling

26

p1, p2, and p3(xn , yn ,0)

a = p3 − p2 and

b = p1 − p2

c = a ×

b

c

+zn

Page 14: Mid-term exam - KAIST

18/04/26

14

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

•  Sotheareaofthetriangleis:•  Takingaccountofthedirection,wecouldcalculatethedirectionofthecrossproductofthesetwovectors.

•  Ifthisisnegative,thepolygonlooksbackward

MathofBackfaceCulling

27

Area = 1

2a ×b

(xn3 − xn

2 )(yn1 − yn

2 )− (yn3 − yn

2 )(xn1 − xn

2 )

NBTherearetyposatpage114inourtextbook.Thenegativeandpositivewereflippedaroundinthetextbook.Theseslidesaretypo-corrected.

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Viewport(NDCàWindow)•  Nowwewanttopositiontheverticesinthewindow.SoitistimetomovetheNDCstowindowcoordinates.–  inNDCs,lowerleftcorneris[-1,-1]tandupperrightcorneris[1,1]t.

•  Eachpixelcenterhasapositiveintegercoordinate.–  Thiswillmakesubsequentpixelcomputationsmorenatural.

•  Wewantthelowerleftpixelcentertohave2Dwindowcoordinatesofandtheupperrightpixelcentertohavecoordinates

28

[0,0]t

[W −1,H−1]t

Page 15: Mid-term exam - KAIST

18/04/26

15

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Viewport•  Wethinkofeachpixelasowningtherealestatewhichextends0.5pixelunitsinthepositiveandnegative,horizontalandverticaldirectionsfromthepixelcenter.

29

12pixels

8pixels

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Viewport•  Thustheextentof2Dwindowrectanglecoveredbytheunionofallourpixelsistherectangleinwindowcoordinateswithlowerleftcornerandupperrightcorner

30

[−0.5,−0.5]t [W − 0.5,H − 0.5]t

Page 16: Mid-term exam - KAIST

18/04/26

16

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Summary:Depthvalues

•  Withthisoptionon:•  Withnear/farfieldà– Farobject:– Nearobject:

31

zn =

1−ze

ze =−1,000,000 ⇒ zn =

11,000,000 ≈0

ze =−1 ⇒ zn =1

glDepthFunc(GL_GREATER)

−1(far)< zn <1(near)

NBThedefaultofOpenGLisglDepthFunc(GL_LESS)!

−2nr − l

0 r+ lr − l

0

0 −2nt −b

t +bt −b

0

0 0 f +nf −n

−2 fnf −n

0 0 −1 0

⎢⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥⎥

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Viewportmatrix

32

•  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

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

Page 17: Mid-term exam - KAIST

18/04/26

17

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Viewportmatrix•  Thisdoesascaleandshiftinbothxandy•  Youcanverifythatitmapsthecornersappropriately

•  InOpenGL,wesetupthisviewportmatrixwiththecallglViewport(0,0,W,H)

•  Thethirdrowofthismatrixisusedtomaptherangeofvaluestothemoreconvenientrange.

•  Sonow(inourconventions),isfarandisnear.

33

[−1,−1] zn[0...1]

zw =0zw =1

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Viewportmatrix•  SowemustalsotellOpenGLthatwhenweclearthez-buffer,weshouldsetitto0;wedothiswiththecallglClearDepth(0.0)

34

Page 18: Mid-term exam - KAIST

18/04/26

18

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

TextureViewport•  Theabstractdomainfortexturesisnotthecanonicalsquare,butinsteadistheunitsquare.

•  Itslowerleftcorneris[0,0]tandupperrightcorneris[1,1]t.

•  Inthiscasethecoordinatetransformationmatrixis:

35

xwyw−1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

=

W 0 0 −1/20 H 0 −1/2− − − −0 0 0 1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

xtyt−1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Summaryoforigins

36

OpenGLNDCs�

(0,0)

(-1,-1)

(1,1)(-1,1)

(1,-1)

GLUT&Image

Coordinates

(0,1)

(1,0)(0,0)

(1,1)

OpenGL

Texture

Coordinates

(0,0)

(1,1)(0,1)

(1,0)

Page 19: Mid-term exam - KAIST

18/04/26

19

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Rasterization•  Startingfromthewindowcoordinatesforthethreevertices,therasterizerneedstofigureoutwhichpixelcentersareinsideofthetriangle.

•  Eachtriangleonthescreencanbedefinedastheintersectionofthreehalf-spaces.

37

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Rasterization•  Startingfromthewindowcoordinatesforthethreevertices,therasterizerneedstofigureoutwhichpixelcentersareinsideofthetriangle.

•  Eachtriangleonthescreencanbedefinedastheintersectionofthreehalf-spaces.

38

Page 20: Mid-term exam - KAIST

18/04/26

20

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Mathofrasterization•  Eachsuchhalfspaceisdefinedbyalinethatcoincideswithoneoftheedgesofthetriangle,andcanbetestedusingan‘edgefunction’oftheform:wheretheareconstantsthatdependonthegeometryoftheedge.

39

edge=axw +byw +c(a,b,c)

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Mathofrasterization•  Apositivevalueofthisfunctionatapixelwithcoordinatesmeansthatthepixelisinsidethespecifiedhalfspace(ontheleft-handside).

•  Ifallthreetestspass,thenthepixelisinsidethetriangle.

40

[xw , yw ]t

positive

Page 21: Mid-term exam - KAIST

18/04/26

21

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Speedup•  Onlylookatpixelsintheboundingboxofthetriangle

•  Testifapixelblockisentirelyoutsideoftriangle•  Useincrementalcalculationsalongascanline.

41

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Interpolation•  Asinputtorasterization,eachvertexalsohassomeauxiliarydataassociatedwithit.– Thisdataincludesavalue,– Aswellasotherdatathatisrelated,butnotidenticaltothevaryingvariables.

•  Itisalsothejoboftherasterizertolinearlyinterpolatethisdataoverthetriangle.

42

zw

Page 22: Mid-term exam - KAIST

18/04/26

22

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Mathofinterpolation•  Eachsuchvaluetobelinearlyinterpolatedcanberepresentedasanaffinefunctionoverscreenspacewiththeform:

•  Anaffinefunctioncanbeeasilyevaluatedateachpixelbytherasterizer.

•  Indeed,thisisnodifferentfromevaluatingtheedgetestfunctionsjustdescribed.

43

v

v = axw + byw + c

p = a+α(b−a)p = a+α!vp = (1−α)a+αb

a bp

α (1-α) !v

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Boundaries•  Forpixelonedgeorvertexitshouldberenderedexactlyonce.

•  Needspecialcareintheimplementationtoavoiddutyedgerepresentation.

44


Top Related