tutorial 5 - matrices and transformations · 2017-06-19 · tutorial 5 - matrices and...

5
Tutorial Sheet 5 - Matrices and Transformations Tutorial 5 - Matrices and transformations Basics In the following, we want to look into the steps of the graphics pipeline dealing with perspective projection, i.e. the matrix multiplications that transform our 3D models and vectors into the 2D representations that are shown on the computer screen. Chapter 7 of the book and the slides from the lecture describe the individual steps involved in this process. It will be handy if you have those around while doing the following exercises. Let's start by taking a closer look into some of the individual steps involved. Exercise 1. Our 3D models and scenes are usually expressed in world coordinates: that is, with respect to a general coordinate system (usually a Cartesian coordinate system). In these world coordinates, the location and orientation of our camera can be described by the eye position specifying the camera's location and the view vector specifying the direction in which our camera is looking. To project the 3D scene towards our camera, it would be much easier if the origin was placed at the position of the eye and the z-axis was pointing in the viewing direction (or negative viewing direction). We can do that by moving from world coordinates to camera coordinates where our 3D models and vectors are expressed with respect to a coordinate system centered at the eye vector, and the axes are aligned with the viewing window and view vector. Let’s look at a simple example in 2D to understand the difference between “expressed in world coordinates” versus “expressed in camera coordinates”. Assume a 2D world coordinate system with base vectors 1 = (1, 0) and 2 = (0, 1), and a camera placed at position = (2, 1), looking in direction =( ! ! 2 , ! ! 2 ). Notice that the view vector is already a unit vector, so = and = (! ! 2 , ! ! 2 ) gives us a camera coordinate system. a) Draw an image of this scene including a point = (3, 1) expressed in world coordinates. b) If a point is expressed with respect to a particular coordinate system, e.g. the world coordinates given by 1 , 2 , we can denote this by xy . Likewise, if this point is expressed in camera coordinates, we denote it uv . Write down the general form of a point xy in world coordinates (i.e., write down xy as a linear combination of the base vectors 1 and 2 ) . Write down the general form of a point uv in camera coordinates (i.e., write down uv as a linear combination of the base vectors and ). Note: you don’t have to fill in and calculate the actual numbers yet. !" = ! ! ! ! ! + ! ! ! ! ! !" = ! ! + ! or: !" = + ! ! + ! , but we will ignore the translate for now.

Upload: others

Post on 07-May-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Tutorial 5 - Matrices and transformations · 2017-06-19 · Tutorial 5 - Matrices and transformations Basics In the following, we want to look into the steps of the graphics pipeline

TutorialSheet5-MatricesandTransformations

Tutorial 5 - Matrices and transformations

Basics

Inthefollowing,wewanttolookintothestepsofthegraphicspipelinedealingwithperspectiveprojection,i.e.thematrixmultiplicationsthattransformour3Dmodelsandvectorsintothe2Drepresentationsthatareshownonthecomputerscreen.Chapter7ofthebookandtheslidesfromthelecturedescribetheindividualstepsinvolvedinthisprocess.Itwillbehandyifyouhavethosearoundwhiledoingthefollowingexercises.Let'sstartbytakingacloserlookintosomeoftheindividualstepsinvolved.

Exercise1.

Our3Dmodelsandscenesareusuallyexpressedinworldcoordinates:thatis,withrespecttoageneralcoordinatesystem(usuallyaCartesiancoordinatesystem).Intheseworldcoordinates,thelocationandorientationofourcameracanbedescribedbytheeyeposition𝐸specifyingthecamera'slocationandtheviewvector𝑉specifyingthedirectioninwhichourcameraislooking.Toprojectthe3Dscenetowardsourcamera,itwouldbemucheasieriftheoriginwasplacedatthepositionoftheeyeandthez-axiswaspointingintheviewingdirection(ornegativeviewingdirection).Wecandothatbymovingfromworldcoordinatestocameracoordinateswhereour3Dmodelsandvectorsareexpressedwithrespecttoacoordinatesystemcenteredattheeyevector,andtheaxesarealignedwiththeviewingwindowandviewvector.

Let’slookatasimpleexamplein2Dtounderstandthedifferencebetween“expressedinworldcoordinates”versus“expressedincameracoordinates”.Assumea2Dworldcoordinatesystemwithbasevectors𝑏1=(1, 0)and𝑏2=(0, 1),andacameraplacedatposition𝐸=(2,1),lookingindirection𝑉=(!

!2, !!2).Noticethattheviewvectorisalreadyaunitvector,so𝑢 = 𝑉and𝑣 = (− !

!2, !!2)

givesusacameracoordinatesystem.

a) Drawanimageofthissceneincludingapoint𝑃=(3, 1)expressedinworldcoordinates.b) Ifapoint𝑃isexpressedwithrespecttoaparticularcoordinatesystem,e.g.theworldcoordinates

