Computer Graphics Course 2006 OpenGL - Modeling and View ing - - PowerPoint PPT Presentation

computer graphics course 2006
SMART_READER_LITE
LIVE PREVIEW

Computer Graphics Course 2006 OpenGL - Modeling and View ing - - PowerPoint PPT Presentation

Computer Graphics Course 2006 OpenGL - Modeling and View ing Transformations OpenGL - Modeling View ing Transformations OpenGLs transformations operate on the vertices and are divided as follows:


slide-1
SLIDE 1

Computer Graphics Course 2006

OpenGL - Modeling and View ing Transformations

slide-2
SLIDE 2

OpenGL - Modeling View ing Transformations

OpenGL’s transformations operate on the

vertices and are divided as follows:

Notice that homogeneous coordinates are

used.

⎟ ⎟ ⎟ ⎟ ⎟ ⎠ ⎞ ⎜ ⎜ ⎜ ⎜ ⎜ ⎝ ⎛

⎟ ⎟ ⎟ ⎟ ⎠ ⎞ ⎜ ⎜ ⎜ ⎜ ⎜ ⎝ ⎛

⎟ ⎟ ⎟ ⎟ ⎠ ⎞ ⎜ ⎜ ⎜ ⎜ ⎜ ⎝ ⎛ °

⎟ ⎟ ⎟ ⎟ ⎠ ⎞ ⎜ ⎜ ⎜ ⎜ ⎜ ⎝ ⎛ − = ⎟ ⎟ ⎟ ⎠ ⎞ ⎜ ⎜ ⎜ ⎝ ⎛ W Z Y X z y x Matrix Modelview Matrix Projection Division Projective ation Transform Viewport

slide-3
SLIDE 3

Modelview Matrix

Is a 4 by 4 matrix. Is the first matrix that multiplies the

vertices.

Its main purpose is:

modeling trans’ - position and orienting the

models.

viewing trans’ - positioning and aiming the

camera.

slide-4
SLIDE 4

Modelview Matrix

Modeling transformations: Are used to move objects in the space

without redefining their vertices values (useful for animations and convenient for defining objects around the origin).

slide-5
SLIDE 5

Modelview Matrix

