Open GL Super Bible

Previous Table of Contents Next


Saving and Restoring States

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.


Figure 14-1  OpenGL attribute stack

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

glPushAttrib(GL_ALL_ATTRIB_BITS);

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

Attribute Bit Description

GL_ACCUM_BUFFER_BIT Accumulation buffer clear value.
GL_COLOR_BUFFER_BIT 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.
GL_CURRENT_BIT 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_ENABLE_BIT GL_ALPHA_TEST, GL_AUTO_NORMAL, and GL_BLEND state. User-defined clipping plane state. GL_COLOR_MATERIAL, GL_CULL_FACE, GL_DEPTH_TEST, GL_DITHER, GL_FOG, GL_LIGHTi, GL_LIGHTING, GL_LINE_SMOOTH, GL_LINE_STIPPLE, GL_LOGIC_OP, GL_MAP1_x, GL_MAP2_x, GL_NORMALIZE, GL_POINT_SMOOTH, GL_POLYGON_SMOOTH, GL_POLYGON_STIPPLE, GL_SCISSOR_TEST, GL_STENCIL_TEST, GL_TEXTURE_1D, GL_TEXTURE_2D, and GL_TEXTURE_GEN_x states.
GL_EVAL_BIT GL_MAP1_x and GL_MAP2_x state, 1D and 2D grid endpoints and divisions, GL_AUTO_NORMAL state.
GL_FOG_BIT GL_FOG state, fog color, fog density, linear fog start, linear fog end, fog index, GL_FOG_MODE value.
GL_HINT_BIT GL_PERSPECTIVE_CORRECTION_HINT, GL_POINT_SMOOTH_HINT, GL_LINE_SMOOTH_HINT, GL_POLYGON_SMOOTH_HINT, and GL_FG_HINT state.
GL_LIGHTING_BIT 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_BIT GL_LINE_SMOOTH and GL_LINE_STIPPLE states. Line stipple pattern and repeat counter. Line width.
GL_LIST_BIT GL_LIST_BASE value.
GL_PIXEL_MODE_BIT GL_RED_BIAS, GL_RED_SCALE, GL_GREEN_BIAS, GL_GREEN_SCALE, GL_BLUE_BIAS, GL_BLUE_SCALE, GL_ALPHA_BIAS, GL_ALPHA_SCALE, GL_DEPTH_BIAS, GL_DEPTH_SCALE, GL_INDEX_OFFSET, GL_INDEX_SHIFT, GL_MAP_COLOR, GL_MAP_DEPTH, GL_ZOOM_X, GL_ZOOM_Y, and GL_READ_BUFFER settings.
GL_POINT_BIT GL_POINT_SMOOTH state, point size.
GL_POLYGON_BIT GL_CULL_FACE, GL_CULL_FACE_MODE, GL_FRONT_FACE, GL_POLYGON_MODE, GL_POLYGON_SMOOTH, GL_POLYGON_STIPPLE.
GL_POLYGON_STIPPLE_BIT Polygon stipple image.
GL_SCISSOR_BIT GL_SCISSOR_TEST state, scissor box.
GL_STENCIL_BUFFER_BIT 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.
GL_TEXTURE_BIT 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.
GL_TRANSFORM_BIT Coefficients of the six clipping planes, enable bits for the clipping planes, GL_MATRIX_MODE setting, GL_NORMALIZE state.
GL_VIEWPORT_BIT 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

Drawing States

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

State Variable Description

GL_ALPHA_TEST Do alpha value testing.
GL_BLEND Perform pixel blending operations.
GL_CLIP_PLANEx Clip drawing operations outside the specified clipping plane.
GL_CULL_FACE Cull back- or front-facing polygons.
GL_DITHER Dither color values.
GL_LINE_SMOOTH Anti-alias lines.
GL_LINE_STIPPLE Apply a bit pattern to lines.
GL_LOGIC_OP Do logical operations on pixels when drawing.
GL_POINT_SMOOTH Anti-alias points.
GL_POLYGON_SMOOTH Anti-alias polygons.
GL_POLYGON_STIPPLE Apply a bit pattern to polygons.
GL_SCISSOR_TEST 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.


Previous Table of Contents Next