Just as OpenGL maintains a stack of Projection, Modelview, and Texture matrices, it has a stack for the current rendering state. Unlike the matrix stack, the state stack gives you much more control over exactly what you save (push) or restore (pop) from the stack; see Figure 14-1.
The OpenGL functions to save and restore rendering state attributes are glPushAttrib and glPopAttrib. The glPushAttrib function works a lot like glPushMatrix, except that you can select the state values to put on the stack! To save all of the current rendering state, you would call
Usually, however, you’re only interested in saving a specific set of information, such as the current color, line width, and so forth. OpenGL defines many constants for specific types of information (see Table 14-1). For example:
glPushAttrib(GL_CURRENT_BIT); /* Save current drawing color, etc */
glPushAttrib(GL_LIGHTING_BIT); /* Save current lighting settings */
glPushAttrib(GL_TEXTURING_BIT); /* Save current texturing settings */
Table 14-1 glPushAttrib attribute bits
Accumulation buffer clear value.
Alpha test state, function, and values. Blending state, function, and values. GL_DITHER state. Current drawing buffer(s). Current logical operation state and function. Current RGBA/index clear color and write masks.
Current RGBA color or color index. Current lighting normal and texture coordinate. Current raster position, GL_CURRENT_RASTER_POSITION_VALID, and GL_EDGE_FLAG. GL_DEPTH_BUFFER_BITGL_DEPTH_TEST state, depth buffer function, depth buffer clear value, and GL_DEPTH_WRITEMASK state.
GL_MAP1_x and GL_MAP2_x state, 1D and 2D grid endpoints and divisions, GL_AUTO_NORMAL state.
GL_FOG state, fog color, fog density, linear fog start, linear fog end, fog index, GL_FOG_MODE value.
GL_PERSPECTIVE_CORRECTION_HINT, GL_POINT_SMOOTH_HINT, GL_LINE_SMOOTH_HINT, GL_POLYGON_SMOOTH_HINT, and GL_FG_HINT state.
GL_COLOR_MATERIAL state. GL_COLOR_MATERIAL_FACE value. Color material parameters that are tracking the ambient scene color. GL_LIGHT_MODEL_LOCAL_VIEWER and GL_LIGHT_MODEL_TWO_SIDE values. GL_LIGHTING and GL_LIGHTx states. All light parameters. GL_SHADE_MODEL value.
GL_LINE_SMOOTH and GL_LINE_STIPPLE states. Line stipple pattern and repeat counter. Line width.
GL_STENCIL_TEST state. Stencil function and reference value. Stencil value mask. Stencil fail, pass, and depth buffer pass action. Stencil buffer clear value and writemask.
Enable bits for all texture coordinates. Border color for each texture image. Minification filter and magnification filter. Texture coordinates and wrap modes. Color and mode for each texture environment. GL_TEXTURE_GEN_x, GL_TEXTURE_GEN_MODE settings. glTexGen plane equations.
Coefficients of the six clipping planes, enable bits for the clipping planes, GL_MATRIX_MODE setting, GL_NORMALIZE state.
Depth range, viewport origin, and extent.
Once you have done your rendering, you restore those state bits with glPopAttrib. This function accepts no arguments and restores only what was saved with the last glPushAttrib
OpenGL has a large number of states associated with drawing actions for the basic glBegin/glEnd primitives. Most are saved with a call to glPushAttrib(GL_CURRENT_BIT | GL_LINE_BIT). See Table 14-2.
Table 14-2 Drawing state variables
Do alpha value testing.
Perform pixel blending operations.
Clip drawing operations outside the specified clipping plane.
Cull back- or front-facing polygons.
Dither color values.
Apply a bit pattern to lines.
Do logical operations on pixels when drawing.
Apply a bit pattern to polygons.
Clip drawing outside the glScissor region.
Depth Buffer States
The most common mistake made by beginning OpenGL programmers is to forget to enable depth testing with glEnable(GL_DEPTH_TEST). Without depth testing, hidden surface removal is not performed using the depth buffer (see Chapter 15). Calling glPushAttrib with GL_DEPTH_BUFFER_BIT takes care of saving the GL_DEPTH_TEST state.