Viewing transformations: (we shall see next that OpenGL’s camera is

positioned in the origin(0,0,0) facing the minus Z - axis (with the up vector (0,1,0)).

In order to position a virtual camera in an

arbitrary position and orientation, we need to “coincide” our virtual camera with OpenGL’s.

z- z x- x y

slide-6
SLIDE 6

Modelview Matrix

This is done by moving the world such that the

virtual camera will be located at the origin. And rotate it such that it will face the minus Z axis (and have up vector(0,1,0)). Exactly as OpenGL’s camera.

slide-7
SLIDE 7

General OpenGL Matrices

glMatrixMode(GLenum mode) -

specifies which matrix is being modified:

GL_MODELVIEW - the Modelview matrix GL_PERSPECTIVE - the Perspective matrix GL_TEXTURE - the Texture mapping matrix

glLoadI dentity() - Sets the identity

matrix to the current matrix modified.

slide-8
SLIDE 8

General OpenGL Matrices

glLoadMatrix{ fd} (const TYPE * m) - sets

the current modified matrix to be the matrix defined in the input array m[i][j].

glMultMatrix{ fd} (const TYPE * m) -

multiplies the current modified matrix with the matrix defined in the input array m[i][j]. (Right side multiplication).

m is defined as float/double m[4][4];

slide-9
SLIDE 9

Modeling Transformations

glTranslate{ fd} (TYPE x, TYPE y, TYPE z) -

Multiplies the current matrix by a matrix that moves (translates) vertices by x, y and z values.

How will such a matrix look like?

Question: In the above equations we took W= 1 what

would happen if W was any non-zero (Real) number?

⎟ ⎟ ⎟ ⎟ ⎟ ⎠ ⎞ ⎜ ⎜ ⎜ ⎜ ⎜ ⎝ ⎛ + + + = ⎟ ⎟ ⎟ ⎟ ⎟ ⎠ ⎞ ⎜ ⎜ ⎜ ⎜ ⎜ ⎝ ⎛

⎟ ⎟ ⎟ ⎟ ⎠ ⎞ ⎜ ⎜ ⎜ ⎜ ⎜ ⎝ ⎛ ⎟ ⎟ ⎟ ⎟ ⎟ ⎠ ⎞ ⎜ ⎜ ⎜ ⎜ ⎜ ⎝ ⎛ 1 1 1 1 1 1 : since 1 1 1 1 z Z y Y x X Z Y X z y x z y x

slide-10
SLIDE 10

Modeling Transformations

glRotate{ fd} (TYPE angle, TYPE x, TYPE, y, TYPE z) -

Multiplies the current matrix by a matrix that rotates the vertices by angle degrees around the axis (x, y, z).

Note that the vertices that lies further from the rotation

axis will be affected more dramatically.

glScale{ fd} (TYPE x, TYPE, y, TYPE z) - Multiplies the

current matrix by a matrix that stretches/shrinks/reflects the vertices along the axes by the multiplication of the corresponding x, y and z. The matrix is defined as:

Question: what values for x,y and z will

stretch, shrink and reflect the object?

⎟ ⎟ ⎟ ⎟ ⎟ ⎠ ⎞ ⎜ ⎜ ⎜ ⎜ ⎜ ⎝ ⎛ 1 z y x

slide-11
SLIDE 11

Modeling Transformations

OpenGL’s matrix multiplication is right

side:

glMatrixMode(GL_MODELVIEW) ; glLoadIdentity() ; glTranslatef(1.0, -1.0, 0.0) ; glRotatef(45, 0.0, 1.0, 0.0) ; glScalef(0.5, -1.0, 1.0) ;

Will result with: T(R(S(x))) = y. The rule is: Last command operates first, First command operates last.

slide-12
SLIDE 12

Modeling Transformations

Does It matter?

Yep:

Remember Matrix multiplication do not generally

commute ( ).

Question: Do the rotation matrices form a commutative

(Abelian) group (what about the translation matrices).

Rotate Rotate

Translate Translate

BA AB ≠

slide-13
SLIDE 13

General OpenGL Matrices

glGetFloatv(GLenum pname,const TYPE * m) glGetDoublev(GLenum pname,const TYPE * m) - fills

the array m with the state variable indicated by pname:

GL_MODELVIEW_MATRIX - the current modelview matrix GL_PROJECTION_MATRIX - the current projection matrix See more on OpenGL Programming Guide 1.1 pages 537+

All the modelview operations multiply the current matrix

from its right: M = > M * R/T/S. This has a geometrical meaning about the order of the transformations.

But what if we need to multiply a matrix from the left?

slide-14
SLIDE 14

General OpenGL Matrices

Multiplying the current matrix

from its left:

double M[16] ; glGetDoublev(GL_MODELVIEW_MATRI

X, M) ;

glMatrixMode(GL_MODELVIEW) ; glLoadIdentity() ; glRotate/glTranslate/glLoadMatrix(A) glMultMatrix(M) ;

The first C

M= C C= Id C= Id* A= A C= C* M= A* M= A* C

slide-15
SLIDE 15

View ing Transformations

As stated before, in order to change the

position and orientation of the viewpoint, we can move and rotate the “world” (all the objects-vertices) instead of moving the camera.

Let us assume that the camera is:

Positioned in the origin (0,0,0). Looking at the -Z axis direction. Its up vector is (0,1,0)

X+ X- Y- Y+ Z- Z+

slide-16
SLIDE 16

View ing Transformations

Now, we can use glTranslate and glRotate to

move the “world” and thus achieve the impression of a change of viewpoint.

For example lets say that we have some objects

located at around the origin and we wish to move the camera away from the origin on the Z axis - that is we want to move the camera to the positive Z direction.

Z+ Z+ Z- Z-

Camera move

) , , ( z −

slide-17
SLIDE 17

View ing Transformations

Considering the following equivalence, in terms of what

the camera sees:

This is a result of the fact that any rigid transformation

  • n the objects and on the camera will not change

what the camera views.

So all we have to do is glTranslatef(0,0,-z)! Question: where in the code should we put this line?

Z+ Z+ Z- Z-

Objects move

Z+ Z-

) , , ( z Equivalent

slide-18
SLIDE 18

View ing Transformations

This applies also to the case of rotations: In this case glRotate will be used. Using glScale we can get camera zoom.

slide-19
SLIDE 19

View ing Transformations

gluLookAt(GLdouble eyex, eyey, eyez, centerx,

centery, centerz, upx, upy, upz) - Is a GLU utility that:

Defines a viewing matrix of the desired viewpoint. Multiplies it to the right of the current matrix (in our

case the Modelview matrix).

slide-20
SLIDE 20

View ing Transformations

(eyex,eyey,eyez) - is the point where the desired

camera is located.

(centerx,centery,centerz) - is a point in space to which

the camera aims (will appear in the center of the projected image of the camera).

(upx,upy,upz) - is the camera up vector. This vector

actually defines which direction in the world will be the up direction in the projected image of the camera:

) , , ( upz upy upx ) , , ( centerz centery centerx ) , , ( eyez eyey eyex

Image up direction Image center

slide-21
SLIDE 21

Projective Transformations

The projection matrix is responsible for

the projective transformation:

glMatrixMode(GL_PROJECTION) ;

The camera model we will use in OpenGL

are:

Pinhole perspective projection Orthographic projection

slide-22
SLIDE 22

Projective Transformations

Pinhole Perspective Projection is when we

project points in to along lines intersecting in a point (in the case of n= 2):

n

1 −

ℜn

Principal axis Projection plane Center of projection

slide-23
SLIDE 23

Projective Transformations

glFrustum(Gldouble left, right, bottom, top,

near, far) - Create a matrix for a perspective view and multiplies the current matrix (near and far are non-zero positive).

slide-24
SLIDE 24

Projective Transformations

gluPerspective(GLdouble fovy, aspect, near, far) -

Creates a matrix for symmetric perspecive view.

fovy is the angle of field of view in the X-Z plane [0…180]. aspect is the aspect ratio of the frustum (its width/height

television/computer screen is 4:3, Theatre wide screen is 16:9).

near and far are the distances from the clipping planes.

slide-25
SLIDE 25

Projective Transformations

glOrtho(GLdouble left, right, bottom, top, near, far) -

Creates a matrix for an orthographic parallel viewing projection and multiplies the current matrix (In this case near and far can be also negative).

Projection plane direction of projection

slide-26
SLIDE 26

Projective Transformations

gluOrtho2D(GLdouble left, right, bottom, top) -

Creates a matrix for an orthographic parallel viewing projection and multiplies the current matrix. Where are near = -1.0 and far is 1.0. This is a simple case for 2D drawing using OpenGL. When glVertex2(x,y) is given a point with coordinate (x,y,0) is actually defined and thus it is not clipped when using gluOrtho2D.

slide-27
SLIDE 27

View port Transformations

The last mapping to window coordinate. glViewport(GLint x, y, width, height) -

Defines a pixel rectangle in the window into which the final image (projected frustum) is mapped.

(x,y) is the lower left corner of the viewport. (width,height) is the size of the viewport

rect.

slide-28
SLIDE 28

View port Transformations

If the aspect ration of the viewing volume isn’t equal to

the viewport’s aspect ratio a distortion will occur during the viewport transformation (square will turn to be rectangles and circles will turn to be ellipses).

Distorted example: gluPerspective(fovy, 1.0, near, far); glViewport(0,0,200,400) ; since Undistorted example: gluPerspective(fovy, 1.0, near, far); glViewport(0,0,400,400) ;

. 1 5 . 400 / 200 ≠ =

slide-29
SLIDE 29

Matrix Stacks

OpenGL supports two stacks of matrices Modelview matrix stack (32 4x4 matrices) Projection matrix stack (2 4x4 matrices) These stacks are useful for constructing hierarchical

  • models. For example a car made of its body and the

four wheels:

Rotate wheels Rotate wheels + Rotate body

slide-30
SLIDE 30

Matrix Stacks

glPushMatrix(void) -

Pushes all matrices in the current stack down one level.

glPopMatrix(void) - Pops

the top matrix off the current stack, losing the topmost matrix!

(The current stack is

determined by

glMatrixMode).

M5 M4 M3 M2 M1 M5 M4 M3 M2 M1

Pop Push

M4 M3 M2 M1 M4 M4 M3 M2 M1

Current matrix level Current matrix level

slide-31
SLIDE 31

Matrix Stacks

Example code:

void drawCar() {

⌧glMatrixMode(GL_MODELVIEW) ; ⌧glTranslatef(x,y,z) ; /* / ⌧glRotatef(car_ang, 0, 1, 0) ; /* / ⌧draw_car_body() ; ⌧glPushMatrix() ; ⌧

glTranslate(-1,0,1) ;

glRotatef(wheels_ang, 0, 1, 0) ;

draw_car_wheel() ;

⌧ glPopMatrix() ; ⌧ glPushMatrix() ; ⌧

glTranslate(1,0,1) ;

glRotatef(wheels_ang, 0, 1, 0) ;

draw_car_wheel() ;

⌧ glPopMatrix() ; ⌧}

First we move and rotate the

car (body + wheels) - as it is the top level in the hierarchy.

Next we push the stack -

and therefore store a copy.

Then we draw the right and

left wheels in their appropriate position and

  • rientation. Note that on

each wheel the transformation /* / will

  • perate.

The last pop will retrieve the

matrix containing only the /* / transformations.