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_glXGetProcAddress) (char *);
40 void (*my_glClear) (GLbitfield);
41 void (*my_glClearColor) (GLclampf, GLclampf, GLclampf, GLclampf);
42 void (*my_glLoadIdentity) (void);
43 void (*my_glMatrixMode) (GLenum);
44 void (*my_glOrtho) (GLdouble, GLdouble, GLdouble, GLdouble, GLdouble, GLdouble);
45 void (*my_glViewport) (GLint, GLint, GLsizei, GLsizei);
46 XVisualInfo * (*my_glXChooseVisual) (Display *, int, int *);
47 GLXContext (*my_glXCreateContext) (Display *, XVisualInfo *, GLXContext, Bool);
48 void (*my_glXDestroyContext) (Display *, GLXContext);
49 Bool (*my_glXMakeCurrent) (Display *, GLXDrawable, GLXContext);
50 void (*my_glXSwapBuffers) (Display *, GLXDrawable);
53 set_2d_projection (int width, int height)
55 my_glMatrixMode (GL_PROJECTION);
57 my_glOrtho (0, width, height, 0, 0, 1);
58 my_glMatrixMode (GL_MODELVIEW);
62 paint_rgb_using_clear (double r, double g, double b)
64 my_glClearColor(r, g, b, 1.0);
65 my_glClear(GL_COLOR_BUFFER_BIT);
69 draw (Display *dpy, Window window, int width, int height)
81 GLX_X_VISUAL_TYPE, GLX_DIRECT_COLOR,
85 /* Window and context setup. */
86 XVisualInfo *visual_info = my_glXChooseVisual(dpy, 0, visual_attr);
87 GLXContext ctx = my_glXCreateContext(dpy, visual_info, NULL, True);
88 my_glXMakeCurrent(dpy, window, ctx);
90 my_glViewport(0, 0, width, height);
92 set_2d_projection (width, height);
94 /* Simply count through some colors, frame by frame. */
95 #define RGB(frame) (((frame+1)/4) % 2), (((frame+1)/2) % 2), ((frame+1) % 2)
98 for (i = 0; i < 2; i++) {
99 /* Frame: Draw a solid (magenta) frame */
100 paint_rgb_using_clear (RGB(frame));
101 my_glXSwapBuffers (dpy, window);
104 /* Frame: Draw a solid (yellow) frame */
105 paint_rgb_using_clear (RGB(frame));
106 my_glXSwapBuffers (dpy, window);
109 /* Frame: Draw a solid (cyan) frame */
110 paint_rgb_using_clear (RGB(frame));
111 my_glXSwapBuffers (dpy, window);
116 my_glXDestroyContext (dpy, ctx);
120 handle_events(Display *dpy, Window window)
126 XNextEvent (dpy, &xev);
129 XNextEvent (dpy, &xev);
131 case ConfigureNotify:
132 width = xev.xconfigure.width;
133 height = xev.xconfigure.height;
136 if (xev.xexpose.count == 0) {
137 draw (dpy, window, width, height);
146 resolve_symbols (void)
151 gl_handle = dlopen ("libGL.so", RTLD_LAZY);
152 if (gl_handle == NULL) {
153 fprintf (stderr, "Error: Failed to open libGL.so\n");
160 my_glXGetProcAddress = dlsym (gl_handle, "glXGetProcAddress");
163 fprintf (stderr, "Failed to dlsym glXGetProcAddress: %s\n", error);
167 my_glClear = my_glXGetProcAddress ("glClear");
168 if (my_glClear == NULL) {
169 fprintf (stderr, "Failed to glXGetProcAddress glClear\n");
173 my_glClearColor = my_glXGetProcAddress ("glClearColor");
174 if (my_glClearColor == NULL) {
175 fprintf (stderr, "Failed to glXGetProcAddress glClearColor\n");
179 my_glLoadIdentity = my_glXGetProcAddress ("glLoadIdentity");
180 if (my_glLoadIdentity == NULL) {
181 fprintf (stderr, "Failed to glXGetProcAddress glLoadIdentity\n");
185 my_glMatrixMode = my_glXGetProcAddress ("glMatrixMode");
186 if (my_glMatrixMode == NULL) {
187 fprintf (stderr, "Failed to glXGetProcAddress glMatrixMode\n");
191 my_glOrtho = my_glXGetProcAddress ("glOrtho");
192 if (my_glOrtho == NULL) {
193 fprintf (stderr, "Failed to glXGetProcAddress glOrtho\n");
197 my_glViewport = my_glXGetProcAddress ("glViewport");
198 if (my_glViewport == NULL) {
199 fprintf (stderr, "Failed to glXGetProcAddress glViewport\n");
203 my_glXChooseVisual = my_glXGetProcAddress ("glXChooseVisual");
204 if (my_glXChooseVisual == NULL) {
205 fprintf (stderr, "Failed to glXGetProcAddress glXChooseVisual\n");
209 my_glXCreateContext = my_glXGetProcAddress ("glXCreateContext");
210 if (my_glXCreateContext == NULL) {
211 fprintf (stderr, "Failed to glXGetProcAddress glXCreateContext\n");
215 my_glXDestroyContext = my_glXGetProcAddress ("glXDestroyContext");
216 if (my_glXDestroyContext == NULL) {
217 fprintf (stderr, "Failed to glXGetProcAddress glXDestroyContext\n");
221 my_glXMakeCurrent = my_glXGetProcAddress ("glXMakeCurrent");
222 if (my_glXMakeCurrent == NULL) {
223 fprintf (stderr, "Failed to glXGetProcAddress glXMakeCurrent\n");
227 my_glXSwapBuffers = my_glXGetProcAddress ("glXSwapBuffers");
228 if (my_glXSwapBuffers == NULL) {
229 fprintf (stderr, "Failed to glXGetProcAddress glXSwapBuffers\n");
240 util_init_display_window (&dpy, &window);
244 handle_events (dpy, window);
246 util_fini_display_window (dpy, window);