CPEN 461/661, Spring 1997 |
OpenGL Tutorial |
Viewing transformation is analogous to positioning and aiming a camera. Modeling transformation is analogous to positioning and orienting the model, or objects to be drawn. Viewing transformations must precede modeling transformations in OpenGL code.
These transformations are accomplished through the use of matrices. At any point in an OpenGL program one of the following matrices is modifiable; the modelview matrix, the projection matrix, or the texture matrix. The uses of the texture matrix will be explained later in the tutorial. The state variable GL_MATRIX_MODE specifies which matrix is currently modifiable. The default modifiable matrix is the modelview matrix.
The command glMatrixMode(GLenum mode) is used to change the value of the GL_MATRIX_MODE state variable. The possible values for the mode argument are GL_MODELVIEW, GL_PROJECTION, and GL_TEXTURE. When glMatrixMode() is called, all subsequent transformation commands affect the specified matrix.
The command glLoadIdentity() is used to set the currently modifiable matrix to the identity matrix.
The command glTranslate(TYPE x, TYPE y, TYPE z) multiplies the currently modifiable matrix by a matrix that moves an object by the given x, y, and z values (or moves the local coordinate system by the same amounts).
The command glRotate(TYPE angle, TYPE x, TYPE y, TYPE z) multiplies the currently modifiable matrix by a matrix that rotates an object (or the local coordinate system) in a counter clockwise direction about the ray from the origin through the point (x, y, z). The angle parameter specifies the angle of rotation in degrees.
The command glScale(TYPE x, TYPE y, TYPE z) multiplies the currently modifiable matrix by a matrix that stretches, shrinks, or reflects and object along the axes. Each x, y, and z coordinate of every point in the object is multiplied by the corresponding argument x, y, or z. With the local coordinate system approach, the local coordinate axes are stretched by the x, y, and z factors, and the associated object is stretched with them.
Viewport transformation is analogous to choosing the size of the developed photograph. By default, OpenGL sets the viewport to be the entire pixel rectangle of the initial program window. The command glViewport(GLint x, GLint y, GLsizei width, GLsizei height) is used to change the size of the drawing region. The x and y parameters specify the upper left corner of the viewport, and width and height are the size of the viewport rectangle. This command is often used the the function to handle window reshape events.