0% found this document useful (0 votes)
28 views

3.6 Transformations in Opengl: Matrix. Whenever You Specify Geometry (Using

Uploaded by

Muhammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

3.6 Transformations in Opengl: Matrix. Whenever You Specify Geometry (Using

Uploaded by

Muhammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CSC418 / CSCD18 / CSC2504 Transformations

3.6 Transformations in OpenGL


OpenGL manages two 4 × 4 transformation matrices: the modelview matrix, and the projection
matrix. Whenever you specify geometry (using glVertex), the vertices are transformed by the
current modelview matrix and then the current projection matrix. Hence, you don’t have to perform
these transformations yourself. You can modify the entries of these matrices at any time. OpenGL
provides several utilities for modifying these matrices. The modelview matrix is normally used to
represent geometric transformations of objects; the projection matrix is normally used to store the
camera transformation. For now, we’ll focus just on the modelview matrix, and discuss the camera
transformation later.

To modify the current matrix, first specify which matrix is going to be manipulated: use glMatrixMode(GL MODE
to modify the modelview matrix. The modelview matrix can then be initialized to the identity with
glLoadIdentity(). The matrix can be manipulated by directly filling its values, multiplying it
by an arbitrary matrix, or using the functions OpenGL provides to multiply the matrix by specific
transformation matrices (glRotate, glTranslate, and glScale). Note that these transforma-
tions right-multiply the current matrix; this can be confusing since it means that you specify
transformations in the reverse of the obvious order. Exercise: why does OpenGL right-multiply
the current matrix?

OpenGL provides a stacks to assist with hierarchical transformations. There is one stack for the
modelview matrix and one for the projection matrix. OpenGL provides routines for pushing and
popping matrices on the stack.

The following example draws an upper arm and forearm with shoulder and elbow joints. The
current modelview matrix is pushed onto the stack and popped at the end of the rendering, so,
for example, another arm could be rendered without the transformations from rendering this arm
affecting its modelview matrix. Since each OpenGL transformation is applied by multiplying a
matrix on the right-hand side of the modelview matrix, the transformations occur in reverse order.
Here, the upper arm is translated so that its shoulder position is at the origin, then it is rotated,
and finally it is translated so that the shoulder is in its appropriate world-space position. Similarly,
the forearm is translated to rotate about its elbow position, then it is translated so that the elbow
matches its position in upper arm coordinates.

glPushMatrix();

glTranslatef(worldShoulderX, worldShoulderY, 0.0f);


drawShoulderJoint();
glRotatef(shoulderRotation, 0.0f, 0.0f, 1.0f);
glTranslatef(-upperArmShoulderX, -upperArmShoulderY, 0.0f);
drawUpperArmShape();

glTranslatef(upperArmElbowX, upperArmElbowY, 0.0f);

Copyright
c 2005 David Fleet and Aaron Hertzmann 16
CSC418 / CSCD18 / CSC2504 Transformations

drawElbowJoint();
glRotatef(elbowRotation, 0.0f, 0.0f, 1.0f);
glTranslatef(-forearmElbowX, -forearmElbowY, 0.0f);
drawForearmShape();

glPopMatrix();

Copyright
c 2005 David Fleet and Aaron Hertzmann 17

You might also like