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.
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�⃗�×𝑏!⃗ = −𝑏!⃗ ×�⃗�.
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.
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 ⎠
⎟⎟⎞.
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.