DISPLAY LISTS

REFERENCE: [Guide, Ch.4].
A display list is a cache of commands.
-- once created, it cannot be modified.
-- E.g., if a definition in a display list uses an integer variable xval, then only the current value of xval is stored. Subsequent changes to xval has no effect on the display list>
-- the list resides with the graphics Server, which may speed up performance over a network.
-- only certain commands can be stored in the display list. E.g., glGet*() and glFlush() are not.
To define a display list:
#define MY_OBJECT_ID 1
glNewList(MY_OBJECT_ID, GL_COMPILE);
    glBegin(GL_POLYGON);
        glVertex3f(0.0,0.0,0.0);
        glVertex3f(1.0,0.0,0.0);
        glVertex3f(0.0,1.0,0.0);
        glVertex3f(1.0,1.0,0.0);
    glEnd();
glEndList();
The option GL_COMPILE says do not immediately execute.
To execute the display list:
glCallList(MY_OBJECT_ID);
You can iterate and do matrix transformations inside the display list:
buildCircle(GLint id) {
    GLint i;
    GLfloat cosine, sine;
    glNewList(id, GL_COMPILE);
      glBegin(GL_POLYGON);
        for(i=0; i<100; i++){
          cosine = cos(i*2*PI/100.0);
          sine = sin(i*2*PI/100.0);
          glVertex2f(cosine,sine);
        }
      glEnd();
      glTranslatef(2.0, 0.0, 0.0);
    glEndList();
}
E.G., to draw a row of 99 touching circles:
buildCircle(MY_CIRCLE_ID);
for (i=0; i<99; i++)
      glCallList(MY_CIRCLE_ID);
Display lists cannot be nested.
But you can have Hierarchical Display Lists in which a display list calls other display lists.
-- E.g., to construct a complex object with many parts.
glNewList(BIKE_ID, GL_COMPILE);
      glCallList(bicycleFrame);
      glTranslatef(1.0, 0.0, 0.0);
      glCallList(Wheel);
      glTranslatef(3.0, 0.0, 0.0);
      glCallList(Wheel);
glEndList();
To avoid the OpenGL state outside the display list to be affected by the list,
-- use glPushMatrix()/glPopMatrix()
-- and glPushAttrib()/glPopAttrib().
Automatic management of display list indices:
Executing Multiple Display Lists
-- put the list indices into an array and call glCallLists().
-- E.g., to display a string of letters.
-- glListBase() is also useful (it specifies an offset for indices).