Next: 5 Comparing OpenGL to Up: 4 A Simple Example Previous: 2.1 Initialization

4.2 Scene Update

The redraw routine does all the OpenGL rendering. The code is slightly complicated by constructing a display list to draw the cube. The first time redraw is called, glNewList and glEndList are used to construct a display list for the object to be rendered. Subsequent redraws call the display list instead of rendering the object each time.

Creating a display list potentially allows improved performance since the commands can be compiled for faster execution. In the case of OpenGL across a network, display lists save having to send all the commands to render the scene whenever the window is redrawn.

The commands to render the object consist of four 3D rectangles of different colors. Notice the rectangles are generated by first calling glBegin(GL_QUADS) and ended with glEnd. Each rectangle is specified by four glVertex3f calls that specify the four vertexes of each rectangle. The glColor3f invocations tell what color each rectangle should be rendered. Figure 5 shows how the program looks.

If the window is double buffered, glXSwapBuffers is called on the window. By default rendering to double buffered windows takes place in the non-visible back buffer. Swapping buffers will quickly swap the front and back buffers avoiding any visual artifacts (the contents of the back buffer should considered undefined after a swap). In effect, the rendering of each frame can be done ``behind the scenes.''

glFlush is called to ensure that the OpenGL rendering commands are actually sent to the graphics hardware. A flush is implicitly done by glXSwapBuffers so the glFlush is only needed explicitly in the single buffered case.


mjk@asd.sgi.com
Wed Oct 19 18:06:42 PDT 1994