21. OpenGL Display Lists

An opengl display list is an object created and stored in the memory that can be called multiple times without opengl having to recreate the object again and again. This saves processing time and thus increases your frames
per second in your application when you move into worlds with multiple objects. In this OpenGL tutorial I will be teaching you how to set up and call these display lists to help optimise your code. I will be doing this with the glut cube but you may not notice the difference unless you change to code to create alot of cubes.

A display list can be treated just like any other object, you
can change its texture, lighting abilities, size, etc.

Although once the list has been created you cannot edit
the actual shape of the object in the list. You can scale it
with the scale command, but you cannot change the vertices etc
without recreating the whole list from scratch.

Now to create a list we first have to setup and unsigned integer
to hold the list. I have done it with:
GLuint cubelist;

next we move onto acctually creating the list. When doing this
there are 2 steps. The first is to set the list the generate a list
with:
cubelist = glGenLists(1);

then we tell it to make a new list using cubelist:
glNewList(cubelist,GL_COMPILE);

after this come whatever code you need for your object.

then after you have done all the code you need for your object
to function, just call:
glEndList();
to end the creation of the current active list.

and to call the list whenever you need it, you simply use
glCallList(cubelist);

and that is it, just remember to create the list before you call it
otherwise nothing will show.

If you have any queries regarding this tutorial, please email me at swiftless@gmail.com

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
    #include <GL/gl.h>
#include <GL/glut.h>
#include <stdlib.h>

GLuint cubelist; //we are going to hold our list in here

//create the cube display list
void createcube (void) {
    cubelist = glGenLists(1); //set the cube list to Generate a 
List

    glNewList(cubelist,GL_COMPILE); //compile the new list
    glPushMatrix();
    glutSolidCube(2); //draw the cube
    glPopMatrix();
    glEndList(); //end the list
}

void init (void) {
    glEnable (GL_DEPTH_TEST); //enable the depth testing
    glEnable (GL_LIGHTING); //enable the lighting
    glEnable (GL_LIGHT0); //enable LIGHT0, our Diffuse Light
    glShadeModel (GL_SMOOTH); //set the shader to smooth shader

    createcube(); //call the command to create the cube
}

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 the color buffer and the depth buffer

    glLoadIdentity();  
    glTranslatef(0,0,-5);
    glCallList(cubelist); //call the cube list
    glutSwapBuffers(); //swap the buffers
}

void reshape (int w, int h) {
    glViewport (0, 0, (GLsizei)w, (GLsizei)h); //set the viewport
 to the current window specifications

    glMatrixMode (GL_PROJECTION); //set the matrix to projection

    glLoadIdentity ();
    gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0); //set the perspective (angle of sight, width, height, , depth
)

    glMatrixMode (GL_MODELVIEW); //set the matrix back to model

}

void keyboard (unsigned char key, int x, int y) {
    if (key==27)
    {
    glutLeaveGameMode(); //set the resolution how it was
    exit(0); //quit the program
    }
}

int main (int argc, char **argv) {
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH); //set 
the display to Double buffer, with depth

    glutGameModeString( “1024×768:32@75); //the settings 
for fullscreen mode

    glutEnterGameMode(); //set glut to fullscreen using the 
settings in the line above

    init (); //call the init function
    glutDisplayFunc (display); //use the display function to 
draw everything

    glutIdleFunc (display); //update any variables in display,
 display can be changed to anyhing, as long as you move the 
variables to be updated, in this case, angle++;

    glutReshapeFunc (reshape); //reshape the window accordingly

    glutKeyboardFunc (keyboard); //check the keyboard
    glutMainLoop (); //call the main loop
    return 0;
}

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>