Transformations Aaron Bloomfield CS 445 Introduction to Graphics
Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)
Graphics coordinate systems n n n X is red Y is green Z is blue 2
Graphics coordinate systems n n If you are on the +z axis, and +y is up, then +x is to the right Math fields have +x going to the left 3
Outline Ø n n n Scaling Rotations Composing Rotations Homogeneous Coordinates Translations Projections 4
Scaling n n Scaling a coordinate means multiplying each of its components by a scalar Uniform scaling means this scalar is the same for all components: 2 5
Scaling n Non-uniform component: scaling: different scalars per X 2, Y 0. 5 n How can we represent this in matrix form? 6
Scaling n Scaling operation: n Or, in matrix form: scaling matrix 7
Outline n Ø n n Scaling Rotations Composing Rotations Homogeneous Coordinates Translations Projections 8
2 -D Rotation (x’, y’) (x, y) x’ = x cos( ) - y sin( ) y’ = x sin( ) + y cos( ) 9
2 -D Rotation n This is easy to capture in matrix form: n 3 -D is more complicated n n Need to specify an axis of rotation Simple cases: rotation about X, Y, Z axes 10
Rotation example: airplane 11
3 -D Rotation n What does the 3 -D rotation matrix look like for a rotation about the Z-axis? n n Build it coordinate-by-coordinate 2 -D rotation from last slide: 12
3 -D Rotation n What does the 3 -D rotation matrix look like for a rotation about the Y-axis? n Build it coordinate-by-coordinate 13
3 -D Rotation n What does the 3 -D rotation matrix look like for a rotation about the X-axis? n Build it coordinate-by-coordinate 14
Rotations about the axes 15
3 -D Rotation n General rotations in 3 -D require rotating about an arbitrary axis of rotation Deriving the rotation matrix for such a rotation directly is a good exercise in linear algebra Another approach: express general rotation as composition of canonical rotations n Rotations about X, Y, Z 16
Outline n n Ø n n n Scaling Rotations Composing Rotations Homogeneous Coordinates Translations Projections 17
Composing Canonical Rotations n Goal: rotate about arbitrary vector A by n n So, rotate about Y by until A lies in the YZ plane Then rotate about X by until A coincides with +Z n Then rotate about Z by n n Idea: we know how to rotate about X, Y, Z Then reverse the rotation about X (by - ) Then reverse the rotation about Y (by - ) Show video… 18
Composing Canonical Rotations n First: rotating about Y by until A lies in YZ n n How exactly do we calculate ? n n Draw it… Project A onto XZ plane Find angle to X: = -(90° - ) = - 90 ° Second: rotating about X by until A lies on Z How do we calculate ? 19
3 -D Rotation Matrices n n n So an arbitrary rotation about A composites several canonical rotations together We can express each rotation as a matrix Compositing transforms == multiplying matrices Thus we can express the final rotation as the product of canonical rotation matrices Thus we can express the final rotation with a single matrix! 20
Compositing Matrices n So we have the following matrices: n n n n p: The point to be rotated about A by Ry : Rotate about Y by Rx : Rotate about X by Rz : Rotate about Z by Rx -1: Undo rotation about X by Ry -1 : Undo rotation about Y by In what order should we multiply them? 21
Compositing Matrices n Remember: the transformations, in order, are written from right to left n n In other words, the first matrix to affect the vector goes next to the vector, the second next to the first, etc. This is the rule with column vectors (Open. GL); row vectors would be the opposite So in our case: p’ = Ry -1 Rx -1 Rz Rx Ry p n 22
Rotation Matrices n n Notice these two matrices: Rx : Rotate about X by Rx -1: Undo rotation about X by How can we calculate Rx -1? n n Obvious answer: calculate Rx (- ) Clever answer: exploit fact that rotation matrices are orthonormal What is an orthonormal matrix? What property are we talking about? 23
Rotation Matrices n Rotation matrix is orthogonal n n n Columns/rows linearly independent Columns/rows sum to 1 The inverse of an orthogonal matrix is just its transpose: 24
Rotation Matrix for Any Axis n Given gl. Rotated (angle, x, y, z) n n Let c = cos(angle) Let s = sin(angle) And normalize the vector so that ||(x, y, z|| == 1 The produced matrix to rotate something by angle degrees around the axis (x, y, z) is: 25
Outline n n n Ø n n Scaling Rotations Composing Rotations Homogeneous Coordinates Translations Projections 26
Translations n n For convenience we usually describe objects in relation to their own coordinate system We can translate or move points to a new position by adding offsets to their coordinates: n Note that this translates all points uniformly 27
Translation Matrices? n n n We can composite scale matrices just as we did rotation matrices But how to represent translation as a matrix? Answer: with homogeneous coordinates 28
Homogeneous Coordinates n Homogeneous coordinates: represent coordinates in 3 dimensions with a 4 -vector n n n [x, y, z, 0]T represents a point at infinity (use for vectors) [0, 0, 0]T is not allowed Note that typically w = 1 in object coordinates 29
Homogeneous Coordinates n n Homogeneous coordinates seem unintuitive, but they make graphics operations much easier Our transformation matrices are now 4 x 4: 30
Homogeneous Coordinates n n Homogeneous coordinates seem unintuitive, but they make graphics operations much easier Our transformation matrices are now 4 x 4: 31
Homogeneous Coordinates n n Homogeneous coordinates seem unintuitive, but they make graphics operations much easier Our transformation matrices are now 4 x 4: 32
Homogeneous Coordinates n n n Homogeneous coordinates seem unintuitive, but they make graphics operations much easier Our transformation matrices are now 4 x 4: Performing a scale: 33
More On Homogeneous Coords n n What effect does the following matrix have? Conceptually, the fourth coordinate w is a bit like a scale factor 34
More On Homogeneous Coords n Intuitively: n n The w coordinate of a homogeneous point is typically 1 Decreasing w makes the point “bigger”, meaning further from the origin Homogeneous points with w = 0 are thus “points at infinity”, meaning infinitely far away in some direction. (What direction? ) To help illustrate this, imagine subtracting two homogeneous points 35
Outline n n Ø n Scaling Rotations Composing Rotations Homogeneous Coordinates Translations Projections 36
Homogeneous Coordinates n How can we represent translation as a 4 x 4 matrix? A: Using the rightmost column: n Performing a translation: n 37
Translation Matrices n n Now that we can represent translation as a matrix, we can composite it with other transformations Ex: rotate 90° about X, then 10 units down Z: 38
Translation Matrices n n Now that we can represent translation as a matrix, we can composite it with other transformations Ex: rotate 90° about X, then 10 units down Z: 39
Translation Matrices n n Now that we can represent translation as a matrix, we can composite it with other transformations Ex: rotate 90° about X, then 10 units down Z: 40
Translation Matrices n n Now that we can represent translation as a matrix, we can composite it with other transformations Ex: rotate 90° about X, then 10 units down Z: 41
Transformation Commutativity n n Is matrix multiplication, in general, commutative? Does AB = BA? What about rotation, scaling, and translation matrices? n n n Does Rx. Ry = Ry. Rx? Does RAS = SRA ? Does RAT = TRA ? 42
Outline n n n Ø Scaling Rotations Composing Rotations Homogeneous Coordinates Translations Projections 43
Perspective Projection n n In the real world, objects exhibit perspective foreshortening: distant objects appear smaller The basic situation: 44
Perspective Projection n When we do 3 -D graphics, we think of the screen as a 2 -D window onto the 3 -D world n The view plane How tall should this bunny be? 45
Perspective Projection n The geometry of the situation is that of similar triangles. View from above: View plane X P (x, y, z) x’ = ? (0, 0, 0) Z d n What is x? 46
Perspective Projection n n Desired result for a point [x, y, z, 1]T projected onto the view plane: What could a matrix look like to do this? 47
A Perspective Projection Matrix n Answer: 48
A Perspective Projection Matrix n Example: n Or, in 3 -D coordinates: 49
A Perspective Projection Matrix n Open. GL’s glu. Perspective() command generates a slightly more complicated matrix: n Can you figure out what this matrix does? 50
Projection Matrices n n Now that we can express perspective foreshortening as a matrix, we can composite it onto our other matrices with the usual matrix multiplication End result: can create a single matrix encapsulating modeling, viewing, and projection transforms n Though you will recall that in practice Open. GL separates the modelview from projection matrix (why? ) 51
- Slides: 51