1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer...

50
1/50 CS148: Introduction to Computer Graphics and Imaging Transforms

Transcript of 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer...

Page 1: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

1/50

CS148: Introduction to Computer Graphics and Imaging

Transforms

CS148: Introduction to Computer Graphics and Imaging

Transforms

Page 2: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

2/50

© Louie Psihoyos

http://www.youtube.com/watch?v=46mcpqOVN08

Page 3: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

3/50

Rotate

glRotatef(angle,ax,ay,az)

Page 4: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

4/50

Translate

glTranslatef(tx,ty,tz)

Page 5: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

5/50

Scale

Uniform

Nonuniform

glScalef(sx,sy,sz)

Page 6: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

6/50

Transformations

What? Just functions acting on points

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

P’ = T(P)

Why?

Viewing■ Window coordinates to framebuffer coordinates

■ Virtual camera: parallel/perspective projections

Modeling■ Create objects in convenient coordinates

■ Multiple instances of a prototype shape

■ Kinematics of linkages/skeletons - characters/robots

Page 7: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

7/50

Scale

Page 8: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

8/50

Scale

Page 9: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

9/50

Reflection

Page 10: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

10/50

Shear

Page 11: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

11/50

Linear Transform = Matrices

Page 12: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

12/50

Lines Map to Lines→ Linear Transform

Page 13: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

13/50

Non-Linear Transform!

Lines Map to Curves

Page 14: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

14/50

Parametric Form of a Line

Page 15: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

15/50

Linear Transform→ Lines Map to Lines

Start with the parametric form of a line

Transform all the points on the line

Thus, a line transforms into a linear

combination of transformed points,

which is a line

Page 16: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

16/50

Coordinate System

An origin o and two unit vectors x, y define a frame of reference

Page 17: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

17/50

Points are Defined wrt to the Frame

Page 18: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

18/50

Coordinate Frames

Can interpret the columns of the matrix

as the x and y axes that define the frame

Page 19: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

19/50

Rotation Matrix?

Page 20: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

20/50

Rotation Matrix

Setting the columns of the matrix

to the x and y axes

creates the transform from that frame

Page 21: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

21/50

Rotation Matrix

Page 22: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

22/50

Page 23: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

23/50

Translate??

Page 24: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

24/50

Solution: Homogenous Coordinates

Page 25: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

25/50

Vectors

Page 26: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

26/50

Composing Transformations

Page 27: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

27/50

Rotate, Then Translate

R(45) T(1,1) R(45)

Page 28: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

28/50

Translate, Then Rotate

T(1,1) R(45) T(1,1)

Page 29: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

29/50

Order Matters

R(45) T(1,1)T(1,1) R(45) ≠

Page 30: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

30/50

Rotate 45 about the Center (1,1)?

Page 31: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

31/50

Rotate 45 @ (1,1)

T(-1,-1)

T(1,1) R(45) T(-1,-1)

R(45)

T(1,1)

Page 32: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

32/50

Order of Transformations

The rightmost transform is applied first

P’ = T(1,1) (R(45)(P))

That is, the rotate is applied before the translate

OpenGL

glTranslatef( 1.0, 1.0, 0.0 );

glRotatef( 45.0, 0., 0., 1. );

The last gl call is applied first; we’ll see why later

Page 33: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

33/50

Advantages of the Matrices

Combine a sequence of transforms into

a single transform

P’ = A ( B ( C ( D ( p ) ) ) )

= ( A B C D ) P

= M P

Why? Matrix multiplication is associative

Advantages■ Very inefficient to recompute the matrix entries

■ Very inefficient to multiply matrix times point multiple times

■ Compute the matrix M once and then apply that matrix to many points

Page 34: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

34/50

Transforming Points

vs.

Transforming Coordinate Systems

Page 35: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

35/50

Transforming the Coordinate System

T(1,1)

R(45) T(1,1)

Transforms are defined wrt the Global/World C.S.

R(45)

T(1,1) R(45)

Page 36: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

36/50

Transforming the Coordinate System

T(1,1)

T(1,1) R(45)

R(45)

R(45) T(1,1)

Transforms are defined wrt the Local/Object C.S.

