CS 4731 Computer Graphics Lecture 8 3 D

  • Slides: 28
Download presentation
CS 4731: Computer Graphics Lecture 8: 3 D Affine transforms Emmanuel Agu

CS 4731: Computer Graphics Lecture 8: 3 D Affine transforms Emmanuel Agu

Introduction to Transformations n Introduce 3 D affine transformation: n n n n Position

Introduction to Transformations n Introduce 3 D affine transformation: n n n n Position (translation) Size (scaling) Orientation (rotation) Shapes (shear) Previously developed 2 D (x, y) Now, extend to 3 D or (x, y, z) case Extend transform matrices to 3 D Enable transformation of points by multiplication

Point Representation n Previously, point in 2 D as column matrix n Now, extending

Point Representation n Previously, point in 2 D as column matrix n Now, extending to 3 D, add z-component: or

3 D Coordinate Systems n All perpendicular: X x Y = Z ; Y

3 D Coordinate Systems n All perpendicular: X x Y = Z ; Y x Z = X; Z x X = Y; n Tip: sweep fingers x-y: thumb is z Y +z y +z x x Left hand coordinate system Right hand coordinate system • Not used in this class and • Not in Open. GL

Transforms in 3 D n n 2 D: 3 x 3 matrix multiplication 3

Transforms in 3 D n n 2 D: 3 x 3 matrix multiplication 3 D: 4 x 4 matrix multiplication: homogenous coordinates Recall: transform object = transform each vertice General form: Xform of P

3 x 3 2 D Translation Matrix §Previously, 2 D :

3 x 3 2 D Translation Matrix §Previously, 2 D :

3 x 3 2 D Translation Matrix §Now, 3 D : Open. GL: gltranslated(tx,

3 x 3 2 D Translation Matrix §Now, 3 D : Open. GL: gltranslated(tx, ty, tz) §Where: x’= x. 1 + y. 0 + z. 0 + tx. 1 = x + tx, … etc

2 D Scaling §Scale: Alter object size by scaling factor (sx, sy). i. e

2 D Scaling §Scale: Alter object size by scaling factor (sx, sy). i. e x’ = x. Sx y’ = y. Sy (4, 4) (2, 2) (1, 1) Sx = 2, Sy = 2 (2, 2)

3 x 3 2 D Scaling Matrix

3 x 3 2 D Scaling Matrix

4 x 4 3 D Scaling Matrix • Example: • If Sx = Sy

4 x 4 3 D Scaling Matrix • Example: • If Sx = Sy = Sz = 0. 5 • Can scale: • big cube (sides = 1) to small cube ( sides = 0. 5) • 2 D: square, 3 D cube Open. GL: gl. Scaled(Sx, Sy, Sz)

Rotation (x, y) -> Rotate about the origin by (x’, y’) (x, y) r

Rotation (x, y) -> Rotate about the origin by (x’, y’) (x, y) r f How to compute (x’, y’) ? x = r cos ( ) y = r sin ( ) x’ = r cos ( + ) y = r sin ( + )

Rotation Using trig identity (x’, y’) x’ = x cos( ) – y sin(

Rotation Using trig identity (x’, y’) x’ = x cos( ) – y sin( ) y’ = y cos( ) + x sin( ) (x, y) r f Matrix form? 3 x 3?

3 x 3 2 D Rotation Matrix (x’, y’) r (x, y) f

3 x 3 2 D Rotation Matrix (x’, y’) r (x, y) f

Rotating in 3 D n n Cannot do mindless conversion like before Why? n

Rotating in 3 D n n Cannot do mindless conversion like before Why? n n n Rotate about what axis? 3 D rotation: about a defined axis Different Xform matrix for: • Rotation about x-axis • Rotation about y-axis • Rotation about z-axis n New terminology n n n X-roll: rotation about x-axis Y-roll: rotation about y-axis Z-roll: rotation about z-axis

Rotating in 3 D n New terminology n n X-roll: rotation about x-axis Y-roll:

Rotating in 3 D n New terminology n n X-roll: rotation about x-axis Y-roll: rotation about y-axis Z-roll: rotation about z-axis Which way is +ve rotation n n Look in –ve direction (into +ve arrow) CCW is +ve rotation y + z x

Rotating in 3 D

Rotating in 3 D

Rotating in 3 D n n For a rotation angle, about an axis Define:

Rotating in 3 D n n For a rotation angle, about an axis Define: A x-roll: Open. GL: glrotated( , 1, 0, 0)

Rotating in 3 D A y-roll: Rules: Open. GL: • Rotate row, column int.

Rotating in 3 D A y-roll: Rules: Open. GL: • Rotate row, column int. is 1 glrotated( , 0, 1, 0) • Rest of row/col is 0 • c, s in rect pattern A z-roll: Open. GL: glrotated( , 0, 0, 1)

Rotating in 3 D Q: Using y-roll equation, rotate P = (3, 1, 4)

Rotating in 3 D Q: Using y-roll equation, rotate P = (3, 1, 4) by 30 degrees: A: c = cos(30) = 0. 866, s = sin(30) = 0. 5, and E. g. first line: 3. c + 1. 0 + 4. s + 1. 0 = 4. 6

Rotating in 3 D Q: Write C code to Multiply point P = (Px,

Rotating in 3 D Q: Write C code to Multiply point P = (Px, Py, Pz, 1) by a 4 x 4 matrix shown below to give new point Q = (Qx, Qy, Qz, 1). i. e. where

Rotating in 3 D n Outline of solution: n Declare P, Q as array:

Rotating in 3 D n Outline of solution: n Declare P, Q as array: • Double P[4], Q[4]; n Declare transform matrix as 2 -dimensional array • Double M[4][4]; n n Remember: C indexes from 0, not 1 Long way: • Write out equations line by line expression for Q[i] • E. g. Q[0] = P[0]*M[0][0] + P[1]*M[0][1] + P[2]*M[0][2] + P[3]*M[0][3] n Cute way: • Use indexing, say i for outer loop, j for inner loop

Rotating in 3 D n Using loops looks like: n n n for(i=0; i<4;

Rotating in 3 D n Using loops looks like: n n n for(i=0; i<4; i++) { temp = 0; for(j=0; j<4; j++) { temp += P[j]*M[i][j]; } Q[i] = temp; } Test matrice code rigorously Use known results (or by hand) and plug into your code

3 D Rotation About Arbitrary Axis n n Arbitrary rotation axis (rx, ry, rz)

3 D Rotation About Arbitrary Axis n n Arbitrary rotation axis (rx, ry, rz) open. GL: rotate( , rx, ry, rz) Without open. GL: a little hairy!! Important: read Hill pp. 239 -241 y (rx, ry, rz) x z

3 D Rotation About Arbitrary Axis n Can compose arbitrary rotation as combination of:

3 D Rotation About Arbitrary Axis n Can compose arbitrary rotation as combination of: n n n X-roll Y-roll Z-roll

3 D Rotation About Arbitrary Axis n n n Classic: use Euler’s theorem: any

3 D Rotation About Arbitrary Axis n n n Classic: use Euler’s theorem: any sequence of rotations = one rotation about some axis Our approach: n n Want to rotate about the axis u through origin and arbitrary point Use two rotations to align u and x-axis Do x-roll through angle Negate two previous rotations to de-align u and x-axis

3 D Rotation About Arbitrary Axis

3 D Rotation About Arbitrary Axis

Composing Transformation n n Composing transformation – applying several transforms in succession to form

Composing Transformation n n Composing transformation – applying several transforms in succession to form one overall transformation Example: M 1 X M 2 X M 3 X P where M 1, M 2, M 3 are transform matrices applied to P Be careful with the order Matrix multiplication is not commutative

References n Hill, chapter 5. 3

References n Hill, chapter 5. 3