Open GL Super Bible
Advanced Matrix ManipulationYou don’t have to use the highlevel functions to produce your transformations. We recommend that you do, however, because those functions often are highly optimized for their particular purpose, whereas the lowlevel functions are designed for general use. Two of these highlevel functions make it possible for you to load your own matrix and multiply it into either the Modelview or Projection matrix stacks. Loading a MatrixYou can load an arbitrary matrix into the Projection, Modelview, or Texture matrix stacks. First, declare an array to hold the 16 values of a 4 x 4 matrix. Make the desired matrix stack the current one, and call glLoadMatrix. The matrix is stored in columnmajor order, which simply means that each column is traversed first from top to bottom. Figure 728 shows the matrix elements in numbered order. The following code shows an array being loaded with the Identity matrix, then being loaded into the Modelview matrix stack. This is equivalent to calling glLoadIdentity using the higherlevel functions. // Equivalent, but more flexible glFloat m[] = { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }; glMatrixMode(GL_MODELVIEW); glLoadMatrixf(m);
Performing Your Own TransformationsYou can load an array with an arbitrary matrix if you want, and multiply it, too, into one of the three matrix stacks. The following code shows a Transformation matrix that translates 10 units along the xaxis. This matrix is then multiplied into the Modelview matrix. You can also achieve this affect by calling glTranslatef. // Define the Translation matrix glFloat m[] = { 1.0f, 0.0f, 0.0f, 10.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }; // Multiply the translation matrix by the current modelview // matrix. The new matrix becomes the modelview matrix glMatrixMode(GL_MODELVIEW); glMultMatrixf(m); Other TransformationsThere’s no particular advantage in duplicating the functionality of gLoadIdentity or glTranslatef by specifying a matrix. The real reason for allowing manipulation of arbitrary matrices is to allow for complex matrix transformations. One such use is for drawing shadows, and you’ll see that in action in Chapter 9. Some other uses are wrapping one object around another object, and certain lens effects. For information on these advanced uses, see Appendix B. SummaryIn this chapter, you’ve learned concepts crucial to using OpenGL for creation of 3D scenes. Even if you can’t juggle matrices in your head, you now know what matrices are and how they are used to perform the various transformations. You’ve also learned how to manipulate the Modelview and Projection matrix stacks to place your objects in the scene and to determine how they are viewed on screen. Finally, we also showed you the functions needed to perform your own matrix magic if you are so inclined. These functions allow you to create your own matrices and load them into the matrix stack, or multiply them by the current matrix first. The tank/robot simulation at this point in the book will now allow you to move around in a threedimensional world and explore objects placed all around. If you study the simulation code thus far, you will find excellent use of perspective projections, as well as the gluLookAt utility function that provides a simple way to specify your viewing transformation. Your 3D world is made of wire for now, but that will be changing very soon. Reference SectionglFrustum
Parameters
Example The code below sets up a Perspective matrix that defines a viewing volume from 0 to –100 on the zaxis. The x and y extents are 100 units in the positive and negative directions. glLoadMatrix(GL_PROJECTION); glLoadIdentify(); glFrustum(100.0f, 100.0f, 100.0f, 100.0f, 0.0f, 100.0f);
