1GLXINTRO() GLXINTRO()
2
3
4
6 glXIntro - Introduction to OpenGL in the X window system
7
8
10 OpenGL (called GL in other pages) is a high-performance 3D-oriented
11 renderer. It is available in the X window system through the GLX
12 extension. To determine whether the GLX extension is supported by an X
13 server, and if so, what version is supported, call glXQueryExtension
14 and glXQueryVersion.
15
16 GLX extended servers make a subset of their visuals available for
17 OpenGL rendering. Drawables created with these visuals can also be
18 rendered using the core X renderer and with the renderer of any other X
19 extension that is compatible with all core X visuals.
20
21 GLX extends drawables with several buffers other than the standard
22 color buffer. These buffers include back and auxiliary color buffers,
23 a depth buffer, a stencil buffer, and a color accumulation buffer.
24 Some or all are included in each X visual that supports OpenGL.
25
26 To render using OpenGL into an X drawable, you must first choose a vis‐
27 ual that defines the required OpenGL buffers. glXChooseVisual can be
28 used to simplify selecting a compatible visual. If more control of the
29 selection process is required, use XGetVisualInfo and glXGetConfig to
30 select among all the available visuals.
31
32 Use the selected visual to create both a GLX context and an X drawable.
33 GLX contexts are created with glXCreateContext, and drawables are cre‐
34 ated with either XCreateWindow or glXCreateGLXPixmap. Finally, bind
35 the context and the drawable together using glXMakeCurrent. This con‐
36 text/drawable pair becomes the current context and current drawable,
37 and it is used by all OpenGL commands until glXMakeCurrent is called
38 with different arguments.
39
40 Both core X and OpenGL commands can be used to operate on the current
41 drawable. The X and OpenGL command streams are not synchronized, how‐
42 ever, except at explicitly created boundaries generated by calling
43 glXWaitGL, glXWaitX, XSync, and glFlush.
44
46 Below is the minimum code required to create an RGBA-format, X window
47 that's compatible with OpenGL and to clear it to yellow. The code is
48 correct, but it does not include any error checking. Return values
49 dpy, vi, cx, cmap, and win should all be tested.
50
51 #include <GL/glx.h> #include <GL/gl.h> #include <unistd.h>
52
53 static int attributeListSgl[] = { GLX_RGBA, GLX_RED_SIZE,
54 1, /*get the deepest buffer with 1 red bit*/ GLX_GREEN_SIZE, 1,
55 GLX_BLUE_SIZE, 1, None };
56
57 static int attributeListDbl[] = { GLX_RGBA, GLX_DOUBLE_BUF‐
58 FER, /*In case single buffering is not supported*/ GLX_RED_SIZE,
59 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1, None };
60
61
62 static Bool WaitForNotify(Display *d, XEvent *e, char *arg) {
63 return (e->type == MapNotify) && (e->xmap.window == (Window)arg); }
64
65 int main(int argc, char **argv) {
66 Display *dpy;
67 XVisualInfo *vi;
68 Colormap cmap;
69 XSetWindowAttributes swa;
70 Window win;
71 GLXContext cx;
72 XEvent event;
73 int swap_flag = FALSE;
74
75
76 /* get a connection */
77 dpy = XOpenDisplay(0);
78
79 /* get an appropriate visual */
80 vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeListSgl);
81 if (vi == NULL) {
82 vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeListDbl);
83 swap_flag = TRUE;
84 }
85
86 /* create a GLX context */
87 cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
88
89 /* create a color map */
90 cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
91 vi->visual, AllocNone);
92
93 /* create a window */
94 swa.colormap = cmap;
95 swa.border_pixel = 0;
96 swa.event_mask = StructureNotifyMask;
97 win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 100,
98 100,
99 0, vi->depth, InputOutput, vi->visual,
100 CWBorderPixel|CWColormap|CWEventMask, &swa);
101 XMapWindow(dpy, win);
102 XIfEvent(dpy, &event, WaitForNotify, (char*)win);
103
104 /* connect the context to the window */
105 glXMakeCurrent(dpy, win, cx);
106
107 /* clear the buffer */
108 glClearColor(1,1,0,1);
109 glClear(GL_COLOR_BUFFER_BIT);
110 glFlush();
111 if (swap_flag) glXSwapBuffers(dpy,win);
112
113 /* wait a while */
114 sleep(10); }
115
117 A color map must be created and passed to XCreateWindow. See the pre‐
118 ceding example code.
119
120 A GLX context must be created and attached to an X drawable before
121 OpenGL commands can be executed. OpenGL commands issued while no con‐
122 text/drawable pair is current result in undefined behavior.
123
124 Exposure events indicate that all buffers associated with the specified
125 window may be damaged and should be repainted. Although certain buf‐
126 fers of some visuals on some systems may never require repainting (the
127 depth buffer, for example), it is incorrect to write a program assuming
128 that these buffers will not be damaged.
129
130 GLX commands manipulate XVisualInfo structures rather than pointers to
131 visuals or visual IDs. XVisualInfo structures contain visual, visu‐
132 alID, screen, and depth elements, as well as other X-specific informa‐
133 tion.
134
136 All supported GLX extensions will have a corresponding definition in
137 glx.h and a token in the extension string returned by glXQueryExten‐
138 sionsString. For example, if the EXT_visual_info extension is sup‐
139 ported, then this token will be defined in glx.h and EXT_visual_info
140 will appear in the extension string returned by glXQueryExtension‐
141 sString. The definitions in glx.h can be used at compile time to deter‐
142 mine if procedure calls corresponding to an extension exist in the
143 library.
144
145 OpenGL itself has also been extended. Refer to glIntro for more infor‐
146 mation.
147
149 GLX 1.2 is now supported. It is backward compatible with GLX 1.1 and
150 GLX 1.0.
151
152 GLX 1.2 corresponds to OpenGL version 1.1 and introduces the following
153 new call: glGetCurrentDisplay.
154
155 GLX 1.1 corresponds to OpenGL version 1.0 and introduces the following
156 new calls: glXQueryExtensionsString, glXQueryServerString, and glXGet‐
157 ClientString.
158
159 Call glQueryVersion to determine at runtime what version of GLX is
160 available. glQueryVersion returns the version that is supported on the
161 connection. Thus if 1.2 is returned, both the client and server support
162 GLX 1.2. You can also check the GLX version at compile time: GLX_VER‐
163 SION_1_1 will be defined in glx.h if GLX 1.1 calls are supported and
164 GLX_VERSION_1_2 will be defined if GLX 1.2 calls are supported.
165
167 glIntro, glFinish, glFlush, glXChooseVisual, glXCopyContext,
168 glXCreateContext, glXCreateGLXPixmap, glXDestroyContext,
169 glXGetClientString, glXGetConfig, glXIsDirect, glXMakeCurrent,
170 glXQueryExtension, glXQueryExtensionsString, glXQueryServerString, glX‐
171 QueryVersion, glXSwapBuffers, glXUseXFont, glXWaitGL, glXWaitX, XCre‐
172 ateColormap, XCreateWindow, XSync
173
174
175
176
177
178
179
180
181
182
183
184 GLXINTRO()