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 paint_rgb_using_clear (double r, double g, double b)
43 glClearColor(r, g, b, 1.0);
44 glClear(GL_COLOR_BUFFER_BIT);
48 paint_rgb_using_glsl (double r, double g, double b)
50 const char * vs_source =
53 " gl_Position = ftransform();\n"
55 const char * fs_source =
57 "uniform vec4 color;\n"
60 " gl_FragColor = color;\n"
63 GLuint vs, fs, program;
66 vs = glCreateShader (GL_VERTEX_SHADER);
67 glShaderSource (vs, 1, &vs_source, NULL);
70 fs = glCreateShader (GL_FRAGMENT_SHADER);
71 glShaderSource (fs, 1, &fs_source, NULL);
74 program = glCreateProgram ();
75 glAttachShader (program, vs);
76 glAttachShader (program, fs);
78 glLinkProgram (program);
79 glUseProgram (program);
81 color = glGetUniformLocation (program, "color");
83 glUniform4f (color, r, g, b, 1.0);
85 glMatrixMode (GL_PROJECTION);
87 glOrtho (0, width, height, 0, 0, 1);
88 glMatrixMode (GL_MODELVIEW);
92 glVertex2f (width, 0);
93 glVertex2f (width, height);
94 glVertex2f (0, height);
101 draw (Display *dpy, Window window, int width, int height)
105 int visual_attr[] = {
114 GLX_X_VISUAL_TYPE, GLX_DIRECT_COLOR,
118 /* Window and context setup. */
119 XVisualInfo *visual_info = glXChooseVisual(dpy, 0, visual_attr);
120 GLXContext ctx = glXCreateContext(dpy, visual_info, NULL, True);
121 glXMakeCurrent(dpy, window, ctx);
123 glew_err = glewInit();
124 if (glew_err != GLEW_OK)
126 fprintf (stderr, "glewInit failed: %s\n",
127 glewGetErrorString(glew_err));
131 glViewport(0, 0, width, height);
133 /* Frame 1: Draw a solid (magenta) frame using glClear. */
134 paint_rgb_using_clear (1, 0, 1);
135 glXSwapBuffers (dpy, window);
137 /* Frame 2: Draw a solid (yellow) frame using GLSL. */
138 paint_rgb_using_glsl (1, 1, 0);
139 glXSwapBuffers (dpy, window);
142 glXDestroyContext (dpy, ctx);
146 handle_events(Display *dpy, Window window, int width, int height)
149 KeyCode quit_code = XKeysymToKeycode (dpy, XStringToKeysym("Q"));
151 XNextEvent (dpy, &xev);
154 XNextEvent (dpy, &xev);
157 if (xev.xkey.keycode == quit_code) {
161 case ConfigureNotify:
162 width = xev.xconfigure.width;
163 height = xev.xconfigure.height;
166 if (xev.xexpose.count == 0) {
167 draw (dpy, window, width, height);
181 dpy = XOpenDisplay (NULL);
184 fprintf(stderr, "Failed to open display %s\n",
189 window = XCreateSimpleWindow(dpy, DefaultRootWindow (dpy),
190 0, 0, width, height, 0,
191 BlackPixel (dpy, DefaultScreen (dpy)),
192 BlackPixel (dpy, DefaultScreen (dpy)));
194 XSelectInput(dpy, window,
195 KeyPressMask | StructureNotifyMask | ExposureMask);
197 XMapWindow (dpy, window);
199 handle_events (dpy, window, width, height);
201 XDestroyWindow (dpy, window);