1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* gears --- 3D gear wheels */
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)gears.c 4.07 97/11/24 xlockmore";
10 * Permission to use, copy, modify, and distribute this software and its
11 * documentation for any purpose and without fee is hereby granted,
12 * provided that the above copyright notice appear in all copies and that
13 * both that copyright notice and this permission notice appear in
14 * supporting documentation.
16 * This file is provided AS IS with no warranties of any kind. The author
17 * shall have no liability with respect to the infringement of copyrights,
18 * trade secrets or any patents by this file or any part thereof. In no
19 * event will the author be liable for any lost revenue or profits or
20 * other special, indirect and consequential damages.
23 * 09-Feb-01: "Planetary" gear system added by jwz@jwz.org.
24 * 10-May-97: Compatible with xscreensaver
25 * 22-Mar-97: Added support for -mono mode, and monochrome X servers.
26 * Ed Mackey, emackey@netaxs.com
27 * 13-Mar-97: Memory leak fix by Tom Schmidt <tschmidt@micron.com>
28 * 1996: "written" by Danny Sung <dannys@ucla.edu>
29 * Based on 3-D gear wheels by Brian Paul which is in the public domain.
33 * PURIFY 3.0a on SunOS4 reports an unitialized memory read on each of
34 * the glCallList() functions below when using MesaGL 2.1. This has
35 * been fixed in MesaGL 2.2 and later releases.
39 * due to a Bug/feature in VMS X11/Intrinsic.h has to be placed before xlock.
40 * otherwise caddr_t is not defined correctly
43 #include <X11/Intrinsic.h>
46 # define PROGCLASS "Gears"
47 # define HACK_INIT init_gears
48 # define HACK_DRAW draw_gears
49 # define HACK_RESHAPE reshape_gears
50 # define HACK_HANDLE_EVENT gears_handle_event
51 # define EVENT_MASK PointerMotionMask
52 # define gears_opts xlockmore_opts
53 # define DEFAULTS "*count: 1 \n" \
56 "*planetary: False \n" \
57 "*showFPS: False \n" \
58 "*wireframe: False \n"
59 # include "xlockmore.h" /* from the xscreensaver distribution */
60 #else /* !STANDALONE */
61 # include "xlock.h" /* from the xlockmore distribution */
62 #endif /* !STANDALONE */
67 #include "gltrackball.h"
70 #define countof(x) (sizeof((x))/sizeof((*x)))
72 #define DEF_PLANETARY "False"
76 static XrmOptionDescRec opts[] = {
77 {"-planetary", ".gears.planetary", XrmoptionNoArg, (caddr_t) "true" },
78 {"+planetary", ".gears.planetary", XrmoptionNoArg, (caddr_t) "false" },
81 static argtype vars[] = {
82 {(caddr_t *) &planetary, "planetary", "Planetary", DEF_PLANETARY, t_Bool},
85 ModeSpecOpt gears_opts = {countof(opts), opts, countof(vars), vars, NULL};
88 ModStruct gears_description =
89 {"gears", "init_gears", "draw_gears", "release_gears",
90 "draw_gears", "init_gears", NULL, &gears_opts,
91 1000, 1, 2, 1, 4, 1.0, "",
92 "Shows GL's gears", 0, NULL};
96 #define SMOOTH_TUBE /* whether to have smooth or faceted tubes */
99 # define TUBE_FACES 20 /* how densely to render tubes */
101 # define TUBE_FACES 6
106 GLuint gear1, gear2, gear3;
107 GLuint gear_inner, gear_outer;
110 GLXContext *glx_context;
113 trackball_state *trackball;
117 static gearsstruct *gears = NULL;
120 * Draw a gear wheel. You'll probably want to call this function when
121 * building a display list since we do a lot of trig here.
123 * Input: inner_radius - radius of hole at center
124 * outer_radius - radius at center of teeth
125 * width - width of gear
126 * teeth - number of teeth
127 * tooth_depth - depth of tooth
128 * wire - true for wireframe mode
131 gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
132 GLint teeth, GLfloat tooth_depth, Bool wire, Bool invert)
142 r1 = outer_radius - tooth_depth / 2.0;
143 r2 = outer_radius + tooth_depth / 2.0;
149 r2 = inner_radius + tooth_depth / 2.0;
150 r1 = outer_radius - tooth_depth / 2.0;
154 da = 2.0 * M_PI / teeth / 4.0;
156 glShadeModel(GL_FLAT);
158 /* This subroutine got kind of messy when I added all the checks
159 * for wireframe mode. A much cleaner solution that I sometimes
160 * use is to have a variable hold the value GL_LINE_LOOP when
161 * in wireframe mode, or hold the value GL_POLYGON otherwise.
162 * Then I just call glBegin(that_variable), give my polygon
163 * coordinates, and glEnd(). Pretty neat eh? Too bad I couldn't
164 * integrate that trick here.
169 glNormal3f(0.0, 0.0, 1.0);
171 /* draw front face */
173 glBegin(GL_QUAD_STRIP);
174 for (i = 0; i <= teeth; i++) {
177 angle = i * 2.0 * M_PI / teeth;
178 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
179 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
181 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
182 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
184 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
185 glVertex3f(r1 * cos(angle + 4 * da), r1 * sin(angle + 4 * da), width * 0.5);
192 /* draw front sides of teeth */
195 da = 2.0 * M_PI / teeth / 4.0;
196 for (i = 0; i < teeth; i++) {
197 angle = i * 2.0 * M_PI / teeth;
200 glBegin(GL_LINE_LOOP);
201 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
202 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
203 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
204 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
213 glNormal3f(0.0, 0.0, -1.0);
217 glBegin(GL_QUAD_STRIP);
218 for (i = 0; i <= teeth; i++) {
219 angle = i * 2.0 * M_PI / teeth;
222 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
223 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
225 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
226 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
228 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
229 glVertex3f(r1 * cos(angle + 4 * da), r1 * sin(angle + 4 * da), -width * 0.5);
236 /* draw back sides of teeth */
239 da = 2.0 * M_PI / teeth / 4.0;
240 for (i = 0; i < teeth; i++) {
241 angle = i * 2.0 * M_PI / teeth;
244 glBegin(GL_LINE_LOOP);
245 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
246 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
247 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
248 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
256 /* draw outward faces of teeth */
258 glBegin(GL_QUAD_STRIP);
259 for (i = 0; i <= teeth; i++) {
260 angle = i * 2.0 * M_PI / teeth;
263 u = r2 * cos(angle + da) - r1 * cos(angle);
264 v = r2 * sin(angle + da) - r1 * sin(angle);
266 u = r2 * cos(angle + da + M_PI/2) - r1 * cos(angle + M_PI/2);
267 v = r2 * sin(angle + da + M_PI/2) - r1 * sin(angle + M_PI/2);
270 len = sqrt(u * u + v * v);
273 glNormal3f(v, -u, 0.0);
277 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
278 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
280 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
281 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
284 glNormal3f(cos(angle), sin(angle), 0.0);
286 glNormal3f(cos(angle + M_PI/2), sin(angle + M_PI/2), 0.0);
288 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
289 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
292 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
293 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
295 u = r1 * cos(angle + 3 * da + M_PI/2) - r2 * cos(angle + 2 * da + M_PI/2);
296 v = r1 * sin(angle + 3 * da + M_PI/2) - r2 * sin(angle + 2 * da + M_PI/2);
299 glNormal3f(v, -u, 0.0);
301 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
302 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
305 glNormal3f(cos(angle), sin(angle), 0.0);
307 glNormal3f(cos(angle + M_PI/2), sin(angle + M_PI/2), 0.0);
314 glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
315 glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
319 glShadeModel(GL_SMOOTH);
321 /* draw inside radius cylinder */
323 glBegin(GL_QUAD_STRIP);
324 for (i = 0; i <= teeth; i++) {
325 angle = i * 2.0 * M_PI / teeth;
330 glNormal3f(-cos(angle), -sin(angle), 0.0);
332 glNormal3f(cos(angle), sin(angle), 0.0);
334 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
335 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
337 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
338 glVertex3f(r0 * cos(angle + 4 * da), r0 * sin(angle + 4 * da), -width * 0.5);
339 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
340 glVertex3f(r0 * cos(angle + 4 * da), r0 * sin(angle + 4 * da), width * 0.5);
351 unit_tube (Bool wire)
354 int faces = TUBE_FACES;
355 GLfloat step = M_PI * 2 / faces;
364 glBegin(wire ? GL_LINES : GL_QUAD_STRIP);
366 glBegin(wire ? GL_LINES : GL_QUADS);
369 for (i = 0, th = 0; i <= faces; i++)
371 GLfloat x = cos (th);
372 GLfloat y = sin (th);
374 glVertex3f(x, 0.0, y);
375 glVertex3f(x, 1.0, y);
381 glVertex3f(x, 1.0, y);
382 glVertex3f(x, 0.0, y);
389 for (z = 0; z <= 1; z++)
391 glFrontFace(z == 0 ? GL_CCW : GL_CW);
392 glNormal3f(0, (z == 0 ? -1 : 1), 0);
393 glBegin(wire ? GL_LINE_LOOP : GL_TRIANGLE_FAN);
394 if (! wire) glVertex3f(0, z, 0);
395 for (i = 0, th = 0; i <= faces; i++)
397 GLfloat x = cos (th);
398 GLfloat y = sin (th);
408 tube (GLfloat x1, GLfloat y1, GLfloat z1,
409 GLfloat x2, GLfloat y2, GLfloat z2,
410 GLfloat diameter, GLfloat cap_size,
413 GLfloat length, angle, a, b, c;
415 if (diameter <= 0) abort();
421 length = sqrt (a*a + b*b + c*c);
422 angle = acos (a / length);
425 glTranslatef(x1, y1, z1);
426 glScalef (length, length, length);
428 if (c == 0 && b == 0)
429 glRotatef (angle / (M_PI / 180), 0, 1, 0);
431 glRotatef (angle / (M_PI / 180), 0, -c, b);
433 glRotatef (-90, 0, 0, 1);
434 glScalef (diameter/length, 1, diameter/length);
436 /* extend the endpoints of the tube by the cap size in both directions */
439 GLfloat c = cap_size/length;
440 glTranslatef (0, -c, 0);
441 glScalef (1, 1+c+c, 1);
450 ctube (GLfloat diameter, GLfloat width, Bool wire)
459 GLfloat width1, GLfloat height1,
460 GLfloat width2, GLfloat height2,
463 glShadeModel(GL_FLAT);
465 #if 0 /* don't need these - they're embedded in other objects */
468 glNormal3f(-1, 0, 0);
469 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
470 glVertex3f(-length/2, -width1/2, -height1/2);
471 glVertex3f(-length/2, width1/2, -height1/2);
472 glVertex3f(-length/2, width1/2, height1/2);
473 glVertex3f(-length/2, -width1/2, height1/2);
479 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
480 glVertex3f(length/2, -width2/2, -height2/2);
481 glVertex3f(length/2, width2/2, -height2/2);
482 glVertex3f(length/2, width2/2, height2/2);
483 glVertex3f(length/2, -width2/2, height2/2);
489 glNormal3f(0, 0, -1);
490 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
491 glVertex3f(-length/2, -width1/2, -height1/2);
492 glVertex3f(-length/2, width1/2, -height1/2);
493 glVertex3f( length/2, width2/2, -height2/2);
494 glVertex3f( length/2, -width2/2, -height2/2);
500 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
501 glVertex3f(-length/2, -width1/2, height1/2);
502 glVertex3f(-length/2, width1/2, height1/2);
503 glVertex3f( length/2, width2/2, height2/2);
504 glVertex3f( length/2, -width2/2, height2/2);
509 glNormal3f(0, -1, 0);
510 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
511 glVertex3f(-length/2, -width1/2, -height1/2);
512 glVertex3f(-length/2, -width1/2, height1/2);
513 glVertex3f( length/2, -width2/2, height2/2);
514 glVertex3f( length/2, -width2/2, -height2/2);
520 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
521 glVertex3f(-length/2, width1/2, -height1/2);
522 glVertex3f(-length/2, width1/2, height1/2);
523 glVertex3f( length/2, width2/2, height2/2);
524 glVertex3f( length/2, width2/2, -height2/2);
534 gearsstruct *gp = &gears[MI_SCREEN(mi)];
535 int wire = MI_IS_WIREFRAME(mi);
538 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
540 glClear(GL_COLOR_BUFFER_BIT);
545 gltrackball_rotate (gp->trackball);
549 get_rotation (gp->rot, &x, &y, &z, !gp->button_down_p);
550 glRotatef (x * 360, 1.0, 0.0, 0.0);
551 glRotatef (y * 360, 0.0, 1.0, 0.0);
552 glRotatef (z * 360, 0.0, 0.0, 1.0);
557 glTranslatef(-3.0, -2.0, 0.0);
558 glRotatef(gp->angle, 0.0, 0.0, 1.0);
559 /* PURIFY 4.0.1 reports an unitialized memory read on the next line when using
560 * MesaGL 2.2 and -mono. This has been fixed in MesaGL 2.3 and later. */
561 glCallList(gp->gear1);
565 glTranslatef(3.1, -2.0, 0.0);
566 glRotatef(-2.0 * gp->angle - 9.0, 0.0, 0.0, 1.0);
567 glCallList(gp->gear2);
571 glTranslatef(-3.1, 4.2, 0.0);
572 glRotatef(-2.0 * gp->angle - 25.0, 0.0, 0.0, 1.0);
573 glCallList(gp->gear3);
576 } else { /* planetary */
578 glScalef(0.8, 0.8, 0.8);
581 glTranslatef(0.0, 4.2, 0.0);
582 glRotatef(gp->angle - 7.0, 0.0, 0.0, 1.0);
583 glCallList(gp->gear1);
587 glRotatef(120, 0.0, 0.0, 1.0);
588 glTranslatef(0.0, 4.2, 0.0);
589 glRotatef(gp->angle - 7.0, 0.0, 0.0, 1.0);
590 glCallList(gp->gear2);
594 glRotatef(240, 0.0, 0.0, 1.0);
595 glTranslatef(0.0, 4.2, 0.0);
596 glRotatef(gp->angle - 7.0, 0.0, 0.0, 1.0);
597 glCallList(gp->gear3);
601 glTranslatef(0.0, 0.0, 0.0);
602 glRotatef(-gp->angle, 0.0, 0.0, 1.0);
603 glCallList(gp->gear_inner);
607 glTranslatef(0.0, 0.0, 0.0);
608 glRotatef((gp->angle / 3.0) - 7.5, 0.0, 0.0, 1.0);
609 glCallList(gp->gear_outer);
613 glTranslatef(0.0, 0.0, 0.0);
614 glCallList(gp->armature);
623 /* new window size or exposure */
625 reshape_gears(ModeInfo *mi, int width, int height)
627 GLfloat h = (GLfloat) height / (GLfloat) width;
629 glViewport(0, 0, (GLint) width, (GLint) height);
630 glMatrixMode(GL_PROJECTION);
632 glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
633 glMatrixMode(GL_MODELVIEW);
635 glTranslatef(0.0, 0.0, -40.0);
637 /* The depth buffer will be cleared, if needed, before the
638 * next frame. Right now we just want to black the screen.
640 glClear(GL_COLOR_BUFFER_BIT);
648 gearsstruct *gp = &gears[MI_SCREEN(mi)];
649 static GLfloat pos[4] =
650 {5.0, 5.0, 10.0, 1.0};
651 static GLfloat red[4] =
652 {0.8, 0.1, 0.0, 1.0};
653 static GLfloat green[4] =
654 {0.0, 0.8, 0.2, 1.0};
655 static GLfloat blue[4] =
656 {0.2, 0.2, 1.0, 1.0};
657 static GLfloat gray[4] =
658 {0.5, 0.5, 0.5, 1.0};
659 static GLfloat white[4] =
660 {1.0, 1.0, 1.0, 1.0};
661 int wire = MI_IS_WIREFRAME(mi);
662 int mono = MI_IS_MONO(mi);
665 glLightfv(GL_LIGHT0, GL_POSITION, pos);
666 glEnable(GL_CULL_FACE);
667 glEnable(GL_LIGHTING);
669 glEnable(GL_DEPTH_TEST);
673 * Messes up on multiscreen Pseudocolor:0 StaticGray(monochrome):1
674 * 2nd time mode is run it is Grayscale on PseudoColor.
675 * The code below forces monochrome on TrueColor.
677 if (MI_IS_MONO(mi)) {
678 red[0] = red[1] = red[2] = 1.0;
679 green[0] = green[1] = green[2] = 1.0;
680 blue[0] = blue[1] = blue[2] = 1.0;
688 gp->gear1 = glGenLists(1);
689 glNewList(gp->gear1, GL_COMPILE);
697 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
699 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
702 gear(1.0, 4.0, 1.0, 20, 0.7, wire, False);
705 gp->gear2 = glGenLists(1);
706 glNewList(gp->gear2, GL_COMPILE);
714 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
716 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
718 gear(0.5, 2.0, 2.0, 10, 0.7, wire, False);
721 gp->gear3 = glGenLists(1);
722 glNewList(gp->gear3, GL_COMPILE);
730 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
732 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
734 gear(1.3, 2.0, 0.5, 10, 0.7, wire, False);
737 glEnable(GL_NORMALIZE);
739 } else { /* planetary */
741 gp->gear1 = glGenLists(1);
742 glNewList(gp->gear1, GL_COMPILE);
750 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
752 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
754 gear(1.3, 2.0, 2.0, 12, 0.7, wire, False);
757 gp->gear2 = glGenLists(1);
758 glNewList(gp->gear2, GL_COMPILE);
766 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
768 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
770 gear(1.3, 2.0, 2.0, 12, 0.7, wire, False);
773 gp->gear3 = glGenLists(1);
774 glNewList(gp->gear3, GL_COMPILE);
782 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
784 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
786 gear(1.3, 2.0, 2.0, 12, 0.7, wire, False);
789 glEnable(GL_NORMALIZE);
792 gp->gear_inner = glGenLists(1);
793 glNewList(gp->gear_inner, GL_COMPILE);
801 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
803 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
805 gear(1.0, 2.0, 2.0, 12, 0.7, wire, False);
808 glEnable(GL_NORMALIZE);
811 gp->gear_outer = glGenLists(1);
812 glNewList(gp->gear_outer, GL_COMPILE);
820 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
822 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
824 gear(5.7, 7.0, 2.0, 36, 0.7, wire, True);
826 /* put some nubs on the outer ring, so we can tell how it's moving */
828 glTranslatef(7.0, 0, 0);
829 glRotatef(90, 0, 1, 0);
831 ctube(0.5, 0.5, wire); /* nub 1 */
835 glRotatef(120, 0, 0, 1);
836 glTranslatef(7.0, 0, 0);
837 glRotatef(90, 0, 1, 0);
838 ctube(0.5, 0.5, wire); /* nub 2 */
842 glRotatef(240, 0, 0, 1);
843 glTranslatef(7.0, 0, 0);
844 glRotatef(90, 0, 1, 0);
845 ctube(0.5, 0.5, wire); /* nub 3 */
851 glEnable(GL_NORMALIZE);
853 gp->armature = glGenLists(1);
854 glNewList(gp->armature, GL_COMPILE);
862 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
864 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
867 glTranslatef(0, 0, 1.5);
868 ctube(0.5, 10, wire); /* center axle */
871 glTranslatef(0.0, 4.2, -1);
872 ctube(0.5, 3, wire); /* axle 1 */
873 glTranslatef(0, 0, 1.8);
874 ctube(0.7, 0.7, wire);
878 glRotatef(120, 0.0, 0.0, 1.0);
879 glTranslatef(0.0, 4.2, -1);
880 ctube(0.5, 3, wire); /* axle 2 */
881 glTranslatef(0, 0, 1.8);
882 ctube(0.7, 0.7, wire);
886 glRotatef(240, 0.0, 0.0, 1.0);
887 glTranslatef(0.0, 4.2, -1);
888 ctube(0.5, 3, wire); /* axle 3 */
889 glTranslatef(0, 0, 1.8);
890 ctube(0.7, 0.7, wire);
893 glTranslatef(0, 0, 1.5); /* center disk */
897 glRotatef(270, 0, 0, 1);
898 glRotatef(-10, 0, 1, 0);
899 glTranslatef(-2.2, 0, 0);
900 arm(4.0, 1.0, 0.5, 2.0, 1.0, wire); /* arm 1 */
904 glRotatef(30, 0, 0, 1);
905 glRotatef(-10, 0, 1, 0);
906 glTranslatef(-2.2, 0, 0);
907 arm(4.0, 1.0, 0.5, 2.0, 1.0, wire); /* arm 2 */
911 glRotatef(150, 0, 0, 1);
912 glRotatef(-10, 0, 1, 0);
913 glTranslatef(-2.2, 0, 0);
914 arm(4.0, 1.0, 0.5, 2.0, 1.0, wire); /* arm 3 */
919 glEnable(GL_NORMALIZE);
925 gears_handle_event (ModeInfo *mi, XEvent *event)
927 gearsstruct *gp = &gears[MI_SCREEN(mi)];
929 if (event->xany.type == ButtonPress &&
930 event->xbutton.button & Button1)
932 gp->button_down_p = True;
933 gltrackball_start (gp->trackball,
934 event->xbutton.x, event->xbutton.y,
935 MI_WIDTH (mi), MI_HEIGHT (mi));
938 else if (event->xany.type == ButtonRelease &&
939 event->xbutton.button & Button1)
941 gp->button_down_p = False;
944 else if (event->xany.type == MotionNotify &&
947 gltrackball_track (gp->trackball,
948 event->xmotion.x, event->xmotion.y,
949 MI_WIDTH (mi), MI_HEIGHT (mi));
958 init_gears(ModeInfo * mi)
960 int screen = MI_SCREEN(mi);
963 /* Boolean rgba, doublebuffer, cmap_installed; */
967 if ((gears = (gearsstruct *) calloc(MI_NUM_SCREENS(mi),
968 sizeof (gearsstruct))) == NULL)
973 gp->window = MI_WINDOW(mi);
975 gp->rot = make_rotator (1, 1, 1, 1, 0, True);
976 gp->trackball = gltrackball_init ();
978 if ((gp->glx_context = init_GL(mi)) != NULL) {
979 reshape_gears(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
987 draw_gears(ModeInfo * mi)
989 gearsstruct *gp = &gears[MI_SCREEN(mi)];
990 Display *display = MI_DISPLAY(mi);
991 Window window = MI_WINDOW(mi);
992 int angle_incr = MI_CYCLES(mi) ? MI_CYCLES(mi) : 2;
997 if (!gp->glx_context)
1000 glDrawBuffer(GL_BACK);
1002 glXMakeCurrent(display, window, *(gp->glx_context));
1005 /* let's do something so we don't get bored */
1006 gp->angle = (int) (gp->angle + angle_incr) % 360;
1008 if (mi->fps_p) do_fps (mi);
1010 glXSwapBuffers(display, window);
1014 release_gears(ModeInfo * mi)
1016 if (gears != NULL) {
1019 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
1020 gearsstruct *gp = &gears[screen];
1022 if (gp->glx_context) {
1023 /* Display lists MUST be freed while their glXContext is current. */
1024 glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
1026 if (glIsList(gp->gear1))
1027 glDeleteLists(gp->gear1, 1);
1028 if (glIsList(gp->gear2))
1029 glDeleteLists(gp->gear2, 1);
1030 if (glIsList(gp->gear3))
1031 glDeleteLists(gp->gear3, 1);
1032 if (glIsList(gp->gear_inner))
1033 glDeleteLists(gp->gear_inner, 1);
1034 if (glIsList(gp->gear_outer))
1035 glDeleteLists(gp->gear_outer, 1);
1039 (void) free((void *) gears);
1046 /*********************************************************/