Вы находитесь на странице: 1из 29

Transformations in OpenGL

Lecturer: Carol OSullivan


Professor of Visual Computing Carol.OSullivan@cs.tcd.ie

Course www:

http://isg.cs.tcd.ie/cosulliv/

M d li Modeling T Transformations f ti

The three OpenGL routines for modelling transformations are: glTranslate*(),


glRotate*(), g (), and g glScale*() ()

These routines transform an object (or coordinate system) by moving, rotating, stretching, shrinking, or reflecting it. All three commands are equivalent to producing an appropriate translation, rotation, or scaling g matrix, and then calling g glMultMatrix*() with that matrix as the argument.

M d li Modeling T Transformations f ti
glLoadIdentity()

creates an identity matrix (used for clearing

all transformations) f i )

glLoadMatrixf(matrixptr)

loads a user specified p transformation matrix

where matrixptr is defined as GLfloat matrixptr[16];

T Translate l t
glTranslate(dx, glTranslate(dx dy, dy dz) translates by displacement vector (dx, dy, dz)

Multiplies the current matrix by a matrix that

moves (translates) an object by the given x-, y-, and z z-values values (or moves the local coordinate system by the same amounts). Using (0 (0.0, 0 0 0.0, 0 0 0.0) 0) is the identity operation which means it has no effect on an object or its local coordinate system. y

R t t Rotate
glRotatef(angle, g otate (a g e, vx, , vy, y, vz) ) rotates about axis (vx, vy, vz) by angle (specified

Multiplies the current matrix by a matrix that

in degrees)

rotates an object (or the local coordinate system) y ) in a counterclockwise direction about the ray from the origin through the point (x,y,z). The angle parameter specifies the angle of rotation in degrees. degrees The effect of glRotatef(45.0, 0.0, 0.0, 1.0) is a rotation of 45 5 degrees g about the z-axis. If angle = 0.0 the command has no effect.

S l Scale
glScalef(sx, sy, sz) apply scaling of sx in x direction, sy in y direction

and sz in z direction (note that these values specify th diagonal the di l of f a required i d matrix) ti ) Multiplies the current matrix by a matrix that stretches shrinks stretches, shrinks, or reflects an object along the axes. Each x-, y-, and z-coordinate of every point in the object is multiplied by the corresponding argument t x,y, or z. With the local coordinate system approach, the local coordinate axes are stretched, stretched shrunk, shrunk or reflected by the x-, y-, and z-factors, and the associated object is transformed with them.

T Transformations f ti i in

O OpenGL GL

OpenGL defines 3 matrices for manipulation of 3D scenes: GL_MODELVIEW: manipulates the view and models simultaneously GL_PROJECTION: performs f 3D 2D projection j ti f for di display l GL_TEXTURE: for manipulating textures prior to mapping on objects Each acts as a state parameter; once set it remains until

altered. Having defined a GL_MODELVIEW GL MODELVIEW matrix, all subsequent vertices are created with the specified transformation. Matrix transformation operations apply to the currently selected system matrix: use glMatrixMode(GL_MODELVIEW) to select modeling
matrix

St Stages of f Vertex V t Transformation T f ti


Vertex Geometry Pipeline
x y z w
original vertex / object coordinates xwin y win

MODELVIEW matrix

PROJECTION matrix

perspective division

viewport transformation

xeye y eye zeye w eye


vertex in i the h eye coordinate space

x proj y proj w proj


2d projection of vertex onto viewing plane / clip coordinates

xdev y dev 1 normalised device coordinates (foreshortened)

final window coordinates

Transformations in OpenGL
The

matrix and therefore has 12 degrees of freedom: a b c tx d e f t y g h i tz 0 0 0 1

MODELVIEW matrix is a 4x4 affine transformation

The MODELVIEW matrix is used for both the model and

the camera transformation rotating the model is equivalent to rotating the camera in the opposite direction OpenGL uses the same transformation matrix this sometimes causes confusion!

Transformations in

OpenGL

Each time an OpenGL transformation M is called

the current MODELVIEW matrix C is altered:

v = Cv

v = CMv

glTranslatef(1.5, 0.0, 0.0); glRotatef(45.0, 0 0 0.0, 0 0 0.0, 0 1 1.0); 0

v = CTRv

E Example l
Draw a single point using three transformations:

glMatrixMode(GL_MODELVIEW); g glLoadIdentity(); y() glMultMatrix(N); glMultMatrix(M); glMultMatrix(L); l l i ( ) glBegin(GL_POINTS); glVertex3f(v); glEnd();

Transformations in OpenGL
Transformations are applied in the order specified (with respect to the vertex) which appears to be in reverse:

glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(1.5, 0.0, 0.0); glRotatef(45.0, 0.0, 0.0, 1.0); glVertex3f(1.0, 0.0, 0.0);

original

rotate

translate

Camera & Model Transformations


Given that camera and model

transformations are specified using a single matrix we must consider the effect of these transformations on the coordinate frames of the camera and models. models Assume we wish to orbit an object at a fixed orientation:
translate l object bj away f from

camera rotate around X to look at top of object bj t then pivot around objects Y in order to orbit properly.

Camera & Model Transformations


If the eG GL_MODELVIEW O matrix a is sa an identity de y matrix a then e the e

camera frame and the model frame are the same.

i.e. they are specified using the same co-ordinate system

If we issue command glTranslatef(0.0,0.0,-10.0);

and then create the model: a vertex at [0, 0 0, 0 0] in the model will be at [0, 0 0 0, -10] in the camera frame i.e. we have moved the object away from the camera

This may be viewed conceptually in 2 ways: we have positioned the object with respect to the

world ld frame f we have moved the world-frame with respect to the camera frame

Camera & Model Transformations