givenby𝑏1,𝑏2,wecandenotethisby𝑃xy.Likewise,ifthispointisexpressedincameracoordinates,wedenoteit𝑃uv.Writedownthegeneralformofapoint𝑃xyinworldcoordinates(i.e.,writedown𝑃xyasalinearcombinationofthebasevectors𝑏1and𝑏2).Writedownthegeneralformofapoint𝑃uvincameracoordinates(i.e.,writedown𝑃uvasalinearcombinationofthebasevectors𝑢and𝑣).Note:youdon’thavetofillinandcalculatetheactualnumbersyet.

𝑃!" = 𝑃!𝑏!!!!⃗ + 𝑃!𝑏!!!!⃗ 𝑃!" = 𝑃!𝑢!⃗ + 𝑃!�⃗�or:𝑃!" = 𝐸 + 𝑃!𝑢!⃗ + 𝑃!�⃗�,butwewillignorethetranslatefornow.

Page 2: Tutorial 5 - Matrices and transformations · 2017-06-19 · Tutorial 5 - Matrices and transformations Basics In the following, we want to look into the steps of the graphics pipeline

TutorialSheet5-MatricesandTransformations

c) Wecantransformbetweenthesetwocoordinatesystemsusingmatrixmultiplication.Theprocedureisthesameasinthecaseofrotationaroundanarbitraryvectorthatwediscussedinthelectureabouttransformations.Givethematrixthattransformsapointgivenincameracoordinatesintoonegiveninworldcoordinates,i.e.amatrix𝑀with𝑃xy=𝑀𝑃uv.Givethematrixthattransformsapointgiveninworldcoordinatesintoonegivenincameracoordinates,i.e.amatrix𝑀with𝑃uv=𝑀𝑃xy.

d) Fillintheactualnumbersandexpressthepoint𝑃xy=(3, 1)incameracoordinates.

Exercise2.

Inthesimple2Dexampleinthepreviousexerciseitwaseasytocreatethebasevectorsforthecameracoordinatesystem.For3D,thisprocessisalittlemorecomplicatedbecauseweneedthreebasevectors,butweonlyhaveone(theviewvector)todothis.Fortunately,wealreadysawhowtogenerateanorthonormalbasisgivenasinglevectorwhenwetalkedabouttransformationmatricesforrotationaroundanarbitraryvectorin3D.

a) Fortherotationaroundanarbitraryvectorin3D,weusedanon-parallelrandomvectortocreateourcoordinatesystemusingthecrossproduct.Here,weintroducedaso-calledviewupvector𝑢𝑝.Howisthatvectorspecified,andwhydoweneedit(i.e.whycan’twejusttakearandomvectorlikebefore)?

b) Oneoftheaxisofourcameracoordinatesystemwillbeanormalizedversionofeithertheviewvectororthenegativeviewvector.Itdependsofcourseifwewanttogetaleftorrighthandedone.Howcanwe,forexample,createarighthandedoneusingtheviewupvectorandthenegativeviewvector?

Cameramatrix:𝑀 = !!!√2 − !

!√2!!√2

!!√2

!

Inversecameramatrix:𝑀′ = 𝑀!! =

𝑀! = !!!√2

!!√2

− !!√2

!!√2

!

𝑃 = 𝑀′𝑃!" = !

12√2

12√2

−12√

212√

2! !31! =

!

12√2 ∗ 3 +

12√2 ∗ 1

−12√

2 ∗ 3 +12√

2 ∗ 1! = !2√2

−√2!.

Forrotationin3D,itdidn'tmatterhowwemappedthecoordinatesystemstoeachother.Here,itdoesmatter.Wewanttomaptheworldtocameracoordinatesinawaythattwoaxesareparalleltothewidthandheight,respectively,oftheviewingplaneandthethirdoneisorthogonaltoit.Thisisachievedbyusingtheupvectorinsteadofarandomone.Theupvectorisdefinedasavectorintheplanebisectingtheviewer'sheadintoleftandrighthalvesand"pointingtothesky".

Ifweareusingthenegativeviewvector,ourcamerapointsinnegativeZ-direction.Wegetthefirstotheraxisusingthecrossproductoftheupvectorwiththeviewvector.Buildingthecrossproductoftheresultingvectorwiththeviewvectorgivesusthe3rdaxisofourcameracoordinatesystem.Buthowcanwecontrolifweendupwithaleftorrighthandedone?Itdependsontheorderinwhichwemultiplythevectorsinthecrossproduct,since�⃗�×𝑏!⃗ = −𝑏!⃗ ×�⃗�.

Page 3: Tutorial 5 - Matrices and transformations · 2017-06-19 · Tutorial 5 - Matrices and transformations Basics In the following, we want to look into the steps of the graphics pipeline

