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" \
25 "*suppressRotationAnimation: True\n" \
27 # define refresh_hypnowheel 0
28 # define release_hypnowheel 0
30 #define countof(x) (sizeof((x))/sizeof((*x)))
32 #include "xlockmore.h"
37 #ifdef USE_GL /* whole file */
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"
54 GLXContext *glx_context;
60 } hypnowheel_configuration;
62 static hypnowheel_configuration *bps = NULL;
65 static GLfloat twistiness;
67 static Bool do_wander;
68 static Bool do_symmetric;
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" },
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},
88 ENTRYPOINT ModeSpecOpt hypnowheel_opts = {
89 countof(opts), opts, countof(vars), vars, NULL};
93 draw_spiral (ModeInfo *mi, disc *d)
95 int wire = MI_IS_WIREFRAME(mi);
96 hypnowheel_configuration *bp = &bps[MI_SCREEN(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;
103 GLfloat twist = d->twist;
104 GLfloat dtwist = M_PI * 2 * twist / steps;
105 double cscale = 65536.0;
107 if (nlayers > 3 && !wire)
108 cscale *= (nlayers-2); /* don't wash out to white */
110 glColor4f (bp->colors[d->color].red / cscale,
111 bp->colors[d->color].green / cscale,
112 bp->colors[d->color].blue / cscale,
114 for (th = 0; th < M_PI*2; th += dth)
118 glBegin (wire ? GL_LINE_STRIP : GL_QUAD_STRIP);
119 for (r = 0; r <= rr; r += dr)
121 GLfloat th2 = th1 + dth/2 + dtwist;
123 glVertex3f (r * cos(th1), r * sin(th1), 0);
125 glVertex3f (r * cos(th2), r * sin(th2), 0);
134 /* Window management, etc
137 reshape_hypnowheel (ModeInfo *mi, int width, int height)
139 GLfloat h = (GLfloat) height / (GLfloat) width;
141 glViewport (0, 0, (GLint) width, (GLint) height);
143 glMatrixMode(GL_PROJECTION);
145 gluPerspective (30.0, 1/h, 1.0, 100.0);
147 glMatrixMode(GL_MODELVIEW);
149 gluLookAt( 0.0, 0.0, 30.0,
153 # ifdef HAVE_MOBILE /* Keep it the same relative size when rotated. */
155 int o = (int) current_device_rotation();
156 if (o != 0 && o != 180 && o != -180)
157 glScalef (1/h, 1/h, 1/h);
161 glClear(GL_COLOR_BUFFER_BIT);
166 init_hypnowheel (ModeInfo *mi)
168 hypnowheel_configuration *bp;
169 int wire = MI_IS_WIREFRAME(mi);
173 bps = (hypnowheel_configuration *)
174 calloc (MI_NUM_SCREENS(mi), sizeof (hypnowheel_configuration));
176 fprintf(stderr, "%s: out of memory\n", progname);
181 bp = &bps[MI_SCREEN(mi)];
183 bp->glx_context = init_GL(mi);
185 reshape_hypnowheel (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
188 bp->rot = make_rotator (0, 0, 0, 0, speed * 0.0025, False);
192 bp->colors = (XColor *) calloc(bp->ncolors, sizeof(XColor));
193 make_smooth_colormap (0, 0, 0,
194 bp->colors, &bp->ncolors,
197 if (MI_COUNT(mi) < 2) MI_COUNT(mi) = 2;
198 if (nlayers < 1) nlayers = 1;
200 bp->discs = (disc *) calloc (nlayers, sizeof (disc));
202 for (i = 0; i < nlayers; i++)
204 double spin_speed = speed * 0.2;
205 double wander_speed = speed * 0.0012;
206 double spin_accel = 0.2;
208 bp->discs[i].twist = 0;
209 bp->discs[i].alpha = 1;
210 bp->discs[i].color = i * bp->ncolors / nlayers;
212 spin_speed += frand (spin_speed / 5);
213 wander_speed += frand (wander_speed * 3);
215 if (!bp->discs[i].rot)
216 bp->discs[i].rot = make_rotator (spin_speed, spin_speed, spin_speed,
218 (do_wander ? wander_speed : 0),
222 glDisable (GL_LIGHTING);
223 glDisable (GL_DEPTH_TEST);
224 glDepthMask (GL_FALSE);
225 glDisable (GL_CULL_FACE);
230 glBlendFunc (GL_ONE, GL_ONE);
236 draw_hypnowheel (ModeInfo *mi)
238 hypnowheel_configuration *bp = &bps[MI_SCREEN(mi)];
239 Display *dpy = MI_DISPLAY(mi);
240 Window window = MI_WINDOW(mi);
243 if (!bp->glx_context)
246 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
248 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
254 get_position (bp->rot, &x, &y, &z, True);
255 glTranslatef((x - 0.5) * 8,
259 get_rotation (bp->rot, &x, &y, &z, True);
260 glRotatef (x * 360, 1.0, 0.0, 0.0);
261 glRotatef (y * 360, 0.0, 1.0, 0.0);
262 glRotatef (z * 360, 0.0, 0.0, 1.0);
265 mi->polygon_count = 0;
267 glScalef (45, 45, 45);
269 for (i = 0; i < nlayers; i++)
271 disc *d = &bp->discs[i];
273 rotator *rot = (do_symmetric ? bp->discs[(i & ~0x1)].rot : d->rot);
274 Bool tick = (!do_symmetric || i == 0);
279 if (d->color >= bp->ncolors)
282 get_position (rot, &x, &y, &z, tick);
288 glTranslatef (x, y, 0);
289 d->twist = (z * twistiness *
292 get_rotation (rot, &x, &y, &z, tick);
294 glRotatef (360 * z, 0, 0, 1); /* rotation of this disc */
296 draw_spiral (mi, &bp->discs[i]);
302 if (mi->fps_p) do_fps (mi);
305 glXSwapBuffers(dpy, window);
309 hypnowheel_handle_event (ModeInfo *mi, XEvent *event)
311 if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
313 init_hypnowheel (mi);
320 XSCREENSAVER_MODULE ("Hypnowheel", hypnowheel)