The first interpretation (the camera view / fixed

coordinate system approach) is:

move away from camera rotate around world X (tilt towards camera) pivot around world Y

g glMatrixMode(GL ( _MODELVIEW); ); glLoadIdentity(); glTranslatef(0.0, 0.0, -10.0); glRotatef(30 0 1 glRotatef(30.0, 1.0, 0 0 0.0, 0 0 0.0); 0); glRotatef(Pivot_Angle, 0.0, 1.0, 0.0); glBegin(GL_POLYGON); glBegin(GL POLYGON); glEnd();

read transformations in reverse order

draw required object

Camera & Model Transformations


The second interpretation (using a local frame

view) is:

clear previous transformations glMatrixMode(GL_MODELVIEW); glLoadIdentity(); move object frame away from camera glTranslatef(0.0, 0.0, -10.0); glRotatef(30.0, 1.0, 0.0, 0.0); rotate object into view glRotatef(Pivot_Angle, lR t t f(Pi t A l 0 0 1 0.0, 1.0, 0 0 0.0); 0) glBegin(GL_POLYGON); glEnd(); pivot around objects Y axis draw required object

Camera & Model Transformations


objects new Y

rotate

translate

Camera & Model Transformations


A local local frame view view is usually adopted as it extends

naturally to the specification of hierarchical model frames. This allows creation of jointed assemblies
articulated figures (animals, robots etc.)

In the hierarchical model, each sub-component has

its own local frame. Changes made to the parent frame are propagated down to the child frames (thus all models in a branch are globally l b ll controlled t ll d by b the th parent). t) This simplifies the specification of animation.

Aside: Display Lists


It is often expensive to compute or create a model for

display. display

We would prefer not to have to recalculate it each time for

each new animation frame.

Display lists allow the creation of an object in memory,

where it resides until destroyed.


a given display list.

Objects are drawn by issuing a request to the server to display

number of list IDs to create begin specification of the list user specified model code used later to display the list

GLuint of first

list ID

list = glGenLists(1); glNewList(list, GL_COMPILE); create_model(); glEndList(); glCallList(list); glDeleteLists(list, 1);

delete when finished (specifying number of lists)

M d l Transformations Model T f ti
As the MODELVIEW matrix is changed objects are

created with respect to a changing transformation. This is often termed the current transformation matrix or CTM. The CTM behaves like a 3D p pointer, selecting g new positions and orientations for the creation of geometries. The only complication with this view is that each new CTM is derived from a previous CTM, i.e. all CTMs are specified ifi d relative l ti to t previous i versions. i A scaling transformation can cause some confusion.

Scaling g Transformation and the CTM


glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(3, g ( , 0, , 0); ); glTranslatef(1, 0, 0); gluCylinder( ); gluCylinder(); g glMatrixMode(GL ( _MODELVIEW); ); glLoadIdentity(); glTranslatef(3, 0, 0);

glScalef(0 glScalef(0.5, glScalef (0 (0.5, 5 0.5, 0 5 0.5); 0 5);


glTranslatef(1, 0, 0); gluCylinder(); l C li d ( )

Hi Hierarchical hi l T Transformations f ti
For geometries with an

implicit p hierarchy y we wish to associate local frames with sub-objects in the assembly. Parent-child frames are related via a transformation. Transformation T f ti linkage li k iis described by a tree: Each E h node d h has its it own local l l co-ordinate system.

Hi Hierarchical hi l T Transformations f ti
R R

Hierarchical transformation allow independent control over sub-parts of an assembly

translate base

rotate joint1

rotate joint2

complex hierarchical transformation

OpenGL Implementation
glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(bx, by, bz); create_base(); (); glTranslatef(0, j1y, 0); glRotatef(joint1_orientation); create joint1(); create_joint1(); glTranslatef(0, uay, 0); create_upperarm(); glTranslatef(0 j2y); glTranslatef(0, glRotatef(joint2_orientation); create_joint2(); glTranslatef(0, l l t f(0 l lay, 0) 0); create_lowerarm(); glTranslatef(0, py, 0); glRotatef(pointer_orientation); create_pointer();

Hierarchical Transformations
The previous example had simple one one-to-one to one

parent-child linkages. In g general there may y be many y child frames derived from a single parent frame. We need some mechanism to remember the parent frame and return to it when creating new children. OpenGL O GL provide id a matrix t i stack t kf for j just t this thi purpose:
glPushMatrix() saves the CTM glPopMatrix() returns to the last saved CTM

Hi Hierarchical hi l T Transformations f ti

Each finger is a child of the parent (wrist)

independent control over the orientation of the fingers relative to the wrist

Hierarchical Transformations

glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(bx, by, bz); create_base(); glTranslatef(0, jy, 0); glRotatef(joint1 g otate (jo t _o orientation); e tat o ); create_joint1(); glTranslatef(0, ay, 0); create_upperarm(); glTranslatef(0, g ( , wy); y); glRotatef(wrist_orientation); create_wrist(); glPushMatrix(); glPushMatrix (); // save frame glTranslatef(-xf, g ( , fy0, y , 0); ); glRotatef(lowerfinger1_orientation); glTranslatef(0, fy1, 0); create_lowerfinger1(); glTranslatef(0, g ( , fy2, y , 0); ); glRotatef(upperfinger1_orientation); create_fingerjoint1(); glTranslatef(0, fy3, 0); create_upperfinger1(); pp g (); glPopMatrix(); glPopMatrix (); // restore frame glPushMatrix(); glPushMatrix (); // do finger 2... Finger1 glPopMatrix(); glPopMatrix g p (); () glPushMatrix(); glPushMatrix (); // do finger 3... glPopMatrix(); glPopMatrix ();

Вам также может понравиться