# 3D Transformations Modeling Transformations 2D Transformations 3D Transformations OpenGL Transformation 1 2D-Transformations Basic Transformations Homogeneous coordinate system Composition of transformations 2 Translation 2D Y Y (4,5) (7,5) (7,1) Before Translation x = x + dx y = y + dy X

Translation by (3,-4) dx x x P P T y y dy Homogenious Form x 1 0 d x x y 0 1 d * y y 1 0 0 1 1 (10,1) X P P T 3 Scaling 2D Y Y (4,5)

Types of Scaling: Differential ( sx != sy ) Uniform ( sx = sy ) (7,5) (2,5/4) X Before Scaling (7/2,5/4) Scaling by (1/2, 1/4) X x s x * x y s y * y S sx 0 * P P 0 x x * sx *

s y y y * s y Homogenious Form x s x y 0 1 0 0 sx 0 0 x 0 * y 1 1 4 te d rota Rotation 2D r cos v r sin r cos

v r sin x r cos cos r sin sin expand y r cos sin r sin cos x r cos x x cos y sin but y r sin y x sin y cos or al n i ig Rotation 2D Y

Y Before Rotation Rotation of 45 deg. w.r.t. origin (4.9,7.8) (2.1,4.9) (5,2) (9,2) X X x * cos y * sin x x * sin y * cos y R *P P cos sin x x * cos y * sin sin cos * y x * sin y * cos Homogenious Form

x cos sin 0 x y sin cos 0 * y 1 0 0 1 1 6 Mirror Reflection Y Y (1,1) (-1,1) (1,1) X X (1,-1) Reflection about X - axis x x y y Reflection about Y - axis x x y y 1 0 0

M x 0 1 0 0 0 1 1 0 0 M y 0 1 0 0 0 1 7 Shearing Transformation 1 a 0 SH x 0 1 0 0 0 1 unit cube Sheared in X direction 1 0 0 SH y b 1 0 0 0 1 Sheared in Y direction SH xy 1 a 0 b 1 0

0 0 1 Sheared in both X and Y direction 8 Inverse 2D - Transformations -1 (dx,dy) Translaiton : T Rotation Sclaing -1 ( ) : R : S R(- ) -1 (sx,sy) Mirror Ref : M M -1 x

-1 y T(-dx,-dy) S ( 1 sx , 1 sy ) M x M y 9 Homogeneous Co-ordinates Translation, scaling and rotation are expressed (non-homogeneously) as: translation: P = P + T Scale: P = S P Rotate: P = R P Composition is difficult to express, since translation not expressed as a matrix multiplication Homogeneous coordinates allow all three to be expressed homogeneously, using multiplication by 3 matrices W is 1 for affine transformations in graphics 10 Homogeneous Co-ordinates P2d is a projection of Ph onto the w = 1 plane So an infinite number of points correspond to :

they constitute the whole line (tx, ty, tw) w w= 1 Ph(x,y,w ) P2d(x,y,1) y x 11 Classification of Transformations 1. Rigid-body Transformation Preserves parallelism of lines Preserves angle and length e.g. any sequence of R() and T(dx,dy) 2. Affine Transformation Preserves parallelism of lines Doesnt preserve angle and length e.g. any sequence of R(), S(sx,sy) and T(dx,dy) unit cube 45 deg rotaton Scale in X not in Y

12 Properties of rigid-body transformation The following Matrix is Orthogonal if the upper left 2X2 matrix has the following properties r11 r 21 0 1.A) Each row are unit vector. sqrt(r11* r11 + r12* r12) = 1 B) Each column are unit vector. sqrt(c11* c11 + c12* c12) = 1 2.A) Rows will be perpendicular to each other (r11 , r12 ) . ( r21 , r22) = 0 cos sin 0 r12 r22 0 sin cos

0 tx t y 1 0 0 1 B) Columns will be perpendicular to each other (c11 , c12 ) . (c21 ,c22) = 0 e.g. Rotation matrix is orthogonal Orthogonal Transformation Rigid-Body Transformation For any orthogonal matrix B B-1 = BT 13 Commutativity of Transformation Matrices In general matrix multiplication is not commutative For the following special cases commutativity holds i.e. M1.M2 = M2.M1 M1 M2 Translate Translate

