1 /* cubetwist, Copyright (c) 2016 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
12 #define DEFAULTS "*delay: 30000 \n" \
13 "*showFPS: False \n" \
14 "*wireframe: False \n" \
15 "*suppressRotationAnimation: True\n" \
17 # define refresh_cube 0
19 #define countof(x) (sizeof((x))/sizeof((*x)))
21 #include "xlockmore.h"
24 #include "gltrackball.h"
27 #ifdef USE_GL /* whole file */
30 #define DEF_SPIN "True"
31 #define DEF_WANDER "True"
32 #define DEF_SPEED "1.0"
33 #define DEF_FLAT "True"
34 #define DEF_THICKNESS "0.0"
35 #define DEF_DISPLACEMENT "0.0"
37 typedef struct cube cube;
39 GLfloat size, thickness;
45 typedef struct oscillator oscillator;
47 double ratio, from, to, speed, *var;
53 GLXContext *glx_context;
55 trackball_state *trackball;
58 oscillator *oscillators;
61 static cube_configuration *bps = NULL;
66 static GLfloat thickness;
67 static GLfloat displacement;
68 static Bool do_wander;
70 static XrmOptionDescRec opts[] = {
71 { "-spin", ".spin", XrmoptionNoArg, "True" },
72 { "+spin", ".spin", XrmoptionNoArg, "False" },
73 { "-wander", ".wander", XrmoptionNoArg, "True" },
74 { "+wander", ".wander", XrmoptionNoArg, "False" },
75 { "-flat", ".flat", XrmoptionNoArg, "True" },
76 { "+flat", ".flat", XrmoptionNoArg, "False" },
77 { "-speed", ".speed", XrmoptionSepArg, 0 },
78 { "-thickness", ".thickness", XrmoptionSepArg, 0 },
79 { "-displacement", ".displacement", XrmoptionSepArg, 0 },
82 static argtype vars[] = {
83 {&do_flat, "flat", "flat", DEF_FLAT, t_Bool},
84 {&do_spin, "spin", "Spin", DEF_SPIN, t_Bool},
85 {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
86 {&speed, "speed", "Speed", DEF_SPEED, t_Float},
87 {&thickness, "thickness", "Thickness", DEF_THICKNESS, t_Float},
88 {&displacement, "displacement", "Displacement", DEF_DISPLACEMENT, t_Float},
91 ENTRYPOINT ModeSpecOpt cube_opts = {countof(opts), opts, countof(vars), vars, NULL};
95 draw_strut (ModeInfo *mi, cube *c)
97 int wire = MI_IS_WIREFRAME(mi);
102 glNormal3f (0, 0, -1);
103 glTranslatef (-c->size/2, -c->size/2, -c->size/2);
105 glBegin (wire ? GL_LINE_LOOP : GL_TRIANGLE_FAN);
106 glVertex3f (0, 0, 0);
107 glVertex3f (c->size, 0, 0);
108 glVertex3f (c->size - c->thickness, c->thickness, 0);
109 glVertex3f (c->thickness, c->thickness, 0);
113 glNormal3f (0, 1, 0);
114 glBegin (wire ? GL_LINE_LOOP : GL_TRIANGLE_FAN);
115 glVertex3f (c->thickness, c->thickness, 0);
116 glVertex3f (c->size - c->thickness, c->thickness, 0);
117 glVertex3f (c->size - c->thickness, c->thickness, c->thickness);
118 glVertex3f (c->thickness, c->thickness, c->thickness);
128 draw_cubes (ModeInfo *mi, cube *c)
134 glColor4fv (c->color);
135 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, c->color);
138 for (j = 0; j < 6; j++)
140 for (i = 0; i < 4; i++)
142 polys += draw_strut (mi, c);
143 glRotatef (90, 0, 0, 1);
146 glRotatef (90, 0, 0, 1);
148 glRotatef (90, 0, 1, 0);
150 glRotatef (180, 1, 0, 0);
156 glRotatef (c->rot.x, 1, 0, 0);
157 glRotatef (c->rot.y, 0, 1, 0);
158 glRotatef (c->rot.z, 0, 0, 1);
159 glTranslatef (c->pos.x, c->pos.y, c->pos.z);
160 c->next->pos = c->pos;
161 c->next->rot = c->rot;
162 polys += draw_cubes (mi, c->next);
171 make_cubes (ModeInfo *mi)
173 cube_configuration *bp = &bps[MI_SCREEN(mi)];
174 GLfloat step = 2 * (thickness + displacement);
177 GLfloat cc[4], cstep;
181 cc[0] = 0.3 + frand(0.7);
182 cc[1] = 0.3 + frand(0.7);
183 cc[2] = 0.3 + frand(0.7);
186 if (bp->cubes) abort();
189 cube *c = (cube *) calloc (1, sizeof (*c));
191 c->thickness = thickness;
205 for (c = bp->cubes; c; c = c->next)
207 memcpy (c->color, cc, sizeof(cc));
218 return cos ((r/2 + 1) * M_PI) + 1; /* Smooth curve up, end at slope 1. */
223 ease_ratio (GLfloat r)
226 if (r <= 0) return 0;
227 else if (r >= 1) return 1;
228 else if (r <= ease) return ease * ease_fn (r / ease);
229 else if (r > 1-ease) return 1 - ease * ease_fn ((1 - r) / ease);
235 tick_oscillators (ModeInfo *mi)
237 cube_configuration *bp = &bps[MI_SCREEN(mi)];
238 oscillator *prev = 0;
239 oscillator *a = bp->oscillators;
240 GLfloat tick = 0.1 / speed;
244 oscillator *next = a->next;
245 a->ratio += tick * a->speed;
249 *a->var = a->from + (a->to - a->from) * ease_ratio (a->ratio);
251 if (a->ratio < 1) /* mid cycle */
253 else if (--a->remaining <= 0) /* ended, and expired */
258 bp->oscillators = next;
261 else /* keep going the other way */
263 GLfloat swap = a->from;
276 add_oscillator (ModeInfo *mi, double *var, GLfloat speed, GLfloat to,
279 cube_configuration *bp = &bps[MI_SCREEN(mi)];
282 /* If an oscillator is already running on this variable, don't
284 for (a = bp->oscillators; a && a->next; a = a->next)
288 a = (oscillator *) calloc (1, sizeof (*a));
289 if (repeat <= 0) abort();
295 a->remaining = repeat;
296 a->next = bp->oscillators;
299 fprintf (stderr, "%s: %3d %6.2f -> %6.2f %s\n",
300 progname, repeat, *var, to,
301 (var == &bp->midpoint.z ? "z" :
302 var == &bp->tilt ? "tilt" :
303 var == &bp->axial_radius ? "r" :
304 var == &bp->speed ? "speed" : "?"));
310 #define RANDSIGN() ((random() & 1) ? 1 : -1)
313 add_random_oscillator (ModeInfo *mi)
315 cube_configuration *bp = &bps[MI_SCREEN(mi)];
317 double s1 = speed * 0.07;
318 double s2 = speed * 0.3;
319 double disp = (thickness + displacement);
320 int c1 = 1 + ((random() % 4) ? 0 : (random() % 3));
322 int n = random() % 6;
325 case 0: add_oscillator (mi, &c->rot.x, s1, 90 * RANDSIGN(), c1); break;
326 case 1: add_oscillator (mi, &c->rot.y, s1, 90 * RANDSIGN(), c1); break;
327 case 2: add_oscillator (mi, &c->rot.z, s1, 90 * RANDSIGN(), c1); break;
328 case 3: add_oscillator (mi, &c->pos.x, s2, disp * RANDSIGN(), c2); break;
329 case 4: add_oscillator (mi, &c->pos.y, s2, disp * RANDSIGN(), c2); break;
330 case 5: add_oscillator (mi, &c->pos.z, s2, disp * RANDSIGN(), c2); break;
331 default: abort(); break;
336 /* Window management, etc
339 reshape_cube (ModeInfo *mi, int width, int height)
341 GLfloat h = (GLfloat) height / (GLfloat) width;
343 glViewport (0, 0, (GLint) width, (GLint) height);
345 glMatrixMode(GL_PROJECTION);
347 gluPerspective (30.0, 1/h, 1.0, 100.0);
349 glMatrixMode(GL_MODELVIEW);
351 gluLookAt( 0.0, 0.0, 30.0,
355 # ifdef HAVE_MOBILE /* Keep it the same relative size when rotated. */
357 int o = (int) current_device_rotation();
358 if (o != 0 && o != 180 && o != -180)
359 glScalef (1/h, 1/h, 1/h);
363 glClear(GL_COLOR_BUFFER_BIT);
368 cube_handle_event (ModeInfo *mi, XEvent *event)
370 cube_configuration *bp = &bps[MI_SCREEN(mi)];
372 if (gltrackball_event_handler (event, bp->trackball,
373 MI_WIDTH (mi), MI_HEIGHT (mi),
376 else if (event->xany.type == KeyPress)
380 XLookupString (&event->xkey, &c, 1, &keysym, 0);
381 if (c == ' ' || c == '\t')
385 cube *c = bp->cubes->next;
390 while (bp->oscillators)
392 oscillator *o = bp->oscillators->next;
393 free (bp->oscillators);
399 thickness = 0.03 + frand(0.02);
400 displacement = (random() & 1) ? 0 : (thickness / 3);
404 thickness = 0.001 + frand(0.02);
419 init_cube (ModeInfo *mi)
421 cube_configuration *bp;
422 int wire = MI_IS_WIREFRAME(mi);
425 bps = (cube_configuration *)
426 calloc (MI_NUM_SCREENS(mi), sizeof (cube_configuration));
428 fprintf(stderr, "%s: out of memory\n", progname);
433 bp = &bps[MI_SCREEN(mi)];
435 bp->glx_context = init_GL(mi);
437 reshape_cube (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
439 if (!wire && !do_flat)
441 GLfloat color[4] = {1, 1, 1, 1};
442 GLfloat cspec[4] = {1, 1, 0, 1};
443 static const GLfloat shiny = 30;
445 static GLfloat pos0[4] = { 0.5, -1, -0.5, 0};
446 static GLfloat pos1[4] = {-0.75, -1, 0, 0};
447 static GLfloat amb[4] = {0, 0, 0, 1};
448 static GLfloat dif[4] = {1, 1, 1, 1};
449 static GLfloat spc[4] = {1, 1, 1, 1};
451 glEnable(GL_LIGHTING);
454 glEnable(GL_DEPTH_TEST);
455 glEnable(GL_CULL_FACE);
457 glLightfv(GL_LIGHT0, GL_POSITION, pos0);
458 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
459 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
460 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
462 glLightfv(GL_LIGHT1, GL_POSITION, pos1);
463 glLightfv(GL_LIGHT1, GL_AMBIENT, amb);
464 glLightfv(GL_LIGHT1, GL_DIFFUSE, dif);
465 glLightfv(GL_LIGHT1, GL_SPECULAR, spc);
467 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
468 glMaterialfv (GL_FRONT, GL_SPECULAR, cspec);
469 glMateriali (GL_FRONT, GL_SHININESS, shiny);
473 double spin_speed = 0.05;
474 double wander_speed = 0.005;
475 double spin_accel = 1.0;
477 bp->rot = make_rotator (do_spin ? spin_speed : 0,
478 do_spin ? spin_speed : 0,
479 do_spin ? spin_speed : 0,
481 do_wander ? wander_speed : 0,
483 bp->trackball = gltrackball_init (True);
488 if (displacement > 0.5)
491 if (thickness <= 0.0001)
495 thickness = 0.03 + frand(0.02);
496 displacement = (random() & 1) ? 0 : (thickness / 3);
500 thickness = 0.001 + frand(0.02);
510 draw_cube (ModeInfo *mi)
512 cube_configuration *bp = &bps[MI_SCREEN(mi)];
513 Display *dpy = MI_DISPLAY(mi);
514 Window window = MI_WINDOW(mi);
516 if (!bp->glx_context)
519 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
521 glShadeModel(GL_SMOOTH);
522 glEnable(GL_DEPTH_TEST);
523 glEnable(GL_NORMALIZE);
524 glEnable(GL_CULL_FACE);
526 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
530 glScalef(1.1, 1.1, 1.1);
534 get_position (bp->rot, &x, &y, &z, !bp->button_down_p);
535 glTranslatef((x - 0.5) * 4,
539 gltrackball_rotate (bp->trackball);
541 get_rotation (bp->rot, &x, &y, &z, !bp->button_down_p);
542 glRotatef (x * 360, 1.0, 0.0, 0.0);
543 glRotatef (y * 360, 0.0, 1.0, 0.0);
544 glRotatef (z * 360, 0.0, 0.0, 1.0);
547 mi->polygon_count = 0;
551 mi->polygon_count = draw_cubes (mi, bp->cubes);
554 if (!bp->button_down_p)
555 tick_oscillators (mi);
557 if (! bp->oscillators &&
558 !bp->button_down_p &&
561 bp->cubes->pos.x = bp->cubes->pos.y = bp->cubes->pos.z = 0;
562 bp->cubes->rot.x = bp->cubes->rot.y = bp->cubes->rot.z = 0;
563 add_random_oscillator (mi);
566 if (mi->fps_p) do_fps (mi);
569 glXSwapBuffers(dpy, window);
573 release_cube (ModeInfo *mi)
575 cube_configuration *bp = &bps[MI_SCREEN(mi)];
578 cube *c = bp->cubes->next;
583 while (bp->oscillators)
585 oscillator *o = bp->oscillators->next;
586 free (bp->oscillators);
592 XSCREENSAVER_MODULE_2 ("CubeTwist", cubetwist, cube)