CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen...

48
CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenn

Transcript of CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen...

Page 1: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

CS559: Computer Graphics

Lecture 8: 3D TransformsLi Zhang

Spring 2008

Most Slides from Stephen Chenney

Page 2: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Today• Finish Color space• 3D Transforms and Coordinate system

• Reading: – Shirley ch 6

Page 3: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

RGB and HSV

Black (0,0,0) Red (1,0,0)

Green(0,1,0)

Blue (0,0,1)Magenta (0,1,1)

White(1,1,1)

Yellow (1,1,0)

Cyan (0,1,1)

Different ways to represent/parameterize color

Page 4: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Photoshop Color Picker

Page 5: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

L-A-B Color Space• L-A-B

– L: luminance/Brightness– A: position between magenta and green (negative

values indicate green while positive values indicate magenta)

– B: position between yellow and blue (negative values indicate blue and positive values indicate yellow)

http://en.wikipedia.org/wiki/Lab_color_space http://en.wikipedia.org/wiki/CIE_1931_color_space

Page 6: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Spatial resolution and color

R

G

B

original

Page 7: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Blurring the G component

R

G

B

original processed

Page 8: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Blurring the R component

original processed

R

G

B

Page 9: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Blurring the B component

original

R

G

Bprocessed

Page 10: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

L

a

b

A rotation of the color coordinates into directions that are more perceptually meaningful: L: luminance, a: magenta-green, b: blue-yellow

Lab Color Component

Page 11: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

L

a

b

original processed

Bluring L

Page 12: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

original

L

a

b

processed

Bluring a

Page 13: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

original

L

a

b

processed

Bluring b

Page 14: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Application to image compression• (compression is about hiding differences from the

true image where you can’t see them).

Page 15: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Where to now…• We are now done with images• We will spend several weeks on the mechanics of

3D graphics– 3D Transform– Coordinate systems and Viewing– Drawing lines and polygons– Lighting and shading

• We will finish the semester with modeling and some additional topics

Page 16: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

3D Graphics Pipeline

Rendering(Creating, shading images from geometry, lighting, materials)

Modeling(Creating 3D Geometry)

Page 17: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

3D Graphics Pipeline

Rendering(Creating, shading images from geometry, lighting, materials)

Modeling(Creating 3D Geometry)

Want to place it at correct location in the worldWant to view it from different anglesWant to scale it to make it bigger or smallerNeed transformation between coordinate systems -- Represent transformations using matrices and matrix-vector multiplications.

Page 18: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Recall: All 2D Linear Transformations• Linear transformations are combinations of …

– Scale,– Rotation,– Shear, and– Mirror

y

x

dc

ba

y

x

'

'

Page 19: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

2D Rotation• Rotate counter-clockwise about the origin by an

angle

y

x

y

x

cossin

sincos

x

y

x

y

Page 20: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Rotating About An Arbitrary Point

• What happens when you apply a rotation transformation to an object that is not at the origin?

x

y

?

Page 21: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Rotating About An Arbitrary Point• What happens when you apply a rotation

transformation to an object that is not at the origin?– It translates as well

x

y

x

Page 22: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

How Do We Fix it?• How do we rotate an about an arbitrary point?

– Hint: we know how to rotate about the origin of a coordinate system

Page 23: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Rotating About An Arbitrary Point

x

y

x

y

x

y

x

y

Page 24: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Scaling an Object not at the Origin

• What happens if you apply the scaling transformation to an object not at the origin?

• Based on the rotating about a point composition, what should you do to resize an object about its own center?

Page 25: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Back to Rotation About a Pt• Say R is the rotation matrix to apply, and p is the

point about which to rotate• Translation to Origin:• Rotation:• Translate back:• How to express all the transformation using

matrix multiplication?

