http://www.tienza.es/crux/src/www.jwz.org/xscreensaver/xscreensaver-5.05.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 # define hypnowheel_handle_event 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 GLfloat 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_Float},
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   glClear(GL_COLOR_BUFFER_BIT);
154 }
155
156
157 ENTRYPOINT void 
158 init_hypnowheel (ModeInfo *mi)
159 {
160   hypnowheel_configuration *bp;
161   int wire = MI_IS_WIREFRAME(mi);
162   int i;
163
164   if (!bps) {
165     bps = (hypnowheel_configuration *)
166       calloc (MI_NUM_SCREENS(mi), sizeof (hypnowheel_configuration));
167     if (!bps) {
168       fprintf(stderr, "%s: out of memory\n", progname);
169       exit(1);
170     }
171   }
172
173   bp = &bps[MI_SCREEN(mi)];
174
175   bp->glx_context = init_GL(mi);
176
177   reshape_hypnowheel (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
178
179   bp->rot = make_rotator (0, 0, 0, 0, speed * 0.0025, False);
180
181   bp->ncolors = 1024;
182   bp->colors = (XColor *) calloc(bp->ncolors, sizeof(XColor));
183   make_smooth_colormap (0, 0, 0,
184                         bp->colors, &bp->ncolors,
185                         False, 0, False);
186
187   if (MI_COUNT(mi) < 2) MI_COUNT(mi) = 2;
188   if (nlayers < 1) nlayers = 1;
189   bp->discs = (disc *) calloc (nlayers, sizeof (disc));
190
191   for (i = 0; i < nlayers; i++)
192     {
193       double spin_speed   = speed * 0.2;
194       double wander_speed = speed * 0.0012;
195       double spin_accel   = 0.2;
196
197       bp->discs[i].twist = 0;
198       bp->discs[i].alpha = 1;
199       bp->discs[i].color = i * bp->ncolors / nlayers;
200
201       spin_speed   += frand (spin_speed   / 5);
202       wander_speed += frand (wander_speed * 3);
203
204       bp->discs[i].rot = make_rotator (spin_speed, spin_speed, spin_speed,
205                                        spin_accel,
206                                        (do_wander ? wander_speed : 0),
207                                        True);
208     }
209
210   glDisable (GL_LIGHTING);
211   glDisable (GL_DEPTH_TEST);
212   glDepthMask (GL_FALSE);
213   glDisable (GL_CULL_FACE);
214
215   if (! wire)
216     {
217       glEnable (GL_BLEND);
218       glBlendFunc (GL_ONE, GL_ONE);
219     }
220 }
221
222
223 ENTRYPOINT void
224 draw_hypnowheel (ModeInfo *mi)
225 {
226   hypnowheel_configuration *bp = &bps[MI_SCREEN(mi)];
227   Display *dpy = MI_DISPLAY(mi);
228   Window window = MI_WINDOW(mi);
229   int i;
230
231   if (!bp->glx_context)
232     return;
233
234   glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
235
236   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
237
238   glPushMatrix ();
239
240   {
241     double x, y, z;
242     get_position (bp->rot, &x, &y, &z, True);
243     glTranslatef((x - 0.5) * 8,
244                  (y - 0.5) * 8,
245                  0);
246
247     get_rotation (bp->rot, &x, &y, &z, True);
248     glRotatef (x * 360, 1.0, 0.0, 0.0);
249     glRotatef (y * 360, 0.0, 1.0, 0.0);
250     glRotatef (z * 360, 0.0, 0.0, 1.0);
251   }
252
253   mi->polygon_count = 0;
254
255   glScalef (45, 45, 45);
256
257   for (i = 0; i < nlayers; i++)
258     {
259       disc *d = &bp->discs[i];
260       double x, y, z;
261       rotator *rot = (do_symmetric ? bp->discs[(i & ~0x1)].rot : d->rot);
262       Bool tick = (!do_symmetric || i == 0);
263
264       glPushMatrix();
265
266       d->color++;
267       if (d->color >= bp->ncolors)
268         d->color = 0;
269
270       get_position (rot, &x, &y, &z, tick);
271       x -= 0.5;
272       y -= 0.5;
273       x *= 0.1;
274       y *= 0.1;
275
276       glTranslatef (x, y, 0);
277       d->twist = (z * twistiness *
278                   ((i & 1) ? 1 : -1));
279
280       get_rotation (rot, &x, &y, &z, tick);
281
282       glRotatef (360 * z, 0, 0, 1);  /* rotation of this disc */
283
284       draw_spiral (mi, &bp->discs[i]);
285       glPopMatrix();
286     }
287
288   glPopMatrix ();
289
290   if (mi->fps_p) do_fps (mi);
291   glFinish();
292
293   glXSwapBuffers(dpy, window);
294 }
295
296 XSCREENSAVER_MODULE ("Hypnowheel", hypnowheel)
297
298 #endif /* USE_GL */