Scale Scale Rotate Rotate Uniform Scale Rotate Some non-commutative Compositions: Non-uniform scale, Rotate Translate, Scale Rotate, Translate Original Transitional Final 14 Associativity of Matirx Multiplication Create new affine transformations by multiplying sequences of the above basic transformations. q = CBAp q = ( (CB) A) p = (C (B A))p = C (B (Ap) ) etc.

matrix multiplication is associative. To transform just a point, better to do q = C(B(Ap)) But to transform many points, best to do M = CBA then do q = Mp for any point p to be rendered. For geometric pipeline transformation, define M and set it up with the model-view matrix and apply it to any vertex subsequently defined to its setting. 15 Rotation of about P(h,k): R,P Step 1: Translate P(h,k) to origin Step 2: Rotate w.r.t to origin Step 3: Translate (0,0) to P(h,k0) R,PP= T(h ,Pk) * R* T(-h ,Pk) Q3(x+h, y +k) P3(h,k ) Q(x,y) P(h,k)

Q1(x,y ) P1 (0,0) Q2(x,y ) P2 (0,0) 16 Scaling w.r.t. P(h,k): Ssx,sy,p Step 1: Translate P(h,k) to origin Step 2: Scale S(sx,sy) w.r.t origin (7,2) Step 3: Translate (0,0) to P(h,k) Ssx,Psy,PP= T(h ,Pk) * S(sx,Psy)*T(-h ,P-k) (1, 1) T(1,P1 (7,1 ) ) (4,3) (1, 1)

(4,1 ) S3/2,P1/2,P(1,P1) (4,2) (0, (4,0 0)T(-1,P-1)) (6,1) (0, (6,0 17 0) S(3/2,P1/2) ) Reflection about line L, ML Y Step 1: Translate (0,b) to origin Step 2: Rotate - degrees (0,b) Step 3: Mirror reflect about X-axis Step 4: Rotate degrees t O X

Step 5: Translate origin to (0,b) ML = T(0 ,Pb) * R() * M x* R(-) * T(0 ,P-b) 18 Problems to be solved: Schaums outline series: Problems: 4.1 4.2 4.3, 4.4, 4.5 => R,P 4.6, 4.7, 4.8 => Ssx,sy,,P 4.9, 4.10, 4.11, 4.21 => ML 4.12 => Shearing Pg-281(1.24), Pg-320(5.19) => Circular view-port 19 3D Transformations Basics of 3D geometry Basic 3D Transformations Composite Transformations

20 Orientation Thumb points to +ve Z-axis Fingers show +ve rotation from X to Y axis Y Y Z (larger z are away from viewer) X X Z (out of page) Right-handed orentation Left-handed orentation 21 Vectors in 3D Have length and direction V = [xv , yv , zv] Length is given by the Euclidean Norm ||V|| = ( xv2 + yv2 + zv2 ) Dot Product V U = [xv, yv, zv][xu, yu, zu]

= xv*xu + yv*yu + zv*zu = ||V|| || U|| cos Cross Product VU = [yv*zu - zv yu , -xv*zu + zv*xu , xv*yu yv*xu ] = ||V|| || U|| sin V U = - ( U x V) z cK + J +b I a V= (xv,yv,zv) y x 22 3D Equation of Curve & Line Parametric equations of Curve & Line Curve z x f t

C C : y g t a t b Line z h t y x V P0 P1 P1 P0 x x0 x1 x0 t t =1 L : y y0 y1 y0 t 0 t 1 z z0 z1 z0 t L P0 t ( P1 P0 ) P0 tV t t= <0 0 <1 t 0< V

P0(x0,y0,z0) t> 1 P1(x1,y1,z1) 23 3D Equation of Surface & Plane Parametric equations of Surface & Plane Surface x f s, t S : y g s, t z h s, t a s b c t d Plane : with Normal, N N Ax By Cz D 0 N Ai Bj Ck P0 24

3D Plane Ways of defining a plane 1. 3 points P0, P1, P2 on the plane 2. Plane Normal N & P0 on plane 3. Plane Normal N & a vector V on the plane N Plane Passing through P0, P1, P2 N P0 P1 P0 P2 Ai Bj Ck if P(x, y, P0 z) is on the plane P1 N P0 P 0 P2 V ( Ai Bj Ck) ( x x0 )i ( y y0 ) j ( z z0 )k 0 Ax By Cz D 0 where D ( Ax0 By0 Cz0 )

