2D Projective trnsformtion The mpping of points from n N-D spce to n M-D subspce (M < N) w' w' w m m m 2 m m m 2 m m m 2 2 22 w' w' w m m m 2 m m m 2 m m 2 2 Boqun Chen 22
2D Projective trnsformtion w' w' w m m m 2 m m m 2 m m 2 2 Which cn be epressed s the forllowing rtionl liner eqution: ' ' m m m m m 2 2 m m m m m 2 2 2 2 m 2, m2 Boqun Chen 22 2
From 2D to 3D: Preliminr Right-hnded vs. left-hnded (out of pge) Z Y X Y (into pge) Z X Z-is determined from X nd Y b cross product: Z=X Y Z X Y X 2 Y 3 X 3 Y 2 X 3 Y X Y 3 X Y 2 X 2 Y Cross product follows right-hnd rule in right-hnded coordinte sstem, nd left-hnd rule in left-hnded sstem. Boqun Chen 22 3
3D Trnsltion T = t t t 2 Boqun Chen 22 4
3D Scling S = s s s 2 Boqun Chen 22 5
3D Rottion R = cos sin sin cos cos sin cos sin R = sin cos R = sin cos Boqun Chen 22 6
Euler Angles for 3-D Rottions Euler ngles - 3 rottions bout ech coordinte is, however ngle interpoltion for nimtion genertes birre motions rottions re order-dependent, nd there re no conventions bout the order to use Emple: R (9 ) R (9 ) R T (2 ) But, R (3 ) R (3 ) R (42 T.3 ) R T (4 ) Widel used nw, becuse the're simple Boqun Chen 22 7
Ais-ngle rottion The mtri R for rottion b bout is (unit) : R T cos ( I T ) sin * T Project onto I T Project onto ' s norml plne * Dul mtri. Project onto norml plne, flip b 9 cos, sin Rotte b in norml plne (ssumes is unit.) Boqun Chen 22 8
9 Boqun Chen 22 ' Rottion s Vector Opertion )sin ( ) cos )( ) ( ) (cos ) ( )sin ( ] )cos ) ( [( ' I T ] )sin ( ) cos )( ( ) [(cos ' * 2D ' 2D 2D D 2 ' ' 2D ' D 2 D 2
Boqun Chen 22 Ais-ngle rottion 2 2 2 T ) ( ) ( Smmetric T
v * Aside: The Dul Mtri If v=[,,] is vector, the mtri is the dul mtri of v Cross-product s mtri multipl: v * = v helps define rottion bout n rbitrr is ngulr velocit nd rottion mtri time derivtives Geometric interprettion of v * project onto the plne norml to v rotte b 9 bout v resulting vector is perpendiculr to v nd Boqun Chen 22
2 Boqun Chen 22 Ais-ngle rottion Skew k j i k j i det det det
3 Boqun Chen 22 Ais-ngle rottion I R T T ) sin( ) )( cos( I T ) cos( ) sin( )) cos( ( cos sin ) cos ( I Skew Smmetric
4 Boqun Chen 22 Ais-ngle rottion I R T ) cos( ) sin( )) cos( ( When =: I I R T ) ( When rotte round -is ( ): T ) cos( ) sin( )) cos( ( R cos sin sin cos
5 Boqun Chen 22 Ais-ngle rottion When rotte round -is ( ): T cos sin ) cos ( R cos sin sin cos
6 Boqun Chen 22 Ais-ngle rottion When rotte round -is ( ): T cos sin ) cos ( R cos sin sin cos
Trnsformtion. 2D Trnsformtion 2. 3D Trnsformtion 3. Viewing Projection Boqun Chen 22 7
Orthogrphic Projection Throw w Z coordintes Get points on the XY plne Y X Boqun Chen 22 8
Perspective http://www.indin.edu/~kglowck/thens/ Boqun Chen 22 9
Perspective Projection Boqun Chen 22 2
A Simple Perspective Cmer Cnonicl cse: cmer looks long the -is focl point is the origin imge plne is prllel to the -plne t distnce d (We cll d the focl length, minl for historicl resons) F=[,,] Imge plne [,,d] Boqun Chen 22 2
Similr Tringles Y (, ) (, ) (, ) (, d) Z Digrm shows -coordinte, -coordinte is similr Boqun Chen 22 22
Similr Tringles Y (, ) (, ) = d / = / /d = / = (d/)* (, ) (, d) point [,,] projects to [(d/), (d/), d] Z Boqun Chen 22 23
24 Boqun Chen 22 A Perspective Projection Mtri Projection using homogeneous coordintes: trnsform [,, ] to [(d/), (d/), d] d d d d d d w w w w ' ' ' d d d ' ' ' w
Cmer Position nd Orienttion Boqun Chen 22 25
LookFrom And LookAt Is This Enough? Boqun Chen 22 26
LookFrom And LookAt Boqun Chen 22 27
Complete Cmer Specifiction VUp LookFrom d LookAt Boqun Chen 22 28
Complete Cmer Specifiction VUp Boqun Chen 22 29
Viewing Volume Boqun Chen 22 3
Rendering from n cmer position Y (, ) Y LookFrom VUp (?,?) (, ) LookAt (, ) (, ) (, d) Z (, ) (, d) Z Boqun Chen 22 3
32 Boqun Chen 22 Coordinte Trnsformtion u v p e o p p o p p p ), ( v p u p e p p p v u v u ), ( v u p p v u v u e e p p v u p p e v v e u u p p e e v v u u p p
33 Boqun Chen 22 Coordinte Trnsformtion u v p e o w v u p p p w v u w v u w v u e e e p p p w v u p p p e e e w w w v v v u u u p p p w
Viewing Trnsformtions Y VUp X LookFrom LookAt Z Boqun Chen 22 34
Viewing Trnsformtions Y VUp X LookAt LookFrom Trnslte LookFrom to origin Z Boqun Chen 22 35
Viewing Trnsformtions Y VUp X LookFrom LookAt Rotte LookAt to Z is (is-ngle rottion) Z Boqun Chen 22 36
Viewing Trnsformtions Y VUp X LookFrom LookAt Rotte bout Z to get the projection of Vup prllel to the Y is Z Boqun Chen 22 37
Implementtion Implementing the lookt/lookfrom/vup viewing scheme Boqun Chen 22 () Trnslte b -lookfrom, bring focl point to origin (2) Rotte lookt-lookfrom to the -is with mtri R:» v = (lookt-lookfrom) (normlied) nd = [,,]» rottion is: = (v)/ v» rottion ngle: cos= v nd sin= v R T v (I T ) v * where * or: glrotte(,, ) (3) Rotte bout -is to get projection of vup prllel to the -is» wtch out if vup is long the -is 38
Screen Coordintes VUp LookFrom d LookAt Boqun Chen 22 39
Viewport Trnsformtions A trnsformtion mps the visible (model) world onto screen or window coordintes In OpenGL viewport trnsformtion, e.g. glortho(), defines wht prt of the world is mpped in stndrd Normlied Device Coordintes ((-,-) to (,)) The viewpoint trnsformtion mps NDC into ctul window, piel coordintes b defult this fills the window otherwise use glviewport (4.7,2) (,) (64,48) (2,) Boqun Chen 22 4
Clipping Boqun Chen 22 4
The Viewing Frustum imge plne ner fr Boqun Chen 22 42
Normliing the Viewing Frustum Trnsform frustum to cube before clipping imge plne ner fr ner fr Converts perspective frustum to orthogrphic frustum Ver similr to our perspective trnsformtion just nother mtri Boqun Chen 22 43
Model nd Trnsformtion Hierrch Boqun Chen 22 44
How to Model Stick Person Mke stick person out of cubes Just trnslte, rotte, nd scle ech one to get the right sie, shpe, position, nd orienttion. Looks gret, until ou tr to mke it move. Boqun Chen 22 45
The Right Control Knobs Boqun Chen 22 As soon s ou wnt to chnge something, the model likel flls prt Reson: the thing ou re modeling is constrined but our model doesn t know it Wnted: some sort of representtion of structure Control knob This kind of control knob is convenient for sttic models, nd vitl for nimtion! Ke: structure the trnsformtions in the right w: using hierrch 46
Mking n Articulted Model r A p B q A miniml 2-D jointed object: Two pieces, A ( forerm ) nd B ( upper rm ) Attch point q on B to point r on A ( elbow ) Desired control knobs:» u: shoulder ngle (A nd B rotte together bout p)» v: elbow ngle (A rottes bout r, which sts ttched to p) Boqun Chen 22 47
Mking n Arm, step r A Strt with A nd B in their untrnsformed configurtions (B is hiding behind A) First ppl series of trnsformtions to A, leving B where it is Boqun Chen 22 48
Mking n Arm, step 2 r A B q r p A Trnslte b -r, bringing r to the origin You cn now see B peeking out from behind A Boqun Chen 22 49
Mking n Arm, step 3 B q r p A p B q Net, we rotte A b v (the elbow ngle) Boqun Chen 22 5
Mking n Arm, step 4 p B q p B q Trnslte A b q, bringing r nd q together to form the elbow joint We cn regrd q s the origin of the elbow coordinte sstem, nd regrd A s being in this coordinte sstem. Boqun Chen 22 5
Mking n Arm, step 5 p B q p B q From now on, ech trnsformtion pplies to both A nd B (This is importnt!) First, trnslte b -p, bringing p to the origin A nd B both move together, so the elbow doesn t seprte! Boqun Chen 22 52
Mking n Arm, step 6 p B q Then, we rotte b u, the shoulder ngle Agin, A nd B rotte together Boqun Chen 22 53
Mking n Arm, step 7 Finll, trnslte b T, bringing the rm where we wnt it p is t origin of shoulder coordinte sstem Boqun Chen 22 54
Trnsformtion Hierrchies Trns T Rot u Trns -p Trns q Rot v Trns -r A Boqun Chen 22 B This is the build-n-rm sequence, represented s tree Interprettion: Leves re geometric primitives Internl nodes re trnsformtions Trnsformtions ppl to everthing under them strt t the bottom nd work our w up You cn build wide rnge of models this w Trnsform Control knob Primitive 55
Trnsformtion Hierrchies Trns T Rot u Trns -p Trns q Rot v A Trns -r A Boqun Chen 22 B Another point of view: The shoulder coordinte trnsformtion moves everthing below it with respect to the shoulder: B A nd its trnsformtion The elbow coordinte trnsformtion moves A with respect to the elbow A Shoulder coordinte trnsform Elbow coordinte trnsform Primitive 56
A Schemtic Humnoid hip torso shoulder l. leg l. leg2 r. leg r. leg 2 l. rm r. rm neck l. rm2 r. rm2 hed Boqun Chen 22 57 Ech node represents rottion(s) geometric primitive(s) struct. trnsformtions The root cn be nwhere. We chose the hip (cn re-root) Control for ech joint ngle, plus globl position nd orienttion A relistic humn would be much more comple
Directed Acclic Grph hip torso shoulder l. leg l. leg2 r. leg r. leg 2 l. rm r. rm neck l. rm2 r. rm2 hed This is grph, so ou cn re-root it. It s directed, rendering trversl onl follows links one w. It s cclic, to void infinite loops in rendering. Not necessril tree. e.g. l.rm2 nd r.rm2 primitives might be two instntitions (one mirrored) of the sme geometr Boqun Chen 22 58
Wht Hierrchies Cn nd Cn t Do Advntges: Resonble control knobs Mintins structurl constrints Disdvntges: Doesn t lws give the right control knobs» e.g. hnd or foot position - re-rooting m help Cn t do closed kinemtic chins (keep hnd on hip) Other constrints: do not wlk through wlls A more generl pproch: inverse kinemtics - more comple, but better knobs Hierrchies re vitl tool for modeling nd nimtion Boqun Chen 22 59
Implementing Hierrchies Building block: mtri stck tht ou cn push/pop Recursive lgorithm tht descends our model tree, doing trnsformtions, pushing, popping, nd drwing Tilored to OpenGL s stte mchine rchitecture (or vice vers) Nuts-nd-bolts issues: Wht kind of nodes should I put in m hierrch? Wht kind of interfce should I use to construct nd edit hierrchicl models? Etensions: epressions, lnguges. Boqun Chen 22 6
Ide of Mtri Stck: The Mtri Stck LIFO stck of mtrices with push nd pop opertions current trnsformtion mtri (product of ll trnsformtions on stck) trnsformtions modif mtri t the top of the stck Recursive lgorithm: lod the identit mtri for ech internl node:» push new mtri onto the stck» conctente trnsformtions onto current trnsformtion mtri» recursivel descend tree» pop mtri off of stck for ech lef node:» drw the geometric primitive using the current trnsformtion mtri Boqun Chen 22 6
Relevnt OpenGL routines glpushmtri(), glpopmtri() push nd pop the stck. push leves cop of the current mtri on top of the stck gllodidentit(), gllodmtrid(m) lod the Identit mtri, or n rbitrr mtri, onto top of the stck glmultmtrid(m) multipl the mtri C on top of stck b M. C = CM glortho (,,,,,) set up prllel projection mtri glrottef(thet,,,), glrotted( ) is/ngle rotte. f nd d tke flots nd doubles, respectivel gltrnsltef(,,), glsclef(,,) trnslte, rotte. (lso eist in d versions.) Boqun Chen 22 62
Two-link rm, revisited, in OpenGL Trce of Opengl clls gllodidentit(); glortho( ); glpushmtri(); gltrnsltef(t,t,); glrottef(u,,,); gltrnsltef(-p,-p,); glpushmtri(); gltrnsltef(q,q,); glrottef(v,,,); gltrnsltef(-r,-r,); Drw(A); glpopmtri(); Drw(B); glpopmtri(); Trns T Rot u Trns -p Trns q Rot v Trns -r A B Boqun Chen 22 63
The following not covered in this course Boqun Chen 22 64
Vector Trnsformtion For ffine trnsformtion, simpl trnsform (,,,). For perspective trnsformtion, more complicted For norml trnsformtion, specil cse Boqun Chen 22 65
Trnsforming Normls It s tempting to think of norml vectors s being like porcupine quills, so the would trnsform like points But it s not so --- consider the 2D emple ffine trnsformtion below. We need different rule to trnsform normls. Boqun Chen 22 66
Normls Do Not Trnsform Like Points If M is 44 trnsformtion mtri, then To trnsform points, use p =Mp, where p=[ ] T So to trnsform normls, n =Mn, where n=[ b c ] T right? Wrong! This formul doesn t work for generl M. Boqun Chen 22 67
Boqun Chen 22 Normls Trnsform Like Plnes A plne b c d cn be written n p n T p, where n b c d (,b,c) is the plne norml, d is the offset. If p is trnsformed, how should n trnsform? To find the nswer, do some mgic : T, p n T Ip eqution for point on plne in originl spce n T (M M)p (n T M )(Mp) n T p eqution for point on plne in trnsformed spce p Mp to trnsform point n (n T M ) T M T n to trnsform plne 68 T
Trnsforming Normls - Cses For generl trnsformtions M tht include perspective, use full formul (M inverse trnspose), use the right d d mtters, becuse prllel plnes do not trnsform to prllel plnes in this cse For ffine trnsformtions, d is irrelevnt, cn use d=. For rottions, M inverse trnspose = M, cn trnsform normls nd points with sme formul. Boqun Chen 22 69
Quternions The rottions re the unit quternions. Quternions, generlition of comple numbers, cn represent 3-D rottions 2 2 2 2 bi cj dk where, b, c, d R nd b c d Emple: rottion b bout the unit vector [b c d] : cos bsin i csin j d sin k 2 2 2 2 Successive rottions corresponds to multipling quternions bsed on distributive lw nd rules: 2 2 2 i j k, ij k ji, jk i kj,ki j ik. A unit quternion represents point on the unit sphere in 4D. Interpoltion: shortest pth between two points on the sphere ( gret rc) T Boqun Chen 22 7
Advntges: Quternions no trigonometr required multipling quternions gives nother rottion (quternion) rottion mtrices cn be clculted from them direct rottion (with no mtri) no fvored direction or is Disdvntges: but, R v ( ) R v ( ) but, Quternion( R ( )) Quternion( R ( )) R ( ) v R v (36 ) v Quternion ( R ( )) Quternion ( (36 )) ( i j k) v v R v Boqun Chen 22 7
Line Clipping Modif endpoints of lines to lie in rectngle How to define interior of rectngle? Convenient def.: intersection of 4 hlf-plnes Nice w to decompose the problem Generlies esil to 3D (intersection of 6 hlf-plnes) m interior = < m > min min min m > min < m Boqun Chen 22 72
Line Clipping Modif end points of lines to lie in rectngle Method: Is end-point inside the clip region? (hlf-plne tests) If outside, clculte intersection between the line nd the clipping rectngle nd mke this the new end point Both endpoints inside: trivil ccept One inside: find intersection nd clip Both outside: either clip or reject (trick cse) Boqun Chen 22 73
Cohen-Sutherlnd Algorithm Uses outcodes to encode the hlf-plne tests results m bit : >m bit 2: <min bit 3: >m bit 4: <min min Rules: Boqun Chen 22 min 74 m Trivil ccept: outcode(end) nd outcode(end2) both ero Trivil reject: outcode(end) & (bitwise nd) outcode(end2) nonero Else subdivide
Cohen-Sutherlnd Algorithm: Subdivision If neither trivil ccept nor reject: Pick n outside endpoint (with nonero outcode) Pick n edge tht is crossed (nonero bit of outcode) Find line's intersection with tht edge Replce outside endpoint with intersection point Repet until trivil ccept or reject Other clipping lgorithms Crus-Beck/Ling-Brks or Nicholl-Lee-Nicholl Boqun Chen 22 75
Polgon Clipping Convert polgon into one or more polgons tht form the intersection of the originl with the clip window Boqun Chen 22 76
Sutherlnd-Hodgmn Polgon Clipping Algorithm Subproblem: clip polgon (verte list) ginst single clip plne output the verte list(s) for the resulting clipped polgon(s) Clip ginst ll four plnes generlies to 3D (6 plnes) generlies to n conve clip polgon/polhedron Boqun Chen 22 77
Sutherlnd-Hodgmn Polgon Clipping Algorithm (Cont.) To clip verte list ginst one hlf-plne: if first verte is inside - output it loop through list testing inside/outside trnsition - output depends on trnsition: > in-to-in: output verte > out-to-out: no output > in-to-out: output intersection > out-to-in: output intersection nd verte Boqun Chen 22 78
Summr Strted with orthogrphic projection: just throw out the Z coordinte Perspective projection from origin long Z is: use projection mtri Moving the cmer: trnsform the entire world so tht we cn do projection from the origin long the Z is Screen coordintes: trnslte nd scle entire world so tht projection ields piel coordintes Clipping: trnsform world so tht viewing frustum becomes unit cube. Clip lines ginst hlfplnes. Boqun Chen 22 79