Display Lists
Display lists are an easy way to optimize your OpenGL rendering by caching and pre-compiling the rendering calls.
As explained by Lighthouse 3D:
When you create a Display List the OpenGL commands within are "precompiled"
and, if possible, stored in the graphics card memory. Therefore, in general
the execution of a Display List is faster than the execution of the commands
contained in it. The commands included in a display list can be optimized by
the driver, namely some geometric transformations can be pre computed.
Furthermore the display lists are stored in the graphics card memory.
If you're rendering an object that changes frequently, then display lists won't help much because you'll constantly have to update or recreate the rendering calls within the display list.
However, they can really speed things up when rendering an object that changes infrequently (in terms of geometry, materials, etc.). For example, in the first assignment, a fractal doesn't change unless the number of iterations is changed.
The OpenGL functions related to display lists are:
GLuint glGenLists(GLsizei n);void glNewList (GLuint id, GLenum mode);void glEndList (void);void glCallList(GLuint listID);void glDeleteLists(GLuint id, GLsizei n);
We'll see how to use them below.
Display lists are easy to integrate into existing code, making them very useful. Consider a function that draws a tire of a car:
void DrawTire()
{
// Set rendering parameters
glEnable(GL_LIGHTING);
...
// Draw geometry
glColor3f(0.1, 0.1, 0.1);
glBegin(GL_TRIANGLES);
...
glEnd();
}
We could use this in car-drawing code like this:
void DrawCar() { for (int i=0; i < numTires; i++) { // Move to position of tire i glTranslatef(tires[i].x, tires[i].y, tires[i].z); // Draw tire DrawTire(); } // Draw rest of car ... }
Now, a version using display lists would look like this:
int DrawTireDL()
{
// Get a display list ID
int ID = glGenLists(1);
// Start compiling the list
glNewList(ID, GL_COMPILE); // or GL_COMPILE_AND_EXECUTE
// RENDER AS BEFORE
...
// End the list
glEndList();
}
and
void DrawCar() { int tireDL = DrawTireDL(); for (int i=0; i < numTires; i++) { // Move to position of tire i glTranslatef(tires[i].x, tires[i].y, tires[i].z); // Draw tire glCallList(tireDL); } // Draw rest of car ... }
When you don't need the display list anymore, you can delete it to free up the ID:
glDeleteLists(tireDL, 1); // Delete 1 display list, starting at ID tireDL
That's pretty much all there is to it. There are some commands that can't be included in a display list (see here), but it shouldn't affect you much.
Further reading:
