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