1 /* Copyright © 2013, Intel Corporation
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 /* Perform some simple drawing via OpenGL as follows:
24 * 1. Using GLX to construct an OpenGL context
25 * 2. By using dlopen to dynamically load libGL.so
26 * 3. By using dlsym to lookup OpenGL functions
29 #define GL_GLEXT_PROTOTYPES
39 void (*my_glClear) (GLbitfield);
40 void (*my_glClearColor) (GLclampf, GLclampf, GLclampf, GLclampf);
41 void (*my_glLoadIdentity) (void);
42 void (*my_glMatrixMode) (GLenum);
43 void (*my_glOrtho) (GLdouble, GLdouble, GLdouble, GLdouble, GLdouble, GLdouble);
44 void (*my_glViewport) (GLint, GLint, GLsizei, GLsizei);
45 XVisualInfo * (*my_glXChooseVisual) (Display *, int, int *);
46 GLXContext (*my_glXCreateContext) (Display *, XVisualInfo *, GLXContext, Bool);
47 void (*my_glXDestroyContext) (Display *, GLXContext);
48 Bool (*my_glXMakeCurrent) (Display *, GLXDrawable, GLXContext);
49 void (*my_glXSwapBuffers) (Display *, GLXDrawable);
52 set_2d_projection (int width, int height)
54 my_glMatrixMode (GL_PROJECTION);
56 my_glOrtho (0, width, height, 0, 0, 1);
57 my_glMatrixMode (GL_MODELVIEW);
61 paint_rgb_using_clear (double r, double g, double b)
63 my_glClearColor(r, g, b, 1.0);
64 my_glClear(GL_COLOR_BUFFER_BIT);
68 draw (Display *dpy, Window window, int width, int height)
80 GLX_X_VISUAL_TYPE, GLX_DIRECT_COLOR,
84 /* Window and context setup. */
85 XVisualInfo *visual_info = my_glXChooseVisual(dpy, 0, visual_attr);
86 GLXContext ctx = my_glXCreateContext(dpy, visual_info, NULL, True);
87 my_glXMakeCurrent(dpy, window, ctx);
89 my_glViewport(0, 0, width, height);
91 set_2d_projection (width, height);
93 /* Simply count through some colors, frame by frame. */
94 #define RGB(frame) (((frame+1)/4) % 2), (((frame+1)/2) % 2), ((frame+1) % 2)
97 for (i = 0; i < 2; i++) {
98 /* Frame: Draw a solid (magenta) frame */
99 paint_rgb_using_clear (RGB(frame));
100 my_glXSwapBuffers (dpy, window);
103 /* Frame: Draw a solid (yellow) frame */
104 paint_rgb_using_clear (RGB(frame));
105 my_glXSwapBuffers (dpy, window);
108 /* Frame: Draw a solid (cyan) frame */
109 paint_rgb_using_clear (RGB(frame));
110 my_glXSwapBuffers (dpy, window);
115 my_glXDestroyContext (dpy, ctx);
119 handle_events(Display *dpy, Window window)
125 XNextEvent (dpy, &xev);
128 XNextEvent (dpy, &xev);
130 case ConfigureNotify:
131 width = xev.xconfigure.width;
132 height = xev.xconfigure.height;
135 if (xev.xexpose.count == 0) {
136 draw (dpy, window, width, height);
145 resolve_symbols (void)
150 gl_handle = dlopen ("libGL.so", RTLD_LAZY);
151 if (gl_handle == NULL) {
152 fprintf (stderr, "Error: Failed to open libGL.so\n");
159 my_glClear = dlsym (gl_handle, "glClear");
162 fprintf (stderr, "Failed to dlsym glClear: %s\n", error);
166 my_glClearColor = dlsym (gl_handle, "glClearColor");
169 fprintf (stderr, "Failed to dlsym glClearColor: %s\n", error);
173 my_glLoadIdentity = dlsym (gl_handle, "glLoadIdentity");
176 fprintf (stderr, "Failed to dlsym glLoadIdentity: %s\n", error);
180 my_glMatrixMode = dlsym (gl_handle, "glMatrixMode");
183 fprintf (stderr, "Failed to dlsym glMatrixMode: %s\n", error);
187 my_glOrtho = dlsym (gl_handle, "glOrtho");
190 fprintf (stderr, "Failed to dlsym glOrtho: %s\n", error);
194 my_glViewport = dlsym (gl_handle, "glViewport");
197 fprintf (stderr, "Failed to dlsym glViewport: %s\n", error);
201 my_glXChooseVisual = dlsym (gl_handle, "glXChooseVisual");
204 fprintf (stderr, "Failed to dlsym glXChooseVisual: %s\n", error);
208 my_glXCreateContext = dlsym (gl_handle, "glXCreateContext");
211 fprintf (stderr, "Failed to dlsym glXCreateContext: %s\n", error);
215 my_glXDestroyContext = dlsym (gl_handle, "glXDestroyContext");
218 fprintf (stderr, "Failed to dlsym glXDestroyContext: %s\n", error);
222 my_glXMakeCurrent = dlsym (gl_handle, "glXMakeCurrent");
225 fprintf (stderr, "Failed to dlsym glXMakeCurrent: %s\n", error);
229 my_glXSwapBuffers = dlsym (gl_handle, "glXSwapBuffers");
232 fprintf (stderr, "Failed to dlsym glXSwapBuffers: %s\n", error);
243 util_init_display_window (&dpy, &window);
247 handle_events (dpy, window);
249 util_fini_display_window (dpy, window);