Composing Transformation Composing Transformation � Composing Transformation – the process of applying Matrix multiplication is associative � several transformation in succession to form one M3 x M2 x M1 = (M3 x M2) x M1 = M3 x (M2 x M1) overall transformation Transformation products may not be commutative A x B != B � x A � If we apply transform a point P using M1 matrix first, Some cases where A x B = B x A � and then transform using M2, and then M3, then we A B have: translation translation (M3 x (M2 x (M1 x P ))) = M3 x M2 x M1 x P scaling scaling rotation rotation (pre-multiply) uniform scaling rotation M (sx = sy) Transformation order matters! How OpenGL does it? � Example: rotation and translation are not � OpenGL’s transformation functions are commutative meant to be used in 3D Translate (5,0) and then Rotate 60 degree � No problem for 2D though – just ignore OR the z dimension Rotate 60 degree and then translate (5,0)?? � Translation: � glTranslatef(d)(tx, ty, tz) -> Rotate and then translate !! glTranslatef(d)tx,ty,0) for 2D
How OpenGL does it? OpenGL Transformation Composition � Rotation: � A global modeling transformation matrix � glRotatef(d)(angle, vx, vy, vz) -> (GL_MODELVIEW, called it M here ) glRotatef(d)(angle, 0,0,1) for 2D glMatrixMode(GL_MODELVIEW) � The user is responsible to reset it if necessary y y (vx, vy, vz) – rotation axis glLoadIdentity() -> M = 1 0 0 x x 0 1 0 z 0 0 1 You can imagine z is pointing out of the slide Transformation Pipeline OpenGL Transformation Composition � Matrices for performing user-specified transformations are multiplied to the model view global matrix � For example, Object Object Modeling World Coordinates Local Coordinates transformation 1 0 1 glTranslated(1,1 0); M = M x 0 1 1 0 0 1 � All the vertices P defined within glBegin() will first go … through the transformation (modeling transformation) P’ = M x P
Something noteworthy Example Revisit � We want rotation and then translation � Very very noteworthy … � Generate wrong results if you do: OpenGL postmultiply each new transformation matrix � M = M x M new glTranslated(5,0,0); Example: perform translation, then rotation glRotated(60,0,0,1); � glRotate(60,0,0,1); 0) M = Identity glTranslated(5,0,0); glBegin() 1) translation T(tx,ty,0) -> M = M x T(tx,ty,0) 2) rotation R( θ ) -> M = M x R( θ ) … glBegin() 3) Now, transform a point P -> P’ = M x P = T(tx, ty, 0) x R( θ ) x P Wrong!!! You need to specify the transformation in the opposite order!! How Strange … OpenGL Transformation � When use OpenGL, we need to think � OpenGL has its reason … object transformations as moving � It wants you to think of transformation in a (transforming) its local coordinate different way frame � Instead of thinking of transform the object in a fixed global coordinate system, you should � All the transformations are performed think of transforming an object as moving relative to the current coordinate frame (transforming) its local coordinate system origin and axes
Translate Coordinate Frame Translate Coordinate Frame (2) Translate (3,3)? Translate (3,3)? Rotate Coordinate Frame Scale Coordinate Frame Scale (0.5,0.5)? Rotate 30 degree? 30 degree
Compose Transformations Another example How do you transform from C1 to C2? C1 Transformations? C2 Translate (5,5) and then Rotate (60) 60 o OR Answer: (5,5) Rotate (60) and then Translate (5,5) ??? 45 o 1. Translate(7,9) 2. Rotate 45 (7,9) 3. Scale (2,2) Answer: Translate(5,5) and then Rotate (60) Another example (cont’d) Transform Objects If you Rotate(60) and then Translate(5,5) … � What does coordinate frame transformation have anything to do with object transformation? � You can view transformation as to tie the 60 o C2 C1 object to a local coordinate frame and move that coordinate frame 5 5 60 o You will be translated (5,5) relative to C2!!
Example Example (cont’d) If you think of transformations as Old way: Transformation as moving moving the local coordinate frame the object relative to the origin of a global world coordinate frame o o 60 60 1) Translate (5,0) o 2) Rotate ( ) 60 o 1) Rotate ( ) 60 2) Translate (5,0) (5,0) Exact the opposite order compared to the (5,0) previous slide!! So … So … (cont’d) If you think of transformations as moving If you think of transformations as the object relative to the origin of a moving the coordinate frame o global world coordinate frame 60 o 60 o 1) Rotate ( ) - M R 60 1) Translate (5,0) - M T o 2) Translate (5,0) - M T 2) Rotate ( ) - M R 60 (5,0) P’ = M T x M R x P is the So if you think in terms of moving coordinate frames, you will want (5,0) Correct multiplication to perform Translate first, and then Rotate (I.e., call glTranslate() first and then glRotate()) Unfortunately, OpenGL will do M R x M T x P if you call OpenGL will do M T x M R x P -> The correct multiplication order!!! glRotate() first, and then glTranslate() because of postmultiplication
Put it all together Change Coordinate System (1) When you use OpenGL … � What constitutes a coordinate system? � Think of transformation as moving coordinate � Origin O frames � Basis vector i, j � Call OpenGL transformation functions in that order � OpenGL will actually perform the Any point P (x,y) in the coordinate system can be j transformations in the reverse order represented: � Everything will be just right!!! P = O + x * i + y * j i O Change Coordinate System (2) Change Coordinate System (3) � Transform a coordinate system � Assuming P (c, d) in C’, and C’ is obtained by transforming C using M, i.e., We can denote the transformation of C’ = M x C C’ coordinate systems as J’ I’ C’ = M x C P = (c,d) J’ Then the coordinates for I’ b C O C’ � O’ = M x O P in C is J � I ’ = M x I J C P’ = M x P d c � J’ = M x J O’ M I O’ (a,b,1) = M x (c,d,1) (Note that when we transform a vector (a,b), we use (a,b,0) I to multiply with the 3x3 matrix M (as opposed to (a,b,1) like we do O for points a
Change Coordinate System (4) Successive Coordinate Changes � What does it have anything to do with object M1 M2 C1 C2 C3 transformation? (a3,b3) � We can view transformation as moving the Given P (a3,b3) in C3 C3 coordinate system (reference frame) and tie the What is P’s coordinates in C1 object with that frame a3 b3 C1? C2 M2 a c 1) Get P’s coordinates in C2 b (a,b) J’ b = M x d I’ P_c2 = M2 x P C’ 1 1 J C M1 2) Get P_c2’s coordinates in C1 What is (a,b)? The coordinates d c P_c1 = M1 x P_c2 (c,d) Of the point P (c,d) in C after the O’ coordinate system change P_c1 = M1 x M2 x P the answer!! i.e, the new coordinates after I O’ transforming (c,d) a Look at transformation again… Look at transformation again (2) Tell OpenGL to transform (c,d) (c,d) Think transformation of Using: C3 C3 point P (c,d) as a M1 (move C1 to C2) sequence of coordinate a3 c b3 d M2 (move C2 to C3) frame change M2 M2 C1 C1 C2 C2 P’s final coordinates = (c,d) (c,d) P (c,d) is always tied P’s coordinates in C1 = to the (local) coordinate M1 x M2 x P M1 M1 frame (c,d) (c,d) This is what we want, and exactly what OpenGL does!! P’s final position after the Transformations? i.e. Apply the last transformation -> (c,d)’s coordinates in C1 (M2) to the point first
Put it all together Look at transformation again (3) Coordinate system transformation � In other words: If you think of � Transform an object from coordinate system C1 with transformations as changing coordinate the origin at (x1,y1) or (x1,y1,z1) in 3D , to coordinate frames, the order that you specify the system C2 with the origin (x2,y2) or (x2,y2,z1) in 3D transformations (for the frames) will be 1. Find the transformation sequence to move C2 exactly opposite to the order that the to C1 (so C2 will align with C1) • Move the origin of C2 to coincide with the c1 transformations are actually applied c2 origin of C1 • Rotate the basis vectors of C2 so that they (i.e. matrix- multiplied) to the object (x1,y1) coincide wih C1’s. • Scale the unit if necessary 2. Apply the above transformation sequence to the (x2,y2) object in the opposite order
Recommend
More recommend