OpenGL Tutorial #4
-- "Display lists" --

paradox/vivid

Introduction

Welcome to tutorial number four! I'll cover two fundamental features of OpenGL, display lists and the matrix stack.

WTF are Display Lists?

Ok, say for example you wanted to have two cubes rotating on the screen, say zbuffered with each other (which looks qeWI). How would you do it? no, not by:

glBegin(GL_QUADS);
   // those damn vertices
glEnd();

glBegin(GL_QUADS);
   // those damn vertices
glEnd();

No.

Actually we'd create a display list, a display list is just a list of vertices we can use to draw a 3d object (I'll explain more below). Here's how it's done. GLuint Cube; // ID for our Cube display list.

Cube = glGenLists(1); // generate one display list.
glNewList(Cube);
glBegin(GL_QUADS);

   // now give it those damn vertices.

glEnd();
glEndList();

Now to display two cubes we would do this:

glCallList(Cube);
// rotate this one differently
glCallList(Cube);

And there we have it!

What about two different objects? Say we wanted two cubes zbuffered and a pyramid beside them. We would do this:

GLuint Cube; // ID for our Cube display list
GLuint Pyramid; // ID for our Pyrmaid display list

Cube = glGenLists(2); // 2 display lists this time.
glNewList(Cube, GL_COMPILE);
glBegin(GL_QUADS);
   // now give it those damn vertices.
glEnd();
glEndList();

Pyramid = Cube + 1; // Pyramid id is cube's id + 1
glNewList(Pyramid, GL_COMPILE);
glBegin(GL_TRIANGLES);
   // now give it those damn vertices.
glEnd();
glEndList();

Then we do the same except this time, we do this as well as the others.

glCallList(Pyramid);

You can set lots of things inside display lists, I've played about and you can bind textures (using glBindTexture), for example. Display lists are really lists of code which can be executed by calling glCallList, they are much faster than standard vertex specification because of these vertex cache buffer thingies I know nothing about, I like them because they structure my code and make it easier to read (and because they're faster).

Notes On This Section

(i) Display lists are very important, make sure you know how to use them (like it's so hard).

(ii) Experiment with this stuff yourself, as always.

(iii) As I said, Display lists are really lists of code, you can have functions like glTranslatef, glBindTexture in there (as well as glBegin, glVertex etc).

The Matrix Stack

The matrix stack under OpenGL is a way of saving information about 3d scenes, like multiple camera views.

We do:

glLoadIdentity();
glTranslatef(..., ..., ...);

Whereas we could do:

glPushMatrix();
glTranslatef(blabla);
glRotatef(blabla);
glPopMatrix();

For example to do two cubes z buffered with each other, we would glTranslate them to whereever we want.

Then we have:

void DrawScene()
{
   glPushMatrix();
      glRotatef(whatever we want this cube to do);
   glPopMatrix();
   glPushMatrix();
      glRotatef(whatever we want this cube to do);
   glPopMatrix();
   return;
}

The matrix stack works like any conventional stack does.

Conclusion

Well, I hope you learned from this tutorial. Drive 3D cards harder than is healthy.

And that's it...

-- diviv\xodarap