119
omp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Embed Size (px)

DESCRIPTION

Comp 175C - Computer Graphics Dan Hebert Transformations A Transformation is a function that takes variable(s) in a domain and maps that into another domain y = f (x) f: x => y Ex: y = sin (x  f x y

Citation preview

Page 1: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Computer Graphics

Comp 175Chapter 4

Instructor: Dan Hebert

Page 2: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Outline 1

2D transformation– Translate– Rotate– Scale

Page 3: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Transformations

A Transformation is a function that takes variable(s) in a domain and maps that into another domain

y = f (x) f: x => y

Ex:

y = sin (xfx

y

Page 4: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Transformations in Graphics

Transformations make possible the projection of 3D objects onto 2D screen

The graphics transformation process is analogous to taking a photograph with a camera

Every transformation can be thought of as changing the representation of a vertex from one coordinate system to another

Page 5: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

2D Transformations

• A special case of 3D (z = 0)• Represented points (vertices) as: (x,y), or• Matrix form: or• Homogeneous coordinates

(x, y) =x y

(x, y) =x y 1

Page 6: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Translation

Translation a operation that repositions points along a

given straight-line path (the translation direction) from one coordinate location to another.

Page 7: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Translation

(x, y)

(x’, y’)

(dx, dy)

x

y

d yydxx

y

x

''

Page 8: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Translation

p' = Tp

1 0 dx

0

1 dy 0 0 1

T(dx, dy ) = Translation matrix

1 0 - dx

0 1

- dy 0 0 1

T-1 (dx, dy ) =

Page 9: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Applying to Objects

(dx, dy)

x

y

Page 10: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Translation

Remember- Translation direction- Translation distance- 3 degrees of freedom (DoF)- Translation is rigid-body

transformation (a transformation that does not change the shape of objects)

Page 11: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Scaling

Scaling a operation that alters the size of an object

about a fixed point

Page 12: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Scaling

(x, y)

(x’, y’)

x

y

Page 13: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Scaling

Scaling matrix

p' = Sp

sx 0 0 0 sy 0 0 0 1

S (sx, sy ) =

1/sx 0 0 0 1/sy 0 0 0 1

S-1 (sx, sy ) =

Page 14: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Scaling

Scale factors – if sx , sy > 1, the objects are stretched– if 0 < sx , sy < 1, the objects are shrunk– if sx , sy < 0, the objects are flipped (reflection)

Uniform/differential scaling– if sx = sy , the scaling is uniform– if sx sy , the scaling is differential (non-uniform)

Page 15: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Scaling

Remember- Scaling factors- Fixed point- Scaling is an affine non-rigid-body

transformation

Page 16: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Rotation

Rotation a operation that repositions points along a

given circular path (the rotation direction) from one coordinate location to another.

Rotation requires an and a pivot point

Page 17: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Rotation

x

y

(x, y)

(x’, y’)

Page 18: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Rotation

p' = Rp

cos -sin

0 sin cos 0 0 0 1

R() =

Rotation matrix

cos sin

0 -sin cos 0 0 0 1

R-1 () =

Page 19: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Rotation

Remember- Rotation direction- Rotation angle- Rotation center- Rotation is rigid-body transformation

Page 20: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

We Should Remember

Translation, Scaling, and Rotation all are affine transformation

Translation and Rotation are also rigid-body transformation

Scaling is non-rigid-body transformation

Page 21: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Outline 2

3D transformation– Translate– Rotate– Scale

Composition of transformations Coordinate transformations Applications - object hierarchy

Page 22: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Extend to 3D

x

y

x

y

z

Page 23: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Translation

Translation a operation that repositions points along a

given straight-line path (the translation direction) from one coordinate location to another.

Page 24: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Translation

(x, y)

(x’, y’)

(dx, dy)

x

y

(x’, y’, z’)

(dx, dy, dz)

(x, y, z)

x

y

z

Page 25: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

3D Translation Matrix

1000100010001

z

y

x

ddd

,, zyx dddTTranslation:

Page 26: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Scaling

Scaling a operation that alters the size of an object

about a fixed point

Page 27: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Scaling

(sx, sy, sz)

x

y

z

(x’, y’, z’)

(x, y, z)(x, y)

(x’, y’)

x

y

(sx, sy)

Page 28: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

3D Scaling Matrix

1000000000000

z

y

x

ss

s

,, zyx sssSScaling:

Page 29: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Rotation

Rotation a operation that repositions points along a

given circular path (the rotation direction) from one coordinate location to another.

Page 30: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Rotation

x

y

(x, y)

(x’, y’)

• To specify a 2D rotation, we need an rotation angle and a pivot point.How about for 3D rotation?

Page 31: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

3D Rotation

To specify a 3D rotation, we requires an rotation angle and a vector (rotation axis)

y

x

z

rotation axis

(x’, y’, z’)(x, y, z)

Page 32: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

3D Rotation About Z axis

2D rotation is just a 3D rotation about the z axis

x

z

y(x’, y’, z’) (x, y, z)

Page 33: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

3D Rotation About Z axis

1000010000cossin00sincos

)(

Rz

Rotation matrix )(Rz

Page 34: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

3D Rotation About X axis

(x, y, z)(x’, y’, z’)

x

y

z

Page 35: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

3D Rotation About X axis

10000cossin00sincos00001

)(Rx

Rotation matrix )(Rx

Page 36: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

3D Rotation About Y axis

(x, y, z)(x’, y’, z’)

x

y

z

Page 37: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

3D Rotation About Y axis

Rotation matrix )(Ry

)(Ry

10000cos0sin00100sin0cos

Page 38: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

3D Rotation About Origin

y

x

z

(x’, y’, z’)(x, y, z)

Page 39: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

3D Rotation About Origin

An arbitrary rotation about the origin can be composed of three successive rotations about three axes

R (

Rz(zRy(yRx(x• The order of the composed rotations is not unique Ex:

Rx (xRy (yRz (z

• The three angles x ,y ,z are often called Euler Angles.

or called Yaw (y), Pitch (x), and Roll (z).

Page 40: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Summary

1000100010001

z

y

x

ddd

,, zyx dddTTranslation:

1000000000000

z

y

x

ss

s

,, zyx sssSScaling:

• Three elementary 3D affine transformations

Page 41: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Summary

10000cossin00sincos00001

)(Rx

)(Ry

10000cos0sin00100sin0cos

1000010000cossin00sincos

)(

Rz

Rotation )(Rx

Rotation )(Ry

Rotation )(Rz

Page 42: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Composition of Transformations

Composition: the process of applying several transformations in succession to form one overall transformation.

Any composition of affine transformations is still affine.

When homogeneous coordinates are used, affine transformations are composed by simple matrix multiplication.

Ex: T = T4T3T2T1

Note: the matrices appear in reverse order to that in which the transformations are applied.

Page 43: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Composition of Transformations

Let’s examine an example:– 2D Rotation about an arbitrary point

x

P(x, y)

P’ (x’, y’)

V = (Vx, Vy)

y

Page 44: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Rotation About an Arbitrary Point

Since we have known rotation about origin, we can use that and construct successive transformations to compose the rotation about an arbitrary pivot point.

Three Steps:1. Translate point P through vector V = (-Vx, -Vy).2. Rotate about the origin through angle 3. Translate P back through vector V = (Vx, Vy).

Page 45: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Rotation About an Arbitrary Point

x

P(x, y)

P’ (x’, y’)

V = (Vx, Vy)

y1. Translate point P through vector -V.

2. Rotate about the origin through angle

3. Translate P back through vector V

Page 46: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Coordinate Transformations

We can think about, alternatively, a transformation is as a change of coordinate systems.

(x,y)

(x’,y’)

(dx, dy)(x, y)

(dx, dy)

Page 47: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Coordinate Transformations Transforming points To apply a sequence of transformations T1 ,T2 , T3 (in that

order) to a point, form matrix T = T3T2T1

Then, P is transformed to TP Transforming the coordinate system To apply a sequence of transformations T1, T2, T3 (in that

order) to the coordinate system, form matrix T = T1T2T3

Then, a point P expressed in the transformed system has coordinates MP in the original system.

Page 48: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Coordinate Transformations

Objects are usually defined in theirs own local coordinate system.

We wish to express these objects’ coordinates in a single, global coordinate.– Object Coordinates– World Coordinates– Screen Coordinates

Page 49: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Object Coordinates

Objects are usually defined in their own local coordinate system.

Page 50: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

World Coordinates

Represent these objects’ coordinates in a single, global coordinate.

Page 51: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Screen Coordinates

Finally, we want to project these objects onto the screen.

Page 52: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Coordinate Hierarchy

Object #1Object Coordinates

TransformationObject #1 ->

World

Object #2Object Coordinates

TransformationObject #2 ->

World

Object #3Object Coordinates

TransformationObject #3 ->

World

World Coordinates

TransformationWorld->Screen

Screen Coordinates

Page 53: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Demonstration

Page 54: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Demonstration

Page 55: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Outline 3

OpenGL transformation pipeline Modeling transformation

– Translate– Rotate– Scale

Composition of transformations– Matrix stack

Applications

Page 56: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Transformations

We can think of graphics transformation as:

The process that is analogous to taking a photograph with a camera.

Changing the representation of a vertex from one coordinate system to another.

Page 57: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Synthetic Camera Model

CameraModel

Page 58: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Transformations and Camera Analogy

Viewing transformation– Positioning and aiming camera in the world.

Modeling transformation– Positioning and moving the model.

Projection transformation– Adjusting the lens of the camera.

Viewport transformation– Enlarging or reducing the physical

photograph.

Page 59: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Transformations and Coordinate Systems

Viewing transformation– specifying camera (camera coordinates).

Modeling transformation– specifying geometry (world coordinates).

Projection transformation– projection (window coordinates).

Viewport transformation– mapping to screen (screen coordinates).

Page 60: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Transformations in OpenGL

Transformations are specified by matrix operations. Desired transformation can be obtained by a sequence of simple transformations that can be concatenated together.

Transformation matrix is usually represented by 4x4 matrix (homogeneous coordinates).

Provides matrix stacks for each type of supported matrix to store matrices.

Page 61: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Transformation Matrices

Model-viewing matrix Projection matrix Texture matrix

Page 62: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

OpenGL Transformation Pipeline

Page 63: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Programming Transformations In OpenGL, the transformation matrices

are part of the state, they must be defined prior to any vertices to which they are to apply.

In modeling, we often have objects specified in their own coordinate systems and must use transformations to bring the objects into the scene.

OpenGL provides matrix stacks for each type of supported matrix (model-view, projection, texture) to store matrices.

Page 64: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Steps in Programming

Prior to rendering, view, locate, and orient:– Eye/camera position– 3D geometry

Manage the matrices– Including matrix stack

Composite transformations

Page 65: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Current Transformation Matrix Current Transformation Matrix (CTM)

– The matrix that is applied to any vertex that is defined subsequent to its setting.

If change the CTM, we change the state of the system.

CTM is a 4 x 4 matrix that can be altered by a set of functions.

Page 66: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Current Transformation MatrixThe CTM can be set/reset/modify (by post-multiplication) by a matrix

Ex:C <= M // set to matrix M

C <= CT // post-multiply by T

C <= CS // post-multiply by S

C <= CR // post-multiply by R

Page 67: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Current Transformation Matrix Each transformation actually creates a

new matrix that multiplies the CTM; the result, which becomes the new CTM.

CTM contains the cumulative product of multiplying transformation matrices.

Ex: If C <= M; C <= CT; C <= CR; C <= CS

Then C = M T R S

Page 68: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Ways to Specify Transformations

In OpenGL, we usually have two styles of specifying transformations:– Specify matrices ( glLoadMatrix,

glMultMatrix )– Specify operations ( glRotate, glTranslate )

Page 69: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Specifying Matrix

Specify current matrix mode Modify current matrix Load current matrix Multiple current matrix

Page 70: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Specifying Matrix (1)

Specify current matrix mode

glMatrixMode (mode) Specified what transformation matrix is modified.

mode: GL_MODELVIEW GL_PROJECTION

GL_TEXTURE

Page 71: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Specifying Matrix (2)

Modify current matrix

glLoadMatrix{fd} ( Type *m )

Set the 16 values of current matrix to those specified by m.

Note: m is the 1D array of 16 elements arranged by the columns of the desired matrix

Page 72: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Specifying Matrix (3)

Modify current matrix

glLoadIdentity ( void )

Set the currently modifiable matrix to the 4x4 identity matrix.

Page 73: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Specifying Matrix (4)

Modify current matrix

glMultMatrix{fd} ( Type *m )

Multiple the matrix specified by the 16 values pointed by m by the current matrix, and stores the result as current matrix.

Note: m is the 1D array of 16 elements arranged by the columns of the desired matrix

Page 74: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Specifying Operations

Three OpenGL operation routines for modeling transformations:– Translation– Scale– Rotation

Page 75: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Recall

1000100010001

z

y

x

ddd

,, zyx dddTTranslation:

1000000000000

z

y

x

ss

s

,, zyx sssSScale:

• Three elementary 3D transformations

Page 76: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Recall

10000cossin00sincos00001

)(Rx

)(Ry

10000cos0sin00100sin0cos

1000010000cossin00sincos

)(

Rz

Rotation )(Rx

Rotation )(Ry

Rotation )(Rz

Page 77: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Problem

• Modify cube program to translate, scale, and rotate using glMultMatrix (changes commented out)

Page 78: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Specifying Operations (1)

Translation

glTranslate {fd} (TYPE x, TYPE y, TYPE z) Multiplies the current matrix by a matrix that

translates an object by the given x, y, z.

Page 79: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Specifying Operations (2)

Scale

glScale {fd} (TYPE x, TYPE y, TYPE z) Multiplies the current matrix by a matrix that

scales an object by the given x, y, z.

Page 80: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Specifying Operations (3)

Rotate

glRotate {fd} (TPE angle, TYPE x, TYPE y, TYPE z) Multiplies the current matrix by a matrix that rotates

an object in a counterclockwise direction about the ray from origin through the point by the given x, y, z. The angle parameter specifies the angle of rotation in degree.

Page 81: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Example

Let’s examine an example:– Rotation about an arbitrary point

Question:

Rotate a object for a 45.0-degree about the line through the origin and the point (1.0, 2.0, 3.0) with a fixed point of (4.0, 5.0, 6.0).

Page 82: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Rotation About an Arbitrary Point

1. Translate object through vector –V.T(-4.0, -5.0, -6.0)

2. Rotate about the origin through angle R(45.0)

3. Translate back through vector VT(4.0, 5.0, 6.0)

M = T(V ) R() T(-V )

Page 83: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

OpenGL Implementation

glMatrixMode (GL_MODEVIEW);glLoadIdentity ();glTranslatef (4.0, 5.0, 6.0);glRotatef (45.0, 1.0, 2.0, 3.0);glTranslatef (-4.0, -5.0, -6.0);

Page 84: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Order of Transformations

The transformation matrices appear in reverse order to that in which the transformations are applied.

In OpenGL, the transformation specified most recently is the one applied first.

Page 85: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Order of Transformations

In each step:C <= IC <= CT(4.0, 5.0, 6.0)C <= CR(45, 1.0, 2.0, 3.0)C < = CT(-4.0, -5.0, -6.0)

FinallyC = T(4.0, 5.0, 6.0) CR(45, 1.0, 2.0, 3.0) CT(-4.0, -5.0, -6.0)

Write it

Read it

Page 86: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Matrix Multiplication is Not Commutative

First rotate, then translate =>

First translate, then rotate =>

Page 87: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Composition of Transformations

We can compose an overall transformation by applying several transformations in succession.

Any composition of affine transformations is still affine.

When homogeneous coordinates are used, affine transformations are composed by simple matrix multiplication.

Page 88: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Problem

1. Draw the car body.

2. Translate to right front and draw wheel.

3. Return back to car body.

4. Translate to left front and draw wheel.

5. Return back to car body.

……..

Always remember where you are!

Page 89: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Matrix Stacks

OpenGL uses matrix stacks mechanism to manage transformation hierarchy.

OpenGL provides matrix stacks for each type of supported matrix to store matrices.– Model-view matrix stack– Projection matrix stack– Texture matrix stack

Page 90: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Matrix Stacks

Top

Bottom

PoppingPushing

• Current matrix is always the topmost matrix of the stack

• We manipulate the current matrix is that we actually manipulate the topmost matrix.

• We can control the current matrix by using push and pop operations.

Page 91: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Manipulating Matrix Stacks (1) Remember where you are

glPushMatrix ( void )

Pushes all matrices in the current stack down one level. The topmost matrix is copied, so its contents are duplicated in both the top and second-from-the top matrix.

Note: current stack is determined by glMatrixModel()

Page 92: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Manipulating Matrix Stacks (2) Go back to where you were

glPopMatrix ( void )

Pops the top matrix off the stack, destroying the contents of the popped matrix. What was the second-from-the top matrix becomes the top matrix.

Note: current stack is determined by glMatrixModel()

Page 93: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Manipulating Matrix Stacks (3)

The depth of matrix stacks are implementation-dependent.

The Modelview matrix stack is guaranteed to be at least 32 matrices deep.

The Projection matrix stack is guaranteed to be at least 2 matrices deep.

glGetIntegerv ( Glenum pname, Glint *parms )

Pname:GL_MAX_MODELVIEW_STACT_DEPTH

GL_MAX_PROJECTION_STACT_DEPTH

Page 94: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Demonstration (look at code)

Page 95: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Demonstration (look at code)

Page 96: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Demonstration (look at code)

Page 97: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Let’s Examine Some Examples

Question 1:Draw a simple solar system with a planet and a sun.

• Sun rotates around its own axis (Y axis)• The planet rotates around its own axis (Y axis)• The planet also rotates on its orbit around the sun.

Page 98: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Example-1

Sun:Locates at the origin and rotates around its own axis (Y axis) M = Ry ()

Planet:1. Rotates around its own axis. M1 = Ry () 2. Translate to its orbit. M2 = T (x, y, z) 3. Rotates around Sun. M3 = Ry () M = M3 M2 M1

Page 99: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

OpenGL Implementationvoid main (int argc, char** argv)

{

glutInit ( &argc, argv );

glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);

glutInitWindowSize (500, 500);

glutCreateWindow ("Composite Modeling Transformation");

init ();

glutDisplayFunc (display);

glutReshapeFunc (reshape);

glutKeyboardFunc (keyboard);

glutMainLoop ();

}

Page 100: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

OpenGL Implementationvoid init (void)

{

glViewport(0, 0, 500, 500);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluPerspective(60.0, 1, 1.0, 20.0);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glTranslatef (0.0, 0.0, -5.0); // viewing transform

glClearColor(0.0, 0.0, 0.0, 0.0);

glShadeModel (GL_FLAT);

}

Page 101: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

OpenGL Implementation

void display(void)

{

glClear(GL_COLOR_BUFFER_BIT);

glColor3f (1.0, 1.0, 1.0);

glPushMatrix();

// draw sun

glPushMatrix();

glRotatef ((GLfloat) ang2, 0.0, 1.0, 0.0);

glRotatef (90.0, 1.0, 0.0, 0.0); // rotate it upright

glutWireSphere(1.0, 20, 16); // glut routine

glPopMatrix();

Page 102: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

OpenGL Implementation (con.)

// draw smaller planet

glRotatef ((GLfloat) ang1, 0.0, 1.0, 0.0);

glTranslatef (2.0, 0.0, 0.0);

glRotatef ((GLfloat) ang3, 0.0, 1.0, 0.0);

glRotatef (90.0, 1.0, 0.0, 0.0); // rotate it upright

glutWireSphere(0.2, 10, 8); // glut routine

glPopMatrix();

glutSwapBuffers();

}

Page 103: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Result

Page 104: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Let’s Examine Some Examples

Question 2: Draw a simple articulated robot arm with three

segments. The arms should be connected with pivot points as the

shoulder, elbow, or other joints. (the three segments have same length, saying 2 units)

Pivot points

Page 105: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Example-2

Red segment:1. Translates unit 1 to its pivot

point. M1 = T (1, 0, 0) 2. Rotates around its pivot

point. M2 = Ro ()3. Translates –1 unit back to

origin. M3 = T (-1, 0, 0) M = M3 M2 M1

Page 106: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

OpenGL Implementationvoid display(void)

{

glClear(GL_COLOR_BUFFER_BIT);

glPushMatrix();

// draw shoulder (red)

glTranslatef (-1.0, 0.0, 0.0);

glRotatef (shoulder, 0.0, 0.0, 1.0);

glTranslatef (1.0, 0.0, 0.0);

glPushMatrix();

glScalef (2.0, 0.4, 0.1);

glColor3f (1.0, 0.0, 0.0);

glutSolidCube (1); // glut routine

glPopMatrix();

Page 107: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Example-2

Green segment:1. Translates unit 1 to its pivot

point. M1 = T (1, 0, 0) 2. Rotates around its pivot

point. M2 = Ro ()3. Translates 1 unit to the

edge of the Red segment. M3 = T (1, 0, 0) M = M3 M2 M1

Page 108: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

OpenGL Implementation

void display(void)

{

……

// draw elbow

glTranslatef (1.0, 0.0, 0.0);

glRotatef (elbow, 0.0, 0.0, 1.0);

glTranslatef (1.0, 0.0, 0.0);

glPushMatrix();

glScalef (2.0, 0.4, 0.1);

glColor3f (0.0, 1.0, 0.0);

glutSolidCube(1); // glut routine

glPopMatrix();

Page 109: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Example-2

Yellow segment:1. Translates unit 1 to its pivot

point. M1 = T (1, 0, 0) 2. Rotates around its pivot

point. M2 = Ro ()3. Translates 1 unit to the

edge of the Green segment.

M3 = T (1, 0, 0) M = M3 M2 M1

Page 110: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Result

Page 111: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Problems Modify the Sailboat program, utilizing matrix multiplication, to do the following:

Translate it to the middle of the window Scale it to half size Make it spin about the :

X axis – left mouse button Y axis – middle mouse button Z axis - right mouse button

Page 112: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Problems Modify the Teapot program, utilizing rotate-scale-translate, to do the following:

Translate it to the bottom of the window Scale it to 1/3 size Rotate it 120 degrees about the x axis

Page 113: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

Remember

Always keep tracking your current position. Remember where you are, and go back to where you were.

Matrix multiplication is not commutative, the transformation order is very important.

In OpenGL, the transformation specified most recently is the one applied first.

Page 114: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

glu quadric Primitives

Quadric primitives– gluCylinder– gluDisk– gluPartialDisk– gluSphere

Displays at 0, 0, 0– Translate to where you want it

Page 115: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

gluNewQuadric

Create a new Quadric ObjectgluNewQuadric ();

Page 116: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

gluCylinder

Draw a cylindergluCylinder ( quad, base, top, height, slices, stacks );

quad – specifies the quadrics object (create with gluNewQuadric)

base – specifies the radius of the cylinder at z=0

top – specifies the radius of the cylinder at z=height

height - specifies the height of the cylinder

slices – specifies the number of subdivisions around the z axis

stacks - specifies the number of subdivisions along the z axis

Page 117: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

gluDisk

Draw a diskgluDisk ( quad, inner, outer, slices, loops );

quad – specifies the quadrics object (create with gluNewQuadric)

inner – specifies the inner radius of the disk (may be 0)

outer – specifies the outer radius of the disk

slices – specifies the number of subdivisions around the z axis

loops - specifies the number of concentric rings about the origin into which the disk is subdivided

Page 118: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

gluPartialDisk

Draw an arc of a diskgluPartialDisk ( quad, inner, outer, slices, loops, start, sweep );

quad – specifies the quadrics object (create with gluNewQuadric)

inner – specifies the inner radius of the disk (may be 0)

outer – specifies the inner radius of the disk

slices – specifies the number of subdivisions around the z axis

loops - specifies the number of concentric rings about the origin into which the disk is subdivided

start – specifies the starting angle, in degrees, of the disk portion

sweep – specifies the sweep angle, in degrees, of the disk portion

Page 119: Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 4 Instructor: Dan Hebert

Comp 175C - Computer Graphics Dan Hebert

gluSphere

Draw a spheregluSphere ( quad, radius, slices, stacks );

quad – specifies the quadrics object (create with gluNewQuadric)

radius – specifies the radius of the sphere

slices – specifies the number of subdivisions around the z axis

stacks - specifies the number of subdivisions along the z axis