1 /* hypnowheel, Copyright (c) 2008 Jamie Zawinski <jwz@jwz.org>
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
11 * Draws overlapping spirals, where the tightness of the spirals changes.
16 * -twistiness 0.2 -layers 20
17 * -count 3 -layers 2 -speed 20 -twist 10 -wander
20 #define DEFAULTS "*delay: 20000 \n" \
22 "*showFPS: False \n" \
23 "*fpsSolid: True \n" \
24 "*wireframe: False \n" \
26 # define refresh_hypnowheel 0
27 # define release_hypnowheel 0
29 #define countof(x) (sizeof((x))/sizeof((*x)))
31 #include "xlockmore.h"
36 #ifdef USE_GL /* whole file */
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"
53 GLXContext *glx_context;
59 } hypnowheel_configuration;
61 static hypnowheel_configuration *bps = NULL;
64 static GLfloat twistiness;
66 static Bool do_wander;
67 static Bool do_symmetric;
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" },
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},
87 ENTRYPOINT ModeSpecOpt hypnowheel_opts = {
88 countof(opts), opts, countof(vars), vars, NULL};
92 draw_spiral (ModeInfo *mi, disc *d)
94 int wire = MI_IS_WIREFRAME(mi);
95 hypnowheel_configuration *bp = &bps[MI_SCREEN(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;
102 GLfloat twist = d->twist;
103 GLfloat dtwist = M_PI * 2 * twist / steps;
104 double cscale = 65536.0;
106 if (nlayers > 3 && !wire)
107 cscale *= (nlayers-2); /* don't wash out to white */
109 glColor4f (bp->colors[d->color].red / cscale,
110 bp->colors[d->color].green / cscale,
111 bp->colors[d->color].blue / cscale,
113 for (th = 0; th < M_PI*2; th += dth)
117 glBegin (wire ? GL_LINE_STRIP : GL_QUAD_STRIP);
118 for (r = 0; r <= rr; r += dr)
120 GLfloat th2 = th1 + dth/2 + dtwist;
122 glVertex3f (r * cos(th1), r * sin(th1), 0);
124 glVertex3f (r * cos(th2), r * sin(th2), 0);
133 /* Window management, etc
136 reshape_hypnowheel (ModeInfo *mi, int width, int height)
138 GLfloat h = (GLfloat) height / (GLfloat) width;
140 glViewport (0, 0, (GLint) width, (GLint) height);
142 glMatrixMode(GL_PROJECTION);
144 gluPerspective (30.0, 1/h, 1.0, 100.0);
146 glMatrixMode(GL_MODELVIEW);
148 gluLookAt( 0.0, 0.0, 30.0,
152 glClear(GL_COLOR_BUFFER_BIT);
157 init_hypnowheel (ModeInfo *mi)
159 hypnowheel_configuration *bp;
160 int wire = MI_IS_WIREFRAME(mi);
164 bps = (hypnowheel_configuration *)
165 calloc (MI_NUM_SCREENS(mi), sizeof (hypnowheel_configuration));
167 fprintf(stderr, "%s: out of memory\n", progname);
172 bp = &bps[MI_SCREEN(mi)];
174 bp->glx_context = init_GL(mi);
176 reshape_hypnowheel (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
179 bp->rot = make_rotator (0, 0, 0, 0, speed * 0.0025, False);
183 bp->colors = (XColor *) calloc(bp->ncolors, sizeof(XColor));
184 make_smooth_colormap (0, 0, 0,
185 bp->colors, &bp->ncolors,
188 if (MI_COUNT(mi) < 2) MI_COUNT(mi) = 2;
189 if (nlayers < 1) nlayers = 1;
191 bp->discs = (disc *) calloc (nlayers, sizeof (disc));
193 for (i = 0; i < nlayers; i++)
195 double spin_speed = speed * 0.2;
196 double wander_speed = speed * 0.0012;
197 double spin_accel = 0.2;
199 bp->discs[i].twist = 0;
200 bp->discs[i].alpha = 1;
201 bp->discs[i].color = i * bp->ncolors / nlayers;
203 spin_speed += frand (spin_speed / 5);
204 wander_speed += frand (wander_speed * 3);
206 if (!bp->discs[i].rot)
207 bp->discs[i].rot = make_rotator (spin_speed, spin_speed, spin_speed,
209 (do_wander ? wander_speed : 0),
213 glDisable (GL_LIGHTING);
214 glDisable (GL_DEPTH_TEST);
215 glDepthMask (GL_FALSE);
216 glDisable (GL_CULL_FACE);
221 glBlendFunc (GL_ONE, GL_ONE);
227 draw_hypnowheel (ModeInfo *mi)
229 hypnowheel_configuration *bp = &bps[MI_SCREEN(mi)];
230 Display *dpy = MI_DISPLAY(mi);
231 Window window = MI_WINDOW(mi);
234 if (!bp->glx_context)
237 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
239 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
245 get_position (bp->rot, &x, &y, &z, True);
246 glTranslatef((x - 0.5) * 8,
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);
256 mi->polygon_count = 0;
258 glScalef (45, 45, 45);
260 for (i = 0; i < nlayers; i++)
262 disc *d = &bp->discs[i];
264 rotator *rot = (do_symmetric ? bp->discs[(i & ~0x1)].rot : d->rot);
265 Bool tick = (!do_symmetric || i == 0);
270 if (d->color >= bp->ncolors)
273 get_position (rot, &x, &y, &z, tick);
279 glTranslatef (x, y, 0);
280 d->twist = (z * twistiness *
283 get_rotation (rot, &x, &y, &z, tick);
285 glRotatef (360 * z, 0, 0, 1); /* rotation of this disc */
287 draw_spiral (mi, &bp->discs[i]);
293 if (mi->fps_p) do_fps (mi);
296 glXSwapBuffers(dpy, window);
300 hypnowheel_handle_event (ModeInfo *mi, XEvent *event)
302 if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
304 init_hypnowheel (mi);
311 XSCREENSAVER_MODULE ("Hypnowheel", hypnowheel)