lecture 9: transformationsteaching.csse.uwa.edu.au/units/cits3003/lectures/...Β Β· β€’a 2d example:...

22
CITS3003 Graphics & Animation Lecture 9: Transformations E. Angel and D. Shreiner: Interactive Computer Graphics 6E Β© Addison-Wesley 2012

Upload: others

Post on 17-Apr-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 9: Transformationsteaching.csse.uwa.edu.au/units/CITS3003/lectures/...Β Β· β€’A 2D example: Objective: want to rotate a square 45 degrees about its own centre, 𝐩. This is

CITS3003 Graphics & Animation

Lecture 9: Transformations

E. Angel and D. Shreiner: Interactive Computer Graphics 6E Β© Addison-Wesley 2012

Page 2: Lecture 9: Transformationsteaching.csse.uwa.edu.au/units/CITS3003/lectures/...Β Β· β€’A 2D example: Objective: want to rotate a square 45 degrees about its own centre, 𝐩. This is

Objectives

β€’ Introduce standard transformations– Rotation

– Translation

– Scaling

– Shear

β€’ Derive homogeneous coordinate transformation matrices

β€’ Learn to build arbitrary transformation matrices from simple transformations

2

Page 3: Lecture 9: Transformationsteaching.csse.uwa.edu.au/units/CITS3003/lectures/...Β Β· β€’A 2D example: Objective: want to rotate a square 45 degrees about its own centre, 𝐩. This is

Rotation (2D)

β€’ Consider a rotation about the origin by q degrees

– radius stays the same, angle increases by q

π‘₯β€² = π‘₯ cos πœƒ – 𝑦 sin πœƒπ‘¦β€² = π‘₯ sin πœƒ + 𝑦 cos πœƒ

π‘₯ = π‘Ÿ cosπœ™π‘¦ = π‘Ÿ sinπœ™

π‘₯β€² = π‘Ÿ cos(πœ™ + πœƒ)𝑦′ = π‘Ÿ sin(πœ™ + πœƒ)

(Old point p before rotation)

(New point 𝐩′ after rotation)

3

Page 4: Lecture 9: Transformationsteaching.csse.uwa.edu.au/units/CITS3003/lectures/...Β Β· β€’A 2D example: Objective: want to rotate a square 45 degrees about its own centre, 𝐩. This is

Rotation about the z axis

β€’ Rotation in two dimensions is equivalent to rotation about the 𝑧 axis in three dimensions.

β€’ Rotation about the 𝑧 axis in 3D leaves the 𝑧 components of all the points unchanged:

𝑅𝑧 πœƒ =

cos πœƒ βˆ’sin πœƒ 0 0sin πœƒ cos πœƒ 0 000

00

10

01

In homogeneous coordinates

𝐩′ = 𝐑𝒁(πœƒ)𝐩

4

Page 5: Lecture 9: Transformationsteaching.csse.uwa.edu.au/units/CITS3003/lectures/...Β Β· β€’A 2D example: Objective: want to rotate a square 45 degrees about its own centre, 𝐩. This is

Rotation about the z axis (cont.)β€’ Consider the example

π‘₯ = π‘Ÿ cosπœ™π‘¦ = π‘Ÿ sinπœ™

𝑧 = 0we have

π‘₯′𝑦′

𝑧′1

= 𝑅𝑧(πœƒ)

π‘Ÿ cosπœ™π‘Ÿ sin πœ™

01

=

cos πœƒ βˆ’sin πœƒ 0 0sin πœƒ cos πœƒ 0 000

00

10

01

π‘Ÿ cosπœ™π‘Ÿ sinπœ™

01

=

π‘Ÿ cos πœƒ cosπœ™ βˆ’ π‘Ÿ sin πœƒ sin πœ™π‘Ÿ sin πœƒ cosπœ™ + π‘Ÿ cos πœƒ sinπœ™

01

5

Page 6: Lecture 9: Transformationsteaching.csse.uwa.edu.au/units/CITS3003/lectures/...Β Β· β€’A 2D example: Objective: want to rotate a square 45 degrees about its own centre, 𝐩. This is

Rotation about the z axis (cont.)β€’ Applying the rule:

cos(πœƒ + πœ™) = cos πœƒ cosπœ™ βˆ’ sin πœƒ sinπœ™sin πœƒ + πœ™ = sin πœƒ cosπœ™ + cos πœƒ sinπœ™

we get

π‘₯′𝑦′

𝑧′1

= 𝑅𝑧(πœƒ)

π‘Ÿ cosπœ™π‘Ÿ sinπœ™

01

=

π‘Ÿ cos(πœƒ + πœ™)π‘Ÿ sin(πœƒ + πœ™)

01

β€’ Thus,In general, π‘₯β€² = π‘₯ cos πœƒ – 𝑦 sin πœƒ 𝑦′ = π‘₯ sin πœƒ + 𝑦 cos πœƒ 𝑧′ = 𝑧

