13.016 Introduction to Geometric Modeling and Computation


Matrix Manipulation

The various types of transformations that we want to use can be performed mathematically by creating the appropriate transformation matrix.

Some of the transformations only require a 3x3 matrix, but one transformation, translation, requires a 4x4 matrix. Because it is conceptually easier to understand these graphics operations with a single unified mathematical model, OpenGL uses a 4x4 matrix for all operations. Also, many display devices can perform these transformations efficiently using hardware optimized for 4x4 matrix operations.

Because all coordinates are stored as 4D values, we can manipulate the coordinates with the 4x4 transformation matrix. For a homogeneous coordinate v and a matrix M:

  v' = Mv

The OpenGL state maintains two separate transformation matrices, the modelview matrix and the projection matrix.

To specify which matrix you want to modify, use:

    glMatrixMode(mode);
where mode is either GL_MODELVIEW or GL_PROJECTION.

The current value of the selected matrix can be initialized by:

    glLoadIdentity();
which sets the matrix to the 4x4 identity matrix.

  M = I =  1 0 0 0
           0 1 0 0
           0 0 1 0
           0 0 0 1

Recall that v = Iv.

To replace the current matrix C with a completely new matrix M:

    float m[16];        /* or double m[16]; */
    glLoadMatrixf(m);   /* or glLoadMatrixd(m); */
  C' = M

You can multiply a new matrix M onto the current matrix C:

    float m[16];        /* or double m[16]; */
    glMultMatrixf(m);   /* or glMultMatrixd(m); */
  C' = CM

Note that the order of matrix multiplication is important, and in general, does not commute, i.e. CM != MC. In OpenGL, new matrices M are always multiplied onto the current matrix C from the right.

The current matrix can be saved and restored by pushing to or popping from the matrix stack:

    glPushMatrix();     /* save the current matrix on the stack */
    glPopMatrix();      /* restore the current matrix from the stack */

To receive a copy of the current matrix, use the following functions:

    float matrix[16];
    glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
    glGetFloatv(GL_PROJECTION, matrix);

    /* there is also a glGetDoublev(...); */


On to translation...
Back to the viewing pipeline...
Back to the overview...