25 Affine Transformation Transformation is a function that takes a point (or vector) and maps that point (or vector) into another point (or vector). A coordinate transformation of the form: x = axx x + axy y + axz z + bx , y = ayx x + ayy y + ayz z + by , z = azx x + azy y + azz z + bz , x' a xx y ' a yx z' a zx w 0 a xy a yy a xz a yz a zy 0 a zz 0

bx x b y y bz z 1 1 is called a 3D affine transformation. The 4th row for affine transformation is always [0 0 0 1]. Properties of affine transformation: translation, scaling, shearing, rotation (or any combination of them) are examples affine transformations. Lines and planes are preserved. parallelism of lines and planes are also preserved, but not angles and length. 26 Translation 3D x x d x y y d y z z d z 1 0 0 0 0 0 dx x x dx 1 0 d y y y d y

* 0 1 dz z z dz 0 0 1 1 1 T ( d x , d y , d z ) * P P 27 Scaling 3D x s x * x y s y * y Original scale Y axis S (sx , s y , sz ) scale all axes z s z * z sx 0 0

0 * P 0 0 sy 0 0 0 sz 0 P 0 x x * s x 0 y y * s y * 0 z z * s z 28

1 1 1 Rotation 3D For 3D-Rotation 2 parameters are needed Rotation about z-axis: Angle of rotation Axis of rotation cos sin 0 0 R ,k * P P sin

cos 0 0 0 0 x x * cos y * sin 0 0 y x * sin y * cos * 1 0 z z 0 1 1 1 29 Rotation about Y-axis & X-axis About y-axis R , j * P P

cos 0 sin 0 About x-axis 0 1 0 cos 0 sin 0 0 0 sin 1 0 0 cos 0 0 0 x x * cos z * sin 0 y

y * 0 z x * sin z * cos 1 1 1 R , i * P P 0 sin cos 0 0 x x

0 y y * cos z * sin * 0 z y * sin z * cos 1 1 1 30 Shear along Z-axis y x z SH xy ( sh x , sh y ) * P 1 0 0 0 0 sh x 1 sh y

0 1 0 0 P 0 x x z * sh x 0 y y z * sh y * 0 z z 1 1 1 31 Object Transformation Line: Can be transformed by transforming the end points

Plane:(described by 3-points) Can be transformed by transforming the 3-points Plane:(described by a point and Normal) Point is transformed as usual. Special treatment is needed for transforming Normal 32 Composite Transformations 3D Some of the composite transformations to be studied are: AV,N = aligning a vector V with a vector N R,L = rotation about an axis L( V, P ) Ssx,sy,P= scaling w.r.t. point P 33 AV : aligning vector V with k Step 1 : Rotate about x - axis by b sin 2 2 b c c cos z ( 0, 0,

) ( 0, b,c b ( a, 0, ) ) c |V| V = aI + bJ + cK |V| k b y a

Av = R,i 34 x AV : aligning vector V with k Step 1 : Rotate about x - axis by b sin 2 2 b c c cos z ( 0, 0, |V|) z a ( 0, b,cb,c b

( a, 0, ) ) a | V | 2 2 2 | V | a b c cos( ) | V | Step 2 : Rotate V about y - axis by - c |V||V| P( a, b, c) sin( ) Av = R-,j * R,i

k b y a 35 x AV : aligning vector V with k AV-1 = AVT AV,N = AN-1 * AV V 0 AV a V 0 - ab V c b V

- ac V b c V 0 0 0 0 0 1 36 R,L : rotation about an axis L Let the axis L be represented by vector V and z passing through point P 1. Translate P to the origin 2. Align V with vector k 3. Rotate about k 4. Reverse step 2 5. Reverse step 1

x L P Q V k R,L = T-P-1 * AV-1 * R,k * AV * T-P Q' y 37 MN,P : Mirror reflection Let the plane be represented by plane normal N and a point P in that plane z N P x y 38

MN,P : Mirror reflection Let the plane be represented by plane normal N and a point P in that plane 1. Translate P to the origin z N P x y MN,P = T-P 39 MN,P : Mirror reflection Let the plane be represented by plane normal N and a point P in that plane 1. Translate P to the origin z N 2. Align N with vector k P

x y MN,P = AN * T-P 40 MN,P : Mirror reflection Let the plane be represented by plane normal N and a point P in that plane 1. Translate P to the origin 2. Align N with vector k 3. Reflect w.r.t xy-plane z N P x y MN,P = S1,1,-1 * AN * T-P