Page 37: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

37/50

Two Interpretations are Equivalent

T(1,1)

R(45) T(1,1)

R(45)

R(45) T(1,1)

World: Read right to left Object: Read left to right

Page 38: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

38/50

Two Interpretations are Equivalent

R(45)

T(1,1) R(45)

T(1,1)

T(1,1) R(45)

World: Read right to left Object: Read left to right

Page 39: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

39/50

Two Interpretations are Equivalent

R(45)

T(1,1) R(45)

T(1,1)

T(1,1) R(45)

World: Read right to left Object: Read left to right

Object way will be veryuseful when creatinghierarchies

Page 40: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

40/50

Hierarchical Transformations

Page 41: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

41/50

Skeleton

body torso head shoulder larm upperarm lowerarm hand rarm upperarm lowerarm hand hips lleg upperleg lowerleg foot rleg upperleg lowerleg foot

Page 42: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

42/50

Skeletons and Linkages

1. Skeleton represents the hierarchy of an assembly of parts

- The arm is made of an upper arm and a lower arm

- Different parts may have the same shape, i.e. are “geometric instances”

2. Parts connected by joints

- “The ankle joint is connected to the knee joint”

- Different types of joints move differently

e.g. a ball and socket joint, a linear actuator

Page 43: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

43/50

How Linkages Work

Lower levels of the hierarchy move when the upper levels moves

■ e.g. moving the left shoulder moves the left arm and the left hand

Motion in one sub-tree does not effect the position of a part in another sub-tree

■ e.g. moving the left hand does not effect the right hand

Page 44: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

44/50Pat Hanrahan, Fall 2011

Skeleton

Text

translate(0,4,0);torso();pushmatrix(); translate(0,3,0); shoulder(); pushmatrix(); rotatey(necky); rotatex(neckx); head(); popmatrix(); pushmatrix(); translate(1.5,0,0); rotatex(lshoulderx); upperarm(); pushmatrix(); translate(0,-2,0); rotatex(lelbowx); lowerarm(); ... popmatrix(); popmatrix();...

translate(0,4,0);torso();pushmatrix(); translate(0,3,0); shoulder(); pushmatrix(); rotatey(necky); rotatex(neckx); head(); popmatrix(); pushmatrix(); translate(1.5,0,0); rotatex(lshoulderx); upperarm(); pushmatrix(); translate(0,-2,0); rotatex(lelbowx); lowerarm(); ... popmatrix(); popmatrix();...

Page 45: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

45/50

OpenGL

Page 46: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

46/50

Current Transformation Matrix

OpenGL maintains a current transformation matrix (CTM).

All geometry is transformed by the CTM.

Transformation commands are concatenated onto the CTM on the right.

CTM *= T

This causes T to be applied before the old CTM

Page 47: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

47/50

OpenGL Matrix Functions

glLoadIdentity( )

■ Set the transform matrix to the identity matrix

glLoadMatrix( matrix M )

■ Replace the transform matrix with M

glMultMatrix( matrix M )

■ Multiplies transform matrix by CTM * M

■ glRotate, glTranslate, glScale etc. are just wrappers for glMultMatrix

Page 48: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

48/50

Graphics Coordinate Frames

Object

■ Raw values as provided by glVertex (ex. teacup centered at origin)

World

■ Object at final location in environment (ex. teacup recentered to be on top of a table)

Screen

■ Object at final screen position

Page 49: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

49/50

OpenGL Matrix Functions

glMatrixMode( mode )

■ Sets which transformation matrix to modify

■ GL_MODELVIEW: object to world transform

■ GL_PROJECTION: world to screen transform

■ CTM = GL_PROJECTION * GL_MODELVIEW

Why? May need to transform by one of these matrices for certain calculations like lighting. More on this latter.

Page 50: 1/50 CS148: Introduction to Computer Graphics and Imaging Transforms CS148: Introduction to Computer Graphics and Imaging Transforms.

50/50

OpenGL Matrix Stack

Save and restore transformations

glPushMatrix ( )

■ Pushes the current matrix onto the top of the matrix stack

glPopMatrix ( )

■ Pops the matrix off the top of the matrix stack and loads it as the current matrix