MirOS Manual: glXIntro(3)


GLXINTRO()          UNIX Programmer's Manual           GLXINTRO()

NAME

     glXIntro - Introduction to OpenGL in the X window system

OVERVIEW

     OpenGL (called GL in other pages) is a high-performance 3D-
     oriented renderer. It is available in the X window system
     through the GLX extension. To determine whether the GLX
     extension is supported by an X server, and if so, what ver-
     sion is supported, call glXQueryExtension and glXQueryVer-
     sion.

     GLX extended servers make a subset of their visuals avail-
     able 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 compati-
     ble with all core X visuals.

     GLX extends drawables with several buffers other than the
     standard color buffer. These buffers include back and auxi-
     liary 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 compati-
     ble 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 with either XCreateWindow or
     glXCreateGLXPixmap. Finally, bind the context and the draw-
     able 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.

EXAMPLES

     Below is the minimum code required to create an RGBA-format,
     X window that's compatible with OpenGL and to 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.

MirOS BSD #10-current   Printed 20.2.2012                       1

GLXINTRO()          UNIX Programmer's Manual           GLXINTRO()

     #include <GL/glx.h> #include <GL/gl.h> #include <unistd.h>

     static int attributeListSgl[] = {      GLX_RGBA,
          GLX_RED_SIZE,   1,      GLX_GREEN_SIZE, 1,
          GLX_BLUE_SIZE,  1,      None };

     static int attributeListDbl[] = {      GLX_RGBA,
          GLX_DOUBLE_BUFFER,      GLX_RED_SIZE,   1,
          GLX_GREEN_SIZE, 1,      GLX_BLUE_SIZE,  1,      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;
         int swap_flag = FALSE;

         dpy = XOpenDisplay(0);

         vi = glXChooseVisual(dpy, DefaultScreen(dpy), attribu-
     teListSgl);
         if (vi == NULL) {
            vi = glXChooseVisual(dpy, DefaultScreen(dpy), attri-
     buteListDbl);
            swap_flag = TRUE;
         }

         cx = glXCreateContext(dpy, vi, 0, GL_TRUE);

         cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
                       vi->visual, AllocNone);

         swa.colormap = cmap;
         swa.border_pixel = 0;
         swa.event_mask = StructureNotifyMask;
         win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0,
     0, 100, 100,

MirOS BSD #10-current   Printed 20.2.2012                       2

GLXINTRO()          UNIX Programmer's Manual           GLXINTRO()

                             0, vi->depth, InputOutput, vi-
     >visual,
                             CWBorderPixel|CWColormap|CWEventMask,
     &swa);
         XMapWindow(dpy, win);
         XIfEvent(dpy, &event, WaitForNotify, (char*)win);

         glXMakeCurrent(dpy, win, cx);

         glClearColor(1,1,0,1);
         glClear(GL_COLOR_BUFFER_BIT);
         glFlush();
         if (swap_flag) glXSwapBuffers(dpy,win);

         sleep(10); }

NOTES

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

     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 result in
     undefined behavior.

     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 write a program 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.

USING GLX EXTENSIONS

     All supported GLX extensions will have a corresponding
     definition in glx.h and a token in the extension string
     returned by glXQueryExtensionsString. For example, if the
     EXT_visual_info extension is supported, then this token will
     be defined in glx.h and EXT_visual_info will appear in the
     extension string returned by glXQueryExtensionsString. The
     definitions in glx.h can be used at compile time to deter-
     mine if procedure calls corresponding to an extension exist
     in the library.

MirOS BSD #10-current   Printed 20.2.2012                       3

GLXINTRO()          UNIX Programmer's Manual           GLXINTRO()

     OpenGL itself has also been extended. Refer to glIntro for
     more information.

GLX 1.1 and GLX 1.2
     GLX 1.2 is now supported. It is backward compatible with GLX
     1.1 and GLX 1.0.

     GLX 1.2 corresponds to OpenGL version 1.1 and introduces the
     following new call: glGetCurrentDisplay.

     GLX 1.1 corresponds to OpenGL version 1.0 and introduces the
     following new calls: glXQueryExtensionsString, glXQuer-
     yServerString, and glXGetClientString.

     Call glQueryVersion to determine at runtime what version of
     GLX is available. glQueryVersion returns the version that is
     supported on the connection. Thus if 1.2 is returned, both
     the client and server support GLX 1.2. You can also check
     the GLX version at compile time: GLX_VERSION_1_1 will be
     defined in glx.h if GLX 1.1 calls are supported and
     GLX_VERSION_1_2 will be defined if GLX 1.2 calls are sup-
     ported.

SEE ALSO

     glIntro, glFinish, glFlush, glXChooseVisual, glXCopyContext,
     glXCreateContext, glXCreateGLXPixmap, glXDestroyContext,
     glXGetClientString, glXGetConfig, glXIsDirect,
     glXMakeCurrent,
     glXQueryExtension, glXQueryExtensionsString, glXQueryServer-
     String, glXQueryVersion, glXSwapBuffers, glXUseXFont,
     glXWaitGL, glXWaitX, XCreateColormap, XCreateWindow, XSync

MirOS BSD #10-current   Printed 20.2.2012                       4

Generated on 2012-02-20 02:47:02 by $MirOS: src/scripts/roff2htm,v 1.70 2011/12/03 18:21:12 tg Exp $

These manual pages and other documentation are copyrighted by their respective writers; their source is available at our CVSweb, AnonCVS, and other mirrors. The rest is Copyright © 2002‒2011 The MirOS Project, Germany.
This product includes material provided by Thorsten Glaser.

This manual page’s HTML representation is supposed to be valid XHTML/1.1; if not, please send a bug report – diffs preferred.