21. OpenGL Display Lists (Version 2.0)

Introduction

Display lists in OpenGL are objects stored in memory that can be reused multiple times without requiring OpenGL to recreate them. This feature significantly improves performance, especially when rendering complex scenes with numerous objects. By using display lists, you can optimize your code while maintaining flexibility for textures, lighting, and transformations.

In this tutorial, you’ll learn how to create and call display lists to enhance the efficiency of your OpenGL applications.

What Are Display Lists?

A display list stores rendering commands for an object. Once created, it can be called repeatedly, reducing the processing overhead. However:

  • You cannot modify the geometry or shape stored in a display list after it’s created.
  • You can still apply transformations, scaling, and textures to objects stored in a display list.

Creating a Display List

Creating a display list involves the following steps:
1. Generate a display list identifier.
2. Define the list using rendering commands.
3. Finalize the list.

Here’s an example:

  1. // Step 1: Generate a display list identifier  
  2. GLuint cubelist = glGenLists(1);  
  3.   
  4. // Step 2: Define the display list  
  5. glNewList(cubelist, GL_COMPILE);  
  6.     glPushMatrix();  
  7.     glutSolidCube(2); // Draw a solid cube  
  8.     glPopMatrix();  
  9. glEndList(); // Step 3: Finalize the display list  

Calling a Display List

Once the display list is created, you can call it anytime using:

  1. glCallList(cubelist); // Call the display list  

Ensure that the list is created before calling it; otherwise, nothing will be displayed.

Tutorial Code

Below is the complete code demonstrating the creation and usage of a display list for a cube:

  1. #include <GL/gl.h>  
  2. #include <GL/glut.h>  
  3.   
  4. GLuint cubelist; // Display list identifier  
  5.   
  6. // Function to create a display list for a cube  
  7. void createcube(void) {  
  8.     cubelist = glGenLists(1); // Generate the display list  
  9.     glNewList(cubelist, GL_COMPILE); // Begin the display list  
  10.     glPushMatrix();  
  11.     glutSolidCube(2); // Draw a solid cube  
  12.     glPopMatrix();  
  13.     glEndList(); // Finalize the display list  
  14. }  
  15.   
  16. // Initialization function  
  17. void init(void) {  
  18.     glEnable(GL_DEPTH_TEST); // Enable depth testing  
  19.     glEnable(GL_LIGHTING);   // Enable lighting  
  20.     glEnable(GL_LIGHT0);     // Enable LIGHT0 (diffuse light)  
  21.     glShadeModel(GL_SMOOTH); // Set shading to smooth  
  22.     createcube();            // Create the display list  
  23. }  
  24.   
  25. // Display callback function  
  26. void display(void) {  
  27.     glClearColor(0.0, 0.0, 0.0, 1.0); // Clear the screen to black  
  28.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear buffers  
  29.     glLoadIdentity();  
  30.     glTranslatef(0.0, 0.0, -5.0); // Translate the scene  
  31.   
  32.     glCallList(cubelist); // Call the display list  
  33.     glutSwapBuffers();    // Swap the buffers  
  34. }  
  35.   
  36. // Reshape callback function  
  37. void reshape(int w, int h) {  
  38.     glViewport(0, 0, (GLsizei)w, (GLsizei)h);  
  39.     glMatrixMode(GL_PROJECTION);  
  40.     glLoadIdentity();  
  41.     gluPerspective(60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);  
  42.     glMatrixMode(GL_MODELVIEW);  
  43. }  
  44.   
  45. // Keyboard callback function  
  46. void keyboard(unsigned char key, int x, int y) {  
  47.     if (key == 27) { // Escape key  
  48.         glutLeaveGameMode(); // Exit fullscreen mode  
  49.         exit(0); // Quit the program  
  50.     }  
  51. }  
  52.   
  53. // Main function  
  54. int main(int argc, char **argv) {  
  55.     glutInit(&argc, argv);  
  56.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); // Double buffering with depth testing  
  57.     glutGameModeString("1024x768:32@75"); // Fullscreen mode settings  
  58.     glutEnterGameMode(); // Enter fullscreen mode  
  59.   
  60.     init(); // Initialize settings  
  61.     glutDisplayFunc(display); // Set display callback  
  62.     glutIdleFunc(display);    // Set idle callback  
  63.     glutReshapeFunc(reshape); // Set reshape callback  
  64.     glutKeyboardFunc(keyboard); // Set keyboard callback  
  65.     glutMainLoop(); // Enter the main event loop  
  66.   
  67.     return 0;  
  68. }  

If you have any questions or run into issues, feel free to email me at swiftless@gmail.com. Happy coding!

  • March 25, 2010
  • 5