The Modelview matrix is a 4 x 4 matrix that represents the transformed coordinate system you are using to place and orient your objects. The vertices you provide for your primitives are used as a single-column matrix and multiplied by the Modelview matrix to yield new transformed coordinates in relation to the eye coordinate system.
In Figure 7-9, a matrix containing data for a single vertex is multiplied by the Modelview matrix to yield new eye coordinates. The vertex data is actually four elements, with an extra value w, that represents a scaling factor. This value is set by default to 1.0, and rarely will you change this yourself.
Letís take an example that modifies the Modelview matrix. Say you wanted to draw a cube using the AUX libraryís auxWireCube() function. You would simply call
and you would have a cube centered at the origin that measures 10 units on a side. To move the cube up the y-axis by 10 units before drawing it, you would multiply the Modelview matrix by a matrix that describes a translation of 10 units up the y-axis, and then do your drawing. In skeleton form, the code looks like this:
// Construct a translation matrix for positive 10 Y ... // Multiply it by the Modelview matrix ... // Draw the cube auxWireCube(10.0f);
Actually, such a matrix is fairly easy to construct, but it would require quite a few lines of code. Fortunately, a high-level function is provided that does this for you:
void glTranslatef(GLfloat x, GLfloat y, GLfloat z);
This function takes as parameters the amount to translate along the x, y, and z directions. It then constructs an appropriate matrix and does the multiplication. Now the pseudocode from above looks like the following, and the effect is illustrated in Figure 7-10.
// Translate up the y-axis 10 units glTranslatef(0.0f, 10.0f, 0.0f); // Draw the cube auxWireCube(10.0f);
To rotate an object about one of the three axes, you would have to devise a Rotation matrix to be multiplied by the Modelview matrix. Again, a high-level function comes to the rescue:
glRotatef((GLfloat angle, GLfloat x, GLfloat y, GLfloat z);
Here we are performing a rotation around the vector specified by the x, y, and z arguments. The angle of rotation is in the counterclockwise direction measured in degrees and specified by the argument angle. In the simplest of cases, the rotation is around one of the axes, so only that value needs to be specified.
You can also perform a rotation around an arbitrary axis by specifying x, y, and z values for that vector. To see the axis of rotation, you can just draw a line from the origin to the point represented by (x,y,z). The following code rotates the cube by 45? around an arbitrary axis specified by (1,1,1), as illustrated in Figure 7-11.
// Perform the transformation glRotatef(90.0f, 1.0f, 1.0f, 1.0f); // Draw the cube auxWireCube(10.0f);
A scaling transformation increases the size of your object by expanding all the vertices along the three axes by the factors specified. The function
glScalef(GLfloat x, GLfloat y, GLfloat z);
multiplies the x, y, and z values by the scaling factors specified.
Scaling does not have to be uniform. You can use it to stretch or squeeze objects, as well. For example, the following code will produce a cube that is twice as large along the x- and z-axis as the cubes discussed in the previous examples, but still the same along the y-axis. The result is shown in Figure 7-12.
// Perform the scaling transformation glScalef(2.0f, 1.0f, 2.0f); // Draw the cube auxWireCube(10.0f);