cs380: introduction to computer graphics track-/arc-ball...
TRANSCRIPT
18/04/06
1
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
CS380:IntroductiontoComputerGraphicsTrack-/Arc-ball
Chapter8
MinH.KimKAISTSchoolofComputing
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
SUMMARYQuaternion
2
18/04/06
2
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Unitnormquats.==rotations• Squarednormissumof4squares.• Anyquaternionoftheformhasaunitform
• Conversely,anysuchunitnormquaternioncanbeinterpreted(alongwithitsnegation)asauniquerotationmatrix.
3
ωc
⎡
⎣⎢
⎤
⎦⎥
1= ω2+ x2+ y2+z2 , c =[x , y ,z]t
cos θ2⎛
⎝⎜⎞
⎠⎟
sin θ2⎛
⎝⎜⎞
⎠⎟k
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
=
cos θ2⎛
⎝⎜⎞
⎠⎟
sin θ2⎛
⎝⎜⎞
⎠⎟k x
sin θ2⎛
⎝⎜⎞
⎠⎟k y
sin θ2⎛
⎝⎜⎞
⎠⎟kz
⎡
⎣
⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥ k =1
ω c
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Operations• Quat*quatmultiply
• Whereandarethedotandcrossproducton3dimensionalcoordinatevectors.
• Correctlymodels(rotationmatrix)*(rotationmatrix)multiplications
• Trackballexample:
4
ω1
c1
⎡
⎣⎢⎢
⎤
⎦⎥⎥
ω 2
c2
⎡
⎣⎢⎢
⎤
⎦⎥⎥=
(ω1ω 2 − c1 ⋅ c2 )(ω1c2 +ω 2c1 + c1 × c2 )
⎡
⎣⎢⎢
⎤
⎦⎥⎥
⋅ ×
M=Matrix4::makeXRotation(-dy)*Matrix4::makeYRotation(dx);
18/04/06
3
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Unitnormquats• Identityrotationexample
• Fliprotationexample
• Unitquaternionmultiplication
5
10
⎡
⎣⎢⎢
⎤
⎦⎥⎥, −1
0
⎡
⎣⎢⎢
⎤
⎦⎥⎥
0k
⎡
⎣⎢⎢
⎤
⎦⎥⎥, 0
− k
⎡
⎣⎢⎢
⎤
⎦⎥⎥
0c1
⎡
⎣⎢⎢
⎤
⎦⎥⎥
0c2
⎡
⎣⎢⎢
⎤
⎦⎥⎥=
−c1 ⋅ c2c1 × c2
⎡
⎣⎢⎢
⎤
⎦⎥⎥
k1 ⋅ k2k1 × k2
⎡
⎣
⎢⎢
⎤
⎦
⎥⎥=
0k2
⎡
⎣⎢⎢
⎤
⎦⎥⎥
0−k1
⎡
⎣⎢⎢
⎤
⎦⎥⎥
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Operations• scalar*quatmultiply
– NBQuaternionsqand–qarethesamerotation!!!
• Unitquaternionmultiplicativeinverse(conjugate)
6
cos θ2⎛
⎝⎜⎞
⎠⎟
sin θ2⎛
⎝⎜⎞
⎠⎟k
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
−1
=
cos θ2⎛
⎝⎜⎞
⎠⎟
−sin θ2⎛
⎝⎜⎞
⎠⎟k
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
, 10
⎡
⎣⎢
⎤
⎦⎥=
cos θ2⎛
⎝⎜⎞
⎠⎟
−sin θ2⎛
⎝⎜⎞
⎠⎟k
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
cos θ2⎛
⎝⎜⎞
⎠⎟
sin θ2⎛
⎝⎜⎞
⎠⎟k
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
−1
α ω
c
⎡
⎣⎢
⎤
⎦⎥= αω
αc
⎡
⎣⎢
⎤
⎦⎥, example)-1 ω
c
⎡
⎣⎢
⎤
⎦⎥= −ω
−c
⎡
⎣⎢
⎤
⎦⎥
18/04/06
4
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Rotateavectorbyaunitquaternion• Performthefollowingtriplequaternionmultiplication:
• Weneedinv(q)additionally,suchthatqandvarenot
orthogonal!• Ifqandvareorthogonal,v’=qv.• Ifwejustmultiplyqwithv,qvcannotbecomeavector(thefirst
elementwillbenon-zero).Theadditionalrightmultiplicationofinverseallowsustogetarotatedvector.
• Butwewillwritethisincodeas:cvec=quat*cvec
7
0ˆ ʹc
⎡
⎣⎢
⎤
⎦⎥=
cos θ2⎛
⎝⎜⎞
⎠⎟
sin θ2⎛
⎝⎜⎞
⎠⎟k
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
0c
⎡
⎣⎢
⎤
⎦⎥
cos θ2⎛
⎝⎜⎞
⎠⎟
sin θ2⎛
⎝⎜⎞
⎠⎟k
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
−1
v '=qvq−1v
q v '
ω1
c1
⎡
⎣
⎢⎢
⎤
⎦
⎥⎥
ω2
c2
⎡
⎣
⎢⎢
⎤
⎦
⎥⎥=
(ω1ω2− c1 ⋅ c2)(ω1c2+ω2c1+ c1× c2)
⎡
⎣
⎢⎢
⎤
⎦
⎥⎥
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
LERP(LinearInterpolation)• Aneveneasierhackistodo4DLinear
intERPolation(LERP)andrenormalization
8
p = a+α(b−a)p = a+α!vp = (1−α)a+αb
a bp
α (1-α) !v
(1−α )cos θ0
2⎛⎝⎜
⎞⎠⎟
sin θ02
⎛⎝⎜
⎞⎠⎟ k0
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
+αcos θ1
2⎛⎝⎜
⎞⎠⎟
sin θ12
⎛⎝⎜
⎞⎠⎟ k1
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
18/04/06
5
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Power-basedSLERP• SphericalLinearInterpolation
9
p = !vq = rq−1q
!v =cos θ
2⎛
⎝⎜⎞
⎠⎟
sin θ2⎛
⎝⎜⎞
⎠⎟k
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
α!v =α θ
2⎛
⎝⎜⎞
⎠⎟k =
αθ2
⎛
⎝⎜
⎞
⎠⎟k
p = !vαq = rq−1( )α q
!vα =cos αθ
2⎛
⎝⎜
⎞
⎠⎟
sin αθ2
⎛
⎝⎜
⎞
⎠⎟k
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
!v = rq−1
q r
pα
!v
p = a+α(b−a)p = a+α!vp = (1−α)a+αb
a bp
α (1-α) !v
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Power-basedSLERP• Inordertoselect“theshortinterpolation”oflessthan180degrees,
• Whenweinterpolate,beforecallingthepoweroperator,wefirstcheckthesignofthefirstcoordinate,andconditionallynegatethequaternion.
• Wecallthistheconditionalnegationoperator• Quaternionsqand–qarethesamerotation!!!• Finally,weoutput:
10
cn(): −1 ω
c
⎡
⎣⎢
⎤
⎦⎥= −ω
−c
⎡
⎣⎢
⎤
⎦⎥
cncos θ1
2⎛⎝⎜
⎞⎠⎟
sin θ12
⎛⎝⎜
⎞⎠⎟ k1
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
cos θ02
⎛⎝⎜
⎞⎠⎟
sin θ02
⎛⎝⎜
⎞⎠⎟ k0
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
−1⎛
⎝
⎜⎜⎜⎜⎜
⎞
⎠
⎟⎟⎟⎟⎟
⎛
⎝
⎜⎜⎜⎜⎜
⎞
⎠
⎟⎟⎟⎟⎟
α
cos θ02
⎛⎝⎜
⎞⎠⎟
sin θ02
⎛⎝⎜
⎞⎠⎟ k0
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
[−ω ,−c]
[ω ,c]
18/04/06
6
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Sphere-basedSLERPing• Inanydimensionn,atrigonometricargumentcanbeusedtoshowthatsphericallinearinterpolationbetweenanytwounitvectorsin,canbecalculatedas:
11
sin[(1−α)Ω]sin(Ω)
!v0+sin[αΩ]sin(Ω)
!v1
whereΩ= cos−1(!v0 ⋅!v1)
sin[(1−α )Ω]sin(Ω)
cos θ02
⎛⎝⎜
⎞⎠⎟
sin θ02
⎛⎝⎜
⎞⎠⎟ k0
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
+ sin[αΩ]sin(Ω)
cos θ12
⎛⎝⎜
⎞⎠⎟
sin θ12
⎛⎝⎜
⎞⎠⎟ k1
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
!n
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
(RBT*RBT)andinv(RBT)• Theresultisanewrigidtransformwithtranslationandrotation– Usethistocodeupthe*operator.– MindtheCvec3s(thet’s)andCvec4s(neededforq*v).
12
t1 + r1t2 r1r2
i t1 + r1t20 1
⎡
⎣⎢⎢
⎤
⎦⎥⎥
r1r2 00 1
⎡
⎣⎢⎢
⎤
⎦⎥⎥
4Dvector3Dvector
i t10 1
⎡
⎣⎢⎢
⎤
⎦⎥⎥
r1 00 1
⎡
⎣⎢⎢
⎤
⎦⎥⎥
i t20 1
⎡
⎣⎢⎢
⎤
⎦⎥⎥
r2 00 1
⎡
⎣⎢⎢
⎤
⎦⎥⎥=
i t0 1
⎡
⎣⎢
⎤
⎦⎥
r 00 1
⎡
⎣⎢
⎤
⎦⎥
⎛
⎝⎜⎞
⎠⎟
−1
=i −r−1t0 1
⎡
⎣⎢⎢
⎤
⎦⎥⎥r−1 00 1
⎡
⎣⎢⎢
⎤
⎦⎥⎥
18/04/06
7
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Morecode• InGLSL,youwillstilluseitsmatrixdatatype.• TheonlyMatrix4s(thatwillsurvive)aretheprojMatrix,theMVMandtheNMVM,whichgetsenttoyourshaders.
• Also,whenweneedtodoobjectscaling,wecannotcapturethisinanRigTform,sothiswillalsobeanMatrix4usedincreatingtheMVM.
• Tocommunicatewiththevertexshaderusing4-by-4matrices,weneedaproceduremakeTranslation(RigTform)andMatrix4quatToMatrix(RigTform)touseT*R,whichturnsquaternionsintoa4-by-4rotationmatrix.
13
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
BALLS:TRACKANDARCChapter8
14
18/04/06
8
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Trackandarcballs• Howshouldwelinkmousemotiontoobjectrotation
• Candobetterthanourcurrentsetup• Wantthefeelingofpushingaspherearound(trackball)
• Wantpathinvariance(arcball)
• Reminders:– Affinetransform:– Rigidbodytransform:
15
Aaffine = TLARBT = TR
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Trackandarcballs
16
18/04/06
9
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Setup• Wearemovinganobjectwithrespecttocube-eye
• Theuserclicksonthescreenanddragsthemouse.WewishtointerpretthisusermotionassomerotationMthatisappliedtowithrespectto
17
at = wt (O)T (E)R
ot
at
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Mentalmodel• Imagineasphereofsomechosenradiusthatiscenteredat,theoriginof.
• Userclicksonthescreenatscreenpixeloverthesphereintheimage– Weinterpretthisastheuserselectingsome3Dpointonthesphere.
• Theuserthenmovesthemousetootherscreenpixeloverthesphere,– Weinterpretasasecondpointonthesphere
18
o ot
s1
p1
s2
p2 s1( !p1)
s2( !p2)
!o
18/04/06
10
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Mentalmodel• Definetheunitdirectionvectors:normalizeandnormalizerespectively.
• Definetheangle
• Definetheaxis
19
v1,v2
( p1 − o) ( p2 − o)
φ = arccos(v1 ⋅v2 )
k = normalize(v1 ×
v2 ) s1( !p1)
s2( !p2)
!o
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Mentalmodel• Let’strytoformulatetherotationabouttheaxiscanberepresentedbythequaternion.– Wherearethecoordinates3-vectorsrepresentingthevectorswithrespecttotheframe.
20
k
k, v1, v2
k , v1,
v2 at
s1( !p1)
s2( !p2)
!o
18/04/06
11
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Ballrotationusingquaternions• Whatwewant
• Derivation
• Therefore21
s1( !p1)
s2( !p2)
!v2 =q
!v1 , q=!v2!v1
−1
φ =
θ2 , θ =2φ.
0v2
⎡
⎣⎢⎢
⎤
⎦⎥⎥
0−v1
⎡
⎣⎢⎢
⎤
⎦⎥⎥=
v1 ⋅ v2v1× v2
⎡
⎣
⎢⎢
⎤
⎦
⎥⎥=
cos(φ)sin(φ)k
⎡
⎣
⎢⎢
⎤
⎦
⎥⎥
q=cos θ2
⎛
⎝⎜⎞
⎠⎟
sin θ2⎛
⎝⎜⎞
⎠⎟k
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
=cos(φ)sin(φ)k
⎡
⎣
⎢⎢
⎤
⎦
⎥⎥.
!o
suchthatqand!v1 areorthogonal,and qand
!v2 areorthogonal.
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Ballrotationusingquaternions• Rotationofdegreesabouttheaxiscanberepresentedbythequaternion.
– Wherearethecoordinates3-vectorsrepresentingthevectorswithrespecttotheframe.
22
2φ k
cos(φ)
sin(φ)k
⎡
⎣⎢⎢
⎤
⎦⎥⎥=
v1 ⋅ v2v1 × v2
⎡
⎣⎢⎢
⎤
⎦⎥⎥=
0v2
⎡
⎣⎢⎢
⎤
⎦⎥⎥
0−v1
⎡
⎣⎢⎢
⎤
⎦⎥⎥
k, v1, v2
k , v1,
v2 at s1( !p1)
s2( !p2)
!o
18/04/06
12
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Ballrotationusingquaternions• Arcball:istherotationofdegreesabouttheaxis.
• Trackball:istherotationofdegreesabouttheaxis.
• Couldbeimplementedwithmatricesorquaternions.
• Arcballisveryeasywithquaternions
23
M
φ kM
2φ
k
s1( !p1)
s2( !p2)
!o
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
s1( !p1)
s2( !p2)
s3( !p3)
Properties• Trackballfeelsliketheuserissimplygrabbingaphysicalpointonasphereanddraggingitaround.
• Butto,followedbytoisdifferentfrommovingdirectlyfromto– willberotatedto,buttheresultscandifferbysome‘twist’abouttheaxis.
– Thispathdependencealsoexistsinoursimplerotationinterface.
• Arcball:theobjectappearstospintwicefasterthanexpected.
24
s1 s2 s2 s3s1 s3
p1 p3
o− p3
!o
18/04/06
13
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
ArcballisPathindependent• Ifwecomposetwoarcballrotations,correspondingtomotionfromtofollowedbymotionfromto,weget
– Readrighttoleft,globalintheunchangingframe
• whichisexactlywhatwewouldhavegottenhadwemoveddirectlyfromto.
25
p1 p2 p3
v2 ⋅ v3v2 × v3
⎡
⎣⎢⎢
⎤
⎦⎥⎥
v1 ⋅ v2v1 × v2
⎡
⎣⎢⎢
⎤
⎦⎥⎥
at
0v3
⎡
⎣⎢⎢
⎤
⎦⎥⎥
0−v2
⎡
⎣⎢⎢
⎤
⎦⎥⎥
0v2
⎡
⎣⎢⎢
⎤
⎦⎥⎥
0−v1
⎡
⎣⎢⎢
⎤
⎦⎥⎥=
0v3
⎡
⎣⎢⎢
⎤
⎦⎥⎥
0−v1
⎡
⎣⎢⎢
⎤
⎦⎥⎥=
v1 ⋅ v3v1× v3
⎡
⎣
⎢⎢
⎤
⎦
⎥⎥
p1 p3
p2
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Implementation• TrackballandArcballcanbedirectlyimplementedusingeither4-by-4matricesorquaternionstorepresentthetransformationM.– Wewillusequaternions,sincewealreadyhavethem.
• Theresultingquaterniondependsonlyonvector– Sooriginofframeisirrelevant
• Wecanworkineyecoordinatesinsteadofcube-eye.
26
v
18/04/06
14
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Gettingeyecoordinates• Oneslightlytrickypartiscomputingthecoordinatesofthepointonthespherecorrespondingtoaselectedpixel– Thisisgeometricraytracing(thisisessentiallyray-tracing,whichwewillcoverlater)
• Hack:workin“windowcoordinates”– X-axisisthehorizontalaxisofthescreen,they-axisistheverticalaxisofthescreen,andthez-axisiscomingoutofthescreen.
– Thinkofthesphere’scenterassimplysittingonthescreen.
27
s1( !p1)
s2( !p2)
!o
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Gettingeyecoordinates• Giventhe(x,y)windowcoordinatesofclick,thezcoordinateonthespherecanbesolvedusing
– arethewindowcoordinatesofthecenterofthesphere
– ristheradiusofthespheremeasuredinpixels
– ifoutsideofthesphere,thenclamptoitsboundary
– Allweneedisnormalized,sojustnormalizedsuchvectors
28
(x − cx )2 + (y − cy )
2 + (z − 0)2 = r2
[cx ,cy ,0]t
v
z = r2 − (x − cx )2 − (y − cy )
2
s1( !p1)
s2( !p2)
!o
18/04/06
15
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Calculation• Needthecenterofthesphere• Sowegiveyoucodethattransformseyecoordinatestoscreencoordinates
• Wedrawtheballusingobjectcoordinates,soweneedtocalculateitssizeineye/objectcoordinates
• Soweprovideyouwith
• Intheballdrawer,yourightmultiplyascalematrixtotheMVM
29
Cvec2getScreenSpaceCoord(constCvec3&p,constMatrix4&projection,doublefrustNear,doublefrustFovY,intscreenWidth,intscreenHeight)
doublegetScreenToEyeScale(doublez,doublefrustFovY,intscreenHeight)
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Translation• NBthedistanceafterperspectiveprojectionshouldbedifferentfromthedistanceintheobjectcoordinates.
• Intranslation,weinterpretmousedisplacement(measuredinpixels)toobjectdisplacement.
• MayaswellusethesamescreenToEyeScalefactorsotheobjectmoveswiththemouse
• Oncetheobjectismoved,orwechangetheeyeweneedtorecalculatethescale– Waitforclickup.
30