reference: “3d math primer for graphics and game development, 2 nd ed.” chapter 5 chapter 6.x ...

27
Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x http://www.teamten.com/lawrence/graphics/homogeneous/ MATRIX TRANSFORMATIONS

Upload: katrina-kelly

Post on 24-Dec-2015

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

Reference: • “3D Math Primer for Graphics and Game Development, 2 nd

ed.”•Chapter 5•Chapter 6.x

•http://www.teamten.com/lawrence/graphics/homogeneous/

MATRIX TRANSFORMATIONS

Page 2: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

Take this vector: Recall, if we think of this as a point, it means:

Start at the origin. Move 0.5 units in the opposite direction of the world x-axis. Move 3 units in the direction of the world y-axis Move 9 units in the direction of the world z-axis.

Said mathematically:

The unit vectors are the basis vectors of our standard coordinate system. Expressing this as a matrix equation: Said another way: “we’re transforming the vector [-0.5 3 9] by the identity matrix.

TRANSFORM MATRICES (4.2 + 5.1)

Page 3: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

Big deal, right?Yes…but…what if we express that point relative to a “non-standard” coordinate system?

In other words, use *non-standard* basis vectors.

Example:Let’s assume +y and +z are [0,1,0] and [0,0,1], respectively (as before)

But…let’s make +x be [-1,0,0]Now the matrix is:

TRANSFORM MATRICES, CONT.

100

010

001

MysteryMat

Page 4: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

Let’s watch what happens as we “transform” a few points with this matrix…

TRANSFORM MATRICES, CONT.

100

010

001

MysteryMat

2151 p

3412 p

0633 p

215*1'1 MysteryMatpp

341*2'2 MysteryMatpp

063*3'3 MysteryMatpp

p3P3’

p2 P2’

p1P1’

MysteryMat does a x-axis “Mirror”.

Page 5: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

Let’s try to apply this idea of basis vectors and local coordinate systems to a new matrix (4.2 in the book)

What does this matrix do? Method1: Try to plot a few points…OK, but not the best

solution (we might not pick points that show the transform in action. Also, it takes a lot of time!)

Method2: Look at the local coordinate system defined by M…(see the next slide)

TRANSFORM MATRICES, CONT.

100

021

012

M

Page 6: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

TRANSFORM MATRICES, CONT.

100

021

012

M

0120 b

0211 b

1002 b

worldX

worldYb0 = localX

b1 = localYThe localX and localY axes are rotated (~27 degrees) and scaled from their worldX and worldY counter-parts. localZ = worldZ, so this transformation leaves the z-values alone.

Imagine “deforming” worldX into b0, worldY into b1, worldZ into b2. This is the same “deformation” the matrix will apply to all points that we transform using this matrix.

Page 7: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

TRANSFORM MATRICES, CONT.

100

021

012

M

worldX

worldYb0 = localX

b1 = localY

worldX

worldY

p0 p1

p2

p3

p4

Let’s say these are defined in world space.

06.00,0125.0

06.05.0,005.0,000

43

210

pp

ppp

02.16.0',025.25.0'

07.14.0',05.01',000'

43

210

pp

ppp

When we multiply (transform) them by M (just as we did on the last slide), these points are transformed in the same way the world axes are transformed into the basis vectors of M.

Page 8: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

Now the problem of defining new transforms just boils down to defining the (transformed) basis vectors.

Let’s apply this to the problem of creating a matrix which rotates θ degrees around the z axis.

ROTATION TRANSFORM (5.1)

Page 9: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

We’re looking in the +z direction:

ROTATION TRANSFORM (5.1), CONT.

worldX

worldY

localX

localY

θ

θ

Now, let’s define the basis vectors…

Sin(θ)

Cos(θ)

0)sin()cos( localX

-Sin(θ)

Cos(θ)

0)cos()sin( localY

100localZ

Page 10: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

So, if we collect the 3 local vectors (basis vectors into a matrix), we have…

We can similarly come up with matrices for x-axis and y-axis rotations…

ROTATION TRANSFORMATION (5.1), CONT.

100

0)cos()sin(

0)sin()cos(

ZRot

)cos()sin(0

)sin()cos(0

001

XRot

)cos(0)sin(

010

)sin(0)cos(

YRot

Note: These matrices are for Left-Handed Systems. A Right-handed system would use the transpose.

Page 11: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

The scale transform matrix is even simpler:

SCALE TRANSFORM (5.2)

worldX

worldY

worldZ

Let’s say we want to scale:2x in the x-direction1x in the y-direction½ in the z-direction

localX

localY

localZ

5.000

010

002

S

sz

sy

sx

S

00

00

00

In general:sx in the x-directionsy in the y-directionsz in the z-direction

If sx = sy = sz, the operation is called a universal scale.

Page 12: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

[Do it on the board]

SHEAR (5.5)

Page 13: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

Let’s say we want to develop a matrix for translation (let’s say by amounts tx, ty, and tz). Seems simple, right? Wrong!

Try it

TRANSLATION (6.4)

Page 14: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

Try it another way:

Try to come up with values for a-i such that all points transformed by the matrix are moved by +4(x), +5(y), +6(z):

You can’t do it!

TRANSLATION (6.4)

ihg

fed

cba

T

T*321375 T*79213142 and

Page 15: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

Up to this point we've been looking at linear transformations (you can do these with a 3x3 (for 3d) matrix). These only transform points about the origin.

Translation is an example of an affi ne transformation (a linear transform followed by a translation). You need to look at the problem in a higher dimension… …enter homogeneous space!

TRANSLATION

Page 16: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

But, by using so-called homogeneous coordinates, we can.

To visualize this, we'll look at a (x-)shear matrix in 2d.

Suppose shearX is 0.5:

TRANSLATION, CONT.

Page 17: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

Represent 2d points in 3d space, with w as 1.0

TRANSLATIONS, CONT.

x

y

x

y

w

2D

3D

Page 18: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

Now transform by a 3d shear in the x direction by 2.0 units

TRANSLATIONS, CONT.

x

y

x

y

w

2D

3D

[1 0 00 1 02 0 1 ]

Page 19: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

So…a shear in 3d, produces a translation in 2d.We can do a translation in 3d by shearing in 4d.

We can't really visualize it – you just have to trust the math

TANSLATIONS, CONT.

1

0100

0010

0001

tztytx

T

Page 20: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

Note, if you put a w of 0, the translation doesn't aff ect the vector Useful for vectors.

W should be: 1 if this is a point. 0 if this is a vector (direction + magnitude).

We'll see another application of this in perspective projections. In this case we'll end up with w values after the

transformation that are not 0 nor 1. In this case we'll have to do a w-divide (homogeneous

divide)