π‘₯β€² = π‘Ÿ cos(πœƒ + πœ™) 𝑦′ = π‘Ÿ sin(πœƒ + πœ™) 𝑧′ = 0

6

Page 7: Lecture 9: Transformationsteaching.csse.uwa.edu.au/units/CITS3003/lectures/...Β Β· β€’A 2D example: Objective: want to rotate a square 45 degrees about its own centre, 𝐩. This is

Rotation about x and y axes

β€’ Same argument as for rotation about z axis– For rotation about x axis, x is unchanged

– For rotation about y axis, y is unchanged

R = Rx(q) =

R = Ry(q) =

qq

qq

1000

0 cos sin0

0 sin- cos0

0001

qq

qq

1000

0 cos0 sin-

0010

0 sin0 cos

Note the negative

sign here7

Page 8: Lecture 9: Transformationsteaching.csse.uwa.edu.au/units/CITS3003/lectures/...Β Β· β€’A 2D example: Objective: want to rotate a square 45 degrees about its own centre, 𝐩. This is

Scaling

𝐒 = 𝐒(𝑠π‘₯, 𝑠𝑦, 𝑠𝑧) =

1000

000

000

000

z

y

x

s

s

s

π‘₯β€² = 𝑠π‘₯π‘₯𝑦′ = 𝑠𝑦𝑦𝑧′ = 𝑠𝑧z

𝐩′ = 𝐒𝐩

β€’ Expand or contract along each axis (fixed point of origin)

8

Page 9: Lecture 9: Transformationsteaching.csse.uwa.edu.au/units/CITS3003/lectures/...Β Β· β€’A 2D example: Objective: want to rotate a square 45 degrees about its own centre, 𝐩. This is

Reflection

corresponds to negative scale factors

originalsx = -1

sy = 1

sx = -1

sy = -1

sx = 1

sy = -1

9

Page 10: Lecture 9: Transformationsteaching.csse.uwa.edu.au/units/CITS3003/lectures/...Β Β· β€’A 2D example: Objective: want to rotate a square 45 degrees about its own centre, 𝐩. This is

Inverses

β€’ Although we could compute inverse matrices by general formulas, we can use simple geometric observations

– Translation: π“βˆ’1(𝑑π‘₯ , 𝑑𝑦, 𝑑𝑧) = 𝐓(βˆ’π‘‘π‘₯ , βˆ’π‘‘π‘¦, βˆ’π‘‘π‘§)

– Rotation: π‘βˆ’1(πœƒ) = 𝑹(βˆ’πœƒ)

β€’ Holds for any rotation matrix

β€’ Note that since cos(βˆ’πœƒ) = cos(πœƒ) and sin(βˆ’πœƒ) =βˆ’ sin(πœƒ)

π‘βˆ’1(πœƒ) = 𝐑T(πœƒ)

– Scaling: π’βˆ’1(𝑠π‘₯, 𝑠𝑦, 𝑠𝑧) = 𝐒(1/𝑠π‘₯, 1/𝑠𝑦, 1/𝑠𝑧)

10

Page 11: Lecture 9: Transformationsteaching.csse.uwa.edu.au/units/CITS3003/lectures/...Β Β· β€’A 2D example: Objective: want to rotate a square 45 degrees about its own centre, 𝐩. This is

Concatenation

β€’ We can form arbitrary affine transformation matrices by multiplying together rotation, translation, and scaling matrices

β€’ Because the same transformation is applied to many vertices, the cost of forming a matrix 𝐌 = 𝐀𝐁𝐂𝐃 is not significant compared to the cost of computing 𝐌𝐩for many vertices 𝐩

β€’ The difficult part is how to form a desired transformation from the specifications in the application

11

Page 12: Lecture 9: Transformationsteaching.csse.uwa.edu.au/units/CITS3003/lectures/...Β Β· β€’A 2D example: Objective: want to rotate a square 45 degrees about its own centre, 𝐩. This is

Order of Transformations

β€’ Note that matrix on the right is the first applied

β€’ Mathematically, the following are equivalent

𝐩′ = 𝐀𝐁𝐂𝐩 = 𝐀(𝐁(𝐂𝐩))

β€’ Note many references use row vectors to represent points. For such references:

𝐩′T = 𝐩T𝐂T𝐁T𝐀T

12

Page 13: Lecture 9: Transformationsteaching.csse.uwa.edu.au/units/CITS3003/lectures/...Β Β· β€’A 2D example: Objective: want to rotate a square 45 degrees about its own centre, 𝐩. This is

General Rotation About the Origin

q

x

z

y

v

A rotation angle of q about an arbitrary axis

can be decomposed into the concatenation

of rotations about the π‘₯, 𝑦, and 𝑧 axes

𝑹(πœƒ) = 𝑹𝑧(πœƒπ‘§) 𝑹𝑦(πœƒπ‘¦) 𝑹π‘₯(πœƒπ‘₯)