pxx RpRxpxRxRx )(

pRpRxpxx

Page 26: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Homogeneous Coordinates• Use three numbers to represent a point

• Translation can now be done with matrix multiplication!

11001

y

x

baa

baa

y

x

yyyyx

xxyxx

1 usually w 0,any wfor

w

wy

wx

y

x

w

y

x

wy

wx

/

/

y

x

y

x

Page 27: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Homogeneous Coordinates• Use three numbers to represent a point

• Translation can now be done with matrix multiplication!

1154321 y

x

MMMMMy

x

1 usually w 0,any wfor

w

wy

wx

y

x

w

y

x

wy

wx

/

/

y

x

y

x

Page 28: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Basic Transformations

• Translation: Rotation:

• Scaling:

100

10

01

y

x

b

b

100

00

00

y

x

s

s

100

0cossin

0sincos

Page 29: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Composing rotations, scales

3 2 2 1

3 1 1

3 1

( ) ( )

x Rx x Sx

x R Sx RS x

x SRx

Rotation and scaling are not commutative.

Page 30: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Inverting Composite Transforms

• Say I want to invert a combination of 3 transforms• Option 1: Find composite matrix, invert• Option 2: Invert each transform and swap order

1 2 3

1 1 1 13 2 1

1 1 1 13 2 1 1 2 3( ( ) )

M M M M

M M M M

M M M M M M M M

Page 31: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Inverting Composite Transforms

• Say I want to invert a combination of 3 transforms• Option 1: Find composite matrix, invert• Option 2: Invert each transform and swap order• Obvious from properties of matrices

1 2 3

1 1 1 13 2 1

1 1 1 13 2 1 1 2 3( ( ) )

M M M M

M M M M

M M M M M M M M

Page 32: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Homogeneous Transform Advantages• Unified view of transformation as matrix

multiplication– Easier in hardware and software

• To compose transformations, simply multiply matrices– Order matters: BA vs AB

• Allows for transforming directional vectors• Allows for non-affine transformations:

– Perspective projections!

Page 33: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Directions vs. Points• We have been talking about transforming points

• Directions are also important in graphics– Viewing directions– Normal vectors– Ray directions

• Directions are represented by vectors, like points, and can be transformed, but not like points

(1,1)(-2,-1)

x

y

Page 34: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Transforming Directions• Say I define a direction as the difference of two

points: d=a–b– This represents the direction of the line between

two points

• Now I translate the points by the same amount: a’=a+t, b’=b+t

• d’=a’–b’=d• How should I transform d?

Page 35: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Homogeneous Directions• Translation does not affect directions!• Homogeneous coordinates give us a very clean way

of handling this• The direction (x,y) becomes the homogeneous

direction (x,y,0)

• The correct thing happens for rotation and scaling also– Scaling changes the length of the vector, but not the

direction– Normal vectors are slightly different – we’ll see more

later

00100

10

01

y

x

y

x

b

b

y

x

Page 36: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Transforming normal vectors

tangentnormal

tangent’

normal’

M

0T tn0''T tn

Mtt '

0))(( 1T MtMnT1T )(' Mnn nM T1)(

If M is a rotation,

MM T1)(

Page 37: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

3D Transformations• Homogeneous coordinates: (x,y,z)=(wx,wy,wz,w)

• Transformations are now represented as 4x4 matrices

1 usually w 0,any wfor

w

wz

wy

wx

z

y

x

w

z

y

x

wz

wy

wx

/

/

/

11000

100

010

001

1

z

y

x

t

t

t

z

y

x

z

y

x

Page 38: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

3D Affine Transform

110001

z

y

x

tihg

tfed

tcba

z

y

x

z

y

x

Page 39: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

3D Rotation• Rotation in 3D is about an axis in 3D space

passing through the origin• Using a matrix representation, any matrix with an

orthonormal top-left 3x3 sub-matrix is a rotation– Rows/columns are mutually orthogonal (0 dot

product)– Determinant is 1– Implies columns are also orthogonal, and that the

transpose is equal to the inverse

.1,1,1,0,0,0 then

1000

0|||

0

0|||

332211323121321

rrrrrrrrrrrrrrr

R

Page 40: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Specifying a rotation matrix

http://pratt.siggraph.org/education/materials/HyperGraph/modeling/mod_tran/3drota.htm

Page 41: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Specifying a rotation matrix• Euler angles: Specify how much to rotate about X,

then how much about Y, then how much about Z– Hard to think about, and hard to compose

Page 42: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

• Specify the axis and the angle (OpenGL method)– Hard to compose multiple rotations

A rotation by an angle around axis specified by the unit vector is given by

Alternative Representations

http://mathworld.wolfram.com/RodriguesRotationFormula.html

Page 43: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Non-Commutativity• Not Commutative (unlike in 2D)!!• Rotate by x, then y is not same as y then x• Order of applying rotations does matter• Follows from matrix multiplication not

commutative– R1 * R2 is not the same as R2 * R1

Page 44: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Other Rotation Issues• Rotation is about an axis at the origin

– For rotation about an arbitrary axis, use the same trick as in 2D: Translate the axis to the origin, rotate, and translate back again

Page 45: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Transformation Leftovers• Scale, shear etc extend naturally from 2D to 3D• Rotation and Translation are the rigid-body

transformations:– Do not change lengths or angles, so a body does not

deform when transformed

Page 46: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Coordinate Frames

• All of discussion in terms of operating on points• But can also change coordinate system • Example, motion means either point moves

backward, or coordinate system moves forward

(2,1)P ' (1,1)P (1,1)P

Page 47: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Coordinate Frames: Rotations

x

y

P

'P

cos sin

sin cosR

y

x

v

u

cossin

sincos

u

v

P P

'P

Page 48: CS559: Computer Graphics Lecture 8: 3D Transforms Li Zhang Spring 2008 Most Slides from Stephen Chenney.

Geometric Interpretation 3D Rotations

• Rows of matrix are 3 unit vectors of new coord frame• Can construct rotation matrix from 3 orthonormal vectors• Effectively, projections of point into new coord frame

u u u

uvw v v v u u u

w w w

x y z

R x y z u x X y Y z Z

x y z

?u u u p

v v v p

w w w p

x y z x

Rp x y z y

x y z z

u p

v p

w p