]> git.cworth.org Git - apitrace-tests/blob - cli/src/glxsimple.c
glxsimple: Add a second frame drawing a solid color with GLSL.
[apitrace-tests] / cli / src / glxsimple.c
1 /**************************************************************************
2  * Copyright 2012 Intel corporation
3  * All Rights Reserved.
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
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
21  * THE SOFTWARE.
22  *
23  **************************************************************************/
24
25 #include <stdio.h>
26
27 #include <X11/Xlib.h>
28 #include <GL/glew.h>
29 #include <GL/gl.h>
30 #include <GL/glx.h>
31
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <string.h>
36
37 int width = 64;
38 int height = 64;
39
40 static void
41 paint_rgb_using_clear (double r, double g, double b)
42 {
43         glClearColor(r, g, b, 1.0);
44         glClear(GL_COLOR_BUFFER_BIT);
45 }
46
47 static void
48 paint_rgb_using_glsl (double r, double g, double b)
49 {
50         const char * vs_source =
51                 "void main()\n"
52                 "{\n"
53                 "        gl_Position = ftransform();\n"
54                 "}\n";
55         const char * fs_source =
56                 "#version 120\n"
57                 "uniform vec4 color;\n"
58                 "void main()\n"
59                 "{\n"
60                 "        gl_FragColor = color;\n"
61                 "}\n";
62
63         GLuint vs, fs, program;
64         GLint color;
65
66         vs = glCreateShader (GL_VERTEX_SHADER);
67         glShaderSource (vs, 1, &vs_source, NULL);
68         glCompileShader (vs);
69
70         fs = glCreateShader (GL_FRAGMENT_SHADER);
71         glShaderSource (fs, 1, &fs_source, NULL);
72         glCompileShader (fs);
73
74         program = glCreateProgram ();
75         glAttachShader (program, vs);
76         glAttachShader (program, fs);
77
78         glLinkProgram (program);
79         glUseProgram (program);
80
81         color = glGetUniformLocation (program, "color");
82
83         glUniform4f (color, r, g, b, 1.0);
84
85         glMatrixMode (GL_PROJECTION);
86         glLoadIdentity ();
87         glOrtho (0, width, height, 0, 0, 1);
88         glMatrixMode (GL_MODELVIEW);
89
90         glBegin (GL_QUADS);
91         glVertex2f (0, 0);
92         glVertex2f (width, 0);
93         glVertex2f (width, height);
94         glVertex2f (0, height);
95         glEnd ();
96
97         glUseProgram (0);
98 }
99
100 static void
101 draw (Display *dpy, Window window, int width, int height)
102 {
103         GLenum glew_err;
104
105         int visual_attr[] = {
106                 GLX_RGBA,
107                 GLX_RED_SIZE,           8,
108                 GLX_GREEN_SIZE,         8,
109                 GLX_BLUE_SIZE,          8,
110                 GLX_ALPHA_SIZE,         8,
111                 GLX_DOUBLEBUFFER,
112                 GLX_DEPTH_SIZE,         24,
113                 GLX_STENCIL_SIZE,       8,
114                 GLX_X_VISUAL_TYPE,      GLX_DIRECT_COLOR,
115                 None
116         };
117
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);
122
123         glew_err = glewInit();
124         if (glew_err != GLEW_OK)
125         {
126                 fprintf (stderr, "glewInit failed: %s\n",
127                          glewGetErrorString(glew_err));
128                 exit (1);
129         }
130
131         glViewport(0, 0, width, height);
132
133         /* Frame 1: Draw a solid (magenta) frame using glClear. */
134         paint_rgb_using_clear (1, 0, 1);
135         glXSwapBuffers (dpy, window);
136
137         /* Frame 2: Draw a solid (yellow) frame using GLSL. */
138         paint_rgb_using_glsl (1, 1, 0);
139         glXSwapBuffers (dpy, window);
140
141         /* Cleanup */
142         glXDestroyContext (dpy, ctx);
143 }
144
145 static void
146 handle_events(Display *dpy, Window window, int width, int height)
147 {
148         XEvent xev;
149         KeyCode quit_code = XKeysymToKeycode (dpy, XStringToKeysym("Q"));
150
151         XNextEvent (dpy, &xev);
152
153         while (1) {
154                 XNextEvent (dpy, &xev);
155                 switch (xev.type) {
156                 case KeyPress:
157                         if (xev.xkey.keycode == quit_code) {
158                                 return;
159                         }
160                         break;
161                 case ConfigureNotify:
162                         width = xev.xconfigure.width;
163                         height = xev.xconfigure.height;
164                         break;
165                 case Expose:
166                         if (xev.xexpose.count == 0) {
167                                 draw (dpy, window, width, height);
168                                 return;
169                         }
170                         break;
171                 }
172         }
173 }
174
175 int
176 main (void)
177 {
178         Display *dpy;
179         Window window;
180
181         dpy = XOpenDisplay (NULL);
182
183         if (dpy == NULL) {
184                 fprintf(stderr, "Failed to open display %s\n",
185                         XDisplayName(NULL));
186                 return 1;
187         }
188
189         window = XCreateSimpleWindow(dpy, DefaultRootWindow (dpy),
190                                      0, 0, width, height, 0,
191                                      BlackPixel (dpy, DefaultScreen (dpy)),
192                                      BlackPixel (dpy, DefaultScreen (dpy)));
193
194         XSelectInput(dpy, window,
195                      KeyPressMask | StructureNotifyMask | ExposureMask);
196
197         XMapWindow (dpy, window);
198
199         handle_events (dpy, window, width, height);
200
201         XDestroyWindow (dpy, window);
202         XCloseDisplay (dpy);
203
204         return 0;
205 }