egl: updated egl.h include paths
[mesa.git] / progs / egl / xeglgears.c
1 /*
2 * Copyright (C) 1999-2001 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 * Ported to X/EGL/GLES. XXX Actually, uses full OpenGL ATM.
24 * Brian Paul
25 * 30 May 2008
26 */
27
28 /*
29 * Command line options:
30 * -info print GL implementation information
31 *
32 */
33
34
35 #include <math.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <X11/Xlib.h>
40 #include <X11/Xutil.h>
41 #include <X11/keysym.h>
42 #include <GL/gl.h>
43 #include <EGL/egl.h>
44
45
46 #define BENCHMARK
47
48 #ifdef BENCHMARK
49
50 /* XXX this probably isn't very portable */
51
52 #include <sys/time.h>
53 #include <unistd.h>
54
55 /* return current time (in seconds) */
56 static double
57 current_time(void)
58 {
59 struct timeval tv;
60 #ifdef __VMS
61 (void) gettimeofday(&tv, NULL );
62 #else
63 struct timezone tz;
64 (void) gettimeofday(&tv, &tz);
65 #endif
66 return (double) tv.tv_sec + tv.tv_usec / 1000000.0;
67 }
68
69 #else /*BENCHMARK*/
70
71 /* dummy */
72 static double
73 current_time(void)
74 {
75 /* update this function for other platforms! */
76 static double t = 0.0;
77 static int warn = 1;
78 if (warn) {
79 fprintf(stderr, "Warning: current_time() not implemented!!\n");
80 warn = 0;
81 }
82 return t += 1.0;
83 }
84
85 #endif /*BENCHMARK*/
86
87
88
89 #ifndef M_PI
90 #define M_PI 3.14159265
91 #endif
92
93
94 static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
95 static GLint gear1, gear2, gear3;
96 static GLfloat angle = 0.0;
97
98 static GLboolean fullscreen = GL_FALSE; /* Create a single fullscreen window */
99
100
101 /*
102 *
103 * Draw a gear wheel. You'll probably want to call this function when
104 * building a display list since we do a lot of trig here.
105 *
106 * Input: inner_radius - radius of hole at center
107 * outer_radius - radius at center of teeth
108 * width - width of gear
109 * teeth - number of teeth
110 * tooth_depth - depth of tooth
111 */
112 static void
113 gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
114 GLint teeth, GLfloat tooth_depth)
115 {
116 GLint i;
117 GLfloat r0, r1, r2;
118 GLfloat angle, da;
119 GLfloat u, v, len;
120
121 r0 = inner_radius;
122 r1 = outer_radius - tooth_depth / 2.0;
123 r2 = outer_radius + tooth_depth / 2.0;
124
125 da = 2.0 * M_PI / teeth / 4.0;
126
127 glShadeModel(GL_FLAT);
128
129 glNormal3f(0.0, 0.0, 1.0);
130
131 /* draw front face */
132 glBegin(GL_QUAD_STRIP);
133 for (i = 0; i <= teeth; i++) {
134 angle = i * 2.0 * M_PI / teeth;
135 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
136 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
137 if (i < teeth) {
138 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
139 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
140 width * 0.5);
141 }
142 }
143 glEnd();
144
145 /* draw front sides of teeth */
146 glBegin(GL_QUADS);
147 da = 2.0 * M_PI / teeth / 4.0;
148 for (i = 0; i < teeth; i++) {
149 angle = i * 2.0 * M_PI / teeth;
150
151 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
152 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
153 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
154 width * 0.5);
155 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
156 width * 0.5);
157 }
158 glEnd();
159
160 glNormal3f(0.0, 0.0, -1.0);
161
162 /* draw back face */
163 glBegin(GL_QUAD_STRIP);
164 for (i = 0; i <= teeth; i++) {
165 angle = i * 2.0 * M_PI / teeth;
166 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
167 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
168 if (i < teeth) {
169 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
170 -width * 0.5);
171 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
172 }
173 }
174 glEnd();
175
176 /* draw back sides of teeth */
177 glBegin(GL_QUADS);
178 da = 2.0 * M_PI / teeth / 4.0;
179 for (i = 0; i < teeth; i++) {
180 angle = i * 2.0 * M_PI / teeth;
181
182 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
183 -width * 0.5);
184 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
185 -width * 0.5);
186 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
187 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
188 }
189 glEnd();
190
191 /* draw outward faces of teeth */
192 glBegin(GL_QUAD_STRIP);
193 for (i = 0; i < teeth; i++) {
194 angle = i * 2.0 * M_PI / teeth;
195
196 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
197 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
198 u = r2 * cos(angle + da) - r1 * cos(angle);
199 v = r2 * sin(angle + da) - r1 * sin(angle);
200 len = sqrt(u * u + v * v);
201 u /= len;
202 v /= len;
203 glNormal3f(v, -u, 0.0);
204 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
205 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
206 glNormal3f(cos(angle), sin(angle), 0.0);
207 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
208 width * 0.5);
209 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
210 -width * 0.5);
211 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
212 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
213 glNormal3f(v, -u, 0.0);
214 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
215 width * 0.5);
216 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
217 -width * 0.5);
218 glNormal3f(cos(angle), sin(angle), 0.0);
219 }
220
221 glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
222 glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
223
224 glEnd();
225
226 glShadeModel(GL_SMOOTH);
227
228 /* draw inside radius cylinder */
229 glBegin(GL_QUAD_STRIP);
230 for (i = 0; i <= teeth; i++) {
231 angle = i * 2.0 * M_PI / teeth;
232 glNormal3f(-cos(angle), -sin(angle), 0.0);
233 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
234 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
235 }
236 glEnd();
237 }
238
239
240 static void
241 draw(void)
242 {
243 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
244
245 glPushMatrix();
246 glRotatef(view_rotx, 1.0, 0.0, 0.0);
247 glRotatef(view_roty, 0.0, 1.0, 0.0);
248 glRotatef(view_rotz, 0.0, 0.0, 1.0);
249
250 glPushMatrix();
251 glTranslatef(-3.0, -2.0, 0.0);
252 glRotatef(angle, 0.0, 0.0, 1.0);
253 glCallList(gear1);
254 glPopMatrix();
255
256 glPushMatrix();
257 glTranslatef(3.1, -2.0, 0.0);
258 glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);
259 glCallList(gear2);
260 glPopMatrix();
261
262 glPushMatrix();
263 glTranslatef(-3.1, 4.2, 0.0);
264 glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
265 glCallList(gear3);
266 glPopMatrix();
267
268 glPopMatrix();
269 }
270
271
272 /* new window size or exposure */
273 static void
274 reshape(int width, int height)
275 {
276 GLfloat ar = (GLfloat) width / (GLfloat) height;
277
278 glViewport(0, 0, (GLint) width, (GLint) height);
279
280 glMatrixMode(GL_PROJECTION);
281 glLoadIdentity();
282 glFrustum(-ar, ar, -1, 1, 5.0, 60.0);
283
284 glMatrixMode(GL_MODELVIEW);
285 glLoadIdentity();
286 glTranslatef(0.0, 0.0, -40.0);
287 }
288
289
290
291 static void
292 init(void)
293 {
294 static GLfloat pos[4] = { 5.0, 5.0, 10.0, 0.0 };
295 static GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 };
296 static GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 };
297 static GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 };
298
299 glLightfv(GL_LIGHT0, GL_POSITION, pos);
300 glEnable(GL_CULL_FACE);
301 glEnable(GL_LIGHTING);
302 glEnable(GL_LIGHT0);
303 glEnable(GL_DEPTH_TEST);
304
305 /* make the gears */
306 gear1 = glGenLists(1);
307 glNewList(gear1, GL_COMPILE);
308 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
309 gear(1.0, 4.0, 1.0, 20, 0.7);
310 glEndList();
311
312 gear2 = glGenLists(1);
313 glNewList(gear2, GL_COMPILE);
314 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
315 gear(0.5, 2.0, 2.0, 10, 0.7);
316 glEndList();
317
318 gear3 = glGenLists(1);
319 glNewList(gear3, GL_COMPILE);
320 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
321 gear(1.3, 2.0, 0.5, 10, 0.7);
322 glEndList();
323
324 glEnable(GL_NORMALIZE);
325
326 glClearColor(0.2, 0.2, 0.2, 0.0);
327 }
328
329
330 /*
331 * Create an RGB, double-buffered X window.
332 * Return the window and context handles.
333 */
334 static void
335 make_x_window(Display *x_dpy, EGLDisplay egl_dpy,
336 const char *name,
337 int x, int y, int width, int height,
338 Window *winRet,
339 EGLContext *ctxRet,
340 EGLSurface *surfRet)
341 {
342 static const EGLint attribs[] = {
343 EGL_RED_SIZE, 1,
344 EGL_GREEN_SIZE, 1,
345 EGL_BLUE_SIZE, 1,
346 /*EGL_DOUBLEBUFFER,*/
347 EGL_DEPTH_SIZE, 1,
348 EGL_NONE
349 };
350
351 int scrnum;
352 XSetWindowAttributes attr;
353 unsigned long mask;
354 Window root;
355 Window win;
356 XVisualInfo *visInfo, visTemplate;
357 int num_visuals;
358 EGLContext ctx;
359 EGLConfig config;
360 EGLint num_configs;
361
362 scrnum = DefaultScreen( x_dpy );
363 root = RootWindow( x_dpy, scrnum );
364
365 if (fullscreen) {
366 x = 0; y = 0;
367 width = DisplayWidth( x_dpy, scrnum );
368 height = DisplayHeight( x_dpy, scrnum );
369 }
370
371 if (!eglChooseConfig( egl_dpy, attribs, &config, 1, &num_configs)) {
372 printf("Error: couldn't get an EGL visual config\n");
373 exit(1);
374 }
375
376 /* choose X window visual similar to EGL config */
377 visTemplate.screen = DefaultScreen(x_dpy);
378 visTemplate.depth = 32; /* 24? */
379 visInfo = XGetVisualInfo(x_dpy,
380 (VisualDepthMask | VisualScreenMask),
381 &visTemplate, &num_visuals);
382 if (!visInfo) {
383 printf("Error: couldn't get X visual\n");
384 exit(1);
385 }
386
387 /* window attributes */
388 attr.background_pixel = 0;
389 attr.border_pixel = 0;
390 attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone);
391 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
392 attr.override_redirect = fullscreen;
393 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect;
394
395 win = XCreateWindow( x_dpy, root, 0, 0, width, height,
396 0, visInfo->depth, InputOutput,
397 visInfo->visual, mask, &attr );
398
399 /* set hints and properties */
400 {
401 XSizeHints sizehints;
402 sizehints.x = x;
403 sizehints.y = y;
404 sizehints.width = width;
405 sizehints.height = height;
406 sizehints.flags = USSize | USPosition;
407 XSetNormalHints(x_dpy, win, &sizehints);
408 XSetStandardProperties(x_dpy, win, name, name,
409 None, (char **)NULL, 0, &sizehints);
410 }
411
412 eglBindAPI(EGL_OPENGL_API);
413
414 ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, NULL );
415 if (!ctx) {
416 printf("Error: glXCreateContext failed\n");
417 exit(1);
418 }
419
420 *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL);
421
422 XFree(visInfo);
423
424 *winRet = win;
425 *ctxRet = ctx;
426 }
427
428
429 static void
430 event_loop(Display *dpy, Window win,
431 EGLDisplay egl_dpy, EGLSurface egl_surf)
432 {
433 while (1) {
434 while (XPending(dpy) > 0) {
435 XEvent event;
436 XNextEvent(dpy, &event);
437 switch (event.type) {
438 case Expose:
439 /* we'll redraw below */
440 break;
441 case ConfigureNotify:
442 reshape(event.xconfigure.width, event.xconfigure.height);
443 break;
444 case KeyPress:
445 {
446 char buffer[10];
447 int r, code;
448 code = XLookupKeysym(&event.xkey, 0);
449 if (code == XK_Left) {
450 view_roty += 5.0;
451 }
452 else if (code == XK_Right) {
453 view_roty -= 5.0;
454 }
455 else if (code == XK_Up) {
456 view_rotx += 5.0;
457 }
458 else if (code == XK_Down) {
459 view_rotx -= 5.0;
460 }
461 else {
462 r = XLookupString(&event.xkey, buffer, sizeof(buffer),
463 NULL, NULL);
464 if (buffer[0] == 27) {
465 /* escape */
466 return;
467 }
468 }
469 }
470 }
471 }
472
473 {
474 static int frames = 0;
475 static double tRot0 = -1.0, tRate0 = -1.0;
476 double dt, t = current_time();
477 if (tRot0 < 0.0)
478 tRot0 = t;
479 dt = t - tRot0;
480 tRot0 = t;
481
482 /* advance rotation for next frame */
483 angle += 70.0 * dt; /* 70 degrees per second */
484 if (angle > 3600.0)
485 angle -= 3600.0;
486
487 draw();
488 eglSwapBuffers(egl_dpy, egl_surf);
489
490 frames++;
491
492 if (tRate0 < 0.0)
493 tRate0 = t;
494 if (t - tRate0 >= 5.0) {
495 GLfloat seconds = t - tRate0;
496 GLfloat fps = frames / seconds;
497 printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames, seconds,
498 fps);
499 tRate0 = t;
500 frames = 0;
501 }
502 }
503 }
504 }
505
506
507 static void
508 usage(void)
509 {
510 printf("Usage:\n");
511 printf(" -display <displayname> set the display to run on\n");
512 printf(" -fullscreen run in fullscreen mode\n");
513 printf(" -info display OpenGL renderer info\n");
514 }
515
516
517 int
518 main(int argc, char *argv[])
519 {
520 const int winWidth = 300, winHeight = 300;
521 Display *x_dpy;
522 Window win;
523 EGLSurface egl_surf;
524 EGLContext egl_ctx;
525 EGLDisplay egl_dpy;
526 char *dpyName = NULL;
527 GLboolean printInfo = GL_FALSE;
528 EGLint egl_major, egl_minor;
529 int i;
530 const char *s;
531
532 for (i = 1; i < argc; i++) {
533 if (strcmp(argv[i], "-display") == 0) {
534 dpyName = argv[i+1];
535 i++;
536 }
537 else if (strcmp(argv[i], "-info") == 0) {
538 printInfo = GL_TRUE;
539 }
540 else if (strcmp(argv[i], "-fullscreen") == 0) {
541 fullscreen = GL_TRUE;
542 }
543 else {
544 usage();
545 return -1;
546 }
547 }
548
549 x_dpy = XOpenDisplay(dpyName);
550 if (!x_dpy) {
551 printf("Error: couldn't open display %s\n",
552 dpyName ? dpyName : getenv("DISPLAY"));
553 return -1;
554 }
555
556 egl_dpy = eglGetDisplay(x_dpy);
557 if (!egl_dpy) {
558 printf("Error: eglGetDisplay() failed\n");
559 return -1;
560 }
561
562 if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) {
563 printf("Error: eglInitialize() failed\n");
564 return -1;
565 }
566
567 s = eglQueryString(egl_dpy, EGL_VERSION);
568 printf("EGL_VERSION = %s\n", s);
569
570 make_x_window(x_dpy, egl_dpy,
571 "glxgears", 0, 0, winWidth, winHeight,
572 &win, &egl_ctx, &egl_surf);
573
574 XMapWindow(x_dpy, win);
575 eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx);
576
577 if (printInfo) {
578 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
579 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
580 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
581 }
582
583 init();
584
585 /* Set initial projection/viewing transformation.
586 * We can't be sure we'll get a ConfigureNotify event when the window
587 * first appears.
588 */
589 reshape(winWidth, winHeight);
590
591 event_loop(x_dpy, win, egl_dpy, egl_surf);
592
593 glDeleteLists(gear1, 1);
594 glDeleteLists(gear2, 1);
595 glDeleteLists(gear3, 1);
596
597 eglDestroyContext(egl_dpy, egl_ctx);
598 eglDestroySurface(egl_dpy, egl_surf);
599 eglTerminate(egl_dpy);
600
601
602 XDestroyWindow(x_dpy, win);
603 XCloseDisplay(x_dpy);
604
605 return 0;
606 }