http://www.tienza.es/crux/src/www.jwz.org/xscreensaver/xscreensaver-5.05.tar.gz
[xscreensaver] / hacks / glx / moebiusgears.c
1 /* moebiusgears, Copyright (c) 2007-2008 Jamie Zawinski <jwz@jwz.org>
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or 
9  * implied warranty.
10  */
11
12 #define DEFAULTS        "*delay:        30000       \n" \
13                         "*count:        17          \n" \
14                         "*showFPS:      False       \n" \
15                         "*wireframe:    False       \n" \
16
17 # define refresh_mgears 0
18 # define release_mgears 0
19 #undef countof
20 #define countof(x) (sizeof((x))/sizeof((*x)))
21
22 #include "xlockmore.h"
23 #include "involute.h"
24 #include "normals.h"
25 #include "rotator.h"
26 #include "gltrackball.h"
27 #include <ctype.h>
28
29 #ifdef USE_GL /* whole file */
30
31
32 #define DEF_SPIN        "True"
33 #define DEF_WANDER      "True"
34 #define DEF_ROLL        "True"
35 #define DEF_SPEED       "1.0"
36 #define DEF_TEETH       "15"
37
38 typedef struct {
39
40   gear g;
41   GLfloat pos_th;  /* position on ring of gear system */
42   GLfloat pos_thz; /* rotation out of plane of gear system */
43 } mogear;
44
45 typedef struct {
46   GLXContext *glx_context;
47   rotator *rot;
48   trackball_state *trackball;
49   Bool button_down_p;
50
51   int ngears;
52   mogear *gears;
53   GLfloat ring_r;  /* radius of gear system */
54   GLfloat roll_th;
55
56 } mgears_configuration;
57
58 static mgears_configuration *bps = NULL;
59
60 static Bool do_spin;
61 static GLfloat speed;
62 static Bool do_wander;
63 static Bool do_roll;
64 static int teeth_arg;
65
66 static XrmOptionDescRec opts[] = {
67   { "-spin",   ".spin",   XrmoptionNoArg, "True"  },
68   { "+spin",   ".spin",   XrmoptionNoArg, "False" },
69   { "-speed",  ".speed",  XrmoptionSepArg, 0      },
70   { "-wander", ".wander", XrmoptionNoArg, "True"  },
71   { "+wander", ".wander", XrmoptionNoArg, "False" },
72   { "-roll",   ".roll",   XrmoptionNoArg, "True"  },
73   { "+roll",   ".roll",   XrmoptionNoArg, "False" },
74   { "-teeth",  ".teeth",  XrmoptionSepArg, 0      },
75 };
76
77 static argtype vars[] = {
78   {&do_spin,   "spin",   "Spin",   DEF_SPIN,   t_Bool},
79   {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
80   {&do_roll,   "roll",   "Roll",   DEF_ROLL,   t_Bool},
81   {&speed,     "speed",  "Speed",  DEF_SPEED,  t_Float},
82   {&teeth_arg, "teeth",  "Teeth",  DEF_TEETH,  t_Int},
83 };
84
85 ENTRYPOINT ModeSpecOpt mgears_opts = {countof(opts), opts, countof(vars), vars, NULL};
86
87
88 /* Window management, etc
89  */
90 ENTRYPOINT void
91 reshape_mgears (ModeInfo *mi, int width, int height)
92 {
93   GLfloat h = (GLfloat) height / (GLfloat) width;
94
95   glViewport (0, 0, (GLint) width, (GLint) height);
96
97   glMatrixMode(GL_PROJECTION);
98   glLoadIdentity();
99   gluPerspective (30.0, 1/h, 1.0, 100.0);
100
101   glMatrixMode(GL_MODELVIEW);
102   glLoadIdentity();
103   gluLookAt( 0.0, 0.0, 30.0,
104              0.0, 0.0, 0.0,
105              0.0, 1.0, 0.0);
106
107   glClear(GL_COLOR_BUFFER_BIT);
108 }
109
110
111 ENTRYPOINT Bool
112 mgears_handle_event (ModeInfo *mi, XEvent *event)
113 {
114   mgears_configuration *bp = &bps[MI_SCREEN(mi)];
115
116   if (event->xany.type == ButtonPress &&
117       event->xbutton.button == Button1)
118     {
119       bp->button_down_p = True;
120       gltrackball_start (bp->trackball,
121                          event->xbutton.x, event->xbutton.y,
122                          MI_WIDTH (mi), MI_HEIGHT (mi));
123       return True;
124     }
125   else if (event->xany.type == ButtonRelease &&
126            event->xbutton.button == Button1)
127     {
128       bp->button_down_p = False;
129       return True;
130     }
131   else if (event->xany.type == ButtonPress &&
132            (event->xbutton.button == Button4 ||
133             event->xbutton.button == Button5 ||
134             event->xbutton.button == Button6 ||
135             event->xbutton.button == Button7))
136     {
137       gltrackball_mousewheel (bp->trackball, event->xbutton.button, 10,
138                               !!event->xbutton.state);
139       return True;
140     }
141   else if (event->xany.type == MotionNotify &&
142            bp->button_down_p)
143     {
144       gltrackball_track (bp->trackball,
145                          event->xmotion.x, event->xmotion.y,
146                          MI_WIDTH (mi), MI_HEIGHT (mi));
147       return True;
148     }
149
150   return False;
151 }
152
153
154
155 ENTRYPOINT void 
156 init_mgears (ModeInfo *mi)
157 {
158   mgears_configuration *bp;
159   int wire = MI_IS_WIREFRAME(mi);
160   int i;
161
162   if (!bps) {
163     bps = (mgears_configuration *)
164       calloc (MI_NUM_SCREENS(mi), sizeof (mgears_configuration));
165     if (!bps) {
166       fprintf(stderr, "%s: out of memory\n", progname);
167       exit(1);
168     }
169
170     bp = &bps[MI_SCREEN(mi)];
171   }
172
173   bp = &bps[MI_SCREEN(mi)];
174
175   bp->glx_context = init_GL(mi);
176
177   reshape_mgears (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
178
179   if (!wire)
180     {
181       GLfloat pos[4] = {1.0, 1.0, 1.0, 0.0};
182       GLfloat amb[4] = {0.0, 0.0, 0.0, 1.0};
183       GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
184       GLfloat spc[4] = {0.0, 1.0, 1.0, 1.0};
185
186       glEnable(GL_LIGHTING);
187       glEnable(GL_LIGHT0);
188       glEnable(GL_DEPTH_TEST);
189       glEnable(GL_CULL_FACE);
190
191       glLightfv(GL_LIGHT0, GL_POSITION, pos);
192       glLightfv(GL_LIGHT0, GL_AMBIENT,  amb);
193       glLightfv(GL_LIGHT0, GL_DIFFUSE,  dif);
194       glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
195     }
196
197   {
198     double spin_speed   = 0.5;
199     double wander_speed = 0.01;
200     double spin_accel   = 2.0;
201
202     bp->rot = make_rotator (do_spin ? spin_speed : 0,
203                             do_spin ? spin_speed : 0,
204                             do_spin ? spin_speed : 0,
205                             spin_accel,
206                             do_wander ? wander_speed : 0,
207                             False /* don't randomize */
208                             );
209     bp->trackball = gltrackball_init ();
210   }
211
212   {
213     int total_gears = MI_COUNT(mi);
214     double gears_per_turn;
215     double gear_r, tw, th, thick, slope;
216     int nubs, size;
217
218     if (! (total_gears & 1)) 
219       total_gears++;            /* must be odd or gears intersect */
220
221     /* Number of teeth must be odd if number of gears is odd, or teeth don't
222        mesh when the loop closes.  And since number of gears must be odd...
223      */
224     if (! (teeth_arg & 1)) teeth_arg++;
225     if (teeth_arg < 7) teeth_arg = 7;
226
227     if (total_gears < 13)       /* gear mesh angle is too steep with less */
228       total_gears = 13;
229
230     thick = 0.2;
231     nubs = (random() & 3) ? 0 : (random() % teeth_arg) / 2;
232
233     slope = 0;
234
235     /* Sloping gears are incompatible with "-roll" ... */
236     /* slope= -M_PI * 2 / total_gears; */
237
238     gears_per_turn = total_gears / 2.0;
239
240     bp->ring_r = 3;
241     gear_r = M_PI * bp->ring_r / gears_per_turn;
242     tw = 0;
243     th = gear_r * 2.5 / teeth_arg;
244
245     /* If the gears are small, use a lower density mesh. */
246     size = (gear_r > 0.32 ? INVOLUTE_LARGE  :
247             gear_r > 0.13 ? INVOLUTE_MEDIUM :
248             INVOLUTE_SMALL);
249
250     /* If there are lots of teeth, use a lower density mesh. */
251     if (teeth_arg > 77)
252       size = INVOLUTE_SMALL;
253     if (teeth_arg > 45 && size == INVOLUTE_LARGE)
254       size = INVOLUTE_MEDIUM;
255
256     bp->ngears = total_gears;
257     bp->gears = (mogear *) calloc (bp->ngears, sizeof(*bp->gears));
258     for (i = 0; i < bp->ngears; i++)
259       {
260         mogear *mg = &bp->gears[i];
261         gear *g = &mg->g;
262
263         g->r           = gear_r;
264         g->size        = size;
265         g->nteeth      = teeth_arg;
266         g->tooth_w     = tw;
267         g->tooth_h     = th;
268         g->tooth_slope = slope;
269         g->thickness   = g->r * thick;
270         g->thickness2  = g->thickness * 0.1;
271         g->thickness3  = g->thickness;
272         g->inner_r     = g->r * 0.80;
273         g->inner_r2    = g->r * 0.60;
274         g->inner_r3    = g->r * 0.55;
275         g->nubs        = nubs;
276         mg->pos_th     = (M_PI * 2 / gears_per_turn) * i;
277         mg->pos_thz    = (M_PI / 2 / gears_per_turn) * i;
278
279         g->th = ((i & 1)
280                  ? (M_PI * 2 / g->nteeth)
281                  : 0);
282
283         /* Colorize
284          */
285         g->color[0] = 0.7 + frand(0.3);
286         g->color[1] = 0.7 + frand(0.3);
287         g->color[2] = 0.7 + frand(0.3);
288         g->color[3] = 1.0;
289
290         g->color2[0] = g->color[0] * 0.85;
291         g->color2[1] = g->color[1] * 0.85;
292         g->color2[2] = g->color[2] * 0.85;
293         g->color2[3] = g->color[3];
294
295         /* Now render the gear into its display list.
296          */
297         g->dlist = glGenLists (1);
298         if (! g->dlist)
299           {
300             check_gl_error ("glGenLists");
301             abort();
302           }
303
304         glNewList (g->dlist, GL_COMPILE);
305         g->polygons += draw_involute_gear (g, wire);
306         glEndList ();
307       }
308   }
309 }
310
311
312 ENTRYPOINT void
313 draw_mgears (ModeInfo *mi)
314 {
315   mgears_configuration *bp = &bps[MI_SCREEN(mi)];
316   Display *dpy = MI_DISPLAY(mi);
317   Window window = MI_WINDOW(mi);
318   int i;
319
320   if (!bp->glx_context)
321     return;
322
323   glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
324
325   glShadeModel(GL_SMOOTH);
326
327   glEnable(GL_DEPTH_TEST);
328   glEnable(GL_NORMALIZE);
329   glEnable(GL_CULL_FACE);
330
331   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
332
333   glPushMatrix ();
334
335   glScalef(1.1, 1.1, 1.1);
336
337   {
338     double x, y, z;
339     get_position (bp->rot, &x, &y, &z, !bp->button_down_p);
340     glTranslatef ((x - 0.5) * 4,
341                   (y - 0.5) * 4,
342                   (z - 0.5) * 7);
343
344     gltrackball_rotate (bp->trackball);
345
346     get_rotation (bp->rot, &x, &y, &z, !bp->button_down_p);
347
348     /* add a little rotation for -no-spin mode */
349     x -= 0.14;
350     y -= 0.06;
351
352     glRotatef (x * 360, 1.0, 0.0, 0.0);
353     glRotatef (y * 360, 0.0, 1.0, 0.0);
354     glRotatef (z * 360, 0.0, 0.0, 1.0);
355   }
356
357   mi->polygon_count = 0;
358
359   glScalef (1.5, 1.5, 1.5);
360
361 /*#define DEBUG*/
362
363 #ifdef DEBUG
364   glScalef (.5, .5, .5);
365   glTranslatef (0, -bp->gears[0].g.r * bp->ngears, 0);
366 #endif
367
368   for (i = 0; i < bp->ngears; i++)
369     {
370       mogear *mg = &bp->gears[i];
371       gear *g = &mg->g;
372
373       glPushMatrix();
374 #ifndef DEBUG
375       glRotatef (mg->pos_th  * 180 / M_PI, 0, 0, 1);  /* rotation on ring */
376       glTranslatef (bp->ring_r, 0, 0);                /* position on ring */
377       glRotatef (mg->pos_thz * 180 / M_PI, 0, 1, 0);  /* twist a bit */
378
379       if (do_roll)
380         {
381           glRotatef (bp->roll_th * 180 / M_PI, 0, 1, 0);
382           bp->roll_th += speed * 0.0005;
383         }
384 #else
385       glTranslatef (0, i * 2 * g->r, 0);
386 #endif
387       glRotatef (g->th * 180 / M_PI, 0, 0, 1);
388
389       glCallList (g->dlist);
390       mi->polygon_count += g->polygons;
391       glPopMatrix ();
392     }
393
394   glPopMatrix ();
395
396 #ifndef DEBUG
397   /* spin gears */
398   for (i = 0; i < bp->ngears; i++)
399     {
400       mogear *mg = &bp->gears[i];
401       mg->g.th += speed * (M_PI / 100) * (i & 1 ? 1 : -1);
402     }
403 #endif
404
405   if (mi->fps_p) do_fps (mi);
406   glFinish();
407
408   glXSwapBuffers(dpy, window);
409 }
410
411 XSCREENSAVER_MODULE_2 ("MoebiusGears", moebiusgears, mgears)
412
413 #endif /* USE_GL */