Notes:
- Choose a visual that defines the required OpenGL buffers.
The glXChooseVisual subroutine can be used to simplify selection of a compatible visual. If more control of the selection process is required, use the XGetVisualInfo and glXGetConfig subroutines to select among the available visuals.
- Use the selected visual to create both a GLX context and an X drawable.
GLX contexts are created with the glXCreateContext subroutine; drawables are created with either the XCreateWindow or glXCreateGLXPixmap subroutines.
- Bind the context and the drawable together using the glXMakeCurrent subroutine.
This context/drawable pair becomes the current context and current drawable, and it is used by all OpenGL commands until the glXMakeCurrent subroutine is called with different arguments.
Following is an example of the minimum code required to create a red, green, blue, alpha (RGBA) format, OpenGL-compatible X window. In this example, the X window is cleared to yellow. Note that although the code is valid, no error checking is included. Under normal conditions, all return values should be tested.
#include <GL/glx.h> #include <GL/gl.h>
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_FALSE);
/* create a colormap */ 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 */ glClearColor(1,1,0,1); glClear(GL_COLOR_BUFFER_BIT); glFlush();
/* wait a while */ sleep(10); }
When creating an X window, keep the following in mind:
Notes:
- A color map must be created and passed to the XCreateWindow subroutine.
- A GLX context must be created and attached to an X drawable before OpenGL commands are processed. 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 some visual buffers on certain systems may never require repainting (the depth buffer, for example), this is not the rule. Do not create code based on the assumption that these buffers cannot be damaged.
- GLX commands manipulate XVisualInfo structures, not pointers to visuals or visual IDs. XVisualInfo structures contain visual, visual ID, screen, and depth parameters, as well as other X-specific information.
OpenGL in the AIXwindows (GLX) Environment.