Featured Posts

8. Bump Mapping in GLSL8. Bump Mapping in GLSL Introduction Bump mapping is essential in todays computer games, and computer graphics in general. Would you like to know the best thing about it? It is extremely simple to implement. Bump mapping works...

Read more

Swiftless GLSL Shader DeveloperSwiftless GLSL Shader Developer Swiftless GLSL Shader Developer   Version 0.1a Currently Swiftless GLSL Shader Developer is in it's first public release, and is currently in alpha status, meaning it is not complete and may contain...

Read more

Wordpress Optimization Wordpress Website Optimizations Introduction Wordpress itself is a fairly wonderful tool. Since switching to it, I find it is a lot quicker to make changes to my website and it is also quicker to get...

Read more

36. OpenGL Framebuffers36. OpenGL Framebuffers Introduction Frame buffers are one of those mythical things that we have all heard of, but many beginner OpenGL developers avoid because there is not much information about them, and they can be confusing...

Read more

1. Terrain Class1. Terrain Class Terrain is one of those things that so far, hasn't been perfectly recreated in computer graphics. But it is almost there! Looking over a beautiful landscape can be one of the most amazing feelings in the...

Read more

  • Prev
  • Next

21. OpenGL Display Lists

Posted on : 25-03-2010 | By : Swiftless | In : OpenGL

Tags: , , , ,

3

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;
}

VN:F [1.9.3_1094]
Please rate so I know where to improve the site. 1 means needs a lot of improvement, 10 means perfect. If you leave a low rating, please state why. I don't want people just coming to bash the site.
Rating: 7.6/10 (5 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)
21. OpenGL Display Lists, 7.6 out of 10 based on 5 ratings

Related posts:

  1. 31. OpenGL Sphere Creation
  2. 32. OpenGL Particle Engine
  3. 20. OpenGL MipMap Generation
  4. 16. OpenGL Texturing
  5. 35. OpenGL Tiling Engine

Comments (3)

Hey people,
I can’t enter into the GameMode, the ms-dos window appears with a message that “unrecognized string in game mode” , “cannot create windows in game mode” . i think its related to the glutGameModeString(“…”);, i copied the string from here too , still got the error
Please help me.

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

It means either the resolution you are trying to use or the refresh rate are not supported by your hardware, try changing the 75 to a 60 and running it.

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

These are great tutorials, you explain things very well :D

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: +1 (from 1 vote)

Write a comment

Improve the web with Nofollow Reciprocity.