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