Open GL Super Bible

Previous Table of Contents Next


glLoadIdentity

Purpose
Sets the current matrix to Identity.
Include File
<gl.h>
Syntax
void glLoadIdentity(void);
Description
This function replaces the current Transformation matrix with the Identity matrix. This essentially resets the coordinate system to eye coordinates.
Returns
None.

Example

The following code shows the Modelview matrix being set to identity:

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
See Also
glLoadMatrix, glMatrixMode, glMultMatrix, glPushMatrix

glLoadMatrix

Purpose
Sets the current matrix to the one specified.
Include File
<gl.h>
Variations
void glLoadMatrixd(const GLdouble *m);
void glLoadMatrixf(const GLfloat *m);
Description
Replaces the current Transformation matrix with an arbitrary matrix supplied. It may be more efficient to use some of the other matrix manipulation functions such as glLoadIdentity, glRotate, glTranslate, and glScale.

Parameters

*m
GLdouble or GLfloat: This array represents a 4 x 4 matrix that will be used for the current Transformation matrix. The array is stored in column-major order as 16 consecutive values.
Returns
None.

Example

The following two segments of code are equivalent. They both load the Modelview matrix with the Identity matrix.

    // Efficient way
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // 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);
See Also
glLoadIdentity, glMatrixMode, glMultMatrix, glPushMatrix

glMatrixMode

Purpose
Specifies the current matrix (PROJECTION, MODELVIEW, TEXTURE).
Include File
<gl.h>
Syntax
void glMatrixMode(GLenum mode );
Description
This function is used to determine which matrix stack (GL_MODELVIEW, GL_PROJECTION, or GL_TEXTURE) will be used for matrix operations.

Parameters

mode
GLenum: Identifies which matrix stack will be used for subsequent matrix operations. Any of the values in Table 7-2 are accepted.
Returns
None.

Example

The following common two lines of code select the Modelview matrix stack for matrix operations, then loads the Identity matrix.

    glMatrixMode(GL_MODELVIEW);
    glLoadMatrixf(m);
See Also
glLoadMatrix, glPushMatrix
Table 7-2 Valid Matrix Mode Identifiers for glMatrixMode()

Mode Matrix Stack

GL_MODELVIEW Matrix operations affect the Modelview matrix stack. (Used to move objects around scene.)
GL_PROJECTION Matrix operations affect the Projection matrix stack. (Used to define clipping volume.)
GL_TEXTURE Matrix operations affect the Texture matrix stack. (Manipulates texture coordinates.)

glMultMatrix

Purpose
Multiplies the current matrix by the one specified.
Include File
<gl.h>
Variations
void glMultMatrixd(const GLdouble *m); void glMultMatrixf(const GLfloat *m);
Description
This function multiplies the currently selected matrix stack with the one specified. The resulting matrix is then stored as the current matrix at the top of the matrix stack.

Parameters

*m
GLdouble or GLfloat: This array represents a 4 x 4 matrix that will be multiplied by the current matrix. The array is stored in column-major order as 16 consecutive values.
Returns
None.

Example

The following code creates a Translation matrix and multiplies it by the current Modelview matrix. The newly created matrix replaces the values in the Modelview matrix. The multiplication shown here could also have been accomplished by calling glTranslate(10.0f, 0.0f, 0.0f);.

    // 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);
See Also
glMatrixMode, glLoadIdentity, glLoadMatrix, glPushMatrix

glPopMatrix

Purpose
Pops the current matrix off the matrix stack.
Include File
<gl.h>
Syntax
void glPopMatrix(void);
Description
This function is used to pop the last (topmost) matrix off the current matrix stack. This is most often used to restore the previous condition of the current Transformation matrix if it was saved with a call to glPushMatrix.
Returns
None.

Example

The code below is from the ATOM example program for this chapter. This section saves the Modelview matrix state with a call to glPushMatrix (which is centered in the atom). Then the coordinate system is rotated and translated appropriately to place the electron, which is represented by a small sphere. The coordinate system is then restored by a call to glPopMatrix before the next electron is drawn.

    // First Electron Orbit
    glPushMatrix();
    glRotatef(fElect1, 0.0f, 1.0f, 0.0f);
    glTranslatef(90.0f, 0.0f, 0.0f);
    auxSolidSphere(6.0f);
    glPopMatrix();
See Also
glPushMatrix

glPushMatrix

Purpose
Pushes the current matrix onto the matrix stack.
Include File
<gl.h>
Syntax
void glPushMatrix(void);
Description
This function is used to push the current matrix onto the current matrix stack. This is most often used to save the current Transformation matrix so that it can be restored later with a call to glPopMatrix.
Returns
None.
Example
See glPopMatrix.
See Also
glPopMatrix


Previous Table of Contents Next