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 * due to a Bug/feature in VMS X11/Intrinsic.h has to be placed before xlock.
39 * otherwise caddr_t is not defined correctly
42 #include <X11/Intrinsic.h>
45 # define PROGCLASS "Gears"
46 # define HACK_INIT init_gears
47 # define HACK_DRAW draw_gears
48 # define HACK_RESHAPE reshape_gears
49 # define HACK_HANDLE_EVENT gears_handle_event
50 # define EVENT_MASK PointerMotionMask
51 # define gears_opts xlockmore_opts
52 # define DEFAULTS "*count: 1 \n" \
55 "*planetary: False \n" \
56 "*showFPS: False \n" \
57 "*wireframe: False \n"
58 # include "xlockmore.h" /* from the xscreensaver distribution */
59 #else /* !STANDALONE */
60 # include "xlock.h" /* from the xlockmore distribution */
61 #endif /* !STANDALONE */
66 #include "gltrackball.h"
69 #define countof(x) (sizeof((x))/sizeof((*x)))
71 #define DEF_PLANETARY "False"
75 static XrmOptionDescRec opts[] = {
76 {"-planetary", ".gears.planetary", XrmoptionNoArg, (caddr_t) "true" },
77 {"+planetary", ".gears.planetary", XrmoptionNoArg, (caddr_t) "false" },
80 static argtype vars[] = {
81 {&planetary, "planetary", "Planetary", DEF_PLANETARY, t_Bool},
84 ModeSpecOpt gears_opts = {countof(opts), opts, countof(vars), vars, NULL};
87 ModStruct gears_description =
88 {"gears", "init_gears", "draw_gears", "release_gears",
89 "draw_gears", "init_gears", NULL, &gears_opts,
90 1000, 1, 2, 1, 4, 1.0, "",
91 "Shows GL's gears", 0, NULL};
95 #define SMOOTH_TUBE /* whether to have smooth or faceted tubes */
98 # define TUBE_FACES 20 /* how densely to render tubes */
100 # define TUBE_FACES 6
105 GLuint gear1, gear2, gear3;
106 GLuint gear_inner, gear_outer;
109 GLXContext *glx_context;
112 trackball_state *trackball;
116 static gearsstruct *gears = NULL;
119 * Draw a gear wheel. You'll probably want to call this function when
120 * building a display list since we do a lot of trig here.
122 * Input: inner_radius - radius of hole at center
123 * outer_radius - radius at center of teeth
124 * width - width of gear
125 * teeth - number of teeth
126 * tooth_depth - depth of tooth
127 * wire - true for wireframe mode
130 gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
131 GLint teeth, GLfloat tooth_depth, Bool wire, Bool invert)
141 r1 = outer_radius - tooth_depth / 2.0;
142 r2 = outer_radius + tooth_depth / 2.0;
148 r2 = inner_radius + tooth_depth / 2.0;
149 r1 = outer_radius - tooth_depth / 2.0;
153 da = 2.0 * M_PI / teeth / 4.0;
155 glShadeModel(GL_FLAT);
157 /* This subroutine got kind of messy when I added all the checks
158 * for wireframe mode. A much cleaner solution that I sometimes
159 * use is to have a variable hold the value GL_LINE_LOOP when
160 * in wireframe mode, or hold the value GL_POLYGON otherwise.
161 * Then I just call glBegin(that_variable), give my polygon
162 * coordinates, and glEnd(). Pretty neat eh? Too bad I couldn't
163 * integrate that trick here.
168 glNormal3f(0.0, 0.0, 1.0);
170 /* draw front face */
172 glBegin(GL_QUAD_STRIP);
173 for (i = 0; i <= teeth; i++) {
176 angle = i * 2.0 * M_PI / teeth;
177 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
178 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
180 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
181 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
183 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
184 glVertex3f(r1 * cos(angle + 4 * da), r1 * sin(angle + 4 * da), width * 0.5);
191 /* draw front sides of teeth */
194 da = 2.0 * M_PI / teeth / 4.0;
195 for (i = 0; i < teeth; i++) {
196 angle = i * 2.0 * M_PI / teeth;
199 glBegin(GL_LINE_LOOP);
200 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
201 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
202 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
203 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
212 glNormal3f(0.0, 0.0, -1.0);
216 glBegin(GL_QUAD_STRIP);
217 for (i = 0; i <= teeth; i++) {
218 angle = i * 2.0 * M_PI / teeth;
221 glVertex3f(r1 * cos(angle), r1 * sin(angle), -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(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
227 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
228 glVertex3f(r1 * cos(angle + 4 * da), r1 * sin(angle + 4 * da), -width * 0.5);
235 /* draw back sides of teeth */
238 da = 2.0 * M_PI / teeth / 4.0;
239 for (i = 0; i < teeth; i++) {
240 angle = i * 2.0 * M_PI / teeth;
243 glBegin(GL_LINE_LOOP);
244 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
245 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
246 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
247 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
255 /* draw outward faces of teeth */
257 glBegin(GL_QUAD_STRIP);
258 for (i = 0; i <= teeth; i++) {
259 angle = i * 2.0 * M_PI / teeth;
262 u = r2 * cos(angle + da) - r1 * cos(angle);
263 v = r2 * sin(angle + da) - r1 * sin(angle);
265 u = r2 * cos(angle + da + M_PI/2) - r1 * cos(angle + M_PI/2);
266 v = r2 * sin(angle + da + M_PI/2) - r1 * sin(angle + M_PI/2);
269 len = sqrt(u * u + v * v);
272 glNormal3f(v, -u, 0.0);
276 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
277 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
279 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
280 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
283 glNormal3f(cos(angle), sin(angle), 0.0);
285 glNormal3f(cos(angle + M_PI/2), sin(angle + M_PI/2), 0.0);
287 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
288 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
291 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
292 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
294 u = r1 * cos(angle + 3 * da + M_PI/2) - r2 * cos(angle + 2 * da + M_PI/2);
295 v = r1 * sin(angle + 3 * da + M_PI/2) - r2 * sin(angle + 2 * da + M_PI/2);
298 glNormal3f(v, -u, 0.0);
300 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
301 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
304 glNormal3f(cos(angle), sin(angle), 0.0);
306 glNormal3f(cos(angle + M_PI/2), sin(angle + M_PI/2), 0.0);
313 glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
314 glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
318 glShadeModel(GL_SMOOTH);
320 /* draw inside radius cylinder */
322 glBegin(GL_QUAD_STRIP);
323 for (i = 0; i <= teeth; i++) {
324 angle = i * 2.0 * M_PI / teeth;
329 glNormal3f(-cos(angle), -sin(angle), 0.0);
331 glNormal3f(cos(angle), sin(angle), 0.0);
333 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
334 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
336 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
337 glVertex3f(r0 * cos(angle + 4 * da), r0 * sin(angle + 4 * da), -width * 0.5);
338 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
339 glVertex3f(r0 * cos(angle + 4 * da), r0 * sin(angle + 4 * da), width * 0.5);
350 unit_tube (Bool wire)
353 int faces = TUBE_FACES;
354 GLfloat step = M_PI * 2 / faces;
363 glBegin(wire ? GL_LINES : GL_QUAD_STRIP);
365 glBegin(wire ? GL_LINES : GL_QUADS);
368 for (i = 0, th = 0; i <= faces; i++)
370 GLfloat x = cos (th);
371 GLfloat y = sin (th);
373 glVertex3f(x, 0.0, y);
374 glVertex3f(x, 1.0, y);
380 glVertex3f(x, 1.0, y);
381 glVertex3f(x, 0.0, y);
388 for (z = 0; z <= 1; z++)
390 glFrontFace(z == 0 ? GL_CCW : GL_CW);
391 glNormal3f(0, (z == 0 ? -1 : 1), 0);
392 glBegin(wire ? GL_LINE_LOOP : GL_TRIANGLE_FAN);
393 if (! wire) glVertex3f(0, z, 0);
394 for (i = 0, th = 0; i <= faces; i++)
396 GLfloat x = cos (th);
397 GLfloat y = sin (th);
407 tube (GLfloat x1, GLfloat y1, GLfloat z1,
408 GLfloat x2, GLfloat y2, GLfloat z2,
409 GLfloat diameter, GLfloat cap_size,
412 GLfloat length, angle, a, b, c;
414 if (diameter <= 0) abort();
420 length = sqrt (a*a + b*b + c*c);
421 angle = acos (a / length);
424 glTranslatef(x1, y1, z1);
425 glScalef (length, length, length);
427 if (c == 0 && b == 0)
428 glRotatef (angle / (M_PI / 180), 0, 1, 0);
430 glRotatef (angle / (M_PI / 180), 0, -c, b);
432 glRotatef (-90, 0, 0, 1);
433 glScalef (diameter/length, 1, diameter/length);
435 /* extend the endpoints of the tube by the cap size in both directions */
438 GLfloat c = cap_size/length;
439 glTranslatef (0, -c, 0);
440 glScalef (1, 1+c+c, 1);
449 ctube (GLfloat diameter, GLfloat width, Bool wire)
458 GLfloat width1, GLfloat height1,
459 GLfloat width2, GLfloat height2,
462 glShadeModel(GL_FLAT);
464 #if 0 /* don't need these - they're embedded in other objects */
467 glNormal3f(-1, 0, 0);
468 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
469 glVertex3f(-length/2, -width1/2, -height1/2);
470 glVertex3f(-length/2, width1/2, -height1/2);
471 glVertex3f(-length/2, width1/2, height1/2);
472 glVertex3f(-length/2, -width1/2, height1/2);
478 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
479 glVertex3f(length/2, -width2/2, -height2/2);
480 glVertex3f(length/2, width2/2, -height2/2);
481 glVertex3f(length/2, width2/2, height2/2);
482 glVertex3f(length/2, -width2/2, height2/2);
488 glNormal3f(0, 0, -1);
489 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
490 glVertex3f(-length/2, -width1/2, -height1/2);
491 glVertex3f(-length/2, width1/2, -height1/2);
492 glVertex3f( length/2, width2/2, -height2/2);
493 glVertex3f( length/2, -width2/2, -height2/2);
499 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
500 glVertex3f(-length/2, -width1/2, height1/2);
501 glVertex3f(-length/2, width1/2, height1/2);
502 glVertex3f( length/2, width2/2, height2/2);
503 glVertex3f( length/2, -width2/2, height2/2);
508 glNormal3f(0, -1, 0);
509 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
510 glVertex3f(-length/2, -width1/2, -height1/2);
511 glVertex3f(-length/2, -width1/2, height1/2);
512 glVertex3f( length/2, -width2/2, height2/2);
513 glVertex3f( length/2, -width2/2, -height2/2);
519 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
520 glVertex3f(-length/2, width1/2, -height1/2);
521 glVertex3f(-length/2, width1/2, height1/2);
522 glVertex3f( length/2, width2/2, height2/2);
523 glVertex3f( length/2, width2/2, -height2/2);
533 gearsstruct *gp = &gears[MI_SCREEN(mi)];
534 int wire = MI_IS_WIREFRAME(mi);
537 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
539 glClear(GL_COLOR_BUFFER_BIT);
544 gltrackball_rotate (gp->trackball);
548 get_rotation (gp->rot, &x, &y, &z, !gp->button_down_p);
549 glRotatef (x * 360, 1.0, 0.0, 0.0);
550 glRotatef (y * 360, 0.0, 1.0, 0.0);
551 glRotatef (z * 360, 0.0, 0.0, 1.0);
556 glTranslatef(-3.0, -2.0, 0.0);
557 glRotatef(gp->angle, 0.0, 0.0, 1.0);
558 /* PURIFY 4.0.1 reports an unitialized memory read on the next line when using
559 * MesaGL 2.2 and -mono. This has been fixed in MesaGL 2.3 and later. */
560 glCallList(gp->gear1);
564 glTranslatef(3.1, -2.0, 0.0);
565 glRotatef(-2.0 * gp->angle - 9.0, 0.0, 0.0, 1.0);
566 glCallList(gp->gear2);
570 glTranslatef(-3.1, 4.2, 0.0);
571 glRotatef(-2.0 * gp->angle - 25.0, 0.0, 0.0, 1.0);
572 glCallList(gp->gear3);
575 } else { /* planetary */
577 glScalef(0.8, 0.8, 0.8);
580 glTranslatef(0.0, 4.2, 0.0);
581 glRotatef(gp->angle - 7.0, 0.0, 0.0, 1.0);
582 glCallList(gp->gear1);
586 glRotatef(120, 0.0, 0.0, 1.0);
587 glTranslatef(0.0, 4.2, 0.0);
588 glRotatef(gp->angle - 7.0, 0.0, 0.0, 1.0);
589 glCallList(gp->gear2);
593 glRotatef(240, 0.0, 0.0, 1.0);
594 glTranslatef(0.0, 4.2, 0.0);
595 glRotatef(gp->angle - 7.0, 0.0, 0.0, 1.0);
596 glCallList(gp->gear3);
600 glTranslatef(0.0, 0.0, 0.0);
601 glRotatef(-gp->angle, 0.0, 0.0, 1.0);
602 glCallList(gp->gear_inner);
606 glTranslatef(0.0, 0.0, 0.0);
607 glRotatef((gp->angle / 3.0) - 7.5, 0.0, 0.0, 1.0);
608 glCallList(gp->gear_outer);
612 glTranslatef(0.0, 0.0, 0.0);
613 glCallList(gp->armature);
622 /* new window size or exposure */
624 reshape_gears(ModeInfo *mi, int width, int height)
626 GLfloat h = (GLfloat) height / (GLfloat) width;
628 glViewport(0, 0, (GLint) width, (GLint) height);
629 glMatrixMode(GL_PROJECTION);
631 glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
632 glMatrixMode(GL_MODELVIEW);
634 glTranslatef(0.0, 0.0, -40.0);
636 /* The depth buffer will be cleared, if needed, before the
637 * next frame. Right now we just want to black the screen.
639 glClear(GL_COLOR_BUFFER_BIT);
647 gearsstruct *gp = &gears[MI_SCREEN(mi)];
648 static GLfloat pos[4] =
649 {5.0, 5.0, 10.0, 1.0};
650 static GLfloat red[4] =
651 {0.8, 0.1, 0.0, 1.0};
652 static GLfloat green[4] =
653 {0.0, 0.8, 0.2, 1.0};
654 static GLfloat blue[4] =
655 {0.2, 0.2, 1.0, 1.0};
656 static GLfloat gray[4] =
657 {0.5, 0.5, 0.5, 1.0};
658 static GLfloat white[4] =
659 {1.0, 1.0, 1.0, 1.0};
660 int wire = MI_IS_WIREFRAME(mi);
661 int mono = MI_IS_MONO(mi);
664 glLightfv(GL_LIGHT0, GL_POSITION, pos);
665 glEnable(GL_CULL_FACE);
666 glEnable(GL_LIGHTING);
668 glEnable(GL_DEPTH_TEST);
672 * Messes up on multiscreen Pseudocolor:0 StaticGray(monochrome):1
673 * 2nd time mode is run it is Grayscale on PseudoColor.
674 * The code below forces monochrome on TrueColor.
676 if (MI_IS_MONO(mi)) {
677 red[0] = red[1] = red[2] = 1.0;
678 green[0] = green[1] = green[2] = 1.0;
679 blue[0] = blue[1] = blue[2] = 1.0;
687 gp->gear1 = glGenLists(1);
688 glNewList(gp->gear1, GL_COMPILE);
696 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
698 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
701 gear(1.0, 4.0, 1.0, 20, 0.7, wire, False);
704 gp->gear2 = glGenLists(1);
705 glNewList(gp->gear2, GL_COMPILE);
713 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
715 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
717 gear(0.5, 2.0, 2.0, 10, 0.7, wire, False);
720 gp->gear3 = glGenLists(1);
721 glNewList(gp->gear3, GL_COMPILE);
729 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
731 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
733 gear(1.3, 2.0, 0.5, 10, 0.7, wire, False);
736 glEnable(GL_NORMALIZE);
738 } else { /* planetary */
740 gp->gear1 = glGenLists(1);
741 glNewList(gp->gear1, GL_COMPILE);
749 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
751 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
753 gear(1.3, 2.0, 2.0, 12, 0.7, wire, False);
756 gp->gear2 = glGenLists(1);
757 glNewList(gp->gear2, GL_COMPILE);
765 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
767 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
769 gear(1.3, 2.0, 2.0, 12, 0.7, wire, False);
772 gp->gear3 = glGenLists(1);
773 glNewList(gp->gear3, GL_COMPILE);
781 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
783 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
785 gear(1.3, 2.0, 2.0, 12, 0.7, wire, False);
788 glEnable(GL_NORMALIZE);
791 gp->gear_inner = glGenLists(1);
792 glNewList(gp->gear_inner, GL_COMPILE);
800 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
802 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
804 gear(1.0, 2.0, 2.0, 12, 0.7, wire, False);
807 glEnable(GL_NORMALIZE);
810 gp->gear_outer = glGenLists(1);
811 glNewList(gp->gear_outer, GL_COMPILE);
819 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
821 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
823 gear(5.7, 7.0, 2.0, 36, 0.7, wire, True);
825 /* put some nubs on the outer ring, so we can tell how it's moving */
827 glTranslatef(7.0, 0, 0);
828 glRotatef(90, 0, 1, 0);
830 ctube(0.5, 0.5, wire); /* nub 1 */
834 glRotatef(120, 0, 0, 1);
835 glTranslatef(7.0, 0, 0);
836 glRotatef(90, 0, 1, 0);
837 ctube(0.5, 0.5, wire); /* nub 2 */
841 glRotatef(240, 0, 0, 1);
842 glTranslatef(7.0, 0, 0);
843 glRotatef(90, 0, 1, 0);
844 ctube(0.5, 0.5, wire); /* nub 3 */
850 glEnable(GL_NORMALIZE);
852 gp->armature = glGenLists(1);
853 glNewList(gp->armature, GL_COMPILE);
861 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
863 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
866 glTranslatef(0, 0, 1.5);
867 ctube(0.5, 10, wire); /* center axle */
870 glTranslatef(0.0, 4.2, -1);
871 ctube(0.5, 3, wire); /* axle 1 */
872 glTranslatef(0, 0, 1.8);
873 ctube(0.7, 0.7, wire);
877 glRotatef(120, 0.0, 0.0, 1.0);
878 glTranslatef(0.0, 4.2, -1);
879 ctube(0.5, 3, wire); /* axle 2 */
880 glTranslatef(0, 0, 1.8);
881 ctube(0.7, 0.7, wire);
885 glRotatef(240, 0.0, 0.0, 1.0);
886 glTranslatef(0.0, 4.2, -1);
887 ctube(0.5, 3, wire); /* axle 3 */
888 glTranslatef(0, 0, 1.8);
889 ctube(0.7, 0.7, wire);
892 glTranslatef(0, 0, 1.5); /* center disk */
896 glRotatef(270, 0, 0, 1);
897 glRotatef(-10, 0, 1, 0);
898 glTranslatef(-2.2, 0, 0);
899 arm(4.0, 1.0, 0.5, 2.0, 1.0, wire); /* arm 1 */
903 glRotatef(30, 0, 0, 1);
904 glRotatef(-10, 0, 1, 0);
905 glTranslatef(-2.2, 0, 0);
906 arm(4.0, 1.0, 0.5, 2.0, 1.0, wire); /* arm 2 */
910 glRotatef(150, 0, 0, 1);
911 glRotatef(-10, 0, 1, 0);
912 glTranslatef(-2.2, 0, 0);
913 arm(4.0, 1.0, 0.5, 2.0, 1.0, wire); /* arm 3 */
918 glEnable(GL_NORMALIZE);
924 gears_handle_event (ModeInfo *mi, XEvent *event)
926 gearsstruct *gp = &gears[MI_SCREEN(mi)];
928 if (event->xany.type == ButtonPress &&
929 event->xbutton.button & Button1)
931 gp->button_down_p = True;
932 gltrackball_start (gp->trackball,
933 event->xbutton.x, event->xbutton.y,
934 MI_WIDTH (mi), MI_HEIGHT (mi));
937 else if (event->xany.type == ButtonRelease &&
938 event->xbutton.button & Button1)
940 gp->button_down_p = False;
943 else if (event->xany.type == MotionNotify &&
946 gltrackball_track (gp->trackball,
947 event->xmotion.x, event->xmotion.y,
948 MI_WIDTH (mi), MI_HEIGHT (mi));
957 init_gears(ModeInfo * mi)
959 int screen = MI_SCREEN(mi);
962 /* Boolean rgba, doublebuffer, cmap_installed; */
966 if ((gears = (gearsstruct *) calloc(MI_NUM_SCREENS(mi),
967 sizeof (gearsstruct))) == NULL)
972 gp->window = MI_WINDOW(mi);
974 gp->rot = make_rotator (1, 1, 1, 1, 0, True);
975 gp->trackball = gltrackball_init ();
977 if ((gp->glx_context = init_GL(mi)) != NULL) {
978 reshape_gears(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
986 draw_gears(ModeInfo * mi)
988 gearsstruct *gp = &gears[MI_SCREEN(mi)];
989 Display *display = MI_DISPLAY(mi);
990 Window window = MI_WINDOW(mi);
991 int angle_incr = MI_CYCLES(mi) ? MI_CYCLES(mi) : 2;
996 if (!gp->glx_context)
999 glDrawBuffer(GL_BACK);
1001 glXMakeCurrent(display, window, *(gp->glx_context));
1004 /* let's do something so we don't get bored */
1005 gp->angle = (int) (gp->angle + angle_incr) % 360;
1007 if (mi->fps_p) do_fps (mi);
1009 glXSwapBuffers(display, window);
1013 release_gears(ModeInfo * mi)
1015 if (gears != NULL) {
1018 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
1019 gearsstruct *gp = &gears[screen];
1021 if (gp->glx_context) {
1022 /* Display lists MUST be freed while their glXContext is current. */
1023 glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
1025 if (glIsList(gp->gear1))
1026 glDeleteLists(gp->gear1, 1);
1027 if (glIsList(gp->gear2))
1028 glDeleteLists(gp->gear2, 1);
1029 if (glIsList(gp->gear3))
1030 glDeleteLists(gp->gear3, 1);
1031 if (glIsList(gp->gear_inner))
1032 glDeleteLists(gp->gear_inner, 1);
1033 if (glIsList(gp->gear_outer))
1034 glDeleteLists(gp->gear_outer, 1);
1038 (void) free((void *) gears);
1045 /*********************************************************/