Next: 2.5.3 Handling Input Up: 2 OpenGL with Widgets Previous: 2.2 The Motif Drawing

2.3 Drawing Area Callbacks

Applications using the Motif drawing area widget or the OpenGL drawing area widgets for their 3D rendering will want to register routines to handle expose, resize, and input callbacks using XtAddCallback. In paperplane.c, the draw, resize, and input routines handle these callbacks.

paperplane's drawing area adjusts OpenGL's viewport by calling glViewport. Note how the made_current variable is used to protect against calling glViewport before we have done the glXMakeCurrent to bind to the drawing area window. In the X Toolkit, the resize callback can be called before the XtRealizeWidget routine returns. Since the program does not call glXMakeCurrent until after the program returns from XtRealizeWidget, the OpenGL rendering context would not be bound. Calling an OpenGL routine before a context is bound has no effect but generates an ugly warning message. An example of when the resize callback can be called before XtRealizeWidget returns is when a -geometry command line option is specified.

Note that glXMakeCurrent is defined to set a context's viewport to the size of the first window it is bound to. (This happens only on the context's first bind.) This is why paperplane.c makes no initial call to glViewport; glXMakeCurrent sets the viewport implicitly.

The paperplane example uses a single window for OpenGL rendering. For this reason, glXMakeCurrent is called only once to bind the OpenGL context to the window. In a program with multiple OpenGL windows, each expose and resize callback should make sure that glXMakeCurrent is called so that OpenGL rendering goes to the correct window.

The draw callback routine issues the OpenGL commands to draw the scene. If the window is double buffered, glXSwapBuffers swaps the window's buffers. If the context is not direct, glFinish is called to avoid the latency from queuing more than one frame at a time; interactivity would suffer if we allowed more than one frame to be queued. Direct rendering involves direct manipulation of the hardware so it generally has less latency than a potentially networked indirect OpenGL context.

Note that you can render OpenGL into any widget (as long as it is created with an OpenGL capable visual). There is nothing special about the Motif or OpenGL-specific drawing area widgets, though drawing area widgets tend to be the most appropriate widget type for a 3D viewing area.

Next: 2.5.3 Handling Input Up: 2 OpenGL with Widgets Previous: 2.2 The Motif Drawing
Wed Oct 19 18:08:51 PDT 1994