transformations aaron bloomfield cs 445: introduction to graphics fall 2006 (slide set originally by...

51
Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

Upload: stewart-hawkins

Post on 26-Dec-2015

224 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

Transformations

Aaron BloomfieldCS 445: Introduction to Graphics

Fall 2006(Slide set originally by David Luebke)

Page 2: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

2

Graphics coordinate systems X is red Y is green Z is blue

Page 3: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

3

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

Page 4: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

4

Outline

Scaling Rotations Composing Rotations Homogeneous Coordinates Translations Projections

Page 5: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

5

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

Page 6: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

6

Scaling

Non-uniform scaling: different scalars per component:

How can we represent this in matrix form?

X 2,Y 0.5

Page 7: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

7

Scaling

Scaling operation:

Or, in matrix form:

cz

by

ax

z

y

x

'

'

'

z

y

x

c

b

a

z

y

x

00

00

00

'

'

'

scaling matrix

Page 8: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

8

Outline

Scaling Rotations Composing Rotations Homogeneous Coordinates Translations Projections

Page 9: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

9

2-D Rotation

(x, y)

(x’, y’)

x’ = x cos() - y sin()y’ = x sin() + y cos()

Page 10: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

10

2-D Rotation

This is easy to capture in matrix form:

3-D is more complicated Need to specify an axis of rotation Simple cases: rotation about X, Y, Z axes

y

x

y

x

cossin

sincos

'

'

Page 11: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

11

Rotation example: airplane

Page 12: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

12

3-D Rotation

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

2-D rotation from last slide:

z

y

x

z

y

x

100

0)cos()sin(

0)sin()cos(

'

'

'

y

x

y

x

cossin

sincos

'

'

Page 13: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

13

3-D Rotation

What does the 3-D rotation matrix look like for a rotation about the Y-axis? Build it coordinate-by-coordinate

z

y

x

z

y

x

)cos(0)sin(

010

)sin(0)cos(

'

'

'

Page 14: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

14

3-D Rotation

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

z

y

x

z

y

x

)cos()sin(0

)sin()cos(0

001

'

'

'

Page 15: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

15

Rotations about the axes

Page 16: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

16

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

Page 17: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

17

Outline

Scaling Rotations Composing Rotations Homogeneous Coordinates Translations Projections

Page 18: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

18

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…

Page 19: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

19

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 ?

Page 20: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

20

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!

Page 21: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

21

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?

Page 22: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

22

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

Page 23: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

23

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?

Page 24: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

24

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:

jfc

ieb

hda

jih

fed

cba

jih

fed

cbaT1

Page 25: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

25

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:

(1 ) (1 ) (1 ) 0

(1 ) (1 ) (1 ) 0

(1 ) (1 ) (1 ) 0

0 0 0 1

xx c c xy c zs xz c ys

yx c zs yy c c yz c xs

zx c ys zy c xs zz c c

Page 26: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

26

Outline

Scaling Rotations Composing Rotations Homogeneous Coordinates Translations Projections

Page 27: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

27

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:

Note that this translates all points uniformly

z

y

x

t

t

t

z

y

x

z

y

x

'

'

'

Page 28: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

28

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

Page 29: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

29

Homogeneous Coordinates

Homogeneous coordinates: represent coordinates in 3 dimensions with a 4-vector

[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

w

z

y

x

wz

wy

wx

zyx

1

/

/

/

),,(

Page 30: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

30

Homogeneous Coordinates

Homogeneous coordinates seem unintuitive, but they make graphics operations much easier

Our transformation matrices are now 4x4:

1000

0)cos()sin(0

0)sin()cos(0

0001

xR

Page 31: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

31

Homogeneous Coordinates

Homogeneous coordinates seem unintuitive, but they make graphics operations much easier

Our transformation matrices are now 4x4:

1000

0)cos(0)sin(

0010

0)sin(0)cos(

yR

Page 32: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

32

Homogeneous Coordinates

Homogeneous coordinates seem unintuitive, but they make graphics operations much easier

Our transformation matrices are now 4x4:

1000

0100

00)cos()sin(

00)sin()cos(

zR

Page 33: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

33

Homogeneous Coordinates

Homogeneous coordinates seem unintuitive, but they make graphics operations much easier

Our transformation matrices are now 4x4:

Performing a scale:

1000

000

000

000

z

y

x

S

S

S

S

1 0 0 0

0 2 0 02 1

0 0 1 0

0 0 0 1 1

x

yx y z

z

Page 34: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

34

More On Homogeneous Coords

What effect does the following matrix have?

Conceptually, the fourth coordinate w is a bit like a scale factor

w

z

y

x

w

z

y

x

10000

0100

0010

0001

'

'

'

'

Page 35: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

35

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

Page 36: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

36

Outline

Scaling Rotations Composing Rotations Homogeneous Coordinates Translations Projections

Page 37: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

37

Homogeneous Coordinates

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

Performing a translation:

1 0 0

0 1 0

0 0 1

0 0 0 1

x

y

z

T

T

T

T

1 0 0 0

0 1 0 010 1

0 0 1 10

0 0 0 1 1

x

yx y z

z

Page 38: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

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:

w

z

y

x

w

z

y

x

1000

0)90cos()90sin(0

0)90sin()90cos(0

0001

1000

10100

0010

0001

'

'

'

'

Page 39: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

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:

w

z

y

x

w

z

y

x

1000

0010

0100

0001

1000

10100

0010

0001

'

'

'

'

Page 40: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

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:

w

z

y

x

w

z

y

x

1000

10010

0100

0001

'

'

'

'

Page 41: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

41

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:

w

y

z

x

w

z

y

x

10

'

'

'

'

Page 42: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

42

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 ?

Page 43: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

43

Outline

Scaling Rotations Composing Rotations Homogeneous Coordinates Translations Projections

Page 44: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

44

Perspective Projection

In the real world, objects exhibit perspective foreshortening: distant objects appear smaller

The basic situation:

Page 45: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

45

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 shouldthis bunny be?

Page 46: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

46

Perspective Projection The geometry of the situation is that of similar triangles.

View from above:

What is x?

P (x, y, z)X

Z

Viewplane

d

(0,0,0) x’ = ?

Page 47: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

47

Perspective Projection

Desired result for a point [x, y, z, 1]T projected onto the view plane:

What could a matrix look like to do this?

dzdz

y

z

ydy

dz

x

z

xdx

z

y

d

y

z

x

d

x

,','

',

'

Page 48: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

48

A Perspective Projection Matrix

Answer:

0100

0100

0010

0001

d

M eperspectiv

Page 49: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

49

A Perspective Projection Matrix

Example:

Or, in 3-D coordinates:

10100

0100

0010

0001

z

y

x

ddz

z

y

x

d

dz

y

dz

x,,

Page 50: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

50

A Perspective Projection Matrix

OpenGL’s gluPerspective() command generates a slightly more complicated matrix:

Can you figure out what this matrix does?

2cotwhere

0100

200

000

000

y

farnear

nearfar

farnear

nearfar

fovf

ZZ

ZZ

ZZ

ZΖf

aspect

f

Page 51: Transformations Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

51

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?)