st/mesa: fix incorrect RowStride computation
[mesa.git] / progs / egl / xegl_tri.c
1 /*
2 * Copyright (C) 2008 Brian Paul All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22 /*
23 * Draw a triangle with X/EGL.
24 * Brian Paul
25 * 3 June 2008
26 */
27
28
29 #include <assert.h>
30 #include <math.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <X11/Xlib.h>
35 #include <X11/Xutil.h>
36 #include <X11/keysym.h>
37 #include <GL/gl.h> /* using full OpenGL for now */
38 #include <GLES/egl.h>
39
40
41 static GLfloat view_rotx = 0.0, view_roty = 0.0, view_rotz = 0.0;
42
43
44 static void
45 draw(void)
46 {
47 static const GLfloat verts[3][2] = {
48 { -1, -1 },
49 { 1, -1 },
50 { 0, 1 }
51 };
52 static const GLfloat colors[3][3] = {
53 { 1, 0, 0 },
54 { 0, 1, 0 },
55 { 0, 0, 1 }
56 };
57
58 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
59
60 glPushMatrix();
61 glRotatef(view_rotx, 1, 0, 0);
62 glRotatef(view_roty, 0, 1, 0);
63 glRotatef(view_rotz, 0, 0, 1);
64
65 {
66 glVertexPointer(2, GL_FLOAT, 0, verts);
67 glColorPointer(3, GL_FLOAT, 0, colors);
68 glEnableClientState(GL_VERTEX_ARRAY);
69 glEnableClientState(GL_COLOR_ARRAY);
70
71 glDrawArrays(GL_TRIANGLES, 0, 3);
72
73 glDisableClientState(GL_VERTEX_ARRAY);
74 glDisableClientState(GL_COLOR_ARRAY);
75 }
76
77 glPopMatrix();
78 }
79
80
81 /* new window size or exposure */
82 static void
83 reshape(int width, int height)
84 {
85 GLfloat ar = (GLfloat) width / (GLfloat) height;
86
87 glViewport(0, 0, (GLint) width, (GLint) height);
88
89 glMatrixMode(GL_PROJECTION);
90 glLoadIdentity();
91 glFrustum(-ar, ar, -1, 1, 5.0, 60.0);
92
93 glMatrixMode(GL_MODELVIEW);
94 glLoadIdentity();
95 glTranslatef(0.0, 0.0, -10.0);
96 }
97
98
99 static void
100 init(void)
101 {
102 glClearColor(0.4, 0.4, 0.4, 0.0);
103 }
104
105
106 /*
107 * Create an RGB, double-buffered X window.
108 * Return the window and context handles.
109 */
110 static void
111 make_x_window(Display *x_dpy, EGLDisplay egl_dpy,
112 const char *name,
113 int x, int y, int width, int height,
114 Window *winRet,
115 EGLContext *ctxRet,
116 EGLSurface *surfRet)
117 {
118 static const EGLint attribs[] = {
119 EGL_RED_SIZE, 1,
120 EGL_GREEN_SIZE, 1,
121 EGL_BLUE_SIZE, 1,
122 EGL_DEPTH_SIZE, 1,
123 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
124 EGL_NONE
125 };
126
127 int scrnum;
128 XSetWindowAttributes attr;
129 unsigned long mask;
130 Window root;
131 Window win;
132 XVisualInfo *visInfo, visTemplate;
133 int num_visuals;
134 EGLContext ctx;
135 EGLConfig config;
136 EGLint num_configs;
137 EGLint vid;
138
139 scrnum = DefaultScreen( x_dpy );
140 root = RootWindow( x_dpy, scrnum );
141
142 if (!eglChooseConfig( egl_dpy, attribs, &config, 1, &num_configs) ||
143 !num_configs) {
144 printf("Error: couldn't get an EGL visual config\n");
145 exit(1);
146 }
147
148 assert(config);
149 assert(num_configs > 0);
150
151 if (!eglGetConfigAttrib(egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) {
152 printf("Error: eglGetConfigAttrib() failed\n");
153 exit(1);
154 }
155
156 /* The X window visual must match the EGL config */
157 visTemplate.visualid = vid;
158 visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals);
159 if (!visInfo) {
160 printf("Error: couldn't get X visual\n");
161 exit(1);
162 }
163
164 /* window attributes */
165 attr.background_pixel = 0;
166 attr.border_pixel = 0;
167 attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone);
168 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
169 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
170
171 win = XCreateWindow( x_dpy, root, 0, 0, width, height,
172 0, visInfo->depth, InputOutput,
173 visInfo->visual, mask, &attr );
174
175 /* set hints and properties */
176 {
177 XSizeHints sizehints;
178 sizehints.x = x;
179 sizehints.y = y;
180 sizehints.width = width;
181 sizehints.height = height;
182 sizehints.flags = USSize | USPosition;
183 XSetNormalHints(x_dpy, win, &sizehints);
184 XSetStandardProperties(x_dpy, win, name, name,
185 None, (char **)NULL, 0, &sizehints);
186 }
187
188 eglBindAPI(EGL_OPENGL_API);
189
190 ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, NULL );
191 if (!ctx) {
192 printf("Error: glXCreateContext failed\n");
193 exit(1);
194 }
195
196 *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL);
197
198 if (!*surfRet) {
199 printf("Error: eglCreateWindowSurface failed\n");
200 exit(1);
201 }
202
203 XFree(visInfo);
204
205 *winRet = win;
206 *ctxRet = ctx;
207 }
208
209
210 static void
211 event_loop(Display *dpy, Window win,
212 EGLDisplay egl_dpy, EGLSurface egl_surf)
213 {
214 while (1) {
215 int redraw = 0;
216 XEvent event;
217
218 XNextEvent(dpy, &event);
219
220 switch (event.type) {
221 case Expose:
222 redraw = 1;
223 break;
224 case ConfigureNotify:
225 reshape(event.xconfigure.width, event.xconfigure.height);
226 break;
227 case KeyPress:
228 {
229 char buffer[10];
230 int r, code;
231 code = XLookupKeysym(&event.xkey, 0);
232 if (code == XK_Left) {
233 view_roty += 5.0;
234 }
235 else if (code == XK_Right) {
236 view_roty -= 5.0;
237 }
238 else if (code == XK_Up) {
239 view_rotx += 5.0;
240 }
241 else if (code == XK_Down) {
242 view_rotx -= 5.0;
243 }
244 else {
245 r = XLookupString(&event.xkey, buffer, sizeof(buffer),
246 NULL, NULL);
247 if (buffer[0] == 27) {
248 /* escape */
249 return;
250 }
251 }
252 }
253 redraw = 1;
254 break;
255 default:
256 ; /*no-op*/
257 }
258
259 if (redraw) {
260 draw();
261 eglSwapBuffers(egl_dpy, egl_surf);
262 }
263 }
264 }
265
266
267 static void
268 usage(void)
269 {
270 printf("Usage:\n");
271 printf(" -display <displayname> set the display to run on\n");
272 printf(" -info display OpenGL renderer info\n");
273 }
274
275
276 int
277 main(int argc, char *argv[])
278 {
279 const int winWidth = 300, winHeight = 300;
280 Display *x_dpy;
281 Window win;
282 EGLSurface egl_surf;
283 EGLContext egl_ctx;
284 EGLDisplay egl_dpy;
285 char *dpyName = NULL;
286 GLboolean printInfo = GL_FALSE;
287 EGLint egl_major, egl_minor;
288 int i;
289 const char *s;
290
291 for (i = 1; i < argc; i++) {
292 if (strcmp(argv[i], "-display") == 0) {
293 dpyName = argv[i+1];
294 i++;
295 }
296 else if (strcmp(argv[i], "-info") == 0) {
297 printInfo = GL_TRUE;
298 }
299 else {
300 usage();
301 return -1;
302 }
303 }
304
305 x_dpy = XOpenDisplay(dpyName);
306 if (!x_dpy) {
307 printf("Error: couldn't open display %s\n",
308 dpyName ? dpyName : getenv("DISPLAY"));
309 return -1;
310 }
311
312 egl_dpy = eglGetDisplay(x_dpy);
313 if (!egl_dpy) {
314 printf("Error: eglGetDisplay() failed\n");
315 return -1;
316 }
317
318 if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) {
319 printf("Error: eglInitialize() failed\n");
320 return -1;
321 }
322
323 s = eglQueryString(egl_dpy, EGL_VERSION);
324 printf("EGL_VERSION = %s\n", s);
325
326 make_x_window(x_dpy, egl_dpy,
327 "xegl_tri", 0, 0, winWidth, winHeight,
328 &win, &egl_ctx, &egl_surf);
329
330 XMapWindow(x_dpy, win);
331 if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) {
332 printf("Error: eglMakeCurrent() failed\n");
333 return -1;
334 }
335
336 if (printInfo) {
337 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
338 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
339 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
340 }
341
342 init();
343
344 /* Set initial projection/viewing transformation.
345 * We can't be sure we'll get a ConfigureNotify event when the window
346 * first appears.
347 */
348 reshape(winWidth, winHeight);
349
350 event_loop(x_dpy, win, egl_dpy, egl_surf);
351
352 eglDestroyContext(egl_dpy, egl_ctx);
353 eglDestroySurface(egl_dpy, egl_surf);
354 eglTerminate(egl_dpy);
355
356
357 XDestroyWindow(x_dpy, win);
358 XCloseDisplay(x_dpy);
359
360 return 0;
361 }