51
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2007 Tamara Munzner http://www.ugrad.cs.ubc.ca/~cs314/Vjan2007 Transformations II Week 2, Wed Jan 17

Transformations II Week 2, Wed Jan 17tmm/courses/314/Vjan2007/slides/week2.day2.pdf · Transformations II Week 2, Wed Jan 17. 2 Readings for Jan 15-22 •FCG Chap 6 Transformation

  • Upload
    vokiet

  • View
    216

  • Download
    0

Embed Size (px)

Citation preview

University of British ColumbiaCPSC 314 Computer Graphics

Jan-Apr 2007

Tamara Munzner

http://www.ugrad.cs.ubc.ca/~cs314/Vjan2007

Transformations II

Week 2, Wed Jan 17

2

Readings for Jan 15-22

• FCG Chap 6 Transformation Matrices• except 6.1.6, 6.3.1

• FCG Sect 13.3 Scene Graphs

• RB Chap Viewing• Viewing and Modeling Transforms until Viewing

Transformations

• Examples of Composing Several Transformationsthrough Building an Articulated Robot Arm

• RB Appendix Homogeneous Coordinates andTransformation Matrices

• until Perspective Projection

• RB Chap Display Lists

3

Review: Event-Driven Programming

• main loop not under your control• vs. procedural

• control flow through event callbacks• redraw the window now

• key was pressed

• mouse moved

• callback functions called from main loopwhen events occur• mouse/keyboard state setting vs. redrawing

4

Review: 2D Rotation

θ

(x, y)

(x_, y_)x_ = x cos(θ) - y sin(θ)y_ = x sin(θ) + y cos(θ)

ν counterclockwise, RHS

( ) ( )( ) ( )

−=

y

x

y

x

θθ

θθ

cossin

sincos

'

'

5

Review: 2D Rotation From Trig Identities

x = r cos (φ)y = r sin (φ)x_ = r cos (φ + θ)y_ = r sin (φ + θ)

Trig Identity…x_ = r cos(φ) cos(θ) – r sin(φ) sin(θ)y_ = r sin(φ) cos(θ) + r cos(φ) sin(θ)

Substitute…x _ = x cos(θ) - y sin(θ)y _ = x sin(θ) + y cos(θ)

θ

(x, y)

(x_, y_)

φ

6

Review: 2D Rotation: Another Derivation

θθ

θθ

cossin'

sincos'

yxy

yxx

+=

−=

xx

xx'

x'= A − B

BB

AA

(x_,y_)

(x,y)

A = x cosθθθ

xx

7

Shear

• shear along x axis• push points to right in proportion to height

x

y

x

y

+

=

?

?

??

??

y

x

y

x

8

Shear

• shear along x axis• push points to right in proportion to height

x

y

x

y

+

=

0

0

10

1

y

xsh

y

x x

9

Reflection

x x

+

=

?

?

??

??

y

x

y

x

• reflect across x axis• mirror

10

Reflection

• reflect across x axis• mirror

+

−=

0

0

10

01

y

x

y

x

x x

11

2D Translation

=

+

+=

+

'

'

y

x

by

ax

b

a

y

