262 Introduction to C++ Programming and Graphics
Transformations
The cube drawing code provides us with the opportunity to discuss trans-
formations. Every point in space is represented by a position vector encapsu-
lating thex,y,andzcoordinates in a fixed (world or laboratory) frame. To
implement translation, we call the function:
glTranslatef(Dx, Dy, Dz)
where the three arguments are thex, y,andzdisplacements.
Multiplying a position vector by a 3×3 transformation matrix gives a
new position vector. In the cube code, the transformation matrix is introduced
as the 3×3 identity matrix:
⎡
⎣
100
010
001
⎤
⎦.
Multiplying a position vector by the identity matrix leaves the vector un-
changed. To implement rotation around the axis defined by the direction cosines
(ax, ay, az), we call the function:
glRotatef(degrees, ax, ay, az)
For example,
glRotatef(48, 0, 0, 1);
performs rotation by 48◦around thezaxis.
Prefabricated shapes
Glutfunctions that display prefabricated shapes are shown in Table
8.1.2. In practice, these functions are used to construct composite objects from
elementary geometries.
The following function produces the snowman wearing shades displayed
in Figure 8.1.1(f):
void snowman(void)
{
glClear(GLCOLORBUFFERBIT);
//--- Introduce a transformation matrix:
glLoadIdentity();