Saving and Restoring Sets of State Variables

You can save and restore the values of a collection of state variables on an attribute stack with the glPushAttrib and glPopAttrib functions. The attribute stack has a depth of at least 16. To obtain the actual depth, use GL_MAX_ATTRIB_STACK_DEPTH with glGetIntegerv. Pushing a full stack or popping an empty one generates an error.

It's generally faster to use glPushAttrib and glPopAttrib than to get and restore the values yourself. Some values might be pushed and popped in the hardware, and saving and restoring them can be expensive. Also, if you're operating on a remote client, all the attribute data must be transferred across the network connection and back as it's saved and restored. However, your OpenGL implementation keeps the attribute stack on the server, avoiding unnecessary network delays.

The prototype of glPushAttrib is:

void glPushAttrib(GLbitfield mask);

Using glPushAttrib saves all the attributes indicated by bits in mask by pushing them onto the attribute stack. For a list of the possible mask bits you can logically OR together to save any combination of attributes, see Attribute Groups. 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 you call glPopAttrib, all those variables are restored. To find out exactly which attributes are saved for particular mask values, see OpenGL State Variables.