πœƒπ‘₯ , πœƒπ‘¦, and πœƒπ‘§ are called the Euler angles

Note that rotations do not commute

We can use rotations in another order but

with different angles

13

Page 14: Lecture 9: Transformationsteaching.csse.uwa.edu.au/units/CITS3003/lectures/...Β Β· β€’A 2D example: Objective: want to rotate a square 45 degrees about its own centre, 𝐩. This is

Rotation About a Fixed Point other than the Origin

1. Move the origin to the fixed point

2. Rotate

3. Move the origin back to fixed point back

=> M = T(pf) R(q) T(-pf)

After Step 1 After Step 2 After Step 3

14

Page 15: Lecture 9: Transformationsteaching.csse.uwa.edu.au/units/CITS3003/lectures/...Β Β· β€’A 2D example: Objective: want to rotate a square 45 degrees about its own centre, 𝐩. This is

Rotation About a Fixed Point other than the Origin (cont.)

β€’ A 2D example:Objective: want to rotate a square 45 degrees about its own centre, 𝐩.

This is the same as rotating about the 𝑧-axis (pointing out of the page) in 3D.

x0

y

Before rotation Output wanted after rotation

y

0 x

𝐩

15

Page 16: Lecture 9: Transformationsteaching.csse.uwa.edu.au/units/CITS3003/lectures/...Β Β· β€’A 2D example: Objective: want to rotate a square 45 degrees about its own centre, 𝐩. This is

Rotation About a Fixed Point other than the Origin (cont.)

β€’ Our aim is to construct a matrix 𝐌 so that when the four vertices of the square are pre-multiplied by we get the desired output.

β€’ Step 1: apply a translation so that the origin is at 𝐩.

x0

y

x0

y

𝐌 = 𝐓(βˆ’π©)

𝐩

16

Page 17: Lecture 9: Transformationsteaching.csse.uwa.edu.au/units/CITS3003/lectures/...Β Β· β€’A 2D example: Objective: want to rotate a square 45 degrees about its own centre, 𝐩. This is

Rotation About a Fixed Point other than the Origin (cont.)

β€’ Step 2: apply a 45 degree rotation about the 𝑧-axis at the origin.

x0

y

𝐌 = 𝐑z 𝜽 𝐓(βˆ’π©)

x0

y

17

Page 18: Lecture 9: Transformationsteaching.csse.uwa.edu.au/units/CITS3003/lectures/...Β Β· β€’A 2D example: Objective: want to rotate a square 45 degrees about its own centre, 𝐩. This is

Rotation About a Fixed Point other than the Origin (cont.)

β€’ Step 3: move the origin back to where it was before.

x0

y

𝐌 = 𝐓 𝐩 𝐑z 𝜽 𝐓(βˆ’π©)

y

0 x

18

Page 19: Lecture 9: Transformationsteaching.csse.uwa.edu.au/units/CITS3003/lectures/...Β Β· β€’A 2D example: Objective: want to rotate a square 45 degrees about its own centre, 𝐩. This is

Instancing

β€’ In modeling, we often start with a simple object centered at the origin, oriented with the axis, and at a standard size

β€’ We apply an instance transformation to its vertices to – Scale

– Orient

– Locate

19

Page 20: Lecture 9: Transformationsteaching.csse.uwa.edu.au/units/CITS3003/lectures/...Β Β· β€’A 2D example: Objective: want to rotate a square 45 degrees about its own centre, 𝐩. This is

Shear

β€’ It is helpful to add one more basic transformation, the shearing transformation, to the collection of transformation we have learnt

β€’ Shearing is equivalent to pulling faces in opposite directions

20

Page 21: Lecture 9: Transformationsteaching.csse.uwa.edu.au/units/CITS3003/lectures/...Β Β· β€’A 2D example: Objective: want to rotate a square 45 degrees about its own centre, 𝐩. This is

Shear Matrix

Consider a simple shear along the x axis

π‘₯β€² = π‘₯ + 𝑦 cot πœƒ 𝑦′ = 𝑦 𝑧′ = 𝑧

q

1000

0100

0010

00cot 1

=> H(q) =

21

Page 22: Lecture 9: Transformationsteaching.csse.uwa.edu.au/units/CITS3003/lectures/...Β Β· β€’A 2D example: Objective: want to rotate a square 45 degrees about its own centre, 𝐩. This is

Further Reading

β€œInteractive Computer Graphics – A Top-Down Approach with Shader-Based OpenGL” by Edward Angel and Dave Shreiner, 6th Ed, 2012

β€’ Sec 3.7 Affine Transformations (all subsections)

β€’ Sec 3.8 Translation, Rotation, and Scaling

β€’ Sec 3.9 Transformations in Homogeneous Coordinates

β€’ Sec 3.10 Concatenation of Transformations

22