Transcript 5

Transformations
CS 445: Introduction to Computer Graphics
David Luebke
University of Virginia
Admin


Call roll
Forums signup – do it NOW please
Demo

Animusic
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
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
Scaling

Non-uniform scaling: different scalars per component:
X  2,
Y  0.5

How can we represent this in matrix form?
Scaling

Scaling operation:

Or, in matrix form:
 x' ax 
 y '  by 
   
 z '   cz 
 x '   a 0 0  x 
 y'  0 b 0  y 
  
 
 z '  0 0 c   z 
scaling matrix
2-D Rotation
(x’, y’)
(x, y)

x’ = x cos() - y sin()
y’ = x sin() + y cos()
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
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 
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 
3-D Rotation

What does the 3-D rotation matrix look like for a rotation
about the X-axis?
– Build it coordinate-by-coordinate
0
0  x
 x' 1
 y '  0 cos()  sin( )  y 
  
 
 z '  0 sin( ) cos()   z 
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
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 -)
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 ?
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!
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?

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

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?
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:
a b
d e

 h i
1
c
a b


f   d e
 h i
j 
T
c
a


f   b
 c
j 
d
e
f
h

i
j 
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
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
Homogeneous
Coordinates


Homogeneous coordinates seem unintuitive, but they make
graphics operations much easier
Our transformation matrices are now 4x4:
0
0
1
0 cos()  sin( )
Rx  
0 sin( ) cos()

0
0
0
0
0
0

1
Homogeneous
Coordinates


Homogeneous coordinates seem unintuitive, but they make
graphics operations much easier
Our transformation matrices are now 4x4:
 cos()
 0
Ry  
 sin( )

 0
0 sin( ) 0
1
0
0
0 cos() 0

0
0
1
Homogeneous
Coordinates


Homogeneous coordinates seem unintuitive, but they make
graphics operations much easier
Our transformation matrices are now 4x4:
cos()  sin( )
 sin( ) cos()
Rz  
 0
0

0
 0
0 0
0 0
1 0

0 1
Homogeneous
Coordinates


Homogeneous coordinates seem unintuitive, but they make
graphics operations much easier
Our transformation matrices are now 4x4:
 Sx 0
 0 Sy
S
0 0

0 0
0 0
0 0
Sz 0 

0 1
Homogeneous
Coordinates


How can we represent translation as a
4x4 matrix?
A: Using the rightmost column:
1
0
T
0

0
0 0 Tx 
1 0 Ty 
0 1 Tz 

0 0 1
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
 y '  0
 
 z '  0
  
 w' 0
0 0 0  1
0
0
1 0 0  0 cos(90)  sin( 90)
0 1 10 0 sin( 90) cos(90)

0 0 1  0
0
0
0  x 
0  y 
0  z 
 
1   w
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
 y '  0
 
 z '  0
  
 w' 0
0 0 0  1
1 0 0  0
0 1 10 0

0 0 1  0
0 0 0  x 
0  1 0  y 
1 0 0  z 
 
0 0 1   w
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
 y '  0
 
 z '  0
  
 w' 0
0 0 0  x 
0  1 0   y 
1 0 10  z 
 
0 0 1   w
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 
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 ?
More On
Homogeneous Coords

What effect does the following matrix have?
 x'  1
 y '  0
 
 z '  0
  
 w' 0

0  x 
0   y 
0  z 
 
0 0 10  w
0 0
1 0
0 1
Conceptually, the fourth coordinate w is a bit like a scale factor
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
Perspective Projection


In the real world, objects exhibit perspective foreshortening:
distant objects appear smaller
The basic situation:
Perspective Projection

When we do 3-D graphics, we think of the
screen as a 2-D window onto the 3-D world:
How tall should
this bunny be?
Perspective Projection

The geometry of the situation is that of similar triangles. View
from above:
View
plane
X
x’ = ?
(0,0,0)
Z
d

P (x, y, z)
What is x’?
Perspective Projection

Desired result for a point [x, y, z, 1]T projected onto the view
plane:
x' x
 ,
d z
dx
x
x' 

,
z
z d

y' y

d z
dy
y
y' 

, zd
z
z d
What could a matrix look like to do this?
A Perspective Projection
Matrix

Answer:
1
0
Mperspective  
0

0
0
1
0
0
0
1
0 1d
0

0
0

0
A Perspective Projection
Matrix


Example:
 x  1
 y  0


 z  0

 
 z d  0
0
1
0
0
0
1
0 1d
0  x 



0  y 
0  z 
 
0  1 
Or, in 3-D coordinates:
 x

,
z d

y
, d 
zd

A Perspective Projection
Matrix

OpenGL’s gluPerspective() command generates a slightly
more complicated matrix:
 f
 aspect

 0

 0

 0
where
0
0
f
0
 Ζ far  Z near 


Z Z 
far 
 near
1
0
0
0
0
 2  Z far  Z near

 Z Z
near
far

0
 fov y 

f  cot 
 2 
– Can you figure out what this matrix does?









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?)