TRANSLATION, CONT.

Page 21: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

Now, we can do translation, by using a 4x4 translation matrix like this:

Take our problem cases from before:

TRANSLATION, CONT.

1

0100

0010

0001

tztytx

T

1654

0100

0010

0001

*13211375

1654

0100

0010

0001

*1792113142

Because it’s a point

It’s still a point

Page 22: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

An extra bonus:

TRANSLATION, CONT.

0

0

0

4

*

1000

6100

5010

4001

0

0

0

4

This one’s a vector

It’s still a vectorNote how the vector is unchanged. Remember that you can re-draw a vector anywhere and it still has the same length and magnitude – here’s proof!

Page 23: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

OK, so now that we’re using 4x4 matrices for translation, what about all the other matrices?

You can use 4x4 matrix, with the “old” 3x3 (linear) matrix embedded in the upper-left. Or…think of it this affi ne transformation as a linear

transform (the 3x3 upper-left part) and a 0-vector translation afterwards.

Example: RotZ now becomes:

4X4 TRANSFORMATION MATRICES

1000

0100

00)cos()sin(

00)sin()cos(

ZRot

Page 24: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

Suppose you want to apply the following transforms to an object (in this order): Translate (10,0,0) Scales (2, 2, 2) Rotate 15 degrees around the z-axis.

TRANSFORM MATRIX CONCATENATION (5.6)

p0 p1

p2

p3

p4

P0’ P1’

P2’

P3’

P4’

P0’’ P1’’

P2’’

P3’’

P4’’

P0’’’

P1’’’

P2’’’

P3’’’

P4’’’

Page 25: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

Numerically:

After Multiplying by the translation (10,0,0) matrix:

After multiplying by the scale (2,2,2) matrix:

After multiplying by the rotation of 15 degrees around the z axis:

TRANSFORM MATRIX CONCATENATION (8.7)

105.32,1060

105.32,1002,1002

43

210

pp

ppp

105.38',10610'

105.312',10012',1008'

43

210

pp

ppp

10010

0100

0010

0001

T

10716'',101220''

10724'',10024'',10016''

43

210

pp

ppp

109.10643.13''',10767.16213.16'''

10973.1237.21''',10212.6182.23''',1014.4455.15'''

43

210

pp

ppp

1000

0200

0020

0002

S

1000

0100

00)15cos()15sin(

00)15sin()15cos(

R

Page 26: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

You can get the same net eff ect by concatenating these 3 transforms.

TRANSFORM MATRIX CONCATENATION, CONT.

1000

0100

00)15cos()15sin(

00)15sin()15cos(

*

1000

0200

0020

0002

*

10010

0100

0010

0001

** RSTC

1000

0100

00)15cos()15sin(

00)15sin()15cos(

*

10020

0200

0020

0002

1018.532.19

0200

0093.152.0

0052.093.1

10)15sin(*20)15cos(*20

0200

00)15cos(*2)15sin(*2

00)15sin(*2)15cos(*2

VERY IMPORTANT: In a right-handed system, multiply in the reverse order you want to apply the transform (In a left-handed system, you do it in the forward order).

You won’t get the same composite matrix if you don’t follow this rule.

Page 27: Reference: “3D Math Primer for Graphics and Game Development, 2 nd ed.” Chapter 5 Chapter 6.x  MATRIX

Let’s test it.

TRANSFORM MATRIX CONCATENATION, CONT.

105.32,1060

105.32,1002,1002

43

210

pp

ppp

1018.532.19

0200

0093.152.0

0052.093.1

C

Transform each of the points by C, and we get…

109.10643.13',10767.16213.16'

10973.1237.21',10212.6182.23',1014.4455.15'

43

210

pp

ppp

Which is the same points we got by transforming by the individual matrices!

Big Deal, right?No! To see why, we’ll need to analyze the costs…[enough time?]