41 MN,P : Mirror reflection Let the plane be represented by plane normal N and a point P in that plane 1. Translate P to the origin z 2. Align N with vector k 3. Reflect w.r.t xy-plane x 4. Reverse step 2 MN,P = AN-1 * S1,1,-1 * AN * T-P y 42 MN,P : Mirror reflection Let the plane be represented by plane normal N and a point P in that plane 1. Translate P to the origin 2. Align N with vector k 3. Reflect w.r.t xy-plane x

4. Reverse step 2 5. Reverse step 1 z N P MN,P = T-P-1 * AN-1 * S1,1,-1 * AN * T-P y 43 Further Composition Translate points in fig. 1 into points in fig 2 such that: P3 is moved to yz plane P2 is on z-axis P1 is at Origin The Composite Transform must have Translation of P1 to Origin T Some Combination of Rotations R y y P3 P2

P1 x z Fig. 1 y P3 T R P2 P1 z P3 P1 x z P2 Fig. 2 x

44 Finding R Let R be r11 r12 R r21 r22 r31 r32 R is Rigid i) ii) r13 Rx .x Rx . y Rx .z r23 R y .x R y . y R y .z r33 Rz .x Rz . y Rz .z - body Transform Rx , R y , Rz are unit vectors Rx , R y , Rz are perpendicu lar to each other Note : Rx .x x component of vextor Rx 45 Finding Rz R aligns

P1P2 along z - axis y P3 P P R 1 2 k P1P2 P1P2 R k P1P2 T Rx . x Rx . y Rx .z R y .x Ry . y R y .z Rz R 1

R T P1 x z R Rz . x 0 P P Rz . y 0 1 2 Rz .z 1 P1P2 Rz . x P1P2 Rz . y Rz Rz .z P1P2 P2

y P3 k z P2 P1 x 46 Finding Rx R aligns P1P3 P1P2 along x - axis y P3 P1P3 P1P2 R i P1P3 P1P2 R i 1 Rx . x

Rx . y Rx . z P1 P1P3 P1P2 x R Rz . x 1 P P P P Rz . y 0 1 3 1 2 Rz .z 0 P1P3 P1P2 Rx . x P1P3 P1P2 Rx . y Rx Rx .z P1P3 P1P2 P2 Rx

z P1P3 P1P2 R y .x Ry . y R y .z Rz y P3 k z P2 P1 i x 47 Finding Ry R aligns

y Rz Rx along y - axis P3 R Rz Rx j Rz P1 R j Rz Rx Rx . x Rx . y Rx .z R y .x Ry . y R y .z x R Rz . x 0

Rz . y 1 Rz Rx Rz .z 0 R y .x R y . y Rz Rx R y R y .z Ry Rx z 1 P2 y P3 k z P2 j P1

i x 48 Problems to be solved: Schaums outline series: Problems: 6.1 6.2, 6.5, 6.9, 6.10, 6.11, 6.12 Av 6.3, 6.4 R,L 6.6, 6.7, 6.8 MN,P 49 Transformations in OpenGL OpenGL transformation commands Transformation Order Hierarchical Modeling 50 Transformations in OpenGL

OpenGL uses 3 stacks to maintain transformation matrices: Model & View transformation matrix stack Projection matrix stack Texture matrix stack You can load, push and pop the stack The top most matrix from each stack is applied to all graphics primitive until it is changed Graphics Primitives (P) M Model-View Matrix Stack N Projection Matrix Stack Output NMP 51 General Transformation Commands Specify current matrix (stack) : void glMatrixMode(GLenum mode)

I B A Mode : GL_MODELVIEW, GL_PROJECTION, GL_TEXTURE glLoadMatrix(M) glLo a dIde n tity C B A M B A Initialize current matrix. void glLoadIdentity(void) Sets the current matrix to 4X4 identity matirx void glLoadMatrix{f|d}(cost TYPE *M) Sets the current matrix to 4X4 matrix specified by M

Note: current matrix Top most matrix of the current matrix stack 52 General Transformation Commands Concatenate Current Matrix: void glMultMatrix(const TYPE *M) Multiplies current matrix C, by M. i.e. C = C*M Caveat: OpenGL matrices are stored in column major order. m1 m 2 m3 m4 m5 m6 m7 m9 m10 m11 m8 m12

