Share via


OpenGL Performance Tips

These programming practices optimize your application's performance:

  • Use glColorMaterial when only a single material property is being varied rapidly (at each vertex, for example). Use glMaterial for infrequent changes, or when more than a single material property is being varied rapidly.

  • Use glLoadIdentity to initialize a matrix, rather than loading your own copy of the identity matrix.

  • Use specific matrix calls such as glRotate, glTranslate, and glScale, rather than composing your own rotation, translation, and scale matrices and calling glMultMatrix.

  • Use glPushAttrib and glPopAttrib to save and restore state values. Use query functions only when your application requires the state values for its own computations.

  • Use display lists to encapsulate potentially memory-intensive state changes. For example, place all the glTexImage calls required to completely specify a texture (and perhaps the associated glTexParameter, glPixelStore, and glPixelTransfer calls as well) into a single display list. Call this display list to select the texture.

  • Use display lists to encapsulate the rendering calls of rigid objects that will be drawn repeatedly.

  • To minimize network bandwidth in client/server environments, use evaluators even for simple surface tessellations.

  • If possible, to avoid the overhead of GL_NORMALIZE, provide unit-length normals. Because glScale almost always requires enabling GL_NORMALIZE, avoid using glScale when doing lighting.

  • If smooth shading isn't required, set glShadeModel to GL_FLAT.

  • Use a single glClear call per frame, if possible. Do not use glClear to clear small subregions of the buffers; use it only to clear the buffer completely or nearly completely.

  • To draw multiple independent triangles, use a single call rather than multiple calls to glBegin(GL_TRIANGLES) or a call to glBegin(GL_POLYGON). Similarly:

    To draw a single triangle, use GL_TRIANGLES rather than GL_POLYGON.

    Use a single call to glBegin(GL_QUADS) rather than calling glBegin(GL_POLYGON) repeatedly.

    Use a single call to glBegin(GL_LINES) to draw multiple independent line segments, rather than calling glBegin(GL_LINES) multiple times.

  • In general, use the vector forms of commands to pass precomputed data, and use the scalar forms of commands to pass values that are computed near call time.

  • Avoid making redundant mode changes, such as setting the color to the same value between each vertex of a flat-shaded polygon.

  • When drawing or copying images, disable rasterization and per-fragment operations, to optimize resources. OpenGL can apply textures to pixel images.