1 /**************************************************************************
2 * Copyright 2012 Intel corporation
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 **************************************************************************/
41 set_2d_projection (void)
43 glMatrixMode (GL_PROJECTION);
45 glOrtho (0, width, height, 0, 0, 1);
46 glMatrixMode (GL_MODELVIEW);
50 draw_fullscreen_quad (void)
54 glVertex2f (width, 0);
55 glVertex2f (width, height);
56 glVertex2f (0, height);
61 draw_fullscreen_textured_quad (void)
64 glTexCoord2f(0, 0); glVertex2f (0, 0);
65 glTexCoord2f(1, 0); glVertex2f (width, 0);
66 glTexCoord2f(1, 1); glVertex2f (width, height);
67 glTexCoord2f(0, 1); glVertex2f (0, height);
72 paint_rgb_using_clear (double r, double g, double b)
74 glClearColor(r, g, b, 1.0);
75 glClear(GL_COLOR_BUFFER_BIT);
79 paint_rgb_using_glsl (double r, double g, double b)
81 const char * vs_source =
84 " gl_Position = ftransform();\n"
86 const char * fs_source =
88 "uniform vec4 color;\n"
91 " gl_FragColor = color;\n"
94 GLuint vs, fs, program;
97 vs = glCreateShader (GL_VERTEX_SHADER);
98 glShaderSource (vs, 1, &vs_source, NULL);
101 fs = glCreateShader (GL_FRAGMENT_SHADER);
102 glShaderSource (fs, 1, &fs_source, NULL);
103 glCompileShader (fs);
105 program = glCreateProgram ();
106 glAttachShader (program, vs);
107 glAttachShader (program, fs);
109 glLinkProgram (program);
110 glUseProgram (program);
112 color = glGetUniformLocation (program, "color");
114 glUniform4f (color, r, g, b, 1.0);
116 draw_fullscreen_quad ();
122 paint_rgb_using_texture (double r, double g, double b)
127 data[0] = (uint8_t) (255.0 * r);
128 data[1] = (uint8_t) (255.0 * g);
129 data[2] = (uint8_t) (255.0 * b);
131 glGenTextures (1, &texture);
133 glBindTexture (GL_TEXTURE_2D, texture);
135 glTexImage2D (GL_TEXTURE_2D,
136 0, GL_COMPRESSED_RGBA,
138 GL_RGB, GL_UNSIGNED_BYTE, data);
140 glEnable (GL_TEXTURE_2D);
142 draw_fullscreen_textured_quad ();
144 glDisable (GL_TEXTURE_2D);
148 draw (Display *dpy, Window window, int width, int height)
153 int visual_attr[] = {
162 GLX_X_VISUAL_TYPE, GLX_DIRECT_COLOR,
166 /* Window and context setup. */
167 XVisualInfo *visual_info = glXChooseVisual(dpy, 0, visual_attr);
168 GLXContext ctx = glXCreateContext(dpy, visual_info, NULL, True);
169 glXMakeCurrent(dpy, window, ctx);
171 glew_err = glewInit();
172 if (glew_err != GLEW_OK)
174 fprintf (stderr, "glewInit failed: %s\n",
175 glewGetErrorString(glew_err));
179 glViewport(0, 0, width, height);
181 set_2d_projection ();
183 /* Simply count through some colors, frame by frame. */
184 #define RGB(frame) (((frame+1)/4) % 2), (((frame+1)/2) % 2), ((frame+1) % 2)
187 for (i = 0; i < 2; i++) {
188 /* Frame: Draw a solid (magenta) frame using glClear. */
189 paint_rgb_using_clear (RGB(frame));
190 glXSwapBuffers (dpy, window);
193 /* Frame: Draw a solid (yellow) frame using GLSL. */
194 paint_rgb_using_glsl (RGB(frame));
195 glXSwapBuffers (dpy, window);
198 /* Frame: Draw a solid (cyan) frame using a texture. */
199 paint_rgb_using_texture (RGB(frame));
200 glXSwapBuffers (dpy, window);
205 glXDestroyContext (dpy, ctx);
209 handle_events(Display *dpy, Window window, int width, int height)
212 KeyCode quit_code = XKeysymToKeycode (dpy, XStringToKeysym("Q"));
214 XNextEvent (dpy, &xev);
217 XNextEvent (dpy, &xev);
220 if (xev.xkey.keycode == quit_code) {
224 case ConfigureNotify:
225 width = xev.xconfigure.width;
226 height = xev.xconfigure.height;
229 if (xev.xexpose.count == 0) {
230 draw (dpy, window, width, height);
244 dpy = XOpenDisplay (NULL);
247 fprintf(stderr, "Failed to open display %s\n",
252 window = XCreateSimpleWindow(dpy, DefaultRootWindow (dpy),
253 0, 0, width, height, 0,
254 BlackPixel (dpy, DefaultScreen (dpy)),
255 BlackPixel (dpy, DefaultScreen (dpy)));
257 XSelectInput(dpy, window,
258 KeyPressMask | StructureNotifyMask | ExposureMask);
260 XMapWindow (dpy, window);
262 handle_events (dpy, window, width, height);
264 XDestroyWindow (dpy, window);