Next: 4.2 Scene Update
Up: 4 A Simple Example
Previous: 4 A Simple Example
The following describes the steps involved in setting up a window
to render OpenGL into it. The numbers listed correspond to numbers
in the comments of the OpenGL program in Appendix A.
- As in all X programs, XOpenDisplay should be called
to open a connection to the X server.
- Make sure the OpenGL GLX extension is supported by the
X server.
- Before creating the window, the program needs to select
an appropriate visual. The GLX routine glXChooseVisual
makes it easy to find the right visual. In the example,
an RGBA (and TrueColor) visual with a depth buffer is
desired and if possible, it should support double buffering.
- Create an OpenGL rendering context by calling glXCreateContext.
- Create a window with the selected visual. Most X programs
always use the default visual but OpenGL programmers will
need to be comfortable with using visuals other than the
default. XCreateWindow is called.
- Bind the rendering context to the window using glXMakeCurrent.
Subsequent OpenGL rendering commands will use the current window
and rendering context.
- To display the window, XMapWindow should be called.
- Set the desired OpenGL state. In this example, depth buffering
is enabled, the clear color is set to black, and the 3D viewing
volume is specified.
- Begin dispatching X events.
Button presses change the angle of rotation for the object to be viewed and
cause a redraw. Expose events also cause a redraw (without
changing the rotation). Window resizes call glViewport to
ensure the OpenGL viewport corresponds to the maximum dimensions
of the window.