2D Transformations 2D Transformations • • • • • • • World Coordinates Translate Rotate Scale Viewport Transforms Hierarchical Model Transforms Putting it all together Transformations • Rigid Body Transformations - transformations that do not change the object. • Translate – If you translate a rectangle, it is still a rectangle • Scale – If you scale a rectangle, it is still a rectangle • Rotate – If you rotate a rectangle, it is still a rectangle Vertices • We have always represented vertices as (x,y) • An alternate method is: x ( x, y ) y • Example: 2.1 (2.1,4.8) 4.8 Matrix * Vector x' a b x y ' c d y x' ax by x' a b c x y ' d e f y z ' g h i z x' ax by cz y ' cx dy y ' dx ey fz z ' gx hy iz 1 0 I 0 1 1 0 0 I 0 1 0 0 0 1 Matrix * Matrix x y a b ,B A w z d c ax bz ay bw A* B dw cy dz cx a b 1 0 ? 0 1 d c Does A*B = B*A? NO What does the identity do? AI=A Translation • Translation - repositioning an object along a straight-line path (the translation distance) from one coordinate location to another. (x’,y’) (tx,ty) (x,y) Translation • Given: • We want: • Matrix form: P ( x, y ) T (t x , t y ) x' x t x y' y t y P (3.7,4.1) T (7.1,8.2) x ' 3 . 7 7 .1 y ' 4.1 8.2 x' 3.7 7.1 x' x t x y ' 4.1 8.2 y ' y t y x ' 3.4 P' P T y ' 4 .1 Translation Examples • P=(2,4), T=(-1,14), P’=(?,?) • P=(8.6,-1), T=(0.4,-0.2), P’=(?,?) • P=(0,0), T=(1,0), P’=(?,?) Recall • A point is a position specified with coordinate values in some reference frame. • We usually label a point in this reference point as the origin. • All points in the reference frame are given with respect to the origin. Applying to Triangles (tx,ty) What do we have here? • You know how to: Scale • Scale - Alters the size of an object. • Scales about a fixed point (x’,y’) (x,y) Scale • Given: • We want: • Matrix P ( x, y ) P (1.4,2.2) S (sx , s y ) S (3,3) x' s x x x ' 3 * 1 .4 y' s y y y ' 3 * 2 .2 x' s x y ' 0 form: P' S P x' 3 0 1.4 y ' 0 3 2.2 0 x s y y x' 4.2 y ' 6 .6 Non-Uniform Scale (x’,y’) (x,y) S=(1,2) Rotation • Rotation - repositions an object along a circular path. • Rotation requires an and a pivot point Rotation p ( x, y ) R ( ) x r cos y r sin x' r cos( ) y ' r sin( ) x' r cos cos r sin sin y ' r cos sin r sin cos x' x cos y sin y ' x sin y cos x' cos y ' sin p ' Rp sin x cos y Example • P=(4,4) • =45 degrees Rotations V(-0.6,0) V(0,-0.6) V(0.6,0.6) Rotate -30 degrees V(0,0.6) V(0.3,0.9) V(0,1.2) Combining Transformations Q: How do we specify each transformation? Specifying 2D Transformations • Translation – T(tx, ty) – Translation distances • Scale – S(sx,sy) – Scale factors • Rotation – R() – Rotation angle Combining Transformations • Using translate, rotation, and scale, how do we get: Combining Transformations • Note there are two ways to combine rotation and translation. Why? Let’s look at the equations P ( x, y ) T (t x , t y ) R( ) P' P T P' ' R P' x' x t x y' y t y x" x' cos y ' sin y" x' sin y ' cos x" x t x cos y t y ' sin y" x t x sin y t y ' cos P' R P P" P 'T x' x cos y sin y ' x sin y cos x" x cos y sin t x y" x sin y cos t y Combining them • We must do each step in turn. First we rotate the points, then we translate, etc. • Since we can represent the transformations by matrices, why don’t we just combine them? P' P T P' R P P' S P 2x2 -> 3x3 Matrices • We can combine transformations by expanding from 2x2 to 3x3 matrices. 1 0 t x x t x T t x , t y 0 1 t y y t y 0 0 1 s x 0 0 s 0 x S s x , s y 0 s 0 y 0 s y 0 0 1 cos sin cos sin R sin cos sin cos 0 0 0 0 1 Homogenous Coordinates • We need to do something to the vertices • By increasing the dimensionality of the problem we can transform the addition component of Translation into multiplication. xh 6 x 3 h 2 4 6 xh x 3 14 y h 4 P yh y Ex. 2, Ex. 14 7 7 h 2 2 y h 1 2 2 h h 2 Homogenous Coordinates • Homogenous Coordinates - term used in mathematics to refer to the effect of this representation on Cartesian equations. Converting a pt(x,y) and f(x,y)=0 -> (xh,yh,h) then in homogenous equations mean (v*xh,v*yh,v*h) can be factored out. • What you should get: By expressing the transformations with homogenous equations and coordinates, all transformations can be expressed as matrix multiplications. Final Transformations Compare Equations x' 1 0 t x x x ' t x x T t x , t y y ' 0 1 t y y y ' t y y 1 0 0 1 1 P T t x , t y P P T t x , t y P x' s x x' s x 0 x S s x , s y y ' 0 y ' 0 s y y 1 0 P S s x , s y P P S s x , s y P 0 sy 0 x' cos x' cos sin x R y ' sin y ' sin cos y 1 0 P R P P R P 0 x 0 y 1 1 sin cos 0 0 x 0 y 1 1 Combining Transformations P' A P, P" B P' P" A B P P(3,4), T (0.4641,2), R(60) x' 1 0 t x x y ' 0 1 t y y 1 0 0 1 1 x" cos sin y" sin cos 1 0 0 x" cos y" sin 1 0 sin cos 0 0 x ' 0 y ' 1 1 0 1 0 t x x' 0 0 1 t y y ' 1 0 0 1 1 x" cos sin t x cos t y sin x y" sin cos t sin t cos y x y 1 0 1 0 1 x" 1 0 t x cos sin 0 x y" 0 1 t sin cos 0 y y 1 0 0 1 0 0 1 1 x" cos sin t x x y" sin cos t y y 1 0 0 1 1 How would we get: How would we get: Coordinate Systems • Object Coordinates • World Coordinates • Eye Coordinates Object Coordinates World Coordinates Screen Coordinates Coordinate Hierarchy Screen Coordinates Transformation World->Screen World Coordinates Transformation Object #1 -> World Transformation Object #2 -> World Transformation Object #3 -> World Object #1 Object Coordinates Object #2 Object Coordinates Object #3 Object Coordinates Let’s reexamine assignment 1 Transformation Hierarchies • (See chapter 10 for details) • For example, a robot arm Transformation Hierarchies • Let’s examine: Transformation Hierarchies • What is a better way? Transformation Hierarchies • What is a better way? World Coordinates Transformation Hierarchies • We can have transformations be in relation to each other Transformation: Upper Arm -> World Upper Arm Coordinates Transformation: Lower -> Upper Lower Arm Coordinates Transformation: Hand-> Lower Hand Coordinates Rotation about a Fixed Point Start with identity matrix: C I Move fixed point to origin: C CT Rotate: C CR Move fixed point back: C CT -1 Result: C = TR T –1 which is backwards – Cp This result is a consequence of doing postmultiplications. Let’s try again. 44 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Reversing the Order We want C = T –1 R T so we must do the operations in the following order CI C CT -1 C CR C CT Each operation corresponds to one function call in the program. Note that the last operation specified is the first executed in the program Angel: Interactive Computer 45 Graphics 5E © Addison-Wesley 2009 OpenGL Example • Rotation about z axis by 30 degrees with a fixed point of (1.0, 2.0, 3.0) glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(1.0, 2.0, 3.0); glRotatef(30.0, 0.0, 0.0, 1.0); glTranslatef(-1.0, -2.0, -3.0); glBegin(GL_TRIANGLES); ... • Remember that last transform specified in the program is the first applied 46 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Matrix Stacks • In many situations we want to save transformation matrices for use later – Traversing hierarchical data structures (Chapter 10) – Avoiding state changes when executing display lists • OpenGL maintains stacks for each type of matrix – Access present type (as set by glMatrixMode) by glPushMatrix() glPopMatrix() 47 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009