Contents / Previous / Next


Creating and Executing a Display List

glNewList() and glEndList() are used to begin and end the definition of a display list, which is then invoked by supplying its identifying index with glCallList().

For example, suppose you want to apply a transformation to some geometric objects and then draw the result. Your code may look like this:

 glNewList(1, GL_COMPILE); 
 draw_some_geometric_objects(); 
 glEndList();

 glLoadMatrix(M);
 glCallList(1);


Commands for display lists:
  void glNewList (GLuint list, GLenum mode);
Specifies the start of a display list. OpenGL routines that are called subsequently (until glEndList() is called to end the display list) are stored in a display list, except for a few restricted OpenGL routines that can't be stored. (Those restricted routines are executed immediately, during the creation of the display list.) list is a nonzero positive integer that uniquely identifies the display list. The possible values for mode are GL_COMPILE and GL_COMPILE_AND_EXECUTE. Use GL_COMPILE if you don't want the OpenGL commands executed as they're placed in the display list; to cause the commands to be executed immediately as well as placed in the display list for later use, specify GL_COMPILE_AND_EXECUTE.

  void glEndList (void);
Marks the end of a display list.

  void glCallList (GLuint list);
This routine executes the display list specified by list. The commands in the display list are executed in the order they were saved, just as if they were issued without using a display list. If list hasn't been defined, nothing happens.


Managing Display List Indices

Each display list is identified by an integer index. When creating a display list, you want to be careful that you don't accidentally choose an index that's already in use, thereby overwriting an existing display list.

Use glGenLists() to obtain unused display-list indices or use glIsList() to determine whether a specific index is in use.

GLuint glGenLists(GLsizei range);
Allocates range number of contiguous, previously unallocated display-list indices. The integer returned is the index that marks the beginning of a contiguous block of empty display-list indices. The returned indices are all marked as empty and used, so subsequent calls to glGenLists() don't return these indices until they're deleted. Zero is returned if the requested number of indices isn't available, or if range is zero.

In the following example, a single index is requested, and if it proves to be available, it's used to create a new display list:

listIndex = glGenLists(1);
if (listIndex != 0) {
   glNewList(listIndex,GL_COMPILE);
      ...
   glEndList();
}

Note: Zero is not a valid display-list index.

GLboolean glIsList(GLuint list);
Returns GL_TRUE if list is already used for a display list and GL_FALSE otherwise.

You can explicitly delete a specific display list or a contiguous range of lists with glDeleteLists(). Using glDeleteLists() makes those indices available again.

void glDeleteLists(GLuint list, GLsizei range);
Deletes range display lists, starting at the index specified by list. An attempt to delete a list that has never been created is ignored.