Modeling Transformations 2 D Transformations 3 D Transformations

  • Slides: 69
Download presentation
Modeling Transformations ¬ 2 D Transformations ¬ 3 D Transformations ¬Open. GL Transformation 1

Modeling Transformations ¬ 2 D Transformations ¬ 3 D Transformations ¬Open. GL Transformation 1

2 D-Transformations ¬Basic Transformations ¬Homogeneous coordinate system ¬Composition of transformations 2

2 D-Transformations ¬Basic Transformations ¬Homogeneous coordinate system ¬Composition of transformations 2

Translation – 2 D x’ = x + dx y’ = y + dy

Translation – 2 D x’ = x + dx y’ = y + dy 3

Scaling – 2 D Types of Scaling: ØDifferential ( sx != sy ) ØUniform

Scaling – 2 D Types of Scaling: ØDifferential ( sx != sy ) ØUniform ( sx = sy ) 4

rota ted Rotation – 2 D l na i g i or 5

rota ted Rotation – 2 D l na i g i or 5

Rotation – 2 D 6

Rotation – 2 D 6

Mirror Reflection 7

Mirror Reflection 7

Shearing Transformation 8

Shearing Transformation 8

Inverse 2 D - Transformations 9

Inverse 2 D - Transformations 9

Homogeneous Co-ordinates ¬ Translation, scaling and rotation are expressed (non-homogeneously) as: – translation: P

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 3 matrices ¬ W is 1 for affine transformations in graphics 10

Homogeneous Co-ordinates ¬ P 2 d is a projection of Ph onto the w

Homogeneous Co-ordinates ¬ P 2 d 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 Ph(x, y, w) w=1 P 2 d(x, y, 1) y x 11

Classification of Transformations 1. Rigid-body Transformation ü Preserves parallelism of lines ü Preserves angle

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 ü Doesn’t preserve angle and length ü e. g. any sequence of R( ), S(sx, sy) and T(dx, dy) 12

Properties of rigid-body transformation The following Matrix is Orthogonal if the upper left 2

Properties of rigid-body transformation The following Matrix is Orthogonal if the upper left 2 X 2 matrix has the following properties 1. A) Each row are unit vector. 1. sqrt(r 11* r 11 + r 12* r 12) = 1 B) Each column are unit vector. sqrt(c 11* c 11 + c 12* c 12) = 1 2. A) Rows will be perpendicular to each other (r 11 , r 12 ). ( r 21 , r 22) = 0 B) Columns will be perpendicular to each other (c 11 , c 12 ). (c 21 , c 22) = 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

Commutativity of Transformation Matrices • In general matrix multiplication is not commutative • For the following special cases commutativity holds i. e. M 1. M 2 = M 2. M 1 M 2 Translate Scale 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

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

Rotation of about P(h, k): R , P Step 1: Translate P(h, k) to origin Step 2: Rotate w. r. t to origin Q 3(x’+h, y’ +k) Step 3: Translate (0, 0) to P(h, k 0) R , P= T(h , k) * R * T(-h , -k) P 3(h, k) Q(x, y) P(h, k) Q 1(x’, y’) P 1 (0, 0) Q 2(x’, y’) P 2 (0, 0) 16

Scaling w. r. t. P(h, k): Ssx, sy, p Step 1: Translate P(h, k)

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, sy, P= T(h , k) * S(sx, sy)* T(-h , -k) (4, 3) (1, 1) T(1, 1) (4, 2) (6, 1) (4, 1) S 3/2, 1/2, (1, 1) (0, 0) (4, 0) T(-1, -1) (7, 1) (0, 0) (6, 0) S(3/2, 1/2) 17

Reflection about line L, ML Step 1: Translate (0, b) to origin Step 2:

Reflection about line L, ML Step 1: Translate (0, b) to origin Step 2: Rotate - degrees Step 3: Mirror reflect about X-axis Step 4: Rotate degrees Step 5: Translate origin to (0, b) ML = T(0 , b) * R( ) * M x* R(- ) * T(0 , -b) 18

Problems to be solved: Schaum’s outline series: Problems: q 4. 1 q 4. 2

Problems to be solved: Schaum’s outline series: Problems: q 4. 1 q 4. 2 q 4. 3, 4. 4, 4. 5 => R , P q 4. 6, 4. 7, 4. 8 => Ssx, sy, , P q 4. 9, 4. 10, 4. 11, 4. 21 => ML q 4. 12 => Shearing q Pg-281(1. 24), Pg-320(5. 19) => Circular view-port 19

3 D Transformations ¬Basics of 3 D geometry ¬Basic 3 D Transformations ¬Composite Transformations

3 D Transformations ¬Basics of 3 D geometry ¬Basic 3 D Transformations ¬Composite Transformations 20

Orientation ¬Thumb points to +ve Z-axis ¬Fingers show +ve rotation from X to Y

Orientation ¬Thumb points to +ve Z-axis ¬Fingers show +ve rotation from X to Y axis 21

Vectors in 3 D Have length and direction V = [xv , yv ,

Vectors in 3 D Have length and direction V = [xv , yv , zv] Length is given by the Euclidean Norm c. K ||V|| = ( xv 2 + yv 2 + zv 2 ) + z J +b I a V= Dot Product (xv, yv, zv) V • U = [xv, yv, zv] • [xu, yu, zu] = xv*xu + yv*yu + zv*zu y = ||V|| || U|| cos ß x Cross Product V U = [yv*zu - zv yu , -xv*zu + zv*xu , xv*yu – yv*xu ] = ||V|| || U|| sin ß V U = - ( U x V) 22

3 D Equation of Curve & Line ¬ Parametric equations of Curve & Line

3 D Equation of Curve & Line ¬ Parametric equations of Curve & Line ¬ Curve z C y ¬ Line x t =1 0 t= t< 0 <1 t 0< V P 0(x 0, y 0, z 0) t> 1 P 1(x 1, y 1, z 1) 23

3 D Equation of Surface & Plane ¬ Parametric equations of Surface & Plane

3 D Equation of Surface & Plane ¬ Parametric equations of Surface & Plane ¬ Surface ¬ Plane : with Normal, N N P 0 24

3 D Plane ¬ Ways of defining a plane 1. 3 points P 0,

3 D Plane ¬ Ways of defining a plane 1. 3 points P 0, P 1, P 2 on the plane 2. Plane Normal N & P 0 on plane 3. Plane Normal N & a vector V on the plane N Plane Passing through P 0, P 1, P 2 P 0 P 1 P 2 V 25

Affine Transformation ¬ Transformation – is a function that takes a point (or vector)

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 , is called a 3 D affine transformation. ¬ The 4 th 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 – 3 D 27

Translation – 3 D 27

Scaling – 3 D Original scale Y axis scale all axes 28

Scaling – 3 D Original scale Y axis scale all axes 28

Rotation – 3 D For 3 D-Rotation 2 parameters are needed Rotation about z-axis:

Rotation – 3 D For 3 D-Rotation 2 parameters are needed Rotation about z-axis: ¬ Angle of rotation ¬ Axis of rotation 29

Rotation about Y-axis & X-axis About y-axis About x-axis 30

Rotation about Y-axis & X-axis About y-axis About x-axis 30

Shear along Z-axis y x z 31

Shear along Z-axis y x z 31

Object Transformation ¬Line: Can be transformed by transforming the end points ¬Plane: (described by

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 – 3 D Some of the composite transformations to be studied are:

Composite Transformations – 3 D 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 Av = R , i 34

AV : aligning vector V with k Av = R , i 34

AV : aligning vector V with k Av = R- , j * R

AV : aligning vector V with k Av = R- , j * R , i 35

AV : aligning vector V with k ¬ AV-1 = AVT ¬ AV, N

AV : aligning vector V with k ¬ AV-1 = AVT ¬ AV, N = AN-1 * AV 36

R , L : rotation about an axis L Let the axis L be

R , L : rotation about an axis L Let the axis L be represented by vector V and passing through point P 1. Translate P to the 2. origin Align V with vector k 3. Rotate about k 4. Reverse step 2 5. Reverse step 1 R , L = T-P-1 * AV-1 * R , k * AV * T-P 37

MN, P : Mirror reflection Let the plane be represented by plane normal N

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

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

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 2. origin Align N with vector k z N P x y MN, P = AN * T-P 40

MN, P : Mirror reflection Let the plane be represented by plane normal N

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 2. origin Align N with vector k z 3. Reflect w. r. t xy-plane P N x y MN, P = S 1, 1, -1 * AN * T-P 41

MN, P : Mirror reflection Let the plane be represented by plane normal N

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 2. origin Align N with vector k z 3. Reflect w. r. t xy-plane x 4. Reverse step 2 MN, P = AN-1 * S 1, 1, -1 * AN * T-P y 42

MN, P : Mirror reflection Let the plane be represented by plane normal N

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 2. origin Align N with vector k z N P 3. Reflect w. r. t xy-plane x 4. Reverse step 2 5. Reverse step 1 MN, P = T-P-1 * AN-1 * S 1, 1, -1 * AN * T-P y 43

Further Composition ¬ Translate points in fig. 1 into points in fig 2 such

Further Composition ¬ Translate points in fig. 1 into points in fig 2 such that: – P 3 is moved to yz plane – P 2 is on z-axis – P 1 is at Origin ¬ The Composite Transform must have – Translation of P 1 to Origin T – Some Combination of Rotations R y y y T x z Fig. 1 R x x z z Fig. 2 44

Finding R 45

Finding R 45

Finding Rz y Rz x z R y x z 46

Finding Rz y Rz x z R y x z 46

Finding Rx y Rz x Rx z R y x z 47

Finding Rx y Rz x Rx z R y x z 47

Finding Ry y Rz Ry x Rx z R y x z 48

Finding Ry y Rz Ry x Rx z R y x z 48

Problems to be solved: Schaum’s outline series: Problems: q 6. 1 q 6. 2,

Problems to be solved: Schaum’s outline series: Problems: q 6. 1 q 6. 2, 6. 5, 6. 9, 6. 10, 6. 11, 6. 12 Av q 6. 3, 6. 4 R , L q 6. 6, 6. 7, 6. 8 MN, P 49

Transformations in Open. GL ¬Open. GL transformation commands ¬Transformation Order ¬Hierarchical Modeling 50

Transformations in Open. GL ¬Open. GL transformation commands ¬Transformation Order ¬Hierarchical Modeling 50

Transformations in Open. GL ¬ Open. GL uses 3 stacks to maintain transformation matrices:

Transformations in Open. GL ¬ Open. GL 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 N • M • P 51

General Transformation Commands ¬ Specify current matrix (stack) : – void gl. Matrix. Mode(GLenum

General Transformation Commands ¬ Specify current matrix (stack) : – void gl. Matrix. Mode(GLenum mode) C B A I B A gl. Load. Matrix(M) gl. Lo ad. Id entit y • Mode : GL_MODELVIEW, GL_PROJECTION, GL_TEXTURE M B A ¬ Initialize current matrix. – void gl. Load. Identity(void) • Sets the current matrix to 4 X 4 identity matirx – void gl. Load. Matrix{f|d}(cost TYPE *M) • Sets the current matrix to 4 X 4 matrix specified by M Note: current matrix Top most matrix of the current matrix 52

General Transformation Commands ¬Concatenate Current Matrix: – void gl. Mult. Matrix(const TYPE *M) •

General Transformation Commands ¬Concatenate Current Matrix: – void gl. Mult. Matrix(const TYPE *M) • Multiplies current matrix C, by M. i. e. C = C*M – Caveat: Open. GL matrices are stored in column major order. – Best use utility function gl. Translate, gl. Rotate, gl. Scale for common transformation tasks. 53

Transformations and Open. GL® ¬ Each time an Open. GL transformation M is called

Transformations and Open. GL® ¬ Each time an Open. GL transformation M is called the current MODELVIEW matrix C is altered: gl. Translatef(1. 5, 0. 0); gl. Rotatef(45. 0, 0. 0, 1. 0); Note: v is any vertex placed in rendering pipeline v’ is the transformed vertex from v. 54

Sample Instance Transformation gl. Matrix. Mode(GL_MODELVIEW); gl. Load. Identity(); gl. Translatef(. . . );

Sample Instance Transformation gl. Matrix. Mode(GL_MODELVIEW); gl. Load. Identity(); gl. Translatef(. . . ); gl. Rotatef(. . . ); gl. Scalef(. . . ); glu. Cylinder(. . . ); 55

Thinking About Transformations ¬ There is a World Coordinate System where: Ø All objects

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 T • R Global View v Rotate Object v Then Translate

Order of Transformation T • R Global View v Rotate Object v Then Translate gl. Load. Identity(); gl. Multi. Matrixf( T); Local View gl. Multi. Matrixf( R); v Translate Object draw_ the_ object( v Then Rotate v); v’ = ITRv Effect is same, but perception is different 57

Order of Transformation R • T Global View v Translate Object v Then Rotate

Order of Transformation R • T Global View v Translate Object v Then Rotate gl. Load. Identity(); gl. Multi. Matrixf( R); Local View gl. Multi. Matrixf( T); v Rotate Object draw_ the_ object( v Then Translate v); v’ = ITRv Effect is same, but perception is different 58

Hierarchical Modeling ¬ Many graphical objects are structured ¬ Exploit structure for – Efficient

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

Open. GL & Hierarchical Model ¬Some of the Open. GL functions helpful for hierarchical

Open. GL & Hierarchical Model ¬Some of the Open. GL functions helpful for hierarchical modeling are: – void gl. Push. Matrix(void); – void gl. Poip. Matrix(void); – void gl. Get. Floatv(GL_MODELVIEW_MATRIX, *m); rix Mat sh gl. Pu C B A C C B A gl. P u sh. M atr ix B A C B A gl. Get. Floatv m C 60

Scene Graph ¬ A scene graph is a hierarchical representation of a scene ¬

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[16]; // 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, . .

Example - Torso ¬ Initializing transformation matrix for node treenode torso, head, . . . ; /* in init function */ gl. Load. Identity(); gl. Rotatef(. . . ); gl. Get. Floatv(GL_MODELVIEW_MATRIX, torso. m); ¬ Initializing pointers torso. f = draw. Torso; torso. sibling = NULL; torso. child = &head; 62

Generic Traversal ¬To render the hierarchy: – Traverse the scene graph depth-first – Going

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

Generic Traversal : Torso ¬ Recursive definition void traverse (treenode *root) { if (root == NULL) return; gl. Push. Matrix(); gl. Mult. Matrixf(root->m); root->f(); if (root->child != NULL) traverse(root>child); gl. Pop. Matrix(); if (root->sibling != NULL) traverse(root>sibling); } 64

Viewing Transformation 65

Viewing Transformation 65

Viewing Pipeline Revisited Graphics Primitives Modeling Transform Po Object Coordinates Pw Viewing Transform Pe

Viewing Pipeline Revisited Graphics Primitives Modeling Transform Po Object Coordinates Pw Viewing Transform Pe World Coordinate s Eye Coordinate s ye yw xe -ze yo pe po pw xo xw zo zw 66

Viewing Transformation in Open. GL ¬ To setup the modelview matrix, Open. GL provides

Viewing Transformation in Open. GL ¬ To setup the modelview matrix, Open. GL provides the following function: glu. Look. At( eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz ) 67

Implementation We want to construct an Orthogonal Frame such that, (1) its origin is

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. Clearly, 68

Thank You 69

Thank You 69