<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook MathML Module V1.1b1//EN"
              "http://www.oasis-open.org/docbook/xml/mathml/1.1CR1/dbmathml.dtd">
<refentry id="glXIntro">
    <refmeta>
        <refmetainfo>
            <copyright>
                <year>1991-2006</year>
                <holder>Silicon Graphics, Inc.</holder>
            </copyright>
        </refmetainfo>
        <refentrytitle>glXIntro</refentrytitle>
        <manvolnum>3G</manvolnum>
    </refmeta>
    <refnamediv>
        <refname>glXIntro</refname>
        <refpurpose>Introduction to OpenGL in the X window system</refpurpose>
    </refnamediv>
    <refsect1 id="overview"><title>Overview</title>
        <para>
        </para>
        <para>
            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 version is supported, call <citerefentry><refentrytitle>glXQueryExtension</refentrytitle></citerefentry> and
            <citerefentry><refentrytitle>glXQueryVersion</refentrytitle></citerefentry>.
        </para>
        <para>
            GLX extended X servers make a subset of their visuals available for OpenGL
            rendering.  Drawables created with these visual can also be rendered into
            using the core X renderer and or any other X extension that is compatible
            with all core X visuals.
        </para>
        <para>
            GLX extends a drawable's standard color buffer with additional buffers.
            These buffers include back and auxiliary color buffers, a depth buffer, a
            stencil buffer, and a color accumulation buffer.  Some or all of the buffers
            listed are included in each X visual that supports OpenGL.
        </para>
        <para>
            GLX supports rendering into three types of drawables: windows, pixmaps, and
            pbuffers (pixel buffers). GLX windows and pixmaps are X resources, and
            capable of accepting core X rendering as well as OpenGL rendering.  
            GLX-pbuffers are GLX only resources and might not accept core X rendering.
        </para>
        <para>
            To render using OpenGL into a GLX drawable, you must determine the
            appropriate GLXFBConfig that supports the rendering features your
            application requires. <citerefentry><refentrytitle>glXChooseFBConfig</refentrytitle></citerefentry> returns a GLXFBConfig matching
            the required attributes or <constant>NULL</constant> if no match is found.  A complete
            list of GLXFBConfigs supported by a server can be obtained by calling
            <citerefentry><refentrytitle>glXGetFBConfigs</refentrytitle></citerefentry>.  Attributes of a particular GLXFBConfig can be
            queried by calling <citerefentry><refentrytitle>glXGetFBConfigAttrib</refentrytitle></citerefentry>.
        </para>
        <para>
            For GLX windows and pixmaps, a suitable X drawable (using either
            <function>XCreateWindow</function> or <function>XCreatePixmap</function>, respectively) with a matching
            visual must be created first.  Call <citerefentry><refentrytitle>glXGetVisualFromFBConfig</refentrytitle></citerefentry> to obtain
            the necessary XVisualInfo structure for creating the X drawable.  For
            pbuffers, no underlying X drawable is required.
        </para>
        <para>
            To create a GLX window from an X window, call <citerefentry><refentrytitle>glXCreateWindow</refentrytitle></citerefentry>.
            Likewise, to create a GLX pixmap, call <citerefentry><refentrytitle>glXCreatePixmap</refentrytitle></citerefentry>. Pbuffers are
            created by calling <citerefentry><refentrytitle>glXCreatePbuffer</refentrytitle></citerefentry>.  Use <citerefentry><refentrytitle>glXDestroyWindow</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXDestroyPixmap</refentrytitle></citerefentry>, and <citerefentry><refentrytitle>glXDestroyPbuffer</refentrytitle></citerefentry> to release previously
            allocated resources.
        </para>
        <para>
            A GLX context is required to bind OpenGL rendering to a GLX resource.  A GLX
            resource and rendering context must have compatible GLXFBConfigs.  To create
            a GLX context, call <citerefentry><refentrytitle>glXCreateNewContext</refentrytitle></citerefentry>.  A context may be bound to a
            GLX drawable by using <citerefentry><refentrytitle>glXMakeContextCurrent</refentrytitle></citerefentry>.  This context/drawable
            pair becomes the current context and current drawable, and is used by all
            OpenGL rendering commands until <citerefentry><refentrytitle>glXMakeContextCurrent</refentrytitle></citerefentry> is called with
            different arguments.
        </para>
        <para>
            Both core X and OpenGL commands can be used to operate on drawables;
            however, the X and OpenGL command streams are not synchronized.
            Synchronization can be explicitly specified using by calling <citerefentry><refentrytitle>glXWaitGL</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXWaitX</refentrytitle></citerefentry>, <function>XSync</function>, and <function>XFlush</function>.
        </para>
        <para>
        </para>
    </refsect1>
    <refsect1 id="examples"><title>Examples</title>
        <para>
            Below is a minimal example of creating an RGBA-format X window that's
            compatible with OpenGL using GLX 1.3 commands.  The window is cleared to
            yellow when the program runs.  The program does minimal error checking; all
            return values should be checked.
        </para>
        <para>
            <programlisting>
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;GL/gl.h&gt;
#include &lt;GL/glx.h&gt;

