1 /* kaleidocycle, Copyright (c) 2013 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 * A loop of rotating tetrahedra. Created by jwz, July 2013.
12 * Inspired by, and some math borrowed from:
13 * http://www.kaleidocycles.de/pdf/kaleidocycles_theory.pdf
14 * http://intothecontinuum.tumblr.com/post/50873970770/an-even-number-of-at-least-8-regular-tetrahedra
18 #define DEFAULTS "*delay: 30000 \n" \
20 "*showFPS: False \n" \
21 "*wireframe: False \n" \
23 # define refresh_kaleidocycle 0
24 # define release_kaleidocycle 0
26 #define countof(x) (sizeof((x))/sizeof((*x)))
28 #include "xlockmore.h"
32 #include "gltrackball.h"
36 #ifdef USE_GL /* whole file */
39 #define DEF_WANDER "False"
40 #define DEF_SPEED "1.0"
43 GLXContext *glx_context;
45 trackball_state *trackball;
48 int min_count, max_count;
58 enum { STATIC, IN, OUT } mode, prev_mode;
60 } kaleidocycle_configuration;
62 static kaleidocycle_configuration *bps = NULL;
66 static Bool do_wander;
68 static XrmOptionDescRec opts[] = {
69 { "-spin", ".spin", XrmoptionSepArg, 0 },
70 { "+spin", ".spin", XrmoptionNoArg, "" },
71 { "-wander", ".wander", XrmoptionNoArg, "True" },
72 { "+wander", ".wander", XrmoptionNoArg, "False" },
73 { "-speed", ".speed", XrmoptionSepArg, 0 },
76 static argtype vars[] = {
77 {&do_spin, "spin", "Spin", DEF_SPIN, t_String},
78 {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
79 {&speed, "speed", "Speed", DEF_SPEED, t_Float},
82 ENTRYPOINT ModeSpecOpt kaleidocycle_opts = {countof(opts), opts, countof(vars), vars, NULL};
86 /* Window management, etc
89 reshape_kaleidocycle (ModeInfo *mi, int width, int height)
91 GLfloat h = (GLfloat) height / (GLfloat) width;
93 glViewport (0, 0, (GLint) width, (GLint) height);
95 glMatrixMode(GL_PROJECTION);
97 gluPerspective (30.0, 1/h, 1.0, 100.0);
99 glMatrixMode(GL_MODELVIEW);
101 gluLookAt( 0.0, 0.0, 30.0,
105 glClear(GL_COLOR_BUFFER_BIT);
110 kaleidocycle_handle_event (ModeInfo *mi, XEvent *event)
112 kaleidocycle_configuration *bp = &bps[MI_SCREEN(mi)];
114 if (event->xany.type == ButtonPress &&
115 event->xbutton.button == Button1)
117 bp->button_down_p = True;
118 gltrackball_start (bp->trackball,
119 event->xbutton.x, event->xbutton.y,
120 MI_WIDTH (mi), MI_HEIGHT (mi));
123 else if (event->xany.type == ButtonRelease &&
124 event->xbutton.button == Button1)
126 bp->button_down_p = False;
129 else if (event->xany.type == ButtonPress &&
130 (event->xbutton.button == Button4 ||
131 event->xbutton.button == Button5 ||
132 event->xbutton.button == Button6 ||
133 event->xbutton.button == Button7))
135 gltrackball_mousewheel (bp->trackball, event->xbutton.button, 5,
136 !!event->xbutton.state);
139 else if (event->xany.type == MotionNotify &&
142 gltrackball_track (bp->trackball,
143 event->xmotion.x, event->xmotion.y,
144 MI_WIDTH (mi), MI_HEIGHT (mi));
154 init_kaleidocycle (ModeInfo *mi)
156 kaleidocycle_configuration *bp;
157 int wire = MI_IS_WIREFRAME(mi);
161 bps = (kaleidocycle_configuration *)
162 calloc (MI_NUM_SCREENS(mi), sizeof (kaleidocycle_configuration));
164 fprintf(stderr, "%s: out of memory\n", progname);
169 bp = &bps[MI_SCREEN(mi)];
171 bp->glx_context = init_GL(mi);
173 reshape_kaleidocycle (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
179 GLfloat pos[4] = {1.0, 1.0, 1.0, 0.0};
180 GLfloat amb[4] = {0.0, 0.0, 0.0, 1.0};
181 GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
182 GLfloat spc[4] = {0.0, 1.0, 1.0, 1.0};
184 glEnable(GL_LIGHTING);
186 glEnable(GL_DEPTH_TEST);
187 glEnable(GL_CULL_FACE);
189 glLightfv(GL_LIGHT0, GL_POSITION, pos);
190 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
191 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
192 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
195 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
199 Bool spinx = False, spiny = False, spinz = False;
200 double spin_speed = 0.25;
201 double wander_speed = 0.005;
202 double spin_accel = 0.2;
203 double twist_speed = 0.25;
204 double twist_accel = 1.0;
209 if (*s == 'x' || *s == 'X') spinx = True;
210 else if (*s == 'y' || *s == 'Y') spiny = True;
211 else if (*s == 'z' || *s == 'Z') spinz = True;
212 else if (*s == '0') ;
216 "%s: spin must contain only the characters X, Y, or Z (not \"%s\")\n",
223 bp->rot = make_rotator (spinx ? spin_speed : 0,
224 spiny ? spin_speed : 0,
225 spinz ? spin_speed : 0,
227 do_wander ? wander_speed : 0,
229 bp->rot2 = make_rotator (twist_speed, 0, 0, twist_accel, 0, True);
231 bp->trackball = gltrackball_init ();
234 if (MI_COUNT(mi) < 8) MI_COUNT(mi) = 8;
235 if (MI_COUNT(mi) & 1) MI_COUNT(mi)++;
238 bp->max_count = 12 + MI_COUNT(mi) * 1.3;
239 if (bp->max_count & 1) bp->max_count++;
240 bp->startup_p = True;
247 bp->count = MI_COUNT(mi);
253 bp->colors = (XColor *) calloc(bp->ncolors, sizeof(XColor));
254 make_uniform_colormap (0, 0, 0,
255 bp->colors, &bp->ncolors,
258 for (i = 0; i < bp->ncolors; i++)
260 /* make colors twice as bright */
261 bp->colors[i].red = (bp->colors[i].red >> 2) + 0x7FFF;
262 bp->colors[i].green = (bp->colors[i].green >> 2) + 0x7FFF;
263 bp->colors[i].blue = (bp->colors[i].blue >> 2) + 0x7FFF;
266 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
271 /* t = toroidal rotation, a = radial position
272 colors = 4 colors, 4 channels each.
275 draw_tetra (ModeInfo *mi, double t, double a, Bool reflect_p,
278 int wire = MI_IS_WIREFRAME(mi);
280 XYZ v1, v2, v3, P, Q;
285 double sint = sin(t);
286 double cost = cos(t);
287 double tana = tan(a);
288 double sint2 = sint * sint;
289 double tana2 = tana * tana;
295 scale = 1 / sqrt (1 + sint2 * tana2);
296 v2.x = scale * -sint;
297 v2.y = scale * -sint * tana;
300 v3.x = scale * -sint2 * tana;
302 v3.z = scale * cost * sint * tana;
304 P.x = v3.y / tana - v3.x;
318 verts[0].x = P.x - scale * v1.x;
319 verts[0].y = P.y - scale * v1.y;
320 verts[0].z = P.z - scale * v1.z;
322 verts[1].x = P.x + scale * v1.x;
323 verts[1].y = P.y + scale * v1.y;
324 verts[1].z = P.z + scale * v1.z;
326 verts[2].x = Q.x - scale * v2.x;
327 verts[2].y = Q.y - scale * v2.y;
328 verts[2].z = Q.z - scale * v2.z;
330 verts[3].x = Q.x + scale * v2.x;
331 verts[3].y = Q.y + scale * v2.y;
332 verts[3].z = Q.z + scale * v2.z;
334 for (i = 0; i < 4; i++)
336 Bool reflect2_p = ((i + (reflect_p != 0)) & 1);
337 XYZ a = verts[(i+1) % 4];
338 XYZ b = verts[(i+2) % 4];
339 XYZ c = verts[(i+3) % 4];
341 ? calc_normal (b, a, c)
342 : calc_normal (a, b, c));
344 glColor4fv (colors + (i * 4));
346 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, colors + (i * 4));
348 glFrontFace (reflect2_p ? GL_CW : GL_CCW);
349 glBegin (wire ? GL_LINE_LOOP : GL_TRIANGLES);
350 glNormal3f (n.x, n.y, n.z);
351 glVertex3f (a.x, a.y, a.z);
352 glVertex3f (b.x, b.y, b.z);
353 glVertex3f (c.x, c.y, c.z);
359 /* Reflect through the plane normal to the given vector.
362 reflect (double x, double y, double z)
366 m[0][0] = 1 - (2 * x * x);
367 m[1][0] = -2 * x * y;
368 m[2][0] = -2 * x * z;
371 m[0][1] = -2 * x * y;
372 m[1][1] = 1 - (2 * y * y);
373 m[2][1] = -2 * y * z;
376 m[0][2] = -2 * x * z;
377 m[1][2] = -2 * y * z;
378 m[2][2] = 1 - (2 * z * z);
386 glMultMatrixf (&m[0][0]);
391 draw_kaleidocycle (ModeInfo *mi)
393 kaleidocycle_configuration *bp = &bps[MI_SCREEN(mi)];
394 Display *dpy = MI_DISPLAY(mi);
395 Window window = MI_WINDOW(mi);
401 GLfloat bspec[4] = {1.0, 1.0, 1.0, 1.0};
402 GLfloat bshiny = 128.0;
404 if (!bp->glx_context)
407 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
409 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
411 mi->polygon_count = 0;
413 glShadeModel(GL_SMOOTH);
414 glEnable(GL_DEPTH_TEST);
415 glEnable(GL_NORMALIZE);
416 glEnable(GL_CULL_FACE);
422 get_position (bp->rot, &x, &y, &z, !bp->button_down_p);
423 glTranslatef((x - 0.5) * 5,
427 /* Do it twice because we don't track the device's orientation. */
428 glRotatef( current_device_rotation(), 0, 0, 1);
429 gltrackball_rotate (bp->trackball);
430 glRotatef(-current_device_rotation(), 0, 0, 1);
432 get_rotation (bp->rot, &x, &y, &z, !bp->button_down_p);
433 glRotatef (x * 360, 1, 0, 0);
434 glRotatef (y * 360, 0, 1, 0);
435 glRotatef (z * 360, 0, 0, 1);
437 get_rotation (bp->rot2, &x, &y, &z, !bp->button_down_p);
438 bp->th = x * 360 * 10 * speed;
440 /* Make sure the twist is always in motion. Without this, the rotator
441 sometimes stops, and for too long, and it's boring looking.
443 bp->th += speed * bp->dth++;
444 while (bp->dth > 360) bp->dth -= 360;
445 while (bp->th > 360) bp->th -= 360;
448 glMaterialfv (GL_FRONT, GL_SPECULAR, bspec);
449 glMateriali (GL_FRONT, GL_SHININESS, bshiny);
452 /* Evenly spread the colors of the faces, and cycle them together.
454 for (i = 0; i < 4; i++)
456 int o = bp->ncolors / 4;
457 int c = (bp->ccolor + (o*i)) % bp->ncolors;
458 colors[i*4+0] = bp->colors[c].red / 65536.0;
459 colors[i*4+1] = bp->colors[c].green / 65536.0;
460 colors[i*4+2] = bp->colors[c].blue / 65536.0;
464 if (bp->ccolor >= bp->ncolors) bp->ccolor = 0;
467 count = (int) floor (bp->count);
468 while (count < 8) count++;
469 if (((int) floor (count)) & 1) count++;
471 a = 2 * M_PI / (bp->count < 8 ? 8 : bp->count);
472 t = bp->th / (180 / M_PI);
476 glRotatef (90, 0, 0, 1);
477 /* glRotatef (45, 0, 1, 0); */
479 for (i = 0; i <= (int) floor (bp->count); i++)
481 Bool flip_p = (i & 1);
483 glRotatef ((i/2) * 4 * 180 / bp->count, 0, 0, 1);
484 if (flip_p) reflect (-sin(a), cos(a), 0);
486 if (bp->mode != STATIC && i >= (int) floor (bp->count))
488 /* Fractional bp->count means the last piece is in transition */
489 GLfloat scale = bp->count - (int) floor (bp->count);
490 GLfloat tick = 0.07 * speed;
491 GLfloat ocount = bp->count;
494 /* Fill in faster if we're starting up */
495 if (bp->count < MI_COUNT(mi))
498 glScalef (scale, scale, scale);
502 case OUT: tick = -tick; break;
503 case STATIC: tick = 0; break;
509 ? floor (ocount) != floor (bp->count)
510 : ceil (ocount) != ceil (bp->count))
513 bp->count = floor (ocount) + 1;
515 bp->count = ceil (ocount) - 1;
517 if (((int) floor (bp->count)) & 1 ||
519 (bp->count < MI_COUNT(mi) &&
522 /* keep going if it's odd, or less than 8. */
523 bp->count = round(bp->count);
528 bp->startup_p = False;
532 alpha = (scale * scale * scale * scale);
533 if (alpha < 0.4) alpha = 0.4;
534 colors[3] = colors[7] = colors[11] = colors[15] = alpha;
537 draw_tetra (mi, t, a, flip_p, colors);
538 mi->polygon_count += 4;
543 if (bp->mode == STATIC && !(random() % 200)) {
544 if (bp->count <= bp->min_count)
546 else if (bp->count >= bp->max_count)
549 bp->mode = bp->prev_mode;
551 bp->prev_mode = bp->mode;
555 mi->recursion_depth = ceil (bp->count);
559 if (mi->fps_p) do_fps (mi);
562 glXSwapBuffers(dpy, window);
565 XSCREENSAVER_MODULE ("Kaleidocycle", kaleidocycle)