You are on page 1of 51

Transformations

Aaron Bloomfield
CS 445: Introduction to Graphics
Fall 2006
(Slide set originally by David Luebke)
Graphics coordinate systems
 X is red
 Y is green
 Z is blue

2
Graphics coordinate systems
 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
 Scaling
 Rotations
 Composing Rotations
 Homogeneous Coordinates
 Translations
 Projections

4
Scaling
 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
 Non-uniform scaling: different scalars per
component:

X  2,
Y  0.5

 How can we represent this in matrix form?


6
Scaling
 Scaling operation:  x' ax 
 y '  by 
   
 z '   cz 
 Or, in matrix form:
 x '   a 0 0  x 
 y '  0 b 0  y 
    
 z '  0 0 c   z 
scaling matrix
7
Outline
 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
 This is easy to capture in matrix form:

 x' cos  sin   x 


 y '   sin  cos   y 
    

 3-D is more complicated


 Need to specify an axis of rotation
 Simple cases: rotation about X, Y, Z axes

10
Rotation example: airplane

11
3-D Rotation
 What does the 3-D rotation matrix look like for a
rotation about the Z-axis?
 Build it coordinate-by-coordinate

 x' cos()  sin( ) 0  x 


 y'   sin( ) cos() 0  y 
    
 z '   0 0 1  z 

 2-D rotation from last slide:  x'  cos  sin   x 


 y '  sin  cos   y 
    
12
3-D Rotation
 What does the 3-D rotation matrix look like for a
rotation about the Y-axis?
 Build it coordinate-by-coordinate

 x'  cos() 0 sin( )   x 


 y'   0 1 0   y 
    
 z '   sin( ) 0 cos()  z 

13
3-D Rotation
 What does the 3-D rotation matrix look like for a
rotation about the X-axis?
 Build it coordinate-by-coordinate

 x' 1 0 0  x
 y'  0 cos()  sin( )  y 
    
 z '  0 sin( ) cos()   z 

14
Rotations about the axes

15
3-D Rotation
 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
 Rotations about X, Y, Z

16
Outline
 Scaling
 Rotations
 Composing Rotations
 Homogeneous Coordinates
 Translations
 Projections

17
Composing Canonical Rotations
 Goal: rotate about arbitrary vector A by 
 Idea: we know how to rotate about X,Y,Z

 So, rotate about Y by  until A lies in the YZ plane


 Then rotate about X by  until A coincides with +Z

 Then rotate about Z by 

 Then reverse the rotation about X (by -)


 Then reverse the rotation about Y (by -)

 Show video…
18
Composing Canonical Rotations
 First: rotating about Y by  until A lies in YZ
 Draw it…
 How exactly do we calculate ?
 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
 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
 So we have the following matrices:
 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
 Remember: the transformations, in order, are
written from right to left
 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 (OpenGL); row
vectors would be the opposite
 So in our case:
p’ = Ry-1 Rx  -1 Rz Rx  Ry p

22
Rotation Matrices
 Notice these two matrices:
Rx  : Rotate about X by 
Rx  -1: Undo rotation about X by 
 How can we calculate Rx  -1?
 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
 Rotation matrix is orthogonal
 Columns/rows linearly independent
 Columns/rows sum to 1
 The inverse of an orthogonal matrix is just its
transpose:

1 T
a b c a b c a d h
d e  
f   d e  
f   b e i

 h i j   h i j   c f j 

24
Rotation Matrix for Any Axis
 Given glRotated (angle, x, y, z)
 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:
 xx(1  c)  c xy (1  c)  zs xz (1  c)  ys 0
 yx(1  c)  zs yy (1  c)  c yz (1  c)  xs 0 

 zx(1  c)  ys zy (1  c)  xs zz (1  c)  c 0
 
 0 0 0 1
25
Outline
 Scaling
 Rotations
 Composing Rotations
 Homogeneous Coordinates
 Translations
 Projections

26
Translations
 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:

 x'  x  tx 
 y '   y   ty 
     
 z '   z  tz 

 Note that this translates all points uniformly

27
Translation Matrices?
 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
 Homogeneous coordinates: represent coordinates
in 3 dimensions with a 4-vector

 x / w  x 
 y / w  y 