TutorialSheet5-MatricesandTransformations

Exercise3.

Thefollowingmatrix𝑀orthmapstheorthographicviewvolumetothecanonicalviewvolume:

𝑀orth=

!!!!!

0 !!!!!!

0

0 !!!!!

!!!!!!

0

0 0 !!!!!!

!!"!!!

0 0 1 0

a) Howistheorthographicviewvolumedefined?b) Howisthecanonicalviewvolumedefined?c) Showthattheabovematrixtakesthecornersofthe

orthographicviewvolumetothecornersofthecanonicalviewvolume.

Exercise4.

Similarlytowhenweintroducedhomogeneouscoordinatesinordertobeabletodoaffinetransformations,wehadtofurtherextendourmatrixframeworktoenableustodoprojectivetransformations.

a) Why?b) Fora4×4matrixwhosetopthreerowsare

arbitraryandwhosebottomrowis(0,0,0,1),showthatpoints(𝑥, 𝑦, 𝑧, 1)and(ℎ𝑥, ℎ𝑦, ℎ𝑧, ℎ)transformtothesamepointafterhomogenization(forℎ ≠ 0).

c) Givenourextendedframework,theoriginal𝑧-coordinateismappedtoavalue𝑧s=𝑛 + 𝑓 −!"!

.Showalgebraicallythattheperspectivematrixpreservestheorderof𝑧valueswithintheviewvolume.

a)Theorthographicviewvolumeisanaxisparallelboxdefinedbyitsenclosingplanes[𝑙, 𝑟]×[𝑏, 𝑡]×[𝑓, 𝑛](seebook,page144-145).b)Thecanonicalviewvolumeisthecubecontainingall3DpointswhoseCartesiancoordinatesarebetween-1and+1(seebook,page143).

c)Toprovethis,wefirsthavetospecifythecornersofthetwovolumes.E.g.,fortheorthographicviewvolume,oneofthemis(r,t,n)(whichrepresentsthecornerontherightandtoponthenearplane).Thecorrespondingcorneronthecanonicalviewvolumeis(1,1,1).Multiplying(r,t,n)withthegivenmatrixshowsthatitdoesindeedrealizethismapping.

a)Becauseweneedtobeabletomapacoordinatetoanewvaluethatiscreatedbydividingthroughthevalueofanother

coordinate(e.g.forthex-coordinate:𝑥! =!"!)whichcan'tbe

donewithmatrixmultiplication.showsthatitdoesindeedrealizethismapping.

b)Toprovethis,webasicallyjusthavetowritedownthematrices(withgenericvaluesforthearbitraryrows)anddotherelatedarithmeticoperationstoseethatitdoesindeedcometothesameresult.Alternatively(andwithlesswriting)youcouldshowthisusingdistributivityofscalarmultiplication.Let𝑀bea4×4matrixasdescribed,thenwehave𝑀(𝑥 ,𝑦, 𝑧, 1) = (𝑥! , 𝑦! , 𝑧!, 1).Then,𝑀(ℎ𝑥, ℎ𝑦, ℎ𝑧, ℎ) = ℎ𝑀(𝑥, 𝑦, 𝑧, 1) =ℎ(𝑥!, 𝑦!, 𝑧!, 1) = (ℎ𝑥! , ℎ𝑦!, ℎ𝑧! , ℎ).Weseethatafterhomogenizationtheybothmapto(x’,y’,z’).

c)seeslides.

Page 4: Tutorial 5 - Matrices and transformations · 2017-06-19 · Tutorial 5 - Matrices and transformations Basics In the following, we want to look into the steps of the graphics pipeline

TutorialSheet5-MatricesandTransformations

Nowthatwehaveabetterunderstandingofsomeofthestepsinvolved,let'sgothroughthewholeprocessofprojectinga3Dmodelontoour2Dscreenwithaconcreteexample.Noticethatthefollowingrequiressomecalculationsthatarenotas“smooth”andeasyastheonesweusuallyhave.Ifyoureallywanttocalculateallthesematrices(andalthoughwewillgiveyou“nicer”onesintheexamitisrecommendedtodoso),youcanofcourseuseacalculatorhere(butnotintheexamwhere,assaid,wetrytomakethenumberseasiertocalculate).

Exercise5.

Let'sassumethatourmodelhasanobjectcenteredatthepoint(7, 16, 18).Now,insteadoflookingatitfromtheorigin,wewanttolookatitfrombehindandabove,soweplacetheoriginofourcameraat

(10, 20, 30).Whatistheviewvector𝑉weshouldspecifysothattheobjectiscenteredintheimage?

Exercise6.

