gl: updated glxext.h to version 27
[mesa.git] / progs / es1 / 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 and OpenGL ES 1.x
24 * Brian Paul
25 * 5 June 2008
26 */
27
28 #define USE_FULL_GL 0
29
30 #define USE_FIXED_POINT 0
31
32
33 #include <assert.h>
34 #include <math.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <X11/Xlib.h>
39 #include <X11/Xutil.h>
40 #include <X11/keysym.h>
41 #if USE_FULL_GL
42 #include <GL/gl.h> /* use full OpenGL */
43 #else
44 #include <GLES/gl.h> /* use OpenGL ES 1.x */
45 #include <GLES/glext.h>
46 #endif
47 #include <EGL/egl.h>
48
49
50 #define FLOAT_TO_FIXED(X) ((X) * 65535.0)
51
52
53
54 static GLfloat view_rotx = 0.0, view_roty = 0.0, view_rotz = 0.0;
55
56
57 static void
58 draw(void)
59 {
60 #if USE_FIXED_POINT
61 static const GLfixed verts[3][2] = {
62 { -65536, -65536 },
63 { 65536, -65536 },
64 { 0, 65536 }
65 };
66 static const GLfixed colors[3][4] = {
67 { 65536, 0, 0, 65536 },
68 { 0, 65536, 0 , 65536},
69 { 0, 0, 65536 , 65536}
70 };
71 #else
72 static const GLfloat verts[3][2] = {
73 { -1, -1 },
74 { 1, -1 },
75 { 0, 1 }
76 };
77 static const GLfloat colors[3][4] = {
78 { 1, 0, 0, 1 },
79 { 0, 1, 0, 1 },
80 { 0, 0, 1, 1 }
81 };
82 #endif
83
84 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
85
86 glPushMatrix();
87 glRotatef(view_rotx, 1, 0, 0);
88 glRotatef(view_roty, 0, 1, 0);
89 glRotatef(view_rotz, 0, 0, 1);
90
91 {
92 #if USE_FIXED_POINT
93 glVertexPointer(2, GL_FIXED, 0, verts);
94 glColorPointer(4, GL_FIXED, 0, colors);
95 #else
96 glVertexPointer(2, GL_FLOAT, 0, verts);
97 glColorPointer(4, GL_FLOAT, 0, colors);
98 #endif
99 glEnableClientState(GL_VERTEX_ARRAY);
100 glEnableClientState(GL_COLOR_ARRAY);
101
102 /* draw triangle */
103 glDrawArrays(GL_TRIANGLES, 0, 3);
104
105 /* draw some points */
106 glPointSizex(FLOAT_TO_FIXED(15.5));
107 glDrawArrays(GL_POINTS, 0, 3);
108
109 glDisableClientState(GL_VERTEX_ARRAY);
110 glDisableClientState(GL_COLOR_ARRAY);
111 }
112
113 if (0) {
114 /* test code */
115 GLfixed size;
116 glGetFixedv(GL_POINT_SIZE, &size);
117 printf("GL_POINT_SIZE = 0x%x %f\n", size, size / 65536.0);
118 }
119
120 glPopMatrix();
121 }
122
123
124 /* new window size or exposure */
125 static void
126 reshape(int width, int height)
127 {
128 GLfloat ar = (GLfloat) width / (GLfloat) height;
129
130 glViewport(0, 0, (GLint) width, (GLint) height);
131
132 glMatrixMode(GL_PROJECTION);
133 glLoadIdentity();
134 #ifdef GL_VERSION_ES_CM_1_0
135 glFrustumf(-ar, ar, -1, 1, 5.0, 60.0);
136 #else
137 glFrustum(-ar, ar, -1, 1, 5.0, 60.0);
138 #endif
139
140 glMatrixMode(GL_MODELVIEW);
141 glLoadIdentity();
142 glTranslatef(0.0, 0.0, -10.0);
143 }
144
145
146 static void
147 test_query_matrix(void)
148 {
149 PFNGLQUERYMATRIXXOESPROC procQueryMatrixx;
150 typedef void (*voidproc)();
151 GLfixed mantissa[16];
152 GLint exponent[16];
153 GLbitfield rv;
154 int i;
155
156 procQueryMatrixx = (PFNGLQUERYMATRIXXOESPROC) eglGetProcAddress("glQueryMatrixxOES");
157 assert(procQueryMatrixx);
158 /* Actually try out this one */
159 rv = (*procQueryMatrixx)(mantissa, exponent);
160 for (i = 0; i < 16; i++) {
161 if (rv & (1<<i)) {
162 printf("matrix[%d] invalid\n", i);
163 }
164 else {
165 printf("matrix[%d] = %f * 2^(%d)\n", i, mantissa[i]/65536.0, exponent[i]);
166 }
167 }
168 assert(!eglGetProcAddress("glFoo"));
169 }
170
171
172 static void
173 init(void)
174 {
175 glClearColor(0.4, 0.4, 0.4, 0.0);
176
177 if (0)
178 test_query_matrix();
179 }
180
181
182 /*
183 * Create an RGB, double-buffered X window.
184 * Return the window and context handles.
185 */
186 static void
187 make_x_window(Display *x_dpy, EGLDisplay egl_dpy,
188 const char *name,
189 int x, int y, int width, int height,
190 Window *winRet,
191 EGLContext *ctxRet,
192 EGLSurface *surfRet)
193 {
194 static const EGLint attribs[] = {
195 EGL_RED_SIZE, 1,
196 EGL_GREEN_SIZE, 1,
197 EGL_BLUE_SIZE, 1,
198 EGL_DEPTH_SIZE, 1,
199 EGL_NONE
200 };
201
202 int scrnum;
203 XSetWindowAttributes attr;
204 unsigned long mask;
205 Window root;
206 Window win;
207 XVisualInfo *visInfo, visTemplate;
208 int num_visuals;
209 EGLContext ctx;
210 EGLConfig config;
211 EGLint num_configs;
212 EGLint vid;
213
214 scrnum = DefaultScreen( x_dpy );
215 root = RootWindow( x_dpy, scrnum );
216
217 if (!eglChooseConfig( egl_dpy, attribs, &config, 1, &num_configs)) {
218 printf("Error: couldn't get an EGL visual config\n");
219 exit(1);
220 }
221
222 assert(config);
223 assert(num_configs > 0);
224
225 if (!eglGetConfigAttrib(egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) {
226 printf("Error: eglGetConfigAttrib() failed\n");
227 exit(1);
228 }
229
230 /* The X window visual must match the EGL config */
231 visTemplate.visualid = vid;
232 visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals);
233 if (!visInfo) {
234 printf("Error: couldn't get X visual\n");
235 exit(1);
236 }
237
238 /* window attributes */
239 attr.background_pixel = 0;
240 attr.border_pixel = 0;
241 attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone);
242 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
243 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
244
245 win = XCreateWindow( x_dpy, root, 0, 0, width, height,
246 0, visInfo->depth, InputOutput,
247 visInfo->visual, mask, &attr );
248
249 /* set hints and properties */
250 {
251 XSizeHints sizehints;
252 sizehints.x = x;
253 sizehints.y = y;
254 sizehints.width = width;
255 sizehints.height = height;
256 sizehints.flags = USSize | USPosition;
257 XSetNormalHints(x_dpy, win, &sizehints);
258 XSetStandardProperties(x_dpy, win, name, name,
259 None, (char **)NULL, 0, &sizehints);
260 }
261
262 #if USE_FULL_GL
263 eglBindAPI(EGL_OPENGL_API);
264 #else
265 eglBindAPI(EGL_OPENGL_ES_API);
266 #endif
267
268 ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, NULL );
269 if (!ctx) {
270 printf("Error: eglCreateContext failed\n");
271 exit(1);
272 }
273
274 *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL);
275
276 if (!*surfRet) {
277 printf("Error: eglCreateWindowSurface failed\n");
278 exit(1);
279 }
280
281 XFree(visInfo);
282
283 *winRet = win;
284 *ctxRet = ctx;
285 }
286
287
288 static void
289 event_loop(Display *dpy, Window win,
290 EGLDisplay egl_dpy, EGLSurface egl_surf)
291 {
292 while (1) {
293 int redraw = 0;
294 XEvent event;
295
296 XNextEvent(dpy, &event);
297
298 switch (event.type) {
299 case Expose:
300 redraw = 1;
301 break;
302 case ConfigureNotify:
303 reshape(event.xconfigure.width, event.xconfigure.height);
304 break;
305 case KeyPress:
306 {
307 char buffer[10];
308 int r, code;
309 code = XLookupKeysym(&event.xkey, 0);
310 if (code == XK_Left) {
311 view_roty += 5.0;
312 }
313 else if (code == XK_Right) {
314 view_roty -= 5.0;
315 }
316 else if (code == XK_Up) {
317 view_rotx += 5.0;
318 }
319 else if (code == XK_Down) {
320 view_rotx -= 5.0;
321 }
322 else {
323 r = XLookupString(&event.xkey, buffer, sizeof(buffer),
324 NULL, NULL);
325 if (buffer[0] == 27) {
326 /* escape */
327 return;
328 }
329 }
330 }
331 redraw = 1;
332 break;
333 default:
334 ; /*no-op*/
335 }
336
337 if (redraw) {
338 draw();
339 eglSwapBuffers(egl_dpy, egl_surf);
340 }
341 }
342 }
343
344
345 static void
346 usage(void)
347 {
348 printf("Usage:\n");
349 printf(" -display <displayname> set the display to run on\n");
350 printf(" -info display OpenGL renderer info\n");
351 }
352
353
354 int
355 main(int argc, char *argv[])
356 {
357 const int winWidth = 300, winHeight = 300;
358 Display *x_dpy;
359 Window win;
360 EGLSurface egl_surf;
361 EGLContext egl_ctx;
362 EGLDisplay egl_dpy;
363 char *dpyName = NULL;
364 GLboolean printInfo = GL_FALSE;
365 EGLint egl_major, egl_minor;
366 int i;
367 const char *s;
368
369 static struct {
370 char *name;
371 GLenum value;
372 enum {GetString, GetInteger} type;
373 } info_items[] = {
374 {"GL_RENDERER", GL_RENDERER, GetString},
375 {"GL_VERSION", GL_VERSION, GetString},
376 {"GL_VENDOR", GL_VENDOR, GetString},
377 {"GL_EXTENSIONS", GL_EXTENSIONS, GetString},
378 {"GL_MAX_PALETTE_MATRICES_OES", GL_MAX_PALETTE_MATRICES_OES, GetInteger},
379 {"GL_MAX_VERTEX_UNITS_OES", GL_MAX_VERTEX_UNITS_OES, GetInteger},
380 };
381
382 for (i = 1; i < argc; i++) {
383 if (strcmp(argv[i], "-display") == 0) {
384 dpyName = argv[i+1];
385 i++;
386 }
387 else if (strcmp(argv[i], "-info") == 0) {
388 printInfo = GL_TRUE;
389 }
390 else {
391 usage();
392 return -1;
393 }
394 }
395
396 x_dpy = XOpenDisplay(dpyName);
397 if (!x_dpy) {
398 printf("Error: couldn't open display %s\n",
399 dpyName ? dpyName : getenv("DISPLAY"));
400 return -1;
401 }
402
403 egl_dpy = eglGetDisplay(x_dpy);
404 if (!egl_dpy) {
405 printf("Error: eglGetDisplay() failed\n");
406 return -1;
407 }
408
409 if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) {
410 printf("Error: eglInitialize() failed\n");
411 return -1;
412 }
413
414 s = eglQueryString(egl_dpy, EGL_VERSION);
415 printf("EGL_VERSION = %s\n", s);
416
417 s = eglQueryString(egl_dpy, EGL_VENDOR);
418 printf("EGL_VENDOR = %s\n", s);
419
420 s = eglQueryString(egl_dpy, EGL_EXTENSIONS);
421 printf("EGL_EXTENSIONS = %s\n", s);
422
423 s = eglQueryString(egl_dpy, EGL_CLIENT_APIS);
424 printf("EGL_CLIENT_APIS = %s\n", s);
425
426 make_x_window(x_dpy, egl_dpy,
427 "OpenGL ES 1.x tri", 0, 0, winWidth, winHeight,
428 &win, &egl_ctx, &egl_surf);
429
430 XMapWindow(x_dpy, win);
431 if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) {
432 printf("Error: eglMakeCurrent() failed\n");
433 return -1;
434 }
435
436 if (printInfo) {
437 for (i = 0; i < sizeof(info_items)/sizeof(info_items[0]); i++) {
438 switch (info_items[i].type) {
439 case GetString:
440 printf("%s = %s\n", info_items[i].name, (char *)glGetString(info_items[i].value));
441 break;
442 case GetInteger: {
443 GLint rv = -1;
444 glGetIntegerv(info_items[i].value, &rv);
445 printf("%s = %d\n", info_items[i].name, rv);
446 break;
447 }
448 }
449 }
450 };
451 init();
452
453 /* Set initial projection/viewing transformation.
454 * We can't be sure we'll get a ConfigureNotify event when the window
455 * first appears.
456 */
457 reshape(winWidth, winHeight);
458
459 event_loop(x_dpy, win, egl_dpy, egl_surf);
460
461 eglDestroyContext(egl_dpy, egl_ctx);
462 eglDestroySurface(egl_dpy, egl_surf);
463 eglTerminate(egl_dpy);
464
465
466 XDestroyWindow(x_dpy, win);
467 XCloseDisplay(x_dpy);
468
469 return 0;
470 }