From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / glx / hypnowheel.c
1 /* hypnowheel, Copyright (c) 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  * Draws overlapping spirals, where the tightness of the spirals changes.
12  * Nice settings:
13  *
14  * -layers 7 -wander
15  * -count 3 -layers 50
16  * -twistiness 0.2 -layers 20
17  * -count 3 -layers 2 -speed 20 -twist 10 -wander
18  */
19
20 #define DEFAULTS        "*delay:        20000       \n" \
21                         "*count:        13          \n" \
22                         "*showFPS:      False       \n" \
23                         "*fpsSolid:     True        \n" \
24                         "*wireframe:    False       \n" \
25                         "*suppressRotationAnimation: True\n" \
26
27 # define refresh_hypnowheel 0
28 # define release_hypnowheel 0
29 #undef countof
30 #define countof(x) (sizeof((x))/sizeof((*x)))
31
32 #include "xlockmore.h"
33 #include "colors.h"
34 #include "rotator.h"
35 #include <ctype.h>
36
37 #ifdef USE_GL /* whole file */
38
39
40 #define DEF_WANDER      "False"
41 #define DEF_SYMMETRIC   "False"
42 #define DEF_SPEED       "1.0"
43 #define DEF_TWISTINESS  "4.0"
44 #define DEF_LAYERS      "4"
45
46 typedef struct {
47   int color;
48   GLfloat twist;
49   GLfloat alpha;
50   rotator *rot;
51 } disc;
52
53 typedef struct {
54   GLXContext *glx_context;
55   rotator *rot;
56   disc *discs;
57   int ncolors;
58   XColor *colors;
59
60 } hypnowheel_configuration;
61
62 static hypnowheel_configuration *bps = NULL;
63
64 static GLfloat speed;
65 static GLfloat twistiness;
66 static GLint nlayers;
67 static Bool do_wander;
68 static Bool do_symmetric;
69
70 static XrmOptionDescRec opts[] = {
71   { "-speed",      ".speed",      XrmoptionSepArg, 0 },
72   { "-twistiness", ".twistiness", XrmoptionSepArg, 0 },
73   { "-layers",     ".layers",     XrmoptionSepArg, 0 },
74   { "-wander",     ".wander",     XrmoptionNoArg, "True" },
75   { "+wander",     ".wander",     XrmoptionNoArg, "False" },
76   { "-symmetric",  ".symmetric",  XrmoptionNoArg, "True" },
77   { "+symmetric",  ".symmetric",  XrmoptionNoArg, "False" },
78 };
79
80 static argtype vars[] = {
81   {&do_wander,    "wander",     "Wander",     DEF_WANDER,     t_Bool},
82   {&do_symmetric, "symmetric",  "Symmetric",  DEF_SYMMETRIC,  t_Bool},
83   {&speed,        "speed",      "Speed",      DEF_SPEED,      t_Float},
84   {&twistiness,   "twistiness", "Twistiness", DEF_TWISTINESS, t_Float},
85   {&nlayers,      "layers",     "Layers",     DEF_LAYERS,     t_Int},
86 };
87
88 ENTRYPOINT ModeSpecOpt hypnowheel_opts = {
89   countof(opts), opts, countof(vars), vars, NULL};
90
91
92 static void
93 draw_spiral (ModeInfo *mi, disc *d)
94 {
95   int wire = MI_IS_WIREFRAME(mi);
96   hypnowheel_configuration *bp = &bps[MI_SCREEN(mi)];
97   GLfloat rr = 0.5;
98   int n = MI_COUNT(mi);
99   int steps = n * (wire ? 3 : (n < 5 ? 60 : n < 10 ? 20 : 10));
100   GLfloat dth = M_PI*2 / n;
101   GLfloat dr = rr / steps;
102   GLfloat th;
103   GLfloat twist = d->twist;
104   GLfloat dtwist = M_PI * 2 * twist / steps;
105   double cscale = 65536.0;
106
107   if (nlayers > 3 && !wire)
108     cscale *= (nlayers-2);   /* don't wash out to white */
109
110   glColor4f (bp->colors[d->color].red   / cscale,
111              bp->colors[d->color].green / cscale, 
112              bp->colors[d->color].blue  / cscale,
113              d->alpha);
114   for (th = 0; th < M_PI*2; th += dth)
115     {
116       GLfloat th1 = th;
117       GLfloat r;
118       glBegin (wire ? GL_LINE_STRIP : GL_QUAD_STRIP);
119       for (r = 0; r <= rr; r += dr)
120         {
121           GLfloat th2 = th1 + dth/2 + dtwist;
122           th1 += dtwist;
123           glVertex3f (r * cos(th1), r * sin(th1), 0);
124           if (! wire)
125             glVertex3f (r * cos(th2), r * sin(th2), 0);
126           mi->polygon_count++;
127         }
128       glEnd();
129     }
130 }
131
132
133
134 /* Window management, etc
135  */
136 ENTRYPOINT void
137 reshape_hypnowheel (ModeInfo *mi, int width, int height)
138 {
139   GLfloat h = (GLfloat) height / (GLfloat) width;
140
141   glViewport (0, 0, (GLint) width, (GLint) height);
142
143   glMatrixMode(GL_PROJECTION);
144   glLoadIdentity();
145   gluPerspective (30.0, 1/h, 1.0, 100.0);
146
147   glMatrixMode(GL_MODELVIEW);
148   glLoadIdentity();
149   gluLookAt( 0.0, 0.0, 30.0,
150              0.0, 0.0, 0.0,
151              0.0, 1.0, 0.0);
152
153 # ifdef HAVE_MOBILE     /* Keep it the same relative size when rotated. */
154   {
155     int o = (int) current_device_rotation();
156     if (o != 0 && o != 180 && o != -180)
157       glScalef (1/h, 1/h, 1/h);
158   }
159 # endif
160
161   glClear(GL_COLOR_BUFFER_BIT);
162 }
163
164
165 ENTRYPOINT void 
166 init_hypnowheel (ModeInfo *mi)
167 {
168   hypnowheel_configuration *bp;
169   int wire = MI_IS_WIREFRAME(mi);
170   int i;
171
172   MI_INIT (mi, bps, NULL);
173
174   bp = &bps[MI_SCREEN(mi)];
175
176   bp->glx_context = init_GL(mi);
177
178   reshape_hypnowheel (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
179
180   if (! bp->rot)
181     bp->rot = make_rotator (0, 0, 0, 0, speed * 0.0025, False);
182
183   bp->ncolors = 1024;
184   if (!bp->colors)
185     bp->colors = (XColor *) calloc(bp->ncolors, sizeof(XColor));
186   make_smooth_colormap (0, 0, 0,
187                         bp->colors, &bp->ncolors,
188                         False, 0, False);
189
190   if (MI_COUNT(mi) < 2) MI_COUNT(mi) = 2;
191   if (nlayers < 1) nlayers = 1;
192   if (!bp->discs)
193     bp->discs = (disc *) calloc (nlayers, sizeof (disc));
194
195   for (i = 0; i < nlayers; i++)
196     {
197       double spin_speed   = speed * 0.2;
198       double wander_speed = speed * 0.0012;
199       double spin_accel   = 0.2;
200
201       bp->discs[i].twist = 0;
202       bp->discs[i].alpha = 1;
203       bp->discs[i].color = i * bp->ncolors / nlayers;
204
205       spin_speed   += frand (spin_speed   / 5);
206       wander_speed += frand (wander_speed * 3);
207
208       if (!bp->discs[i].rot)
209       bp->discs[i].rot = make_rotator (spin_speed, spin_speed, spin_speed,
210                                        spin_accel,
211                                        (do_wander ? wander_speed : 0),
212                                        True);
213     }
214
215   glDisable (GL_LIGHTING);
216   glDisable (GL_DEPTH_TEST);
217   glDepthMask (GL_FALSE);
218   glDisable (GL_CULL_FACE);
219
220   if (! wire)
221     {
222       glEnable (GL_BLEND);
223       glBlendFunc (GL_ONE, GL_ONE);
224     }
225 }
226
227
228 ENTRYPOINT void
229 draw_hypnowheel (ModeInfo *mi)
230 {
231   hypnowheel_configuration *bp = &bps[MI_SCREEN(mi)];
232   Display *dpy = MI_DISPLAY(mi);
233   Window window = MI_WINDOW(mi);
234   int i;
235
236   if (!bp->glx_context)
237     return;
238
239   glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
240
241   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
242
243   glPushMatrix ();
244
245   {
246     double x, y, z;
247     get_position (bp->rot, &x, &y, &z, True);
248     glTranslatef((x - 0.5) * 8,
249                  (y - 0.5) * 8,
250                  0);
251
252     get_rotation (bp->rot, &x, &y, &z, True);
253     glRotatef (x * 360, 1.0, 0.0, 0.0);
254     glRotatef (y * 360, 0.0, 1.0, 0.0);
255     glRotatef (z * 360, 0.0, 0.0, 1.0);
256   }
257
258   mi->polygon_count = 0;
259
260   glScalef (45, 45, 45);
261
262   for (i = 0; i < nlayers; i++)
263     {
264       disc *d = &bp->discs[i];
265       double x, y, z;
266       rotator *rot = (do_symmetric ? bp->discs[(i & ~0x1)].rot : d->rot);
267       Bool tick = (!do_symmetric || i == 0);
268
269       glPushMatrix();
270
271       d->color++;
272       if (d->color >= bp->ncolors)
273         d->color = 0;
274
275       get_position (rot, &x, &y, &z, tick);
276       x -= 0.5;
277       y -= 0.5;
278       x *= 0.1;
279       y *= 0.1;
280
281       glTranslatef (x, y, 0);
282       d->twist = (z * twistiness *
283                   ((i & 1) ? 1 : -1));
284
285       get_rotation (rot, &x, &y, &z, tick);
286
287       glRotatef (360 * z, 0, 0, 1);  /* rotation of this disc */
288
289       draw_spiral (mi, &bp->discs[i]);
290       glPopMatrix();
291     }
292
293   glPopMatrix ();
294
295   if (mi->fps_p) do_fps (mi);
296   glFinish();
297
298   glXSwapBuffers(dpy, window);
299 }
300
301 ENTRYPOINT Bool
302 hypnowheel_handle_event (ModeInfo *mi, XEvent *event)
303 {
304   if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
305     {
306       init_hypnowheel (mi);
307       return True;
308     }
309   return False;
310 }
311
312
313 XSCREENSAVER_MODULE ("Hypnowheel", hypnowheel)
314
315 #endif /* USE_GL */