programming hints

OpenGL performance hints

state change

OpenGL state setting commands can be classified into two different categories. The first category is vertex-data commands. These are the calls that can occur between a glBegin/glEnd pair:

glVertex glColor glIndex glNormal glEdgeFlag glMaterial glTexCoord

The processing of these calls is very fast. Restructuring a program to eliminate some vertex data commands will not significantly improve performance.

The second category is modal state-settingcommands, or sometimes referred to as “mode changes.” These are the commands that:

Turn on/off capabilities,

Change attribute settings for capabilities,

Define lights,

Change matrices,

etc.

These calls cannot occur between a glBegin/glEnd pair. Examples of such commands are:

glEnable(GL_LIGHTING); glFogf(GL_FOG_MODE, GL_LINEAR); glLightf(..); glLoadMatrixf(..);

Changes to the modal state are significantly more expensive to process than simple vertex-data commands. Also, application performance can be optimized by grouping modal-state changes, and by minimizing the number of modal-state changes:

Chapter 5

57