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:

// Step 1: Generate a display list identifier
GLuint cubelist = glGenLists(1);

// Step 2: Define the display list
glNewList(cubelist, GL_COMPILE);
    glPushMatrix();
    glutSolidCube(2); // Draw a solid cube
    glPopMatrix();
glEndList(); // Step 3: Finalize the display list

Calling a Display List

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

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:

#include <GL/gl.h>
#include <GL/glut.h>

GLuint cubelist; // Display list identifier

// Function to create a display list for a cube
void createcube(void) {
    cubelist = glGenLists(1); // Generate the display list
    glNewList(cubelist, GL_COMPILE); // Begin the display list
    glPushMatrix();
    glutSolidCube(2); // Draw a solid cube
    glPopMatrix();
    glEndList(); // Finalize the display list
}

// Initialization function
void init(void) {
    glEnable(GL_DEPTH_TEST); // Enable depth testing
    glEnable(GL_LIGHTING);   // Enable lighting
    glEnable(GL_LIGHT0);     // Enable LIGHT0 (diffuse light)
    glShadeModel(GL_SMOOTH); // Set shading to smooth
    createcube();            // Create the display list
}

// Display callback function
void display(void) {
    glClearColor(0.0, 0.0, 0.0, 1.0); // Clear the screen to black
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear buffers
    glLoadIdentity();
    glTranslatef(0.0, 0.0, -5.0); // Translate the scene

    glCallList(cubelist); // Call the display list
    glutSwapBuffers();    // Swap the buffers
}

// Reshape callback function
void reshape(int w, int h) {
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
    glMatrixMode(GL_MODELVIEW);
}

// Keyboard callback function
void keyboard(unsigned char key, int x, int y) {
    if (key == 27) { // Escape key
        glutLeaveGameMode(); // Exit fullscreen mode
        exit(0); // Quit the program
    }
}

// Main function
int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); // Double buffering with depth testing
    glutGameModeString("1024x768:32@75"); // Fullscreen mode settings
    glutEnterGameMode(); // Enter fullscreen mode

    init(); // Initialize settings
    glutDisplayFunc(display); // Set display callback
    glutIdleFunc(display);    // Set idle callback
    glutReshapeFunc(reshape); // Set reshape callback
    glutKeyboardFunc(keyboard); // Set keyboard callback
    glutMainLoop(); // Enter the main event loop

    return 0;
}

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

  • March 25, 2010
  • 5