Brian Paul | 355da23 | 2001-03-23 22:46:26 +0000 | [diff] [blame] | 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 | * This is a port of the infamous "gears" demo to straight GLX (i.e. no GLUT) |
| 24 | * Port by Brian Paul 23 March 2001 |
| 25 | * |
| 26 | * Command line options: |
| 27 | * -info print GL implementation information |
| 28 | * |
| 29 | */ |
| 30 | |
| 31 | |
| 32 | #include <math.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <stdio.h> |
| 35 | #include <string.h> |
| 36 | #include <X11/Xlib.h> |
| 37 | #include <X11/keysym.h> |
| 38 | #include <GL/gl.h> |
| 39 | #include <GL/glx.h> |
| 40 | |
| 41 | |
| 42 | #define BENCHMARK |
| 43 | |
| 44 | #ifdef BENCHMARK |
| 45 | |
| 46 | /* XXX this probably isn't very portable */ |
| 47 | |
| 48 | #include <sys/time.h> |
| 49 | #include <unistd.h> |
| 50 | |
| 51 | /* return current time (in seconds) */ |
| 52 | static int |
| 53 | current_time(void) |
| 54 | { |
| 55 | struct timeval tv; |
| 56 | struct timezone tz; |
| 57 | (void) gettimeofday(&tv, &tz); |
| 58 | return (int) tv.tv_sec; |
| 59 | } |
| 60 | |
| 61 | #else /*BENCHMARK*/ |
| 62 | |
| 63 | /* dummy */ |
| 64 | static int |
| 65 | current_time(void) |
| 66 | { |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | #endif /*BENCHMARK*/ |
| 71 | |
| 72 | |
| 73 | |
| 74 | #ifndef M_PI |
| 75 | #define M_PI 3.14159265 |
| 76 | #endif |
| 77 | |
| 78 | |
| 79 | static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0; |
| 80 | static GLint gear1, gear2, gear3; |
| 81 | static GLfloat angle = 0.0; |
| 82 | |
| 83 | |
| 84 | /* |
| 85 | * |
| 86 | * Draw a gear wheel. You'll probably want to call this function when |
| 87 | * building a display list since we do a lot of trig here. |
| 88 | * |
| 89 | * Input: inner_radius - radius of hole at center |
| 90 | * outer_radius - radius at center of teeth |
| 91 | * width - width of gear |
| 92 | * teeth - number of teeth |
| 93 | * tooth_depth - depth of tooth |
| 94 | */ |
| 95 | static void |
| 96 | gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width, |
| 97 | GLint teeth, GLfloat tooth_depth) |
| 98 | { |
| 99 | GLint i; |
| 100 | GLfloat r0, r1, r2; |
| 101 | GLfloat angle, da; |
| 102 | GLfloat u, v, len; |
| 103 | |
| 104 | r0 = inner_radius; |
| 105 | r1 = outer_radius - tooth_depth / 2.0; |
| 106 | r2 = outer_radius + tooth_depth / 2.0; |
| 107 | |
| 108 | da = 2.0 * M_PI / teeth / 4.0; |
| 109 | |
| 110 | glShadeModel(GL_FLAT); |
| 111 | |
| 112 | glNormal3f(0.0, 0.0, 1.0); |
| 113 | |
| 114 | /* draw front face */ |
| 115 | glBegin(GL_QUAD_STRIP); |
| 116 | for (i = 0; i <= teeth; i++) { |
| 117 | angle = i * 2.0 * M_PI / teeth; |
| 118 | glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5); |
| 119 | glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5); |
| 120 | if (i < teeth) { |
| 121 | glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5); |
| 122 | glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), |
| 123 | width * 0.5); |
| 124 | } |
| 125 | } |
| 126 | glEnd(); |
| 127 | |
| 128 | /* draw front sides of teeth */ |
| 129 | glBegin(GL_QUADS); |
| 130 | da = 2.0 * M_PI / teeth / 4.0; |
| 131 | for (i = 0; i < teeth; i++) { |
| 132 | angle = i * 2.0 * M_PI / teeth; |
| 133 | |
| 134 | glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5); |
| 135 | glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5); |
| 136 | glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), |
| 137 | width * 0.5); |
| 138 | glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), |
| 139 | width * 0.5); |
| 140 | } |
| 141 | glEnd(); |
| 142 | |
| 143 | glNormal3f(0.0, 0.0, -1.0); |
| 144 | |
| 145 | /* draw back face */ |
| 146 | glBegin(GL_QUAD_STRIP); |
| 147 | for (i = 0; i <= teeth; i++) { |
| 148 | angle = i * 2.0 * M_PI / teeth; |
| 149 | glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5); |
| 150 | glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5); |
| 151 | if (i < teeth) { |
| 152 | glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), |
| 153 | -width * 0.5); |
| 154 | glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5); |
| 155 | } |
| 156 | } |
| 157 | glEnd(); |
| 158 | |
| 159 | /* draw back sides of teeth */ |
| 160 | glBegin(GL_QUADS); |
| 161 | da = 2.0 * M_PI / teeth / 4.0; |
| 162 | for (i = 0; i < teeth; i++) { |
| 163 | angle = i * 2.0 * M_PI / teeth; |
| 164 | |
| 165 | glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), |
| 166 | -width * 0.5); |
| 167 | glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), |
| 168 | -width * 0.5); |
| 169 | glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5); |
| 170 | glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5); |
| 171 | } |
| 172 | glEnd(); |
| 173 | |
| 174 | /* draw outward faces of teeth */ |
| 175 | glBegin(GL_QUAD_STRIP); |
| 176 | for (i = 0; i < teeth; i++) { |
| 177 | angle = i * 2.0 * M_PI / teeth; |
| 178 | |
| 179 | glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5); |
| 180 | glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5); |
| 181 | u = r2 * cos(angle + da) - r1 * cos(angle); |
| 182 | v = r2 * sin(angle + da) - r1 * sin(angle); |
| 183 | len = sqrt(u * u + v * v); |
| 184 | u /= len; |
| 185 | v /= len; |
| 186 | glNormal3f(v, -u, 0.0); |
| 187 | glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5); |
| 188 | glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5); |
| 189 | glNormal3f(cos(angle), sin(angle), 0.0); |
| 190 | glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), |
| 191 | width * 0.5); |
| 192 | glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), |
| 193 | -width * 0.5); |
| 194 | u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da); |
| 195 | v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da); |
| 196 | glNormal3f(v, -u, 0.0); |
| 197 | glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), |
| 198 | width * 0.5); |
| 199 | glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), |
| 200 | -width * 0.5); |
| 201 | glNormal3f(cos(angle), sin(angle), 0.0); |
| 202 | } |
| 203 | |
| 204 | glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5); |
| 205 | glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5); |
| 206 | |
| 207 | glEnd(); |
| 208 | |
| 209 | glShadeModel(GL_SMOOTH); |
| 210 | |
| 211 | /* draw inside radius cylinder */ |
| 212 | glBegin(GL_QUAD_STRIP); |
| 213 | for (i = 0; i <= teeth; i++) { |
| 214 | angle = i * 2.0 * M_PI / teeth; |
| 215 | glNormal3f(-cos(angle), -sin(angle), 0.0); |
| 216 | glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5); |
| 217 | glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5); |
| 218 | } |
| 219 | glEnd(); |
| 220 | } |
| 221 | |
| 222 | |
| 223 | static void |
| 224 | draw(void) |
| 225 | { |
| 226 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 227 | |
| 228 | glPushMatrix(); |
| 229 | glRotatef(view_rotx, 1.0, 0.0, 0.0); |
| 230 | glRotatef(view_roty, 0.0, 1.0, 0.0); |
| 231 | glRotatef(view_rotz, 0.0, 0.0, 1.0); |
| 232 | |
| 233 | glPushMatrix(); |
| 234 | glTranslatef(-3.0, -2.0, 0.0); |
| 235 | glRotatef(angle, 0.0, 0.0, 1.0); |
| 236 | glCallList(gear1); |
| 237 | glPopMatrix(); |
| 238 | |
| 239 | glPushMatrix(); |
| 240 | glTranslatef(3.1, -2.0, 0.0); |
| 241 | glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0); |
| 242 | glCallList(gear2); |
| 243 | glPopMatrix(); |
| 244 | |
| 245 | glPushMatrix(); |
| 246 | glTranslatef(-3.1, 4.2, 0.0); |
| 247 | glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0); |
| 248 | glCallList(gear3); |
| 249 | glPopMatrix(); |
| 250 | |
| 251 | glPopMatrix(); |
| 252 | } |
| 253 | |
| 254 | |
| 255 | /* new window size or exposure */ |
| 256 | static void |
| 257 | reshape(int width, int height) |
| 258 | { |
| 259 | GLfloat h = (GLfloat) height / (GLfloat) width; |
| 260 | |
| 261 | glViewport(0, 0, (GLint) width, (GLint) height); |
| 262 | glMatrixMode(GL_PROJECTION); |
| 263 | glLoadIdentity(); |
| 264 | glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0); |
| 265 | glMatrixMode(GL_MODELVIEW); |
| 266 | glLoadIdentity(); |
| 267 | glTranslatef(0.0, 0.0, -40.0); |
| 268 | } |
| 269 | |
| 270 | |
| 271 | static void |
| 272 | init(void) |
| 273 | { |
| 274 | static GLfloat pos[4] = { 5.0, 5.0, 10.0, 0.0 }; |
| 275 | static GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 }; |
| 276 | static GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 }; |
| 277 | static GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 }; |
| 278 | |
| 279 | glLightfv(GL_LIGHT0, GL_POSITION, pos); |
| 280 | glEnable(GL_CULL_FACE); |
| 281 | glEnable(GL_LIGHTING); |
| 282 | glEnable(GL_LIGHT0); |
| 283 | glEnable(GL_DEPTH_TEST); |
| 284 | |
| 285 | /* make the gears */ |
| 286 | gear1 = glGenLists(1); |
| 287 | glNewList(gear1, GL_COMPILE); |
| 288 | glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red); |
| 289 | gear(1.0, 4.0, 1.0, 20, 0.7); |
| 290 | glEndList(); |
| 291 | |
| 292 | gear2 = glGenLists(1); |
| 293 | glNewList(gear2, GL_COMPILE); |
| 294 | glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green); |
| 295 | gear(0.5, 2.0, 2.0, 10, 0.7); |
| 296 | glEndList(); |
| 297 | |
| 298 | gear3 = glGenLists(1); |
| 299 | glNewList(gear3, GL_COMPILE); |
| 300 | glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue); |
| 301 | gear(1.3, 2.0, 0.5, 10, 0.7); |
| 302 | glEndList(); |
| 303 | |
| 304 | glEnable(GL_NORMALIZE); |
| 305 | } |
| 306 | |
| 307 | |
| 308 | /* |
| 309 | * Create an RGB, double-buffered window. |
| 310 | * Return the window and context handles. |
| 311 | */ |
| 312 | static void |
| 313 | make_window( Display *dpy, const char *name, |
| 314 | int x, int y, int width, int height, |
| 315 | Window *winRet, GLXContext *ctxRet) |
| 316 | { |
| 317 | int attrib[] = { GLX_RGBA, |
| 318 | GLX_RED_SIZE, 1, |
| 319 | GLX_GREEN_SIZE, 1, |
| 320 | GLX_BLUE_SIZE, 1, |
| 321 | GLX_DOUBLEBUFFER, |
| 322 | GLX_DEPTH_SIZE, 1, |
| 323 | None }; |
| 324 | int scrnum; |
| 325 | XSetWindowAttributes attr; |
| 326 | unsigned long mask; |
| 327 | Window root; |
| 328 | Window win; |
| 329 | GLXContext ctx; |
| 330 | XVisualInfo *visinfo; |
| 331 | |
| 332 | scrnum = DefaultScreen( dpy ); |
| 333 | root = RootWindow( dpy, scrnum ); |
| 334 | |
| 335 | visinfo = glXChooseVisual( dpy, scrnum, attrib ); |
| 336 | if (!visinfo) { |
| 337 | printf("Error: couldn't get an RGB, Double-buffered visual\n"); |
| 338 | exit(1); |
| 339 | } |
| 340 | |
| 341 | /* window attributes */ |
| 342 | attr.background_pixel = 0; |
| 343 | attr.border_pixel = 0; |
| 344 | attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone); |
| 345 | attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask; |
| 346 | mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; |
| 347 | |
| 348 | win = XCreateWindow( dpy, root, 0, 0, width, height, |
| 349 | 0, visinfo->depth, InputOutput, |
| 350 | visinfo->visual, mask, &attr ); |
| 351 | |
| 352 | /* set hints and properties */ |
| 353 | { |
| 354 | XSizeHints sizehints; |
| 355 | sizehints.x = x; |
| 356 | sizehints.y = y; |
| 357 | sizehints.width = width; |
| 358 | sizehints.height = height; |
| 359 | sizehints.flags = USSize | USPosition; |
| 360 | XSetNormalHints(dpy, win, &sizehints); |
| 361 | XSetStandardProperties(dpy, win, name, name, |
| 362 | None, (char **)NULL, 0, &sizehints); |
| 363 | } |
| 364 | |
| 365 | ctx = glXCreateContext( dpy, visinfo, NULL, True ); |
| 366 | if (!ctx) { |
| 367 | printf("Error: glXCreateContext failed\n"); |
| 368 | exit(1); |
| 369 | } |
| 370 | |
| 371 | XFree(visinfo); |
| 372 | |
| 373 | *winRet = win; |
| 374 | *ctxRet = ctx; |
| 375 | } |
| 376 | |
| 377 | |
| 378 | static void |
| 379 | event_loop(Display *dpy, Window win) |
| 380 | { |
| 381 | while (1) { |
| 382 | while (XPending(dpy) > 0) { |
| 383 | XEvent event; |
| 384 | XNextEvent(dpy, &event); |
| 385 | switch (event.type) { |
| 386 | case Expose: |
| 387 | /* we'll redraw below */ |
| 388 | break; |
| 389 | case ConfigureNotify: |
| 390 | reshape(event.xconfigure.width, event.xconfigure.height); |
| 391 | break; |
| 392 | case KeyPress: |
| 393 | { |
| 394 | char buffer[10]; |
| 395 | int r, code; |
| 396 | code = XLookupKeysym(&event.xkey, 0); |
| 397 | if (code == XK_Left) { |
| 398 | view_roty += 5.0; |
| 399 | } |
| 400 | else if (code == XK_Right) { |
| 401 | view_roty -= 5.0; |
| 402 | } |
| 403 | else if (code == XK_Up) { |
| 404 | view_rotx += 5.0; |
| 405 | } |
| 406 | else if (code == XK_Down) { |
| 407 | view_rotx -= 5.0; |
| 408 | } |
| 409 | else { |
| 410 | r = XLookupString(&event.xkey, buffer, sizeof(buffer), |
| 411 | NULL, NULL); |
| 412 | if (buffer[0] == 27) { |
| 413 | /* escape */ |
| 414 | return; |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | /* next frame */ |
| 422 | angle += 2.0; |
| 423 | |
| 424 | draw(); |
| 425 | glXSwapBuffers(dpy, win); |
| 426 | |
| 427 | /* calc framerate */ |
| 428 | { |
| 429 | static int t0 = -1; |
| 430 | static int frames = 0; |
| 431 | int t = current_time(); |
| 432 | |
| 433 | if (t0 < 0) |
| 434 | t0 = t; |
| 435 | |
| 436 | frames++; |
| 437 | |
| 438 | if (t - t0 >= 5.0) { |
| 439 | GLfloat seconds = t - t0; |
| 440 | GLfloat fps = frames / seconds; |
| 441 | printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames, seconds, |
| 442 | fps); |
| 443 | t0 = t; |
| 444 | frames = 0; |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | |
| 451 | int |
| 452 | main(int argc, char *argv[]) |
| 453 | { |
| 454 | Display *dpy; |
| 455 | Window win; |
| 456 | GLXContext ctx; |
| 457 | char *dpyName = ":0"; |
| 458 | GLboolean printInfo = GL_FALSE; |
| 459 | int i; |
| 460 | |
| 461 | for (i = 1; i < argc; i++) { |
| 462 | if (strcmp(argv[i], "-display") == 0) { |
| 463 | dpyName = argv[i+1]; |
| 464 | i++; |
| 465 | } |
| 466 | else if (strcmp(argv[i], "-info") == 0) { |
| 467 | printInfo = GL_TRUE; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | dpy = XOpenDisplay(dpyName); |
| 472 | if (!dpy) { |
| 473 | printf("Error: couldn't open display %s\n", dpyName); |
| 474 | return -1; |
| 475 | } |
| 476 | |
| 477 | make_window(dpy, "glxgears", 0, 0, 300, 300, &win, &ctx); |
| 478 | XMapWindow(dpy, win); |
| 479 | glXMakeCurrent(dpy, win, ctx); |
| 480 | |
| 481 | if (printInfo) { |
| 482 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 483 | printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); |
| 484 | printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR)); |
| 485 | printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS)); |
| 486 | } |
| 487 | |
| 488 | init(); |
| 489 | |
| 490 | event_loop(dpy, win); |
| 491 | |
| 492 | glXDestroyContext(dpy, ctx); |
| 493 | XDestroyWindow(dpy, win); |
| 494 | XCloseDisplay(dpy); |
| 495 | |
| 496 | return 0; |
| 497 | } |