Forourcamerainthepreviousproblem,wespecifyanupvector𝑢𝑝of(0, 1, 0).WearegoingtodoprojectioninthewayitisexplainedinChapter7ofthetextbookandtherelatedlecture.Explainhowwecomputethematrix𝑀camthatdoesthetransformationfromworldspacetocameraspace.Ifyouwanttocomputetheactualnumbers,bewarethattheyarenotreallynice(theycontainfractionsandsquareroots),socalculatingmaytakeawhile.Butassaid,itisinstructionaltodosuchcalculationsatleast

once.

Theviewvectoristhevectorspecifyingthelookingdirection.Inthiscase,itis𝑉!⃗ = (3,4,12).

First,weneedtoconstructanorthonormalbasis(𝑢!⃗ , �⃗�,𝑤!!⃗ )forthecamera.Here,𝑤!!⃗ issimplythenormalizedoppositeviewvector(notethatwelookintothenegative𝑤!!⃗ -direction),so𝑤!!⃗ = −𝑉!⃗ /|!𝑉!⃗ !|.Fillinginthenumbersgives𝑤!!⃗ =(3/13,4/13,12/13).Thevector𝑢!⃗ isperpendiculartotheplanespannedby𝑤!!⃗ andtheupvector.Sowetakethecrossproductofthesetwovectors,andnormalize:𝑢!⃗ = 𝑢𝑝!!!!⃗ ×𝑤!!⃗ /(||𝑢𝑝!!!!⃗ ×𝑤!!⃗ ||). Inourconcretecaseweget:𝑢!⃗ = ! !

√!", 0, !!

√!"!.

Finally,�⃗�isperpendicularto𝑤!!⃗ and𝑢!⃗ ,so�⃗� = 𝑤!!⃗ ×𝑢!⃗ .Inourcase,thisgives�⃗� = ( !!!"√!"

, !"!"√!"

, !!"!"√!"

).Wefindthematrix𝑀!"#byfirsttranslatingover(−𝑥! ,−𝑦! ,−𝑧!),andnextmultiplyingbythematrixwheretherowsareformedby𝑢!⃗ ,�⃗�,and𝑤!!⃗ (completedwithzerosandonesintheappropriateplaces).Thelattermatrixalignsthecameracoordinatesystemwiththeglobalworldcoordinatesystem.Ifyouworkoutthenumbers,youshouldendupwiththematrix:

⎜⎜⎛

!√!"

0 !!√!"

!!"√!"

!!!"√!"

!"!"√!"

!!"!"√!"

!!""!"√!"

!!"

!!"

!"!"

!!"#!"

0 0 0 1 ⎠

⎟⎟⎞.

Page 5: Tutorial 5 - Matrices and transformations · 2017-06-19 · Tutorial 5 - Matrices and transformations Basics In the following, we want to look into the steps of the graphics pipeline

TutorialSheet5-MatricesandTransformations

Exercise7.Ourcamerahasn'tbeencompletelyspecifiedyet.Amongstotherthings,weneedtosetthenearandfarplanedistances.Let'ssetthemat𝑛 = −1and𝑓 = −100,respectively(recallthatweviewalongthenegative𝑧-axis).Whatisthematrix𝑃thatdoestheperspectivetransform,giventhesevalues?(Note

thatinthiscase,thenumbersaremuchnicer.)

Exercise8.

Thefinalmatrixthatweneedtoconstructis𝑀orth,theonethattakescareoftheorthographicprojection.Let'sspecifyanimagewherewidthtimesheightis1024×768,andtheleft,right,topand

bottomplaneparametersare−4, 4, 4,and−3,respectively.Determine𝑀orth,giventheseparameters.

Exercise9.

Ifyoudare,computethefullmatrix𝑀=𝑀orth𝑃𝑀cam(or,ifyouusethenotationfromthe2ndeditionofthebook:𝑀=𝑀o𝑀p𝑀v).Ontowhichpixelisthepoint(7, 16, 18)projected?Recallthatwewantedittobecenteredintheimage.

The End (fornow)

Thismatrixcanbefoundinthelectureslides,andinthetextbookonpage152.Fillinginthenumbersisleftasanexerciseforthereader.

Seeexercise3.Youcanalsofinditintheslides,andinthetextbookonpage145.Completingtheanswerisamatteroffillinginthenumbersanddoingthematrixmultiplications.Noticehowever,thatyouarestronglyadvisedtonotjustcopytheformulasandfillinthenumbers,buttomakesureyouunderstandhowwegottothismatrixinthefirstplace!

Anyonebraveenoughtocomputeallmatrixmultiplicationssuchthattheresultingmatrixmapsthepoint(7,16,18)ontothecenteroftheimagedeservestheutmostrespect.Ididn'tdoitmyself;theresultisnotveryimportant,butunderstandingthewholeprocedureis.