Transformations

A transformation is a function that converts a set of (X,Y,Z) coordinates into another set of (X,Y,Z) coordinates.

(So far we're working in 2D - (X,Y) coordinates - but OpenGL treats this as 3D coordinates with Z=0)






Transformations

There are three common types of "model" transformations:






Transformations

Translation Rotation
 
Scaling





Transformations as State

A transformation is considered part of the rendering state, in 'classic' OpenGL.



Transformations are defined before the geometry that they should affect.



When drawing, vertex data is transformed by the currently active transformation.






Transformations in OpenGL

vlist.draw(GL_TRIANGLES)
     
glTranslatef(0.5, -0.2, 0.0)
vlist.draw(GL_TRIANGLES)
     





Translation

glTranslatef(x, y, z)

Moves subsequent objects by (x, y, z) units.


Note: there is no 2D glTranslate function; for a 2D translation, pass 0 for z.


Example: basicXform.py






Rotation


glRotatef(angle, x, y, z)

Rotates objects around the axis (x, y, z), by angle degrees.

In 2D, use 0, 0, 1 as the axis.



OpenGL rotations are right-handed - a positive angle rotates counter-clockwise (in 2D).






Rotation

Rotations are around the origin, not necessarily the center of an object.

vlist.draw(GL_TRIANGLES)
     
glRotatef(90.0, 0.0, 0.0, 1.0)
vlist.draw(GL_TRIANGLES)
     





Scaling


glScalef(GLfloat x, GLfloat y, GLfloat z)

Scales objects by the factor x in the X direction, y in the Y direction, and z in the Z direction.

In 2D, pass 1 for z.

A uniform scale - changing the size of an object without distorting it - is done with x = y = z.






Scaling

As with rotation, scaling is relative to the origin.

vlist.draw(GL_TRIANGLES)
     
glScalef(0.25, 0.5, 1.0)
vlist.draw(GL_TRIANGLES)
     





Multiple Transformations

Individual transformations can be combined.

Each transformation call accumulates with all the previous transformations.

glColor3f(0, 0, 1)
drawTriangle() 

glTranslatef(0.25, 0.25, 0.0)
glColor3f(0, 1, 0)
drawTriangle()

glTranslatef(-0.25, -1.0, 0.0)
glColor3f(1, 0, 0)
drawTriangle()





Identity Transformation

glLoadIdentity()

An identity transformation does nothing - it transforms a point (x,y,z) back into itself.


glLoadIdentity() will clear the current transformation back to identity.


Example: interactiveXform.py






Order

Transformations are not, in general, commutative.

This means that applying the same transformations in different orders can produce different results.

no transformation glTranslatef(0.5, 0, 0)
glRotatef(45, 0, 0, 1)
glRotatef(45, 0, 0, 1)
glTranslatef(0.5, 0, 0)





Order

Multiple transformations are effectively applied in reverse order.
i.e., the last transformation (the one immediately before the geometry function calls) is the first one applied to the raw vertex data.

glTranslatef(0.5, 0, 0)
glRotatef(45, 0, 0, 1)
drawTriangle()





Example

An object can be animated by adding transformations over time
i.e. making further calls to glTranslate / glRotate / glScale without doing glLoadIdentity between frames

This does not work well in general - only for a single object

Best to keep track of a position & orientation (& size) for each object, and apply transformations from scratch on each frame

Example: animXform.py






PushMatrix / PopMatrix

Fixed-function OpenGL maintains a stack of saved transformation matrices.

glPushMatrix() & glPopMatrix() save and restore transformation state.



Make sure you always have the same number of pushes & pops.

The number of transformations you can save is finite; it is at least 32.

Example: pushpop.py






Hierarchical Transformations

Hierarchical transformations can be thought of as transformations that are "attached to" other transformations.

They are used to transform objects relative to other objects.

A common use is in articulated bodies.






Hierarchical Transformations

We can draw this "car" by transforming the two wheels relative to the body of the car.

We'd like each wheel to be affected by the car body's transformation, but not by any other wheel's transformation.






Hierarchical Transformations

This can be accomplished by saving & restoring transformation state.

apply body transformation
draw body
save state
apply front wheel transformation
draw wheel
restore saved state
apply rear wheel transformation
draw wheel

Examples:






Transformation Hierarchy

Hierarchical transformations are often represented as a tree of transformations.

This is the basis of scene graph systems.



Creative Commons License
This document is by Dave Pape, and is released under a Creative Commons License.