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