Fall 2017 CSCI 420: Computer Graphics 2.2 Transformations Hao Li http://cs420.hao-li.com 1
OpenGL Transformations Matrices • Model-view matrix (4x4 matrix) • Projection matrix (4x4 matrix) vertices in canonical 3D world coordinate system vertices vertices in 3D in 2D Model-view Projection 2
4x4 Model-view Matrix (this lecture) • Translate, rotate, scale objects • Position the camera vertices in canonical 3D world coordinate system vertices vertices in 3D in 2D Model-view Projection 3
4x4 Model-view Matrix (next lecture) • Projection from 3D to 2D vertices in canonical 3D world coordinate system vertices vertices in 3D in 2D Model-view Projection 4
OpenGL Transformation Matrices Model-view Projection • Manipulated separately in OpenGL (must set matrix mode) : glMatrixMode (GL_MODELVIEW); glMatrixMode (GL_PROJECTION; 5
Setting the Current Model-view Matrix • Load or post-multiply glMatrixMode (GL_MODELVIEW); glLoadIdentity(); // very common usage float m[16] = { … }; glLoadMatrixf(m); // rare, advanced glMultMatrixf(m); // rare, advanced • Use library functions glTranslatef(dx, dy, dz); glRotatef(angle, vx, vy, vz); glScalef(sx, sy, sz); 6
Translated, rotated, scaled object world 7
The rendering coordinate system Initially (after glLoadIdentity()) : rendering coordinate system = world coordinate system world 8
The rendering coordinate system glTranslatef(x, y, z); [x, y ,z] rendering coordinate system world 9
The rendering coordinate system glRotatef(angle,ax, ay, az); rendering coordinate world system 10
The rendering coordinate system glScalef(sx, sy, sz); rendering coordinate world system 11
OpenGL code glMatrixMode (GL_MODELVIEW); glLoadIdentity(); glTranslatef(x, y, z); glRotatef(angle, ax, ay, az); glScalef(sx, sy, sz); renderBunny(); rendering coordinate world system 12
Rendering more objects How to obtain this frame? rendering coordinate world system 13
Solution 1 Find glTranslate(…), glRotatef(…), glScalef(…) How to obtain this frame? world 14
Solution 2: gl{Push,Pop}Matrix glMatrixMode (GL_MODELVIEW); glLoadIdentity(); // render first bunny glPushMatrix(); // store current matrix glTranslate3f(…); glRotatef(…); renderBunny(); glPopMatrix(); // pop matrix // render second bunny glPushMatrix(); // store current matrix glTranslate3f(…); glRotatef(…); renderBunny(); glPopMatrix(); // pop matrix 15
Recall: Linear Algebra 16
Scalars • Scalars , , from a scalar field α β γ α + β αβ 0 1 − α () − 1 • Operations , , , , , • “Expected” laws apply • Examples: rationals or reals with addition and multiplication 17
Vectors • Vectors from a vector space u , v , w • Vector addition , subtraction u + v u − v • Zero vector 0 • Scalar multiplication α v 18
Euclidean Space • Vector space over real numbers • Three-dimensional in computer graphics α = u > v = u 1 v 1 + u 2 v 2 + u 3 v 3 • Dot product: 0 > 0 = 0 • , u > v = 0 • are orthogonal if u , v k v k 2 = v > v k v k • defines , the length of v 19
Lines and Line Segments p ( α ) = p 0 + α d • Parametric form of line: • Line segment between and : q r p ( α ) = (1 − α ) q + α r for 0 ≤ α ≤ 1 20
Convex Hull • Convex hull defined by p = α 1 p 1 + . . . + α n p n α 1 + . . . + α n = 1 for 0 ≤ α 1 ≤ 1 , i = 1 . . . n and 21
Projection • Dot product projects one vector onto another vector u > v = u 1 v 1 + u 2 v 2 + u 3 v 3 = k u kk v k cos( θ ) π v ( u ) = ( u > v ) v / k v k 2 22
Cross Product k a ⇥ b k = | a || b || sin( θ ) | • Cross product is b perpendicular to both and a • Right-hand rule 23
Plane • Plane defined by point and p 0 vectors and u v • and should not be parallel v u • Parametric form: t ( α , β ) = p 0 + α u + β v • is the normal n = u ⇥ v / k u ⇥ v k n > ( p − p 0 ) = 0 • if and only if lies in plane p 24
Coordinate Systems • Let be three linearly independent v 1 , v 2 , v 3 vectors in a 3-dimensional vector space • Can write any vector as w w = α 1 v 1 + α 2 v 2 + α 3 v 3 for some scalars α 1 , α 2 , α 3 25
Frames • Frame = origin + coordinate system p 0 • Any point p = p 0 + α 1 v 1 + α 2 v 2 + α 3 v 3 26
In Practice, Frames are Often Orthogonal 27
Change of Coordinate System • Bases { } and { } u 1 , u 2 , u 3 v 1 , v 2 , v 3 • Express basis vectors in terms of v j u i u 1 = γ 11 v 1 + γ 12 v 2 + γ 13 v 3 u 2 = γ 21 v 1 + γ 22 v 2 + γ 23 v 3 u 3 = γ 31 v 1 + γ 32 v 2 + γ 33 v 3 • Represent in matrix form: u > v > 1 1 u > v > = M M 2 2 u > v > 3 3 28
Representing 3D transformations (and model-view matrices) 29
Linear Transformations • 3 x 3 matrices represent linear transformations a = Mb • Can represent rotation, scaling, and reflection • Cannot represent translation M 30
Homogeneous Coordinates • In order to represent rotations, scales AND translations [ α 1 , α 2 , α 3 ] > • Augment by adding a fourth component (1): p = [ α 1 , α 2 , α 3 , 1] > • Homogeneous property: p = [ α 1 , α 2 , α 3 , 1] > = [ α 1 , α 2 , α 3 ] > , for any scalar 6 = 0 31
Homogeneous Coordinates • Homogeneous coordinates are transformed by 4x4 matrices q p q = Ap 4-vector 4-vector world 4x4 matrix 32
Affine Transformations (4x4 matrices) • Translation • Rotation • Scaling • Any composition of the above • Later: projective (perspective) transformations -Also expressible as 4 x 4 matrices! 33
Translation d = [ α x , α y , α z , 0] > • where , q = p + d p = [ x, y, z, 1] > , • q = [ x 0 , y 0 , z 0 , 1] > , • • Express in matrix form and solve for q = Tp T 34
Scaling x 0 = β x x y 0 = β y y z 0 = β z z • Express as and solve for q = Sp S 35
Rotation in 2 Dimensions • Rotation by about the origin θ x 0 = x cos( θ ) − y sin( θ ) y 0 = x sin( θ ) + y cos( θ ) • Express in matrix form: • Note that the determinant is 1 36
Rotation in 3 Dimensions • Orthogonal matrices: RR > = R > R = I det( R ) = 1 • Affine transformation: 37
Affine Matrices are Composed by Matrix Multiplication A = A 1 A 2 A 3 • Applied from right to left Ap = ( A 1 A 2 A 3 ) p = A 1 ( A 2 ( A 3 p )) • When calling glTranslate3f, glRotatef, or glScalef, OpenGL forms the corresponding 4x4 matrix, and multiplies the current modelview matrix with it. 38
Summary • OpenGL Transformation Matrices • Vector Spaces • Frames • Homogeneous Coordinates • Transformation Matrices 39
Next Time: Viewing & Projection 40
http://cs420.hao-li.com Thanks! 41
Recommend
More recommend