transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb...

28
mjb – August 23, 2019 1 Computer Graphics Transformations transformations.pptx Mike Bailey [email protected] This work is licensed under a Creative Commons Attribution-NonCommercial- NoDerivatives 4.0 International License

Transcript of transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb...

Page 1: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

1

Computer Graphics

Transformations

transformations.pptx

Mike [email protected]

This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License

Page 2: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

2

Computer Graphics

Geometry vs. Topology

Where things are (e.g., coordinates)

Geometry:How things are connected

Topology:

1

2

3

4

1

2

3

4

1

2

3

4

Geometry = changedTopology = same (1-2-3-4-1)

Geometry = sameTopology = changed (1-2-4-3-1)

Original Object

Page 3: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

3

Computer Graphics

3D Coordinate Systems

Right-Handed

X

Y

Z

X

Y

Z

Left-Handed

Page 4: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

4

Computer Graphics

Transformations

X

YSuppose you have a point P and you want to move it over by 2 units in X.

How would you change P’s coordinates?P P’

( , ) ( 2., )x y x yP P P P P

This is known as a coordinate transformation

Page 5: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

5

Computer Graphics

Transform the geometry – leave the topology as is

General Form of 3D Linear Transformations

It’s called a “Linear Transformation” because all of the coordinates are raised to the 1st power, that is, there are no x2, x3, etc. terms.

x Ax By Cz D y Ex Fy Gz H z Ix Jy Kz L

Page 6: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

6

Computer Graphics

Translation Equations

xx x T

yy y T

zz z T

xT

yT

Page 7: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

7

Computer Graphics

Scaling About the Origin

x’ = xꞏSx

y’ = yꞏSy

z’ = zꞏSz

Page 8: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

8

Computer Graphics

2D Rotation About the Origin

cos sinx x y

sin cosy x y

Page 9: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

9

Computer Graphics

Linear Equations in Matrix Form

x Ax By Cz D y Ex Fy Gz H z Ix Jy Kz L

1 0 0 0 1 1

x A B C D xy E F G H yz I J K L z

x consuming column

x’ producing row

y consuming columnz consuming column

constant column

y’ producing row

z’ producing row

Page 10: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

10

Computer Graphics

Identity Matrix ( [ I ] )

[ I ] signifies that “Nothing has changed”

1 0 0 00 1 0 00 0 1 0

1 0 0 0 1 1

x xy yz z

x x y y z z

Page 11: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

11

Computer Graphics

Matrix Inverse

[M]•[M]-1 = [I]

[M]•[M]-1 = “Nothing has changed”

“Whatever [M] does, [M]-1 undoes”

Page 12: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

12

Computer Graphics

Translation Matrix

1 0 00 1 00 0 1

1 0 0 0 1 1

x

y

z

x T xy T yz T z

Quick! What is the inverse of this matrix?

Page 13: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

13

Computer Graphics

Scaling Matrix

0 0 00 0 00 0 0

1 0 0 0 1 1

x

y

z

x S xy S yz S z

Quick! What is the inverse of this matrix?

Page 14: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

14

Computer Graphics

3D Rotation Matrix About Z

cos sin 0 0sin cos 0 0

0 0 1 01 0 0 0 1 1

x xy yz z

Right-handed positive rotation rule

Right-handed coordinates

+90º rotation gives: y’ = x

X

Y

Z

Page 15: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

15

Computer Graphics

3D Rotation Matrix About Y

X

Y

Z cos 0 sin 00 1 0 0

sin 0 cos 01 0 0 0 1 1

x xy yz z

+90º rotation gives: x’ = z

Page 16: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

16

Computer Graphics

3D Rotation Matrix About X

X

Y

Z 1 0 0 00 cos sin 00 sin cos 0

1 0 0 0 1 1

x xy yz z

+90º rotation gives: z’ = y

Page 17: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

17

Computer Graphics

http://xkcd.com

How it Really Works :-)

Page 18: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

18

Computer Graphics

Compound Transformations

A

B

Write it

Say it

X

Y

, ,

1 1

A B A B

x xy y

T R Tz z

Q: Our rotation matrices only work around the origin. What if we want to rotate about an arbitrary point (A,B)?

A: Use more than one matrix.

123

Page 19: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

19

Computer Graphics

Matrix Multiplication is not Commutative

X

YRotate, then translate

Translate, then rotate

X

Y

X

Y

Page 20: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

20

Computer Graphics

Matrix Multiplication is Associative

, ,

1 1

A B A B

