WIP - port to Mali EGL
[mesa-demos/mali.git] / src / xdemos / glxcontexts.c
blob0f7f6d2558ace6e7bb36f50cdf3543c032029683
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:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
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.
23 * Version of glxgears that creates/destroys the rendering context for each
24 * frame. Also periodically destroy/recreate the window.
25 * Good for finding memory leaks, etc.
27 * Command line options:
28 * -info print GL implementation information
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/keysym.h>
40 #include <GL/gl.h>
41 #include <GL/glx.h>
44 #define BENCHMARK
46 #ifdef BENCHMARK
48 /* XXX this probably isn't very portable */
50 #include <sys/time.h>
51 #include <unistd.h>
53 /* return current time (in seconds) */
54 static double
55 current_time(void)
57 struct timeval tv;
58 #ifdef __VMS
59 (void) gettimeofday(&tv, NULL );
60 #else
61 struct timezone tz;
62 (void) gettimeofday(&tv, &tz);
63 #endif
64 return (double) tv.tv_sec + tv.tv_usec / 1000000.0;
67 #else /*BENCHMARK*/
69 /* dummy */
70 static double
71 current_time(void)
73 /* update this function for other platforms! */
74 static double t = 0.0;
75 static int warn = 1;
76 if (warn) {
77 fprintf(stderr, "Warning: current_time() not implemented!!\n");
78 warn = 0;
80 return t += 1.0;
83 #endif /*BENCHMARK*/
87 #ifndef M_PI
88 #define M_PI 3.14159265
89 #endif
92 static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
93 static GLint gear1, gear2, gear3;
94 static GLfloat angle = 0.0;
96 static XVisualInfo *visinfo = NULL;
97 static int WinWidth = 300, WinHeight = 300;
102 * Draw a gear wheel. You'll probably want to call this function when
103 * building a display list since we do a lot of trig here.
105 * Input: inner_radius - radius of hole at center
106 * outer_radius - radius at center of teeth
107 * width - width of gear
108 * teeth - number of teeth
109 * tooth_depth - depth of tooth
111 static void
112 gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
113 GLint teeth, GLfloat tooth_depth)
115 GLint i;
116 GLfloat r0, r1, r2;
117 GLfloat angle, da;
118 GLfloat u, v, len;
120 r0 = inner_radius;
121 r1 = outer_radius - tooth_depth / 2.0;
122 r2 = outer_radius + tooth_depth / 2.0;
124 da = 2.0 * M_PI / teeth / 4.0;
126 glShadeModel(GL_FLAT);
128 glNormal3f(0.0, 0.0, 1.0);
130 /* draw front face */
131 glBegin(GL_QUAD_STRIP);
132 for (i = 0; i <= teeth; i++) {
133 angle = i * 2.0 * M_PI / teeth;
134 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
135 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
136 if (i < teeth) {
137 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
138 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
139 width * 0.5);
142 glEnd();
144 /* draw front sides of teeth */
145 glBegin(GL_QUADS);
146 da = 2.0 * M_PI / teeth / 4.0;
147 for (i = 0; i < teeth; i++) {
148 angle = i * 2.0 * M_PI / teeth;
150 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
151 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
152 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
153 width * 0.5);
154 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
155 width * 0.5);
157 glEnd();
159 glNormal3f(0.0, 0.0, -1.0);
161 /* draw back face */
162 glBegin(GL_QUAD_STRIP);
163 for (i = 0; i <= teeth; i++) {
164 angle = i * 2.0 * M_PI / teeth;
165 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
166 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
167 if (i < teeth) {
168 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
169 -width * 0.5);
170 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
173 glEnd();
175 /* draw back sides of teeth */
176 glBegin(GL_QUADS);
177 da = 2.0 * M_PI / teeth / 4.0;
178 for (i = 0; i < teeth; i++) {
179 angle = i * 2.0 * M_PI / teeth;
181 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
182 -width * 0.5);
183 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
184 -width * 0.5);
185 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
186 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
188 glEnd();
190 /* draw outward faces of teeth */
191 glBegin(GL_QUAD_STRIP);
192 for (i = 0; i < teeth; i++) {
193 angle = i * 2.0 * M_PI / teeth;
195 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
196 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
197 u = r2 * cos(angle + da) - r1 * cos(angle);
198 v = r2 * sin(angle + da) - r1 * sin(angle);
199 len = sqrt(u * u + v * v);
200 u /= len;
201 v /= len;
202 glNormal3f(v, -u, 0.0);
203 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
204 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
205 glNormal3f(cos(angle), sin(angle), 0.0);
206 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
207 width * 0.5);
208 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
209 -width * 0.5);
210 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
211 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
212 glNormal3f(v, -u, 0.0);
213 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
214 width * 0.5);
215 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
216 -width * 0.5);
217 glNormal3f(cos(angle), sin(angle), 0.0);
220 glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
221 glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
223 glEnd();
225 glShadeModel(GL_SMOOTH);
227 /* draw inside radius cylinder */
228 glBegin(GL_QUAD_STRIP);
229 for (i = 0; i <= teeth; i++) {
230 angle = i * 2.0 * M_PI / teeth;
231 glNormal3f(-cos(angle), -sin(angle), 0.0);
232 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
233 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
235 glEnd();
239 static void
240 do_draw(void)
242 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
244 glPushMatrix();
245 glRotatef(view_rotx, 1.0, 0.0, 0.0);
246 glRotatef(view_roty, 0.0, 1.0, 0.0);
247 glRotatef(view_rotz, 0.0, 0.0, 1.0);
249 glPushMatrix();
250 glTranslatef(-3.0, -2.0, 0.0);
251 glRotatef(angle, 0.0, 0.0, 1.0);
252 glCallList(gear1);
253 glPopMatrix();
255 glPushMatrix();
256 glTranslatef(3.1, -2.0, 0.0);
257 glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);
258 glCallList(gear2);
259 glPopMatrix();
261 glPushMatrix();
262 glTranslatef(-3.1, 4.2, 0.0);
263 glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
264 glCallList(gear3);
265 glPopMatrix();
267 glPopMatrix();
271 /* new window size or exposure */
272 static void
273 reshape(int width, int height)
275 glViewport(0, 0, (GLint) width, (GLint) height);
278 GLfloat h = (GLfloat) height / (GLfloat) width;
280 glMatrixMode(GL_PROJECTION);
281 glLoadIdentity();
282 glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
285 glMatrixMode(GL_MODELVIEW);
286 glLoadIdentity();
287 glTranslatef(0.0, 0.0, -40.0);
291 static void
292 init(void)
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 };
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);
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();
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();
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();
324 glEnable(GL_NORMALIZE);
328 static void
329 draw( Display *dpy, Window win )
331 GLXContext ctx;
333 ctx = glXCreateContext( dpy, visinfo, NULL, True );
334 if (!ctx) {
335 printf("Error: glXCreateContext failed\n");
336 exit(1);
339 glXMakeCurrent(dpy, win, ctx);
341 init();
343 reshape(WinWidth, WinHeight);
345 do_draw();
347 glDeleteLists(gear1, 1);
348 glDeleteLists(gear2, 1);
349 glDeleteLists(gear3, 1);
351 glXSwapBuffers(dpy, win);
352 glXDestroyContext(dpy, ctx);
357 * Create an RGB, double-buffered window.
358 * Return the window and context handles.
360 static void
361 make_window( Display *dpy, const char *name,
362 int x, int y, int width, int height,
363 Window *winRet)
365 int attribs[] = { GLX_RGBA,
366 GLX_RED_SIZE, 1,
367 GLX_GREEN_SIZE, 1,
368 GLX_BLUE_SIZE, 1,
369 GLX_DOUBLEBUFFER,
370 GLX_DEPTH_SIZE, 1,
371 None };
372 int scrnum;
373 XSetWindowAttributes attr;
374 unsigned long mask;
375 Window root;
376 Window win;
378 scrnum = DefaultScreen( dpy );
379 root = RootWindow( dpy, scrnum );
381 if (visinfo)
382 XFree(visinfo);
384 visinfo = glXChooseVisual( dpy, scrnum, attribs );
385 if (!visinfo) {
386 printf("Error: couldn't get an RGB, Double-buffered visual\n");
387 exit(1);
390 /* window attributes */
391 attr.background_pixel = 0;
392 attr.border_pixel = 0;
393 attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
394 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
395 attr.override_redirect = 0;
396 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect;
398 win = XCreateWindow( dpy, root, x, y, width, height,
399 0, visinfo->depth, InputOutput,
400 visinfo->visual, mask, &attr );
402 /* set hints and properties */
404 XSizeHints sizehints;
405 sizehints.x = x;
406 sizehints.y = y;
407 sizehints.width = width;
408 sizehints.height = height;
409 sizehints.flags = USSize | USPosition;
410 XSetNormalHints(dpy, win, &sizehints);
411 XSetStandardProperties(dpy, win, name, name,
412 None, (char **)NULL, 0, &sizehints);
415 *winRet = win;
419 static void
420 event_loop(Display *dpy)
422 Window win;
423 make_window(dpy, "glxgears", 0, 0, WinWidth, WinHeight, &win);
424 XMapWindow(dpy, win);
426 while (1) {
427 while (XPending(dpy) > 0) {
428 XEvent event;
429 XNextEvent(dpy, &event);
430 switch (event.type) {
431 case Expose:
432 /* we'll redraw below */
433 break;
434 case ConfigureNotify:
435 WinWidth = event.xconfigure.width;
436 WinHeight = event.xconfigure.height;
437 break;
438 case KeyPress:
440 char buffer[10];
441 int code;
442 code = XLookupKeysym(&event.xkey, 0);
443 if (code == XK_Left) {
444 view_roty += 5.0;
446 else if (code == XK_Right) {
447 view_roty -= 5.0;
449 else if (code == XK_Up) {
450 view_rotx += 5.0;
452 else if (code == XK_Down) {
453 view_rotx -= 5.0;
455 else {
456 XLookupString(&event.xkey, buffer, sizeof(buffer),
457 NULL, NULL);
458 if (buffer[0] == 27) {
459 /* escape */
460 return;
468 static int frames = 0;
469 static double tRot0 = -1.0, tRate0 = -1.0;
470 double dt, t = current_time();
471 if (tRot0 < 0.0)
472 tRot0 = t;
473 dt = t - tRot0;
474 tRot0 = t;
476 /* advance rotation for next frame */
477 angle += 70.0 * dt; /* 70 degrees per second */
478 if (angle > 3600.0)
479 angle -= 3600.0;
481 draw( dpy, win );
483 frames++;
485 if (tRate0 < 0.0)
486 tRate0 = t;
488 if (t - tRate0 >= 1.0) {
489 GLfloat seconds = t - tRate0;
490 GLfloat fps = frames / seconds;
491 printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames, seconds,
492 fps);
493 fflush(stdout);
494 tRate0 = t;
496 /* Destroy window and create new one */
497 XDestroyWindow(dpy, win);
498 make_window(dpy, "glxgears",
499 (int)(fps * 100) % 100, (int)(fps * 100) % 100, /* x,y */
500 WinWidth, WinHeight, &win);
501 XMapWindow(dpy, win);
503 frames = 0;
511 main(int argc, char *argv[])
513 Display *dpy;
514 char *dpyName = NULL;
515 GLboolean printInfo = GL_FALSE;
516 int i;
518 for (i = 1; i < argc; i++) {
519 if (strcmp(argv[i], "-display") == 0) {
520 dpyName = argv[i+1];
521 i++;
523 else if (strcmp(argv[i], "-info") == 0) {
524 printInfo = GL_TRUE;
526 else
527 printf("Warrning: unknown parameter: %s\n", argv[i]);
530 dpy = XOpenDisplay(dpyName);
531 if (!dpy) {
532 fprintf(stderr, "Error: couldn't open display %s\n",
533 XDisplayName(dpyName));
534 return -1;
537 if (printInfo) {
538 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
539 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
540 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
541 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
544 event_loop(dpy);
546 XCloseDisplay(dpy);
548 return 0;