m13 m14 m15 m16 Best use utility function glTranslate, glRotate, glScale for common transformation tasks. 53 Transformations and OpenGL Each time an OpenGL transformation M is called the current MODELVIEW matrix C is altered: v Cv v CMv glTranslatef(1.5, 0.0, 0.0); glRotatef(45.0, 0.0, 0.0, 1.0); v CTRv Note: v is any vertex placed in rendering pipeline v is the transformed vertex from v. 54 Sample Instance Transformation glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(...); glRotatef(...);

glScalef(...); gluCylinder(...); 55 Thinking About Transformations There is a World Coordinate System where: All objects are defined Transformations are in World Coordinate space Two Different Views As a Global System Objects moves but coordinates stay the same Think of transformation in reverse order as they appear in code As a Local System Objects moves and coordinates move with it Think of transformation in same order as they appear in code 56 Order of Transformation TR Global View Rotate Object Then Translate

glLoadIdentity(); Local View glMultiMatrixf( T); Translate Object glMultiMatrixf( R); Then Rotate draw_ the_ object( v); v = ITRv Effect is same, but perception is different 57 Order of Transformation RT Global View Translate Object Then Rotate glLoadIdentity(); Local View glMultiMatrixf( R); Rotate Object glMultiMatrixf( T); Then Translate draw_ the_ object( v); v = ITRv Effect is same, but perception is different 58

Hierarchical Modeling Many graphical objects are structured Exploit structure for Efficient rendering Concise specification of model parameters Physical realism Often we need several instances of an object Wheels of a car Arms or legs of a figure Chess pieces Encapsulate basic object in a function Object instances are created in standard form Apply transformations to different instances Typical order: scaling, rotation, translation 59 OpenGL & Hierarchical Model Some of the OpenGL functions helpful for hierarchical modeling are: void glPushMatrix(void); void glPoipMatrix(void); void glGetFloatv(GL_MODELVIEW_MATRIX, *m); s

glPu C B A trix a M h glP ush Ma trix C C B A B A C B A glGetFloatv m

C 60 Scene Graph A scene graph is a hierarchical representation of a scene We will use trees for representing hierarchical objects such that: Nodes represent parts of an object Topology is maintained using parent-child relationship Edges represent transformations that applies to a part and all the subparts connected to that part Scene typedef struct treenode { GLfloat m; // Transformation Sun Star X void (*f) ( ); // Draw function struct treenode *sibling; Earth Venus Saturn struct treenode *child; } treenode; Moon Ring 61

Example - Torso Initializing transformation matrix for node treenode torso, head, ...; /* in init function */ glLoadIdentity(); glRotatef(...); glGetFloatv(GL_MODELVIEW_MATRIX, torso.m); Initializing pointers torso.f = drawTorso; torso.sibling = NULL; torso.child = &head; 62 Generic Traversal To render the hierarchy: Traverse the scene graph depth-first Going down an edge: push the top matrix onto the stack apply the edge's transformation(s) At each node, render with the top matrix Going up an edge: pop the top matrix off the stack 63 Generic Traversal : Torso Recursive definition

void traverse (treenode *root) { if (root == NULL) return; glPushMatrix(); glMultMatrixf(root->m); root->f(); if (root->child != NULL) traverse(root->child); glPopMatrix(); if (root->sibling != NULL) traverse(root->sibling); } C is really not the right language for this !! 64 Viewing Transformation 65 Viewing Pipeline Revisited Graphics Primitives Modeling Transform Po Object Coordinates Pw

Viewing Transform Pe World Coordinates Eye Coordinates ye yw xe yo -ze pe po pw xo xw zo

zw 66 Viewing Transformation in OpenGL To setup the modelview matrix, OpenGL provides the following function: gluLookAt( eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz ) up (upx, upy, upz) eye (eyex, eyey, eyez) z y center (centerx, centery, centerz) x 67 Implementation We want to construct an Orthogonal Frame such that,

(1) its origin is the point eye (2) its -z basis vector points towards the point center (3) the up vector projects to the up direction (+ve y-axis) Let C (for camera) denote this frame.C.e Clearly, C.O eye v normalize center eye C.ez v C.ex normalize v up C.e y C.ez C.ex y up (upx, upy, upz) v center

C.O (eye) C.e C.e x 68 z Thank You 69