x xy y

T R Tz z

, ,

1 1

A B A B

x xy y

T R Tz z

One matrix –the Current Transformation Matrix, or CTM

Page 21: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

21

Computer Graphics

Can Multiply All Geometry by One Matrix !

Graphics hardware can do this very quickly!

A

B

X

Y

1 1

[ ]x xy yz z

M

Page 22: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

22

Computer Graphics

OpenGL Will Do the Transformation Compounding for You !

glTranslatef( A, B, C );

glRotatef( (GLfloat)Yrot, 0., 1., 0. );glRotatef( (GLfloat)Xrot, 1., 0., 0. );

glScalef( (GLfloat)Scale, (GLfloat)Scale, (GLfloat)Scale );

glCallList( BoxList );

glTr

ansl

atef

( A, B

, C );

glR

otat

ef(

(GLf

loat

)Yro

t, 0.

, 1.,

0. );

glR

otat

ef(

(GLf

loat

)Xro

t, 1.

, 0.,

0. );

glSc

alef

( (G

Lflo

at)S

cale

, (G

Lflo

at)S

cale

, (G

Lflo

at)S

cale

);

glC

allL

ist(

BoxL

ist)

;

Typically objects are modeled around their own local origin, so theglTranslate( -A, -B, -C )

step is unnecessary.

Page 23: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

23

Computer Graphics

OpenGL Will Do the Transformation Compounding for You !

for( ; ; ){

<< Turn mouse position into Xrot and Yrot >>

glLoadIdentity( );

glTranslatef( A, B, C );

glRotatef( (GLfloat)Yrot, 0., 1., 0. );glRotatef( (GLfloat)Xrot, 1., 0., 0. );

glScalef( (GLfloat)Scale, (GLfloat)Scale, (GLfloat)Scale );

glCallList( BoxList );}

Page 24: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

24

Computer Graphics

The Funky Rotation Matrix for an Arbitrary Axis and Angle

A

P

P’

ˆ ˆ( )P A A P

ˆ ˆ( )P P P P A A P

ˆ ˆ ˆ ˆ ˆ ˆ0Q A P A P A P A P A P P A P

Q

PP

Page 25: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

25

Computer Graphics

' ' 'P P P

'P P ' cos sinP P Q

ˆ ˆ ˆ ˆ ˆ' ( ) cos ( ) sinP A A P P A A P A P

The Funky Rotation Matrix for an Arbitrary Axis and Angle

Page 26: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

26

Computer Graphics

ˆ ˆ( )x x x y x z

y x y y y z

z x z y z z

A A A A A AA A P A A A A A A P

A A A A A A

The Funky Rotation Matrix for an Arbitrary Axis and Angle

0

ˆ 00

z y

z x

y x

A AA P A A P

A A

Page 27: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

27

Computer Graphics

The Funky Rotation Matrix for an Arbitrary Axis and Angle

ˆ ˆ ˆ ˆ ˆ' ( ) cos ( ) sinP A A P P A A P A P

0

' cos sin 00

x x x y x z x x x y x z z y

y x y y y z y x y y y z z x

z x z y z z z x z y z z y x

A A A A A A A A A A A A A AP A A A A A A I A A A A A A A A P

A A A A A A A A A A A A A A

1 0

cos 1 sin 01 0

x x x y x z x x x y x z z y

y x y y y z y x y y y z z x

z x z y z z z x z y z z y x

A A A A A A A A A A A A A AM A A A A A A A A A A A A A A

A A A A A A A A A A A A A A

Page 28: transformations - College of Engineeringweb.engr.oregonstate.edu/~mjb/cs491/Handouts/... · mjb –August 23, 2019 2 Computer Graphics Geometry vs. Topology Where things are (e.g.,

mjb – August 23, 2019

28

Computer Graphics

The Funky Rotation Matrix for an Arbitrary Axis and Angle

cos 1 cos sin cos sin

cos sin cos 1 cos sin

cos sin cos sin cos 1

x x x x x y x y z x z x z y

y x y x z y y y y y z y z x

z x z x y z y z y x z z z z

A A A A A A A A A A A A A A

M A A A A A A A A A A A A A A

A A A A A A A A A A A A A A

1 0

cos 1 sin 01 0

x x x y x z x x x y x z z y

y x y y y z y x y y y z z x

z x z y z z z x z y z z y x

A A A A A A A A A A A A A AM A A A A A A A A A A A A A A

A A A A A A A A A A A A A A

For this to be correct, A must be a unit vector