Merge branch 'asm-shader-rework-1'
[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, vid;
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 if (!eglGetConfigAttrib(egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) {
377 printf("Error: eglGetConfigAttrib() failed\n");
378 exit(1);
379 }
380
381 /* The X window visual must match the EGL config */
382 visTemplate.visualid = vid;
383 visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals);
384 if (!visInfo) {
385 printf("Error: couldn't get X visual\n");
386 exit(1);
387 }
388
389 /* window attributes */
390 attr.background_pixel = 0;
391 attr.border_pixel = 0;
392 attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone);
393 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
394 attr.override_redirect = fullscreen;
395 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect;
396
397 win = XCreateWindow( x_dpy, root, 0, 0, width, height,
398 0, visInfo->depth, InputOutput,
399 visInfo->visual, mask, &attr );
400
401 /* set hints and properties */
402 {
403 XSizeHints sizehints;
404 sizehints.x = x;
405 sizehints.y = y;
406 sizehints.width = width;
407 sizehints.height = height;
408 sizehints.flags = USSize | USPosition;
409 XSetNormalHints(x_dpy, win, &sizehints);
410 XSetStandardProperties(x_dpy, win, name, name,
411 None, (char **)NULL, 0, &sizehints);
412 }
413
414 eglBindAPI(EGL_OPENGL_API);
415
416 ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, NULL );
417 if (!ctx) {
418 printf("Error: glXCreateContext failed\n");
419 exit(1);
420 }
421
422 *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL);
423
424 XFree(visInfo);
425
426 *winRet = win;
427 *ctxRet = ctx;
428 }
429
430
431 static void
432 event_loop(Display *dpy, Window win,
433 EGLDisplay egl_dpy, EGLSurface egl_surf)
434 {
435 while (1) {
436 while (XPending(dpy) > 0) {
437 XEvent event;
438 XNextEvent(dpy, &event);
439 switch (event.type) {
440 case Expose:
441 /* we'll redraw below */
442 break;
443 case ConfigureNotify:
444 reshape(event.xconfigure.width, event.xconfigure.height);
445 break;
446 case KeyPress:
447 {
448 char buffer[10];
449 int r, code;
450 code = XLookupKeysym(&event.xkey, 0);
451 if (code == XK_Left) {
452 view_roty += 5.0;
453 }
454 else if (code == XK_Right) {
455 view_roty -= 5.0;
456 }
457 else if (code == XK_Up) {
458 view_rotx += 5.0;
459 }
460 else if (code == XK_Down) {
461 view_rotx -= 5.0;
462 }
463 else {
464 r = XLookupString(&event.xkey, buffer, sizeof(buffer),
465 NULL, NULL);
466 if (buffer[0] == 27) {
467 /* escape */
468 return;
469 }
470 }
471 }
472 }
473 }
474
475 {
476 static int frames = 0;
477 static double tRot0 = -1.0, tRate0 = -1.0;
478 double dt, t = current_time();
479 if (tRot0 < 0.0)
480 tRot0 = t;
481 dt = t - tRot0;
482 tRot0 = t;
483
484 /* advance rotation for next frame */
485 angle += 70.0 * dt; /* 70 degrees per second */
486 if (angle > 3600.0)
487 angle -= 3600.0;
488
489 draw();
490 eglSwapBuffers(egl_dpy, egl_surf);
491
492 frames++;
493
494 if (tRate0 < 0.0)
495 tRate0 = t;
496 if (t - tRate0 >= 5.0) {
497 GLfloat seconds = t - tRate0;
498 GLfloat fps = frames / seconds;
499 printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames, seconds,
500 fps);
501 tRate0 = t;
502 frames = 0;
503 }
504 }
505 }
506 }
507
508
509 static void
510 usage(void)
511 {
512 printf("Usage:\n");
513 printf(" -display <displayname> set the display to run on\n");
514 printf(" -fullscreen run in fullscreen mode\n");
515 printf(" -info display OpenGL renderer info\n");
516 }
517
518
519 int
520 main(int argc, char *argv[])
521 {
522 const int winWidth = 300, winHeight = 300;
523 Display *x_dpy;
524 Window win;
525 EGLSurface egl_surf;
526 EGLContext egl_ctx;
527 EGLDisplay egl_dpy;
528 char *dpyName = NULL;
529 GLboolean printInfo = GL_FALSE;
530 EGLint egl_major, egl_minor;
531 int i;
532 const char *s;
533
534 for (i = 1; i < argc; i++) {
535 if (strcmp(argv[i], "-display") == 0) {
536 dpyName = argv[i+1];
537 i++;
538 }
539 else if (strcmp(argv[i], "-info") == 0) {
540 printInfo = GL_TRUE;
541 }
542 else if (strcmp(argv[i], "-fullscreen") == 0) {
543 fullscreen = GL_TRUE;
544 }
545 else {
546 usage();
547 return -1;
548 }
549 }
550
551 x_dpy = XOpenDisplay(dpyName);
552 if (!x_dpy) {
553 printf("Error: couldn't open display %s\n",
554 dpyName ? dpyName : getenv("DISPLAY"));
555 return -1;
556 }
557
558 egl_dpy = eglGetDisplay(x_dpy);
559 if (!egl_dpy) {
560 printf("Error: eglGetDisplay() failed\n");
561 return -1;
562 }
563
564 if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) {
565 printf("Error: eglInitialize() failed\n");
566 return -1;
567 }
568
569 s = eglQueryString(egl_dpy, EGL_VERSION);
570 printf("EGL_VERSION = %s\n", s);
571
572 make_x_window(x_dpy, egl_dpy,
573 "glxgears", 0, 0, winWidth, winHeight,
574 &win, &egl_ctx, &egl_surf);
575
576 XMapWindow(x_dpy, win);
577 eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx);
578
579 if (printInfo) {
580 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
581 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
582 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
583 }
584
585 init();
586
587 /* Set initial projection/viewing transformation.
588 * We can't be sure we'll get a ConfigureNotify event when the window
589 * first appears.
590 */
591 reshape(winWidth, winHeight);
592
593 event_loop(x_dpy, win, egl_dpy, egl_surf);
594
595 glDeleteLists(gear1, 1);
596 glDeleteLists(gear2, 1);
597 glDeleteLists(gear3, 1);
598
599 eglDestroyContext(egl_dpy, egl_ctx);
600 eglDestroySurface(egl_dpy, egl_surf);
601 eglTerminate(egl_dpy);
602
603
604 XDestroyWindow(x_dpy, win);
605 XCloseDisplay(x_dpy);
606
607 return 0;
608 }