x),( ba(x,y)

(x’,y’)

12

2D Translation

=

+

+=

+

'

'

y

x

by

ax

b

a

y

x),( ba(x,y)

(x’,y’)

( ) ( )( ) ( )

−=

y

x

y

x

θθ

θθ

cossin

sincos

'

'

=

y

x

b

a

y

x

0

0

'

'

scaling matrix rotation matrix

13

2D Translation

=

+

+=

+

'

'

y

x

by

ax

b

a

y

x),( ba(x,y)

(x’,y’)

( ) ( )( ) ( )

−=

y

x

y

x

θθ

θθ

cossin

sincos

'

'

=

y

x

b

a

y

x

0

0

'

'

scaling matrix rotation matrix

vector addition

matrix multiplicationmatrix multiplication

14

2D Translation

=

+

+=

+

'

'

y

x

by

ax

b

a

y

x),( ba(x,y)

(x’,y’)

( ) ( )( ) ( )

−=

y

x

y

x

θθ

θθ

cossin

sincos

'

'

=

y

x

b

a

y

x

0

0

'

'

scaling matrix rotation matrix

=

'

'

y

x

y

x

dc

ba

translation multiplication matrix??

vector addition

matrix multiplicationmatrix multiplication

15

Linear Transformations

• linear transformations are combinations of• shear• scale• rotate• reflect

• properties of linear transformations• satisifes T(sx+ty) = s T(x) + t T(y)• origin maps to origin• lines map to lines• parallel lines remain parallel• ratios are preserved• closed under composition

=

y

x

dc

ba

y

x

'

'dycxy

byaxx

+=

+=

'

'

16

Challenge

• matrix multiplication• for everything except translation

• how to do everything with multiplication?• then just do composition, no special cases

• homogeneous coordinates trick• represent 2D coordinates (x,y) with 3-vector (x,y,1)

17

Homogeneous Coordinates

• our 2D transformation matrices are now 3x3:

=

100

0)cos()sin(

0)sin()cos(

θθ

θθ

otationR

=

100

00

00

b

a

caleS

=

100

10

01

y

x

T

T

ranslationT

+

+

=

∗+∗

∗+∗

=

11

11

11

1100

10

01

by

ax

by

ax

y

x

b

a

• use rightmost column

18

Homogeneous Coordinates Geometrically

• point in 2D cartesian

xx

yy

y

x

19

Homogeneous Coordinates Geometrically

• point in 2D cartesian + weight w =point P in 3D homog. coords

• multiples of (x,y,w)

• form a line L in 3D

• all homogeneous points on Lrepresent same 2D cartesian point

• example: (2,2,1) = (4,4,2) =(1,1,0.5)

),,( wyx

homogeneoushomogeneous

),(w

y

w

xcartesiancartesian

/ w/ w

xx

yy

ww

w=w=11

w

wy

wx

1

y

x

20

Homogeneous Coordinates Geometrically

• homogenize to convert homog. 3Dpoint to cartesian 2D point:• divide by w to get (x/w, y/w, 1)

• projects line to point onto w=1 plane

• like normalizing, one dimension up

• when w=0, consider it as direction

• points at infinity

• these points cannot be homogenized

• lies on x-y plane

• (0,0,0) is undefined

),,( wyx

homogeneoushomogeneous

),(w

y

w

xcartesiancartesian

/ w/ w

xx

yy

ww

w=w=11

w

wy

wx

1

y

x

21

Affine Transformations

• affine transforms are combinations of• linear transformations• translations

• properties of affine transformations• origin does not necessarily map to origin• lines map to lines• parallel lines remain parallel• ratios are preserved• closed under composition

=

w

y

x

fed

cba

w

y

x

100

'

'

22

Homogeneous Coordinates Summary

• may seem unintuitive, but they makegraphics operations much easier

• allow all affine transformations to beexpressed through matrix multiplication• we’ll see even more later...

• use 3x3 matrices for 2D transformations• use 4x4 matrices for 3D transformations

23

3D Rotation About Z Axis

=

11000

0100

00cossin

00sincos

1

'

'

'

z

y

x

z

y

x

θθ

θθ

glRotatef(angle,x,y,z);glRotatef(angle,x,y,z);

θ

P

y

x

zz

yxy

yxx

=

+=

−=

'

cossin'

sincos'

θθ

θθ

θθ

'P

glRotatef(angle,0,0,1);glRotatef(angle,0,0,1);

ν general OpenGL command

ν rotate in z

24

3D Rotation in X, Y

−=

11000

0cossin0

0sincos0

0001

1

'

'

'

z

y

x

z

y

x

θθ

θθ

glRotatef(angle,1,0,0);glRotatef(angle,1,0,0);around x axis:

−=

11000

0cos0sin

0010

0sin0cos

1

'

'

'

z

y

x

z

y

x

θθ

θθ

glRotatef(angle,0,1,0);glRotatef(angle,0,1,0);around y axis:

25

3D Scaling

=

11000

000

000

000

1

'

'

'

z

y

x

c

b

a

z

y

x

glScalef(a,b,c);glScalef(a,b,c);

26

3D Translation

=

11000

100

010

001

1

'

'

'

z

y

x

c

b

a

z

y

x

>< cba ,,

glTranslatef(a,b,c);glTranslatef(a,b,c);

27

3D Shear

• shear in x

• shear in y

• shear in z

xshear(sy,sz) =

1 sy sz 00 1 0 00 0 1 00 0 0 1

yshear(sx,sz) =

1 0 0 0sx 1 sz 00 0 1 00 0 0 1

zshear(sx,sy) =

1 0 0 00 1 0 0sx sy 1 00 0 0 1

28

Summary: Transformations

=

11

1

1

1

1

'

'

'

z

y

x

c

b

a

z

y

x

translate(a,b,c)translate(a,b,c)

−=

11

cossin

sincos

1

1

'

'

'

z

y

x

z

y

x

θθ

θθ

),(Rotate θx

=

111

'

'

'

z

y

x

c

b

a

z

y

x

scale(a,b,c)scale(a,b,c)

1

cossin

1

sincos

θθ

θθ

),(Rotate θy

1

1

cossin

sincos

θθ

θθ

),(Rotate θz

29

Undoing Transformations: Inverses

T(x,y,z)−1

= T(−x,−y,−z)

T(x,y,z) T(−x,−y,−z) = I

=RT (z,θ)

= R(z,−θ)

R(z,θ) R(z,−θ) = I

S(sx,sy,sz)−1

= S( 1sx, 1sy, 1sz)€

R(z,θ)−1 (R is orthogonal)(R is orthogonal)

S(sx,sy,sz)S( 1sx, 1sy, 1sz) = I

30

Composing Transformations

31

Composing Transformations

• translation

T1= T(dx1,dy1) =

1 dx11 dy11

1

T2 = T(dx 2,dy 2) =

1 dx21 dy21

1

P' '= T2•P'= T2•[T1•P] = [T2•T1]•P,where

T2•T1=

1 dx1 + dx21 dy1 + dy21

1

so translations addso translations add

32

Composing Transformations

• scaling

• rotation

=•∗

1

112

21

21

sysy

dxsx

SSso scales multiplyso scales multiply

++

+−+

=•

1

1

)21cos()21sin(

)21sin()21cos(

12θθθθ

θθθθ

RRso rotations addso rotations add

33

Composing Transformations

Ta Tb = Tb Ta, but RaTa Tb = Tb Ta, but Ra Rb Rb !=!= Rb Rb Ra and TaRa and Ta Rb Rb !=!= Rb Rb TaTa

34

Composing Transformations

jFFhh

FFWW

suppose we wantsuppose we want

i

i€

j

35

Composing Transformations

jFFhh

FFWW

suppose we wantsuppose we want

FFWW

FFhh

Rotate(z,-90)Rotate(z,-90)

p'=R(z,−90)p

i

i€

j

36

Composing Transformations

jFFhh

FFWW

suppose we wantsuppose we want

FFWW

FFhh

Rotate(z,-90)Rotate(z,-90)

p'=R(z,−90)p

i

i€

j FFWW

FFhh

Translate(2,3,0)Translate(2,3,0)

p''= T(2,3,0)p'

37

Composing Transformations

jFFhh

FFWW

suppose we wantsuppose we want

FFWW

FFhh

Rotate(z,-90)Rotate(z,-90)

p'=R(z,−90)p

i

i€

j FFWW

FFhh

Translate(2,3,0)Translate(2,3,0)

p''= T(2,3,0)p'

p''= T(2,3,0)R(z,−90)p = TRp

38

p'= TRpComposing Transformations

• which direction to read?• right to left

• interpret operations wrt fixed coordinates

• moving object

• left to right• interpret operations wrt local coordinates

• changing coordinate system

39

p'= TRpComposing Transformations

• which direction to read?• right to left

• interpret operations wrt fixed coordinates• moving object

• left to right• interpret operations wrt local coordinates• changing coordinate system

OpenGL pipeline ordering!

40

p'= TRpComposing Transformations

• which direction to read?• right to left

• interpret operations wrt fixed coordinates• moving object

• left to right• interpret operations wrt local coordinates• changing coordinate system• OpenGL updates current matrix with postmultiply

• glTranslatef(2,3,0);• glRotatef(-90,0,0,1);• glVertexf(1,1,1);

• specify vector last, in final coordinate system• first matrix to affect it is specified second-to-last

OpenGL pipeline ordering!

41

(2,1)(1,1)

(1,1)

changing coordinate system

moving objecttranslate by (-1,0)

Interpreting Transformations

• same relative position between object andbasis vectors

intuitive?

OpenGL

42

Matrix Composition

• matrices are convenient, efficient way to representseries of transformations• general purpose representation• hardware matrix multiply• matrix multiplication is associative

• p_ = (T*(R*(S*p)))• p_ = (T*R*S)*p

• procedure• correctly order your matrices!• multiply matrices together• result is one matrix, multiply vertices by this matrix• all vertices easily transformed with one matrix multiply

43

Rotation About a Point: Moving Object

FFWW

translate p translate p to originto origin

θ

p = (x,y)

rotate about rotate about p by : p by :θ

rotate aboutrotate aboutoriginorigin

translate p translate p backback

T(x,y,z)R(z,θ)T(−x,−y,−z)

44

Rotation: Changing Coordinate Systems

• same example: rotation around arbitrarycenter

45

Rotation: Changing Coordinate Systems

• rotation around arbitrary center• step 1: translate coordinate system to rotation

center

46

Rotation: Changing Coordinate Systems

• rotation around arbitrary center• step 2: perform rotation

47

Rotation: Changing Coordinate Systems

• rotation around arbitrary center• step 3: back to original coordinate system

48

General Transform Composition

• transformation of geometry into coordinatesystem where operation becomes simpler• typically translate to origin

• perform operation

• transform geometry back to originalcoordinate system

49

Rotation About an Arbitrary Axis

• axis defined by two points

• translate point to the origin

• rotate to align axis with z-axis (or x or y)

• perform rotation

• undo aligning rotations

• undo translation

Arbitrary Rotation

• problem:• given two orthonormal coordinate systems XYZ and UVW

• find transformation from one to the other• answer:

• transformation matrix R whose columns are U,V,W:

R =

ux vx wx

uy vy wy

uz vz wz

Y Z

X

W

V

U

51

Arbitrary Rotation

• why?

• similarly R(Y) = V & R(Z) = W€

R(X) =

ux vx wx

uy vy wy

uz vz wz

100

= (ux,uy,uz)=U