glXIntro -- Introduction to OpenGL in the X window system


OpenGL is a high-performance 3-D-oriented renderer. It is available in the X window system through the GLX extension. Use glXQueryExtension and glXQueryVersion to establish whether the GLX extension is supported by an X server, and if so, what version is supported.

GLX extended servers make a subset of their visuals available for OpenGL rendering. Drawables created with these visuals can also be rendered using the core X renderer and with the renderer of any other X extension that is compatible with all core X visuals.

GLX extends drawables with several buffers other than the standard color buffer. These buffers include back and auxiliary color buffers, a depth buffer, a stencil buffer, and a color accumulation buffer. Some or all are included in each X visual that supports OpenGL.

To render using OpenGL into an X drawable, you must first choose a visual that defines the required OpenGL buffers. glXChooseVisual can be used to simplify selecting a compatible visual. If more control of the selection process is required, use XGetVisualInfo and glXGetConfig to select among all the available visuals.

Use the selected visual to create both a GLX context and an X drawable. GLX contexts are created with glXCreateContext, and drawables are created ith either XCreateWindow or glXCreateGLXPixmap. Finally, bind the context and the drawable together using glXMakeCurrent. This context/drawable pair becomes the current context and current drawable, and it is used by all OpenGL commands until glXMakeCurrent is called with different arguments.

Both core X and OpenGL commands can be used to operate on the current drawable. The X and OpenGL command streams are not synchronized, however, except at explicitly created boundaries generated by calling glXWaitGL, glXWaitX, XSync, and glFlush.


Below is the minimum code required to create an RGBA-format, OpenGL-compatible X window and clear it to yellow. The code is correct, but it does not include any error checking. Return values dpy, vi, cx, cmap, and win should all be tested.

    static int attributeList[] = { GLX_RGBA, None };

    static Bool WaitForNotify(Display *d, XEvent *e, char *arg) {
        return (e->type == MapNotify) && (e->xmap.window == (Window)arg);

    int main(int argc, char **argv) {
        Display *dpy;
        XVisualInfo *vi;
        Colormap cmap;
        XSetWindowAttributes swa;
        Window win;
        GLXContext cx;
        XEvent event;

        /* get a connection */
         dpy = XOpenDisplay(0);

        /* get an appropriate visual */
        vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList);

        /* create a GLX context */
        cx = glXCreateContext(dpy, vi, 0, GL_TRUE);

        /* create a color map */
        cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
                               vi->visual, AllocNone);

        /* create a window */
        swa.colormap = cmap;
        swa.border_pixel = 0;
        swa.event_mask = StructureNotifyMask;
        win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 100, 100,
                            0, vi->depth, InputOutput, vi->visual,
                            CWBorderPixel|CWColormap|CWEventMask, &swa);
        XMapWindow(dpy, win);
        XIfEvent(dpy, &event, WaitForNotify, (char*)win);

        /* connect the context to the window */
        glXMakeCurrent(dpy, win, cx);

        /* clear the buffer */

        /* wait a while */


A color map must be created and passed to XCreateWindow. See the example code above.

A GLX context must be created and attached to an X drawable before OpenGL commands can be executed. OpenGL commands issued while no context/drawable pair is current are ignored.

Exposure events indicate that all buffers associated with the specified window may be damaged and should be repainted. Although certain buffers of some visuals on some systems may never require repainting (the depth buffer, for example), it is incorrect to code assuming that these buffers will not be damaged.

GLX commands manipulate XVisualInfo structures rather than pointers to visuals or visual IDs. XVisualInfo structures contain visual, visualID, screen, and depth elements, as well as other X-specific information.


glFinish, glFlush, glXChooseVisual, glXCopyContext, glXCreateContext, glXCreateGLXPixmap, glXDestroyContext, glXGetClientString, glXGetConfig, glXIsDirect, glXMakeCurrent, glXQueryExtension, glXQueryExtensionsString, glXQueryServerString, glXQueryVersion, glXSwapBuffers, glXUseXFont, glXWaitGL, glXWaitX, XCreateColormap, XreateWindow, XSync

back to the OpenGL index page

© 1995 Uwe Behrens All rights reserved