Attribute Groups

In "Basic State Management," you saw how to set or query an individual state or state variable. Well, you can also save and restore the values of a collection of related state variables with a single command.

OpenGL groups related state variables into an attribute group. For example, the GL_LINE_BIT attribute consists of five state variables: the line width, the GL_LINE_STIPPLE enable status, the line stipple pattern, the line stipple repeat counter, and the GL_LINE_SMOOTH enable status. (See "Antialiasing" in Chapter 6.) With the commands glPushAttrib() and glPopAttrib(), you can save and restore all five state variables, all at once.

Some state variables are in more than one attribute group. For example, the state variable, GL_CULL_FACE, is part of both the polygon and the enable attribute groups.

In OpenGL Version 1.1, there are now two different attribute stacks. In addition to the original attribute stack (which saves the values of server state variables), there is also a client attribute stack, accessible by the commands glPushClientAttrib() and glPopClientAttrib().

In general, it's faster to use these commands than to get, save, and restore the values yourself. Some values might be maintained in the hardware, and getting them might be expensive. Also, if you're operating on a remote client, all the attribute data has to be transferred across the network connection and back as it is obtained, saved, and restored. However, your OpenGL implementation keeps the attribute stack on the server, avoiding unnecessary network delays.

There are about twenty different attribute groups, which can be saved and restored by glPushAttrib() and glPopAttrib(). There are two client attribute groups, which can be saved and restored by glPushClientAttrib() and glPopClientAttrib(). For both server and client, the attributes are stored on a stack, which has a depth of at least 16 saved attribute groups. (The actual stack depths for your implementation can be obtained using GL_MAX_ATTRIB_STACK_DEPTH and GL_MAX_CLIENT_ATTRIB_STACK_DEPTH with glGetIntegerv().) Pushing a full stack or popping an empty one generates an error.

(See the tables in Appendix B to find out exactly which attributes are saved for particular mask values; that is, which attributes are in a particular attribute group.)

void glPushAttrib(GLbitfield mask);
void glPopAttrib(void);

glPushAttrib() saves all the attributes indicated by bits in mask by pushing them onto the attribute stack. glPopAttrib() restores the values of those state variables that were saved with the last glPushAttrib(). Table 2-7 lists the possible mask bits that can be logically ORed together to save any combination of attributes. Each bit corresponds to a collection of individual state variables. For example, GL_LIGHTING_BIT refers to all the state variables related to lighting, which include the current material color, the ambient, diffuse, specular, and emitted light, a list of the lights that are enabled, and the directions of the spotlights. When glPopAttrib() is called, all those variables are restored.

The special mask, GL_ALL_ATTRIB_BITS, is used to save and restore all the state variables in all the attribute groups.

Table 2-6 : (continued) Attribute Groups

Mask Bit

Attribute Group

GL_ACCUM_BUFFER_BIT

accum-buffer

GL_ALL_ATTRIB_BITS

--

GL_COLOR_BUFFER_BIT

color-buffer

GL_CURRENT_BIT

current

GL_DEPTH_BUFFER_BIT

depth-buffer

GL_ENABLE_BIT

enable

GL_EVAL_BIT

eval

GL_FOG_BIT

fog

GL_HINT_BIT

hint

GL_LIGHTING_BIT

lighting

GL_LINE_BIT

line

GL_LIST_BIT

list

GL_PIXEL_MODE_BIT

pixel

GL_POINT_BIT

point

GL_POLYGON_BIT

polygon

GL_POLYGON_STIPPLE_BIT

polygon-stipple

GL_SCISSOR_BIT

scissor

GL_STENCIL_BUFFER_BIT

stencil-buffer

GL_TEXTURE_BIT

texture

GL_TRANSFORM_BIT

transform

GL_VIEWPORT_BIT

viewport

void glPushClientAttrib(GLbitfield mask);
void glPopClientAttrib(void);

glPushClientAttrib() saves all the attributes indicated by bits in mask by pushing them onto the client attribute stack. glPopClientAttrib() restores the values of those state variables that were saved with the last glPushClientAttrib(). Table 2-7 lists the possible mask bits that can be logically ORed together to save any combination of client attributes.

There are two client attribute groups, feedback and select, that cannot be saved or restored with the stack mechanism.

Table 2-7 : Client Attribute Groups

Mask Bit

Attribute Group

GL_CLIENT_PIXEL_STORE_BIT

pixel-store

GL_CLIENT_VERTEX_ARRAY_BIT

vertex-array

GL_ALL_CLIENT_ATTRIB_BITS

--

can't be pushed or popped

feedback

can't be pushed or popped

select