Vous êtes sur la page 1sur 14

Coordinate transformations and

OpenGL

1
Coordinate Transforms
You should be quite familiar with the matrix representation of
coordinate transformations:
q = Mp
What is happening when we apply a transformation and why refer
to it as a coordinate transformation ?
Consider the following scene:

It shows two 3D coordinate systems: the gray system


has the same origin as the black but the y and z axes
have been rotated by 45 degrees about the x axis.

In fact we could get the gray system (we will call this the
transformed axes or frame of reference) by rotating the original
black system (the original or global axes or frame) about the x
axis by 45 degrees. 2
Now consider a point p(px, py, pz)…

In terms of the global frame of


reference, this is the point p rotated
by 45 degrees about the x axis

If we represent this as q(qx, qy, qz) and


recall our old familiar equation q = Mp
Then the Matrix M represents the mapping that will convert
from the transformed coordinate system onto the global
coordinate system. 3
Modelling Transforms
When we apply a transformation in OpenGL we change the current (transformed)
frame of reference. Everytime we draw a primitive it is drawn with respect to the
current frame. This is converted to the global representation. The following
examples show how the same cube (of size 1, centered at the origin) in terms of
transformed coordinates relates to a different representation in global coordinates:

glScalef(2, 2, 2);
glutWireCube(1);

glTranslatef(1, 1, 0);
glutWireCube(1);

glRotatef(60, 1, 0, 0); glRotatef(45, 1, 1, 1);


glutWireCube(1); glutWireCube(1); 4
Local vs. Global Coordinates
glutWireSphere We can use coordinate transforms to build
(1, 10, 10);
objects up based on primitive shapes. The
primitive shapes are defined in terms of local
coordinates. Coordinate transformations are
used to place, orient and scale them in the global
glutWireCube(1); frame

With Transforms in Global Space


glTranslatef(4, 0, -7);
glPushMatrix();
createCylinder glScalef(4, 1, 4);
(0.5, 2, 10, 10); glutSolidCube(1);
glPopMatrix();
glTranslatef(0, 0.5, 0);
glutSolidSphere(.6, 8, 8);
glRotatef(theta1, 1, 0, 0);
createCylinder(.4, 2, 8, 8);
glTranslatef(0, 0, 2);
glutSolidSphere(.6, 8, 8);
glRotatef(theta2, 1, 0, 0);
glutSolidCone createCylinder(.4, 2, 8, 8);
(0.5, 1, 10, 10); glTranslatef(0,0,2);
glRotatef(theta3, 1, 0, 0);
glutSolidCone(.6, 1, 8, 8); 5
Example The following example describes how we use hierarchical
transformations to build up a complex object based on
primitives defined in a local frame. This is the robot arm from
Lab6.
Drawing starts at the origin (we will denote this by the black
axes) viewing transformations have been applied and we may
wish to save this by Pushing the Modelview Matrix

Translate down the y-axis so that our full object will be


centered. Gray axes will denote our current coordinate frame

glTranslatef
(0, -2, 0)

Scale by a factor of (4, 1, 4)..


Non-symetrical scaling.
glScalef(4, 1, 4)
6
Draw a cube. We used scaling because
glutWireCube only draws symmetrical
cubes.
This scaling is not really useful for the
rest of our steps so we should save the
frame and pop it after the cube is drawn glPushMatrix();
glScalef(4, 4,1);
glutWireCube(1,0,0)
glPopMatrix();

Translate back up by 0.5 (half the height of the


cube)

glTranslate (0, -0.5, 0)

Draw a sphere (radius of .6)


glutWireSpher(0.6, 8, 8);
7
Now we have to draw the lower arm: a cylinder of radius 0.5
If we just draw it now it will be drawn as in the diagram left
(gluCylinder draws from origin to z=height) Note that at this
stage we want the next object to be hinged at the joint
(rotatable)

createCylinder(0.5, 2, 8, 8);

So if we step back and apply a rotation first (before


drawing the cylinder). Rotate by a variable theta (let
theta1 start off as a –90 degree rotation about the x axis)

glRotatef(
theta1, 1, 0, 0);

Notice how our current frame has the x-axis pointing


upwards – so when we draw our cylinder it is drawn
rotated. All we need to do to swing the arm (later on) is
to change the value of theta1
createCylinder(
8
0.5, 2, 8, 8);
We need to draw the next joint at the other
end of the cylinder. Although it looks like we
should be translating along the y-axis (in
terms of our global frame) our current frame
calls for a translation along the z axis (along
the length of the cylinder).

The next steps are just a repeat of the


previous ones. Rotate by theta2 (here we set
theta2=45 degrees about x axis) draw the
cylinder.

9
Translate down the length of the upper arm,
apply rotation of theta3 (here theta3 is 30 degrees
about –x axis. And draw a cone (as with the
cylinder the Glut Cone is drawn about the z axis).

All that’s required to animate this


properly is to change the values of
theta1, theta2, and theta3

10
Coordinate Transforms and the
Camera
glLoadIdentity();
//insert camera transformation here
glutWireCube(1);

No Camera transformation: cube is drawn


around the viewer (not visible)
up (direction)

eye(position)
gluLookAt Implements a
convenient Camera Model
lookAt(positi
on)
void gluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez,
GLdouble lookAtx, GLdouble lookAty, GLdouble lookAtz,
GLdouble upx, GLdouble upy, GLdouble upz ); 11
Camera Transforms
We need to express our scene in terms of a
new coordinate frame with the viewer at the
origin and looking down the z axis.

N
n
lookAt x  eyex  N
N  lookAt y  eyey 
u  vn
 lookAt z  eyez 
v  n  UP'
UP
UP  (upx , up y , upz ) UP' 
UP u x uy uz 0
v vy vz 0
M  x
From this we get three unit vectors u, v and n camera
nx ny nz 0
 
the basis vectors for the viewing coordinate 0 0 0 1
frame
M is applied to correct the orientation, we also need to apply a
translation which will place the camera at the coordinate origin. This
is simply a Translation by (-eyex, -eyey, -eyez); 12
gluLookAt
void gluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez,
GLdouble lookAtx, GLdouble lookAty, GLdouble lookAtz,
GLdouble upx, GLdouble upy, GLdouble upz );

This is equivalent to glMultMatrix(M);


glTranslated(-eyex, -eyey, -eyez)

u x uy uz 0
v vy vz 0
where M  x
n x ny nz 0
 
0 0 0 1

13
Camera Transforms The Image

The scene The code

gluLookAt(4, 0, 0, 0, 0, 0, 0, 1, 0);

gluLookAt(0, 0, 4, 0, 1, 1, 1, 1, 0);

gluLookAt(2, 2, 2, 0, 0, 0, 0, 1, 0);

14

Vous aimerez peut-être aussi