int singleBufferAttributess[] = {
    GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
    GLX_RENDER_TYPE,   GLX_RGBA_BIT,
    GLX_RED_SIZE,      1,   /* Request a single buffered color buffer */
    GLX_GREEN_SIZE,    1,   /* with the maximum number of color bits  */
    GLX_BLUE_SIZE,     1,   /* for each component                     */
    None
};

int doubleBufferAttributes[] = {
    GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
    GLX_RENDER_TYPE,   GLX_RGBA_BIT,
    GLX_DOUBLEBUFFER,  True,  /* Request a double-buffered color buffer with */
    GLX_RED_SIZE,      1,     /* the maximum number of bits per component    */
    GLX_GREEN_SIZE,    1, 
    GLX_BLUE_SIZE,     1,
    None
};


static Bool WaitForNotify( Display *dpy, XEvent *event, XPointer arg ) {
    return (event-&gt;type == MapNotify) &amp;&amp; (event-&gt;xmap.window == (Window) arg);
}
int main( int argc, char *argv[] )
{
    Display              *dpy;
    Window                xWin;
    XEvent                event;
    XVisualInfo          *vInfo;
    XSetWindowAttributes  swa;
    GLXFBConfig          *fbConfigs;
    GLXContext            context;
    GLXWindow             glxWin;
    int                   swaMask;
    int                   numReturned;
    int                   swapFlag = True;

    /* Open a connection to the X server */
    dpy = XOpenDisplay( NULL );
    if ( dpy == NULL ) {
        printf( "Unable to open a connection to the X server\n" );
        exit( EXIT_FAILURE );
    }

    /* Request a suitable framebuffer configuration - try for a double 
    ** buffered configuration first */
    fbConfigs = glXChooseFBConfig( dpy, DefaultScreen(dpy),
                                   doubleBufferAttributes, &amp;numReturned );

    if ( fbConfigs == NULL ) {  /* no double buffered configs available */
      fbConfigs = glXChooseFBConfig( dpy, DefaultScreen(dpy),
                                     singleBufferAttributess, &amp;numReturned );
      swapFlag = False;
    }

    /* Create an X colormap and window with a visual matching the first
    ** returned framebuffer config */
    vInfo = glXGetVisualFromFBConfig( dpy, fbConfigs[0] );

    swa.border_pixel = 0;
    swa.event_mask = StructureNotifyMask;
    swa.colormap = XCreateColormap( dpy, RootWindow(dpy, vInfo-&gt;screen),
                                    vInfo-&gt;visual, AllocNone );

    swaMask = CWBorderPixel | CWColormap | CWEventMask;

    xWin = XCreateWindow( dpy, RootWindow(dpy, vInfo-&gt;screen), 0, 0, 256, 256,
                          0, vInfo-&gt;depth, InputOutput, vInfo-&gt;visual,
                          swaMask, &amp;swa );

    /* Create a GLX context for OpenGL rendering */
    context = glXCreateNewContext( dpy, fbConfigs[0], GLX_RGBA_TYPE,
				 NULL, True );

    /* Create a GLX window to associate the frame buffer configuration
    ** with the created X window */
    glxWin = glXCreateWindow( dpy, fbConfigs[0], xWin, NULL );
    
    /* Map the window to the screen, and wait for it to appear */
    XMapWindow( dpy, xWin );
    XIfEvent( dpy, &amp;event, WaitForNotify, (XPointer) xWin );

    /* Bind the GLX context to the Window */
    glXMakeContextCurrent( dpy, glxWin, glxWin, context );

    /* OpenGL rendering ... */
    glClearColor( 1.0, 1.0, 0.0, 1.0 );
    glClear( GL_COLOR_BUFFER_BIT );

    glFlush();
    
    if ( swapFlag )
        glXSwapBuffers( dpy, glxWin );

    sleep( 10 );
    exit( EXIT_SUCCESS );
}
            </programlisting>
        </para>
        <para>
        </para>
    </refsect1>
    <refsect1 id="notes"><title>Notes</title>
        <para>
            An X color map must be created and passed to <function>XCreateWindow</function>.
        </para>
        <para>
            A GLX context must be created and bound to a GLX drawable before OpenGL
            commands can be executed.  OpenGL commands executed while no
            context/drawable pair is current result in undefined behavior.
        </para>
        <para>
            Exposure events indicate that <emphasis>all</emphasis> 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.
        </para>
        <para>
            GLX commands utilize XVisualInfo structures rather than pointers to visuals
            or visualIDs directly.  XVisualInfo structures contain <emphasis>visual</emphasis>,
            <emphasis>visualID</emphasis>, <emphasis>screen</emphasis>, and <emphasis>depth</emphasis> elements, as well as other
            X-specific information.
        </para>
        <para>
        </para>
    </refsect1>
    <refsect1 id="usingglxextensions"><title>Using GLX Extensions</title>
        <para>
            All supported GLX extensions will have a corresponding definition in glx.h
            and a token in the extension string returned by
            <citerefentry><refentrytitle>glXQueryExtensionsString</refentrytitle></citerefentry>.  For example, if the
            <code>EXT_visual_info</code> extension is supported, then this token will be
            defined in glx.h and <code>EXT_visual_info</code> will appear in the extension
            string returned by <citerefentry><refentrytitle>glXQueryExtensionsString</refentrytitle></citerefentry>. The definitions in glx.h
            can be used at compile time to determine if procedure calls corresponding to
            an extension exist in the library.
        </para>
        <para>
            OpenGL itself is capable of being extended.
        </para>
        <para>
        </para>
    </refsect1>
    <refsect1 id="glx11glx12andglx13"><title>GLX 1.1, GLX 1.2, and GLX 1.3</title>
        <para>
            GLX 1.3 is now supported and is backward compatible with GLX 1.1 and GLX
            1.2.  It introduces new functionality (namely GLXFBConfigs) that supersedes
            the GLX 1.2 functionality.  GLX 1.2 commands are supported, but their use in
            new application development is not recommended.
        </para>
        <para>
            GLX 1.3 corresponds to OpenGL versions 1.2 and introduces the following new
            calls: <citerefentry><refentrytitle>glXGetFBConfigs</refentrytitle></citerefentry>, <citerefentry><refentrytitle>glXGetFBConfigAttrib</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXGetVisualFromFBConfig</refentrytitle></citerefentry>, <citerefentry><refentrytitle>glXCreateWindow</refentrytitle></citerefentry>, <citerefentry><refentrytitle>glXDestroyWindow</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXCreatePixmap</refentrytitle></citerefentry>, <citerefentry><refentrytitle>glXDestroyPixmap</refentrytitle></citerefentry>, <citerefentry><refentrytitle>glXCreatePbuffer</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXDestroyPbuffer</refentrytitle></citerefentry>, <citerefentry><refentrytitle>glXQueryDrawable</refentrytitle></citerefentry>, <citerefentry><refentrytitle>glXCreateNewContext</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXMakeContextCurrent</refentrytitle></citerefentry>, <citerefentry><refentrytitle>glXGetCurrentReadDrawable</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXGetCurrentDisplay</refentrytitle></citerefentry>, <citerefentry><refentrytitle>glXQueryContext</refentrytitle></citerefentry>, and <citerefentry><refentrytitle>glXSelectEvent</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXGetSelectedEvent</refentrytitle></citerefentry>.
        </para>
        <para>
            GLX 1.2 corresponds to OpenGL version 1.1 and introduces the following new
            call: <citerefentry><refentrytitle>glXGetCurrentDisplay</refentrytitle></citerefentry>.
        </para>
        <para>
            GLX 1.1 corresponds to OpenGL version 1.0 and introduces the following new
            calls: <citerefentry><refentrytitle>glXQueryExtensionsString</refentrytitle></citerefentry>, <citerefentry><refentrytitle>glXQueryServerString</refentrytitle></citerefentry>, and
            <citerefentry><refentrytitle>glXGetClientString</refentrytitle></citerefentry>.
        </para>
        <para>
            Call <citerefentry><refentrytitle>glXQueryVersion</refentrytitle></citerefentry> to determine at runtime what version of GLX is
            available. <citerefentry><refentrytitle>glXQueryVersion</refentrytitle></citerefentry> returns the version that is supported on the
            connection. Thus, if 1.3 is returned, both the client and server support GLX
            1.3.  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, GLX_VERSION_1_2
            will be defined if GLX 1.2 calls are supported, and GLX_VERSION_1_3 will be
            defined if GLX 1.3 calls are supported.
        </para>
        <para>
        </para>
    </refsect1>
    <refsect1 id="seealso"><title>See Also</title>
        <para>
            <citerefentry><refentrytitle>glFinish</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glFlush</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXChooseVisual</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXCopyContext</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXCreateContext</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXCreateGLXPixmap</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXCreateNewContext</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXCreatePbuffer</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXCreatePixmap</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXCreateWindow</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXDestroyContext</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXDestroyPbuffer</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXDestroyPixmap</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXDestroyWindow</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXGetClientString</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXGetConfig</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXGetCurrentDisplay</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXGetCurrentReadDrawable</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXGetFBConfigAttrib</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXGetFBConfigs</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXGetProcAddress</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXGetSelectedEvent</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXGetVisualFromFBConfig</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXIsDirect</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXMakeContextCurrent</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXMakeCurrent</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXQueryContext</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXQueryDrawable</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXQueryExtension</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXQueryExtensionsString</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXQueryServerString</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXQueryVersion</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXSelectEvent</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXSwapBuffers</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXUseXFont</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXWaitGL</refentrytitle></citerefentry>,
            <citerefentry><refentrytitle>glXWaitX</refentrytitle></citerefentry>.
            <function>XCreateColormap</function>,
            <function>XCreateWindow</function>,
            <function>XSync</function>
        </para>
    </refsect1>
    <refsect1 id="Copyright"><title>Copyright</title>
        <para>
            Copyright <trademark class="copyright"></trademark> 1991-2006
            Silicon Graphics, Inc. This document is licensed under the SGI
            Free Software B License. For details, see
            <ulink url="http://oss.sgi.com/projects/FreeB/">http://oss.sgi.com/projects/FreeB/</ulink>.
        </para>
    </refsect1>
</refentry>
