1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* gears --- 3D gear wheels */
5 static const char sccsid[] = "@(#)gears.c 4.07 97/11/24 xlockmore";
9 * Permission to use, copy, modify, and distribute this software and its
10 * documentation for any purpose and without fee is hereby granted,
11 * provided that the above copyright notice appear in all copies and that
12 * both that copyright notice and this permission notice appear in
13 * supporting documentation.
15 * This file is provided AS IS with no warranties of any kind. The author
16 * shall have no liability with respect to the infringement of copyrights,
17 * trade secrets or any patents by this file or any part thereof. In no
18 * event will the author be liable for any lost revenue or profits or
19 * other special, indirect and consequential damages.
22 * 09-Feb-01: "Planetary" gear system added by jwz@jwz.org.
23 * 10-May-97: Compatible with xscreensaver
24 * 22-Mar-97: Added support for -mono mode, and monochrome X servers.
25 * Ed Mackey, emackey@netaxs.com
26 * 13-Mar-97: Memory leak fix by Tom Schmidt <tschmidt@micron.com>
27 * 1996: "written" by Danny Sung <dannys@ucla.edu>
28 * Based on 3-D gear wheels by Brian Paul which is in the public domain.
32 * PURIFY 3.0a on SunOS4 reports an unitialized memory read on each of
33 * the glCallList() functions below when using MesaGL 2.1. This has
34 * been fixed in MesaGL 2.2 and later releases.
38 # define PROGCLASS "Gears"
39 # define HACK_INIT init_gears
40 # define HACK_DRAW draw_gears
41 # define HACK_RESHAPE reshape_gears
42 # define HACK_HANDLE_EVENT gears_handle_event
43 # define EVENT_MASK PointerMotionMask
44 # define gears_opts xlockmore_opts
45 # define DEFAULTS "*count: 1 \n" \
48 "*showFPS: False \n" \
49 "*wireframe: False \n"
50 # include "xlockmore.h" /* from the xscreensaver distribution */
51 #else /* !STANDALONE */
52 # include "xlock.h" /* from the xlockmore distribution */
53 #endif /* !STANDALONE */
58 #include "gltrackball.h"
61 #define countof(x) (sizeof((x))/sizeof((*x)))
63 #define DEF_PLANETARY "False"
64 #define DEF_SPIN "True"
69 static XrmOptionDescRec opts[] = {
70 {"-planetary", ".gears.planetary", XrmoptionNoArg, "true" },
71 {"+planetary", ".gears.planetary", XrmoptionNoArg, "false" },
72 {"-spin", ".gears.spin", XrmoptionNoArg, "true" },
73 {"+spin", ".gears.spin", XrmoptionNoArg, "false" },
76 static argtype vars[] = {
77 {&planetary, "planetary", "Planetary", DEF_PLANETARY, t_Bool},
78 {&spin, "spin", "Spin", DEF_SPIN, t_Bool},
81 ModeSpecOpt gears_opts = {countof(opts), opts, countof(vars), vars, NULL};
84 ModStruct gears_description =
85 {"gears", "init_gears", "draw_gears", "release_gears",
86 "draw_gears", "init_gears", NULL, &gears_opts,
87 1000, 1, 2, 1, 4, 1.0, "",
88 "Shows GL's gears", 0, NULL};
92 #define SMOOTH_TUBE /* whether to have smooth or faceted tubes */
95 # define TUBE_FACES 20 /* how densely to render tubes */
102 GLuint gear1, gear2, gear3;
103 GLuint gear_inner, gear_outer;
106 GLXContext *glx_context;
109 trackball_state *trackball;
113 static gearsstruct *gears = NULL;
116 * Draw a gear wheel. You'll probably want to call this function when
117 * building a display list since we do a lot of trig here.
119 * Input: inner_radius - radius of hole at center
120 * outer_radius - radius at center of teeth
121 * width - width of gear
122 * teeth - number of teeth
123 * tooth_depth - depth of tooth
124 * wire - true for wireframe mode
127 gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
128 GLint teeth, GLfloat tooth_depth, Bool wire, Bool invert)
138 r1 = outer_radius - tooth_depth / 2.0;
139 r2 = outer_radius + tooth_depth / 2.0;
145 r2 = inner_radius + tooth_depth / 2.0;
146 r1 = outer_radius - tooth_depth / 2.0;
150 da = 2.0 * M_PI / teeth / 4.0;
152 glShadeModel(GL_FLAT);
154 /* This subroutine got kind of messy when I added all the checks
155 * for wireframe mode. A much cleaner solution that I sometimes
156 * use is to have a variable hold the value GL_LINE_LOOP when
157 * in wireframe mode, or hold the value GL_POLYGON otherwise.
158 * Then I just call glBegin(that_variable), give my polygon
159 * coordinates, and glEnd(). Pretty neat eh? Too bad I couldn't
160 * integrate that trick here.
165 glNormal3f(0.0, 0.0, 1.0);
167 /* draw front face */
169 glBegin(GL_QUAD_STRIP);
170 for (i = 0; i <= teeth; i++) {
173 angle = i * 2.0 * M_PI / teeth;
174 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
175 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
177 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
178 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
180 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
181 glVertex3f(r1 * cos(angle + 4 * da), r1 * sin(angle + 4 * da), width * 0.5);
188 /* draw front sides of teeth */
191 da = 2.0 * M_PI / teeth / 4.0;
192 for (i = 0; i < teeth; i++) {
193 angle = i * 2.0 * M_PI / teeth;
196 glBegin(GL_LINE_LOOP);
197 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
198 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
199 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
200 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
209 glNormal3f(0.0, 0.0, -1.0);
213 glBegin(GL_QUAD_STRIP);
214 for (i = 0; i <= teeth; i++) {
215 angle = i * 2.0 * M_PI / teeth;
218 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
219 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
221 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
222 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
224 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
225 glVertex3f(r1 * cos(angle + 4 * da), r1 * sin(angle + 4 * da), -width * 0.5);
232 /* draw back sides of teeth */
235 da = 2.0 * M_PI / teeth / 4.0;
236 for (i = 0; i < teeth; i++) {
237 angle = i * 2.0 * M_PI / teeth;
240 glBegin(GL_LINE_LOOP);
241 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
242 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
243 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
244 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
252 /* draw outward faces of teeth */
254 glBegin(GL_QUAD_STRIP);
255 for (i = 0; i <= teeth; i++) {
256 angle = i * 2.0 * M_PI / teeth;
259 u = r2 * cos(angle + da) - r1 * cos(angle);
260 v = r2 * sin(angle + da) - r1 * sin(angle);
262 u = r2 * cos(angle + da + M_PI/2) - r1 * cos(angle + M_PI/2);
263 v = r2 * sin(angle + da + M_PI/2) - r1 * sin(angle + M_PI/2);
266 len = sqrt(u * u + v * v);
269 glNormal3f(v, -u, 0.0);
273 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
274 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
276 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
277 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
280 glNormal3f(cos(angle), sin(angle), 0.0);
282 glNormal3f(cos(angle + M_PI/2), sin(angle + M_PI/2), 0.0);
284 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
285 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
288 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
289 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
291 u = r1 * cos(angle + 3 * da + M_PI/2) - r2 * cos(angle + 2 * da + M_PI/2);
292 v = r1 * sin(angle + 3 * da + M_PI/2) - r2 * sin(angle + 2 * da + M_PI/2);
295 glNormal3f(v, -u, 0.0);
297 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
298 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
301 glNormal3f(cos(angle), sin(angle), 0.0);
303 glNormal3f(cos(angle + M_PI/2), sin(angle + M_PI/2), 0.0);
310 glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
311 glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
315 glShadeModel(GL_SMOOTH);
317 /* draw inside radius cylinder */
319 glBegin(GL_QUAD_STRIP);
320 for (i = 0; i <= teeth; i++) {
321 angle = i * 2.0 * M_PI / teeth;
326 glNormal3f(-cos(angle), -sin(angle), 0.0);
328 glNormal3f(cos(angle), sin(angle), 0.0);
330 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
331 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
333 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
334 glVertex3f(r0 * cos(angle + 4 * da), r0 * sin(angle + 4 * da), -width * 0.5);
335 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
336 glVertex3f(r0 * cos(angle + 4 * da), r0 * sin(angle + 4 * da), width * 0.5);
347 unit_tube (Bool wire)
350 int faces = TUBE_FACES;
351 GLfloat step = M_PI * 2 / faces;
360 glBegin(wire ? GL_LINES : GL_QUAD_STRIP);
362 glBegin(wire ? GL_LINES : GL_QUADS);
365 for (i = 0, th = 0; i <= faces; i++)
367 GLfloat x = cos (th);
368 GLfloat y = sin (th);
370 glVertex3f(x, 0.0, y);
371 glVertex3f(x, 1.0, y);
377 glVertex3f(x, 1.0, y);
378 glVertex3f(x, 0.0, y);
385 for (z = 0; z <= 1; z++)
387 glFrontFace(z == 0 ? GL_CCW : GL_CW);
388 glNormal3f(0, (z == 0 ? -1 : 1), 0);
389 glBegin(wire ? GL_LINE_LOOP : GL_TRIANGLE_FAN);
390 if (! wire) glVertex3f(0, z, 0);
391 for (i = 0, th = 0; i <= faces; i++)
393 GLfloat x = cos (th);
394 GLfloat y = sin (th);
404 tube (GLfloat x1, GLfloat y1, GLfloat z1,
405 GLfloat x2, GLfloat y2, GLfloat z2,
406 GLfloat diameter, GLfloat cap_size,
409 GLfloat length, angle, a, b, c;
411 if (diameter <= 0) abort();
417 length = sqrt (a*a + b*b + c*c);
418 angle = acos (a / length);
421 glTranslatef(x1, y1, z1);
422 glScalef (length, length, length);
424 if (c == 0 && b == 0)
425 glRotatef (angle / (M_PI / 180), 0, 1, 0);
427 glRotatef (angle / (M_PI / 180), 0, -c, b);
429 glRotatef (-90, 0, 0, 1);
430 glScalef (diameter/length, 1, diameter/length);
432 /* extend the endpoints of the tube by the cap size in both directions */
435 GLfloat c = cap_size/length;
436 glTranslatef (0, -c, 0);
437 glScalef (1, 1+c+c, 1);
446 ctube (GLfloat diameter, GLfloat width, Bool wire)
455 GLfloat width1, GLfloat height1,
456 GLfloat width2, GLfloat height2,
459 glShadeModel(GL_FLAT);
461 #if 0 /* don't need these - they're embedded in other objects */
464 glNormal3f(-1, 0, 0);
465 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
466 glVertex3f(-length/2, -width1/2, -height1/2);
467 glVertex3f(-length/2, width1/2, -height1/2);
468 glVertex3f(-length/2, width1/2, height1/2);
469 glVertex3f(-length/2, -width1/2, height1/2);
475 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
476 glVertex3f(length/2, -width2/2, -height2/2);
477 glVertex3f(length/2, width2/2, -height2/2);
478 glVertex3f(length/2, width2/2, height2/2);
479 glVertex3f(length/2, -width2/2, height2/2);
485 glNormal3f(0, 0, -1);
486 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
487 glVertex3f(-length/2, -width1/2, -height1/2);
488 glVertex3f(-length/2, width1/2, -height1/2);
489 glVertex3f( length/2, width2/2, -height2/2);
490 glVertex3f( length/2, -width2/2, -height2/2);
496 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
497 glVertex3f(-length/2, -width1/2, height1/2);
498 glVertex3f(-length/2, width1/2, height1/2);
499 glVertex3f( length/2, width2/2, height2/2);
500 glVertex3f( length/2, -width2/2, height2/2);
505 glNormal3f(0, -1, 0);
506 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
507 glVertex3f(-length/2, -width1/2, -height1/2);
508 glVertex3f(-length/2, -width1/2, height1/2);
509 glVertex3f( length/2, -width2/2, height2/2);
510 glVertex3f( length/2, -width2/2, -height2/2);
516 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
517 glVertex3f(-length/2, width1/2, -height1/2);
518 glVertex3f(-length/2, width1/2, height1/2);
519 glVertex3f( length/2, width2/2, height2/2);
520 glVertex3f( length/2, width2/2, -height2/2);
530 gearsstruct *gp = &gears[MI_SCREEN(mi)];
531 int wire = MI_IS_WIREFRAME(mi);
534 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
536 glClear(GL_COLOR_BUFFER_BIT);
541 gltrackball_rotate (gp->trackball);
546 get_rotation (gp->rot, &x, &y, &z, !gp->button_down_p);
547 glRotatef (x * 360, 1.0, 0.0, 0.0);
548 glRotatef (y * 360, 0.0, 1.0, 0.0);
549 glRotatef (z * 360, 0.0, 0.0, 1.0);
554 glTranslatef(-3.0, -2.0, 0.0);
555 glRotatef(gp->angle, 0.0, 0.0, 1.0);
556 /* PURIFY 4.0.1 reports an unitialized memory read on the next line when using
557 * MesaGL 2.2 and -mono. This has been fixed in MesaGL 2.3 and later. */
558 glCallList(gp->gear1);
562 glTranslatef(3.1, -2.0, 0.0);
563 glRotatef(-2.0 * gp->angle - 9.0, 0.0, 0.0, 1.0);
564 glCallList(gp->gear2);
568 glTranslatef(-3.1, 4.2, 0.0);
569 glRotatef(-2.0 * gp->angle - 25.0, 0.0, 0.0, 1.0);
570 glCallList(gp->gear3);
573 } else { /* planetary */
575 glScalef(0.8, 0.8, 0.8);
578 glTranslatef(0.0, 4.2, 0.0);
579 glRotatef(gp->angle - 7.0, 0.0, 0.0, 1.0);
580 glCallList(gp->gear1);
584 glRotatef(120, 0.0, 0.0, 1.0);
585 glTranslatef(0.0, 4.2, 0.0);
586 glRotatef(gp->angle - 7.0, 0.0, 0.0, 1.0);
587 glCallList(gp->gear2);
591 glRotatef(240, 0.0, 0.0, 1.0);
592 glTranslatef(0.0, 4.2, 0.0);
593 glRotatef(gp->angle - 7.0, 0.0, 0.0, 1.0);
594 glCallList(gp->gear3);
598 glTranslatef(0.0, 0.0, 0.0);
599 glRotatef(-gp->angle, 0.0, 0.0, 1.0);
600 glCallList(gp->gear_inner);
604 glTranslatef(0.0, 0.0, 0.0);
605 glRotatef((gp->angle / 3.0) - 7.5, 0.0, 0.0, 1.0);
606 glCallList(gp->gear_outer);
610 glTranslatef(0.0, 0.0, 0.0);
611 glCallList(gp->armature);
620 /* new window size or exposure */
622 reshape_gears(ModeInfo *mi, int width, int height)
624 GLfloat h = (GLfloat) height / (GLfloat) width;
626 glViewport(0, 0, (GLint) width, (GLint) height);
627 glMatrixMode(GL_PROJECTION);
629 glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
630 glMatrixMode(GL_MODELVIEW);
632 glTranslatef(0.0, 0.0, -40.0);
634 /* The depth buffer will be cleared, if needed, before the
635 * next frame. Right now we just want to black the screen.
637 glClear(GL_COLOR_BUFFER_BIT);
645 gearsstruct *gp = &gears[MI_SCREEN(mi)];
646 static GLfloat pos[4] =
647 {5.0, 5.0, 10.0, 1.0};
648 static GLfloat red[4] =
649 {0.8, 0.1, 0.0, 1.0};
650 static GLfloat green[4] =
651 {0.0, 0.8, 0.2, 1.0};
652 static GLfloat blue[4] =
653 {0.2, 0.2, 1.0, 1.0};
654 static GLfloat gray[4] =
655 {0.5, 0.5, 0.5, 1.0};
656 static GLfloat white[4] =
657 {1.0, 1.0, 1.0, 1.0};
658 int wire = MI_IS_WIREFRAME(mi);
659 int mono = MI_IS_MONO(mi);
662 glLightfv(GL_LIGHT0, GL_POSITION, pos);
663 glEnable(GL_CULL_FACE);
664 glEnable(GL_LIGHTING);
666 glEnable(GL_DEPTH_TEST);
670 * Messes up on multiscreen Pseudocolor:0 StaticGray(monochrome):1
671 * 2nd time mode is run it is Grayscale on PseudoColor.
672 * The code below forces monochrome on TrueColor.
674 if (MI_IS_MONO(mi)) {
675 red[0] = red[1] = red[2] = 1.0;
676 green[0] = green[1] = green[2] = 1.0;
677 blue[0] = blue[1] = blue[2] = 1.0;
685 gp->gear1 = glGenLists(1);
686 glNewList(gp->gear1, GL_COMPILE);
694 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
696 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
699 gear(1.0, 4.0, 1.0, 20, 0.7, wire, False);
702 gp->gear2 = glGenLists(1);
703 glNewList(gp->gear2, GL_COMPILE);
711 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
713 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
715 gear(0.5, 2.0, 2.0, 10, 0.7, wire, False);
718 gp->gear3 = glGenLists(1);
719 glNewList(gp->gear3, GL_COMPILE);
727 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
729 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
731 gear(1.3, 2.0, 0.5, 10, 0.7, wire, False);
734 glEnable(GL_NORMALIZE);
736 } else { /* planetary */
738 gp->gear1 = glGenLists(1);
739 glNewList(gp->gear1, GL_COMPILE);
747 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
749 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
751 gear(1.3, 2.0, 2.0, 12, 0.7, wire, False);
754 gp->gear2 = glGenLists(1);
755 glNewList(gp->gear2, GL_COMPILE);
763 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
765 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
767 gear(1.3, 2.0, 2.0, 12, 0.7, wire, False);
770 gp->gear3 = glGenLists(1);
771 glNewList(gp->gear3, GL_COMPILE);
779 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
781 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
783 gear(1.3, 2.0, 2.0, 12, 0.7, wire, False);
786 glEnable(GL_NORMALIZE);
789 gp->gear_inner = glGenLists(1);
790 glNewList(gp->gear_inner, GL_COMPILE);
798 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
800 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
802 gear(1.0, 2.0, 2.0, 12, 0.7, wire, False);
805 glEnable(GL_NORMALIZE);
808 gp->gear_outer = glGenLists(1);
809 glNewList(gp->gear_outer, GL_COMPILE);
817 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
819 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
821 gear(5.7, 7.0, 2.0, 36, 0.7, wire, True);
823 /* put some nubs on the outer ring, so we can tell how it's moving */
825 glTranslatef(7.0, 0, 0);
826 glRotatef(90, 0, 1, 0);
828 ctube(0.5, 0.5, wire); /* nub 1 */
832 glRotatef(120, 0, 0, 1);
833 glTranslatef(7.0, 0, 0);
834 glRotatef(90, 0, 1, 0);
835 ctube(0.5, 0.5, wire); /* nub 2 */
839 glRotatef(240, 0, 0, 1);
840 glTranslatef(7.0, 0, 0);
841 glRotatef(90, 0, 1, 0);
842 ctube(0.5, 0.5, wire); /* nub 3 */
848 glEnable(GL_NORMALIZE);
850 gp->armature = glGenLists(1);
851 glNewList(gp->armature, GL_COMPILE);
859 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
861 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
864 glTranslatef(0, 0, 1.5);
865 ctube(0.5, 10, wire); /* center axle */
868 glTranslatef(0.0, 4.2, -1);
869 ctube(0.5, 3, wire); /* axle 1 */
870 glTranslatef(0, 0, 1.8);
871 ctube(0.7, 0.7, wire);
875 glRotatef(120, 0.0, 0.0, 1.0);
876 glTranslatef(0.0, 4.2, -1);
877 ctube(0.5, 3, wire); /* axle 2 */
878 glTranslatef(0, 0, 1.8);
879 ctube(0.7, 0.7, wire);
883 glRotatef(240, 0.0, 0.0, 1.0);
884 glTranslatef(0.0, 4.2, -1);
885 ctube(0.5, 3, wire); /* axle 3 */
886 glTranslatef(0, 0, 1.8);
887 ctube(0.7, 0.7, wire);
890 glTranslatef(0, 0, 1.5); /* center disk */
894 glRotatef(270, 0, 0, 1);
895 glRotatef(-10, 0, 1, 0);
896 glTranslatef(-2.2, 0, 0);
897 arm(4.0, 1.0, 0.5, 2.0, 1.0, wire); /* arm 1 */
901 glRotatef(30, 0, 0, 1);
902 glRotatef(-10, 0, 1, 0);
903 glTranslatef(-2.2, 0, 0);
904 arm(4.0, 1.0, 0.5, 2.0, 1.0, wire); /* arm 2 */
908 glRotatef(150, 0, 0, 1);
909 glRotatef(-10, 0, 1, 0);
910 glTranslatef(-2.2, 0, 0);
911 arm(4.0, 1.0, 0.5, 2.0, 1.0, wire); /* arm 3 */
916 glEnable(GL_NORMALIZE);
922 gears_handle_event (ModeInfo *mi, XEvent *event)
924 gearsstruct *gp = &gears[MI_SCREEN(mi)];
926 if (event->xany.type == ButtonPress &&
927 event->xbutton.button == Button1)
929 gp->button_down_p = True;
930 gltrackball_start (gp->trackball,
931 event->xbutton.x, event->xbutton.y,
932 MI_WIDTH (mi), MI_HEIGHT (mi));
935 else if (event->xany.type == ButtonRelease &&
936 event->xbutton.button == Button1)
938 gp->button_down_p = False;
941 else if (event->xany.type == ButtonPress &&
942 (event->xbutton.button == Button4 ||
943 event->xbutton.button == Button5))
945 gltrackball_mousewheel (gp->trackball, event->xbutton.button, 10,
946 !!event->xbutton.state);
949 else if (event->xany.type == MotionNotify &&
952 gltrackball_track (gp->trackball,
953 event->xmotion.x, event->xmotion.y,
954 MI_WIDTH (mi), MI_HEIGHT (mi));
963 init_gears(ModeInfo * mi)
965 int screen = MI_SCREEN(mi);
968 /* Boolean rgba, doublebuffer, cmap_installed; */
972 if ((gears = (gearsstruct *) calloc(MI_NUM_SCREENS(mi),
973 sizeof (gearsstruct))) == NULL)
978 gp->window = MI_WINDOW(mi);
980 gp->rot = make_rotator (1, 1, 1, 1, 0, True);
981 gp->trackball = gltrackball_init ();
983 if ((gp->glx_context = init_GL(mi)) != NULL) {
984 reshape_gears(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
992 draw_gears(ModeInfo * mi)
994 gearsstruct *gp = &gears[MI_SCREEN(mi)];
995 Display *display = MI_DISPLAY(mi);
996 Window window = MI_WINDOW(mi);
997 int angle_incr = MI_CYCLES(mi) ? MI_CYCLES(mi) : 2;
1002 if (!gp->glx_context)
1005 glDrawBuffer(GL_BACK);
1007 glXMakeCurrent(display, window, *(gp->glx_context));
1010 /* let's do something so we don't get bored */
1011 gp->angle = (int) (gp->angle + angle_incr) % 360;
1013 if (mi->fps_p) do_fps (mi);
1015 glXSwapBuffers(display, window);
1019 release_gears(ModeInfo * mi)
1021 if (gears != NULL) {
1024 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
1025 gearsstruct *gp = &gears[screen];
1027 if (gp->glx_context) {
1028 /* Display lists MUST be freed while their glXContext is current. */
1029 glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
1031 if (glIsList(gp->gear1))
1032 glDeleteLists(gp->gear1, 1);
1033 if (glIsList(gp->gear2))
1034 glDeleteLists(gp->gear2, 1);
1035 if (glIsList(gp->gear3))
1036 glDeleteLists(gp->gear3, 1);
1037 if (glIsList(gp->gear_inner))
1038 glDeleteLists(gp->gear_inner, 1);
1039 if (glIsList(gp->gear_outer))
1040 glDeleteLists(gp->gear_outer, 1);
1044 (void) free((void *) gears);
1051 /*********************************************************/