( x, y , z )    
 z / w  z 
   
 1   w

 [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
 Homogeneous coordinates seem unintuitive, but
they make graphics operations much easier
 Our transformation matrices are now 4x4:

1 0 0 0
0 cos()  sin( ) 0
Rx  
0 sin( ) cos() 0
 
0 0 0 1

30
Homogeneous Coordinates
 Homogeneous coordinates seem unintuitive, but
they make graphics operations much easier
 Our transformation matrices are now 4x4:

 cos() 0 sin( ) 0
 0 1 0 0
Ry  
 sin( ) 0 cos() 0
 
 0 0 0 1

31
Homogeneous Coordinates
 Homogeneous coordinates seem unintuitive, but
they make graphics operations much easier
 Our transformation matrices are now 4x4:

cos()  sin( ) 0 0
 sin( ) cos() 0 0
Rz  
 0 0 1 0
 
 0 0 0 1

32
Homogeneous Coordinates
 Homogeneous coordinates seem unintuitive, but
they make graphics operations much easier
 Our transformation matrices
 Sx 0 0 0
are now 4x4:  
0 Sy 0 0
S
 Performing a scale: 0 0 Sz 0
 
1 0 0 0  x  0 0 0 1
0 2 0 0   y 
  x 2y z 1
0 0 1 0  z 
  
0 0 0 1  1  33
More On Homogeneous Coords
 What effect does the following matrix have?

 x'  1 0 0 0  x 
 y '  0 1 0 0   y 
 
 z '  0 0 1 0  z 
    
 w' 0 0 0 10  w

 Conceptually, the fourth coordinate w is a bit like a


scale factor
34
More On Homogeneous Coords
 Intuitively:
 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
 Scaling
 Rotations
 Composing Rotations
 Homogeneous Coordinates
 Translations
 Projections

36
Homogeneous Coordinates
 How can we represent translation as a 4x4 matrix?
 A: Using the rightmost column:

1 0 0 Tx 
0 1 0 Ty 
T 
0 0 1 Tz 
 Performing a translation:  
0 0 0 1 
1 0 0 0   x
0 1  
0 0   y 
  x y z  10 1
0 0 1 10   z 
  
0 0 0 1  1 
37
Translation Matrices
 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:

 x'  1 0 0 0  1 0 0 0  x 
 y '  0 1 0 0  0 cos(90)  sin( 90) 0  y 
 
 z '  0 0 1 10 0 sin( 90) cos(90) 0  z 
     
 w' 0 0 0 1  0 0 0 1  w

38
Translation Matrices
 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:

 x'  1 0 0 0  1 0 0 0  x 
 y '  0 1 0 0  0 0 1 0  y 
 
 z '  0 0 1 10 0 1 0 0  z 
     
 w' 0 0 0 1  0 0 0 1   w

39
Translation Matrices
 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:

 x'  1 0 0 0  x 
 y '  0 0  1 0   y 
 
 z '  0 1 0 10  z 
    
 w' 0 0 0 1   w

40
Translation Matrices
 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:

 x'   x 
 y'   z 
  
 z '   y  10
   
 w'  w 

41
Transformation Commutativity
 Is matrix multiplication, in general, commutative?
Does AB = BA?
 What about rotation, scaling, and translation
matrices?
 Does RxRy = RyRx?
 Does RAS = SRA ?
 Does RAT = TRA ?

42
Outline
 Scaling
 Rotations
 Composing Rotations
 Homogeneous Coordinates
 Translations
 Projections

43
Perspective Projection
 In the real world, objects exhibit perspective
foreshortening: distant objects appear smaller
 The basic situation:

44
Perspective Projection
 When we do 3-D graphics, we think of the
screen as a 2-D window onto the 3-D world
 The view plane

How tall should


this bunny be?

45
Perspective Projection
 The geometry of the situation is that of similar triangles.
View from above:

View
X plane P (x, y, z)

(0,0,0) x’ = ?
Z

 What is x?
46
Perspective Projection
 Desired result for a point [x, y, z, 1]T projected onto
the view plane:

x' x y' y
 , 
d z d z

dx x dy y
x'   , y'   , zd
z z d z z d

 What could a matrix look like to do this?


47
A Perspective Projection Matrix
 Answer:

1 0 0 0
0 1 0 
0
Mperspective  
0 0 1 0
 
0 0 1d 0

48
A Perspective Projection Matrix
 Example:

 x  1 0 0 0  x 
 y  0 1 0   
0  y 
 
 z  0 0 1 0  z 
    
 z d  0 0 1d 0  1 
 Or, in 3-D coordinates:

 x y 
 , , d 
z d zd 
49
A Perspective Projection Matrix
 OpenGL’s gluPerspective() command
generates a slightly more complicated matrix:
 f 
 aspect 0 0 0 
 
 0 f 0 0 
  Ζ far  Z near   2  Z far  Z near 
 0 0    
Z Z   Z Z 
  near far   near far 
 0 0 1 0 
 fov y 
where f  cot  
 2 

 Can you figure out what this matrix does? 50


Projection Matrices
 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
 Though you will recall that in practice OpenGL
separates the modelview from projection matrix
(why?)

51

You might also like