1 /* hydrostat, Copyright (C) 2012 by Justin Windle
2 * Copyright (c) 2016 Jamie Zawinski <jwz@jwz.org>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * Tentacle simulation using inverse kinematics.
24 * http://soulwire.co.uk/experiments/muscular-hydrostats/
25 * https://github.com/soulwire/Muscular-Hydrostats/
27 * Ported to C from Javascript by jwz, May 2016
30 #define DEFAULTS "*delay: 20000 \n" \
32 "*showFPS: False \n" \
33 "*wireframe: False \n" \
34 "*suppressRotationAnimation: True\n" \
36 # define refresh_hydrostat 0
37 # define release_hydrostat 0
39 #define countof(x) (sizeof((x))/sizeof((*x)))
41 #include "xlockmore.h"
45 #include "gltrackball.h"
48 #ifdef USE_GL /* whole file */
50 /* It looks bad when you rotate it with the trackball, because it reveals
51 that the tentacles are moving in a 2d plane. But it's useful for
55 #define DEF_SPEED "1.0"
56 #define DEF_PULSE "True"
57 #define DEF_HEAD_RADIUS "60"
58 #define DEF_TENTACLES "35"
59 #define DEF_THICKNESS "18"
60 #define DEF_LENGTH "55"
61 #define DEF_GRAVITY "0.5"
62 #define DEF_CURRENT "0.25"
63 #define DEF_FRICTION "0.02"
64 #define DEF_OPACITY "0.8"
83 GLfloat ratio, pulse, rate;
92 GLXContext *glx_context;
97 trackball_state *trackball;
99 } hydrostat_configuration;
101 static hydrostat_configuration *bps = NULL;
103 static Bool do_pulse;
104 static GLfloat speed_arg;
105 static GLfloat head_radius_arg;
106 static GLfloat ntentacles_arg;
107 static GLfloat thickness_arg;
108 static GLfloat length_arg;
109 static GLfloat gravity_arg;
110 static GLfloat current_arg;
111 static GLfloat friction_arg;
112 static GLfloat opacity_arg;
114 static XrmOptionDescRec opts[] = {
115 { "-pulse", ".pulse", XrmoptionNoArg, "True" },
116 { "+pulse", ".pulse", XrmoptionNoArg, "False" },
117 { "-speed", ".speed", XrmoptionSepArg, 0 },
118 { "-head-radius", ".headRadius", XrmoptionSepArg, 0 },
119 { "-tentacles", ".tentacles", XrmoptionSepArg, 0 },
120 { "-thickness", ".thickness", XrmoptionSepArg, 0 },
121 { "-length", ".length", XrmoptionSepArg, 0 },
122 { "-gravity", ".gravity", XrmoptionSepArg, 0 },
123 { "-current", ".current", XrmoptionSepArg, 0 },
124 { "-friction", ".friction", XrmoptionSepArg, 0 },
125 { "-opacity", ".opacity", XrmoptionSepArg, 0 },
128 static argtype vars[] = {
129 { &do_pulse, "pulse", "Pulse", DEF_PULSE, t_Bool },
130 { &speed_arg, "speed", "Speed", DEF_SPEED, t_Float },
131 { &head_radius_arg, "headRadius", "HeadRadius", DEF_HEAD_RADIUS, t_Float },
132 { &ntentacles_arg, "tentacles", "Tentacles", DEF_TENTACLES, t_Float },
133 { &thickness_arg, "thickness", "Thickness", DEF_THICKNESS, t_Float },
134 { &length_arg, "length", "Length", DEF_LENGTH, t_Float },
135 { &gravity_arg, "gravity", "Gravity", DEF_GRAVITY, t_Float },
136 { ¤t_arg, "current", "Current", DEF_CURRENT, t_Float },
137 { &friction_arg, "friction", "Friction", DEF_FRICTION, t_Float },
138 { &opacity_arg, "opacity", "Opacity", DEF_OPACITY, t_Float },
141 ENTRYPOINT ModeSpecOpt hydrostat_opts = {countof(opts), opts,
142 countof(vars), vars, NULL};
146 move_tentacle (squid *sq, tentacle *t)
149 node *prev = &t->nodes[0];
150 int rot = (int) current_device_rotation();
152 for (i = 1, j = 0; i < t->length; i++, j++)
156 node *n = &t->nodes[i];
158 /* Sadly, this is still computing motion in a 2d plane, so the
159 tentacles look dumb if the scene is rotated. */
165 d.x = prev->pos.x - n->pos.x;
166 d.y = prev->pos.y - n->pos.y;
167 d.z = prev->pos.z - n->pos.z;
168 da = atan2 (d.z, d.x);
170 p.x = n->pos.x + cos (da) * t->spacing * t->length;
171 p.y = n->pos.y + cos (da) * t->spacing * t->length;
172 p.z = n->pos.z + sin (da) * t->spacing * t->length;
174 n->pos.x = prev->pos.x - (p.x - n->pos.x);
175 n->pos.y = prev->pos.y - (p.y - n->pos.y);
176 n->pos.z = prev->pos.z - (p.z - n->pos.z);
178 n->v.x = n->pos.x - n->opos.x;
179 n->v.y = n->pos.y - n->opos.y;
180 n->v.z = n->pos.z - n->opos.z;
182 n->v.x *= t->friction * (1 - friction_arg);
183 n->v.y *= t->friction * (1 - friction_arg);
184 n->v.z *= t->friction * (1 - friction_arg);
188 n->v.x += gravity_arg;
189 n->v.y -= current_arg;
190 n->v.z -= current_arg;
193 n->v.x -= gravity_arg;
194 n->v.y += current_arg;
195 n->v.z += current_arg;
198 n->v.x -= current_arg;
199 n->v.y -= current_arg;
200 n->v.z -= gravity_arg;
203 n->v.x += current_arg;
204 n->v.y += current_arg;
205 n->v.z += gravity_arg;
209 n->opos.x = n->pos.x;
210 n->opos.y = n->pos.y;
211 n->opos.z = n->pos.z;
221 return cos ((r/2 + 1) * M_PI) + 1; /* Smooth curve up, end at slope 1. */
225 /* Squirty motion: fast acceleration, then fade. */
227 ease_ratio (GLfloat r)
230 GLfloat ease2 = 1-ease;
232 else if (r >= 1) r = 1;
233 else if (r <= ease) r = ease * ease_fn (r / ease);
234 else r = 1 - ease2 * ease_fn ((1 - r) / ease2);
240 move_squid (ModeInfo *mi, squid *sq)
242 hydrostat_configuration *bp = &bps[MI_SCREEN(mi)];
243 GLfloat step = M_PI * 2 / sq->ntentacles;
245 GLfloat radius = head_radius_arg;
248 /* Move to a new position */
250 if (! bp->button_down_p)
252 sq->ratio += speed_arg * 0.01;
255 sq->ratio = -(frand(2.0) + frand(2.0) + frand(2.0));
256 sq->from.x = sq->to.x;
257 sq->from.y = sq->to.y;
258 sq->from.z = sq->to.z;
259 sq->to.x = 250 - frand(500);
260 sq->to.y = 250 - frand(500);
261 sq->to.z = 250 - frand(500);
264 r = sq->ratio > 0 ? ease_ratio (sq->ratio) : 0;
265 sq->pos.x = sq->from.x + r * (sq->to.x - sq->from.x);
266 sq->pos.y = sq->from.y + r * (sq->to.y - sq->from.y);
267 sq->pos.z = sq->from.z + r * (sq->to.z - sq->from.z);
272 GLfloat p = pow (sin (sq->pulse * M_PI), 18);
273 sq->head_radius = (head_radius_arg * 0.7 +
274 head_radius_arg * 0.3 * p);
275 radius = sq->head_radius * 0.25;
276 sq->pulse += sq->rate * speed_arg * 0.02;
277 if (sq->pulse > 1) sq->pulse = 0;
280 for (i = 0; i < sq->ntentacles; i++)
282 tentacle *tt = &sq->tentacles[i];
283 GLfloat th = i * step;
284 GLfloat px = cos (th) * radius;
285 GLfloat py = sin (th) * radius;
287 tt->nodes[0].pos.x = sq->pos.x + px;
288 tt->nodes[0].pos.y = sq->pos.y + py;
289 tt->nodes[0].pos.z = sq->pos.z;
290 move_tentacle (sq, tt);
295 /* Find the angle at which the head should be tilted in the XY plane.
298 head_angle (ModeInfo *mi, squid *sq)
303 for (i = 0; i < sq->ntentacles; i++)
305 tentacle *t = &sq->tentacles[i];
306 int j = t->length / 3; /* Pick a node toward the top */
307 node *n = &t->nodes[j];
313 sum.x /= sq->ntentacles;
314 sum.y /= sq->ntentacles;
315 sum.z /= sq->ntentacles;
321 return (-atan2 (sum.x, sum.z) * (180 / M_PI));
326 draw_head (ModeInfo *mi, squid *sq, GLfloat scale)
328 int wire = MI_IS_WIREFRAME(mi);
329 int i = wire ? 8 : 64;
331 GLfloat angle = head_angle (mi, sq);
336 glTranslatef (sq->pos.x, sq->pos.y, sq->pos.z);
337 glScalef (sq->head_radius, sq->head_radius, sq->head_radius);
338 glScalef (scale, scale, scale);
339 glRotatef (90, 1, 0, 0);
341 memcpy (c2, sq->color, sizeof(c2));
342 if (opacity_arg < 1.0 && scale >= 1.0)
345 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, c2);
347 glTranslatef (0, 0.3, 0);
348 glRotatef (angle, 0, 0, 1);
350 glScalef (1, 1.1, 1);
351 unit_dome (i, i/2, wire);
352 glRotatef (180, 0, 0, 1);
353 glScalef (1, 0.5, 1);
354 unit_dome (i, i/2, wire);
356 mi->polygon_count += i * i;
362 draw_squid (ModeInfo *mi, squid *sq)
364 int wire = MI_IS_WIREFRAME(mi);
367 glRotatef (90, 1, 0, 0);
369 if (opacity_arg < 1.0)
370 draw_head (mi, sq, 0.75);
372 for (i = 0; i < sq->ntentacles; i++)
374 tentacle *t = &sq->tentacles[i];
377 glColor4fv (t->color);
378 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, t->color);
382 glBegin (GL_LINE_STRIP);
383 for (j = 0; j < t->length; j++)
384 glVertex3f (t->nodes[j].pos.x,
388 mi->polygon_count += t->length;
392 GLfloat radius = t->radius * thickness_arg;
393 GLfloat rstep = radius / t->length;
395 glFrontFace (GL_CCW);
396 for (j = 0; j < t->length-1; j++)
399 node *n1 = &t->nodes[j];
400 node *n2 = &t->nodes[j+1];
401 GLfloat X = (n1->pos.x - n2->pos.x);
402 GLfloat Y = (n1->pos.y - n2->pos.y);
403 GLfloat Z = (n1->pos.z - n2->pos.z);
404 GLfloat L = sqrt (X*X + Y*Y + Z*Z);
405 GLfloat r2 = radius - rstep;
408 glTranslatef (n1->pos.x, n1->pos.y, n1->pos.z);
409 glRotatef (-atan2 (X, Y) * (180 / M_PI), 0, 0, 1);
410 glRotatef ( atan2 (Z, sqrt(X*X + Y*Y)) * (180 / M_PI), 1, 0, 0);
412 glBegin (wire ? GL_LINE_LOOP : GL_QUAD_STRIP);
413 for (k = 0; k <= faces; k++)
415 GLfloat th = k * M_PI * 2 / faces;
418 GLfloat x1 = radius * c;
419 GLfloat y1 = radius * s;
425 glNormal3f (x1, z1, y1);
426 glVertex3f (x1, z1, y1);
427 glVertex3f (x2, z2, y2);
437 draw_head (mi, sq, 1.0);
444 make_squid (ModeInfo *mi, int which)
446 squid *sq = calloc (1, sizeof(*sq));
449 sq->head_radius = head_radius_arg;
450 sq->thickness = thickness_arg;
451 sq->ntentacles = ntentacles_arg;
453 sq->color[0] = 0.1 + frand(0.7);
454 sq->color[1] = 0.5 + frand(0.5);
455 sq->color[2] = 0.1 + frand(0.7);
456 sq->color[3] = opacity_arg;
458 sq->from.x = sq->to.x = sq->pos.x = 200 - frand(400);
459 sq->from.y = sq->to.y = sq->pos.y = 200 - frand(400);
460 sq->from.z = sq->to.z = sq->pos.z = -frand(200);
462 sq->ratio = -frand(3);
464 if (which > 0) /* Start others off screen, and moving in */
466 sq->from.x = sq->to.x = sq->pos.x = 800 + frand(500)
467 * (random()&1 ? 1 : -1);
468 sq->from.y = sq->to.y = sq->pos.y = 800 + frand(500)
469 * (random()&1 ? 1 : -1);
474 sq->pulse = frand(1.0);
475 sq->rate = 0.8 + frand(0.2);
477 sq->tentacles = (tentacle *)
478 calloc (sq->ntentacles, sizeof(*sq->tentacles));
479 for (i = 0; i < sq->ntentacles; i++)
482 tentacle *t = &sq->tentacles[i];
483 GLfloat shade = 0.75 + frand(0.25);
485 t->length = 2 + length_arg * (0.8 + frand (0.4));
486 t->radius = 0.05 + frand (0.95);
487 t->spacing = 0.02 + frand (0.08);
488 t->friction = 0.7 + frand (0.18);
489 t->nodes = (node *) calloc (t->length + 1, sizeof (*t->nodes));
491 t->color[0] = shade * sq->color[0];
492 t->color[1] = shade * sq->color[1];
493 t->color[2] = shade * sq->color[2];
494 t->color[3] = sq->color[3];
496 for (j = 0; j < t->length; j++)
498 node *n = &t->nodes[j];
499 n->pos.x = sq->pos.x;
500 n->pos.y = sq->pos.y;
501 n->pos.z = sq->pos.z + j;
508 /* qsort comparator for sorting squid by depth */
510 cmp_squid (const void *aa, const void *bb)
512 squid * const *a = aa;
513 squid * const *b = bb;
514 return ((int) ((*b)->pos.y * 10000) -
515 (int) ((*a)->pos.y * 10000));
520 free_squid (squid *sq)
523 for (i = 0; i < sq->ntentacles; i++)
524 free (sq->tentacles[i].nodes);
525 free (sq->tentacles);
530 /* Window management, etc
533 reshape_hydrostat (ModeInfo *mi, int width, int height)
535 GLfloat h = (GLfloat) height / (GLfloat) width;
537 glViewport (0, 0, (GLint) width, (GLint) height);
539 glMatrixMode(GL_PROJECTION);
541 gluPerspective (30.0, 1/h, 1.0, 100.0);
543 glMatrixMode(GL_MODELVIEW);
545 gluLookAt( 0.0, 0.0, 30.0,
549 # ifdef HAVE_MOBILE /* Keep it the same relative size when rotated. */
551 int o = (int) current_device_rotation();
552 if (o != 0 && o != 180 && o != -180)
553 glScalef (1/h, 1/h, 1/h);
557 glClear(GL_COLOR_BUFFER_BIT);
563 hydrostat_handle_event (ModeInfo *mi, XEvent *event)
565 hydrostat_configuration *bp = &bps[MI_SCREEN(mi)];
566 int w = MI_WIDTH(mi);
567 int h = MI_HEIGHT(mi);
570 # ifdef USE_TRACKBALL
571 if (gltrackball_event_handler (event, bp->trackball,
572 MI_WIDTH (mi), MI_HEIGHT (mi),
577 switch (event->xany.type) {
578 case ButtonPress: case ButtonRelease:
579 x = event->xbutton.x;
580 y = event->xbutton.y;
583 x = event->xmotion.x;
584 y = event->xmotion.y;
595 if (event->xany.type == ButtonPress)
600 /* This is pretty halfassed hit detection, but it works ok... */
601 for (i = 0; i < MI_COUNT(mi); i++)
603 squid *s = bp->squids[i];
604 GLfloat X = s->pos.x - x;
605 GLfloat Y = s->pos.z - y;
606 GLfloat D = sqrt(X*X + Y*Y);
614 if (D0 > 300) /* Too far away, missed hit */
620 bp->squids[bp->dragging]->ratio = -3;
621 bp->button_down_p = True;
625 else if (event->xany.type == ButtonRelease && bp->dragging >= 0)
627 bp->button_down_p = False;
631 else if (event->xany.type == MotionNotify && bp->dragging >= 0)
633 squid *s = bp->squids[bp->dragging];
634 s->from.x = s->to.x = s->pos.x = x;
635 s->from.z = s->to.z = s->pos.z = y;
636 s->from.y = s->to.y = s->pos.y;
644 static void free_hydrostat (ModeInfo *mi);
647 init_hydrostat (ModeInfo *mi)
649 int wire = MI_IS_WIREFRAME(mi);
650 hydrostat_configuration *bp;
653 MI_INIT (mi, bps, free_hydrostat);
655 bp = &bps[MI_SCREEN(mi)];
657 bp->glx_context = init_GL(mi);
659 reshape_hydrostat (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
663 GLfloat pos[4] = {1.0, 1.0, 1.0, 0.0};
664 GLfloat amb[4] = {0.0, 0.0, 0.0, 1.0};
665 GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
666 GLfloat spc[4] = {0.0, 1.0, 1.0, 1.0};
668 glEnable(GL_LIGHTING);
670 glEnable(GL_DEPTH_TEST);
671 glEnable(GL_CULL_FACE);
673 glLightfv(GL_LIGHT0, GL_POSITION, pos);
674 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
675 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
676 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
679 glShadeModel(GL_SMOOTH);
681 glEnable(GL_DEPTH_TEST);
682 glEnable(GL_NORMALIZE);
684 if (MI_COUNT(mi) <= 0)
688 current_arg = -current_arg;
690 if (MI_COUNT(mi) == 1 || wire)
692 if (opacity_arg < 0.1) opacity_arg = 0.1;
693 if (opacity_arg > 1.0) opacity_arg = 1.0;
695 bp->squids = (squid **) calloc (MI_COUNT(mi), sizeof(*bp->squids));
696 for (i = 0; i < MI_COUNT(mi); i++)
697 bp->squids[i] = make_squid (mi, i);
701 if (opacity_arg < 1.0)
704 glBlendFunc (GL_SRC_ALPHA, GL_ONE);
707 # ifdef USE_TRACKBALL
708 bp->trackball = gltrackball_init (True);
714 draw_hydrostat (ModeInfo *mi)
716 hydrostat_configuration *bp = &bps[MI_SCREEN(mi)];
717 Display *dpy = MI_DISPLAY(mi);
718 Window window = MI_WINDOW(mi);
721 if (!bp->glx_context)
724 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
726 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
730 glScalef (0.03, 0.03, 0.03);
732 # ifdef USE_TRACKBALL
733 gltrackball_rotate (bp->trackball);
736 mi->polygon_count = 0;
738 if (opacity_arg < 1.0)
739 qsort (bp->squids, MI_COUNT(mi), sizeof(*bp->squids), cmp_squid);
741 for (i = 0; i < MI_COUNT(mi); i++)
743 squid *sq = bp->squids[i];
746 if (opacity_arg < 1.0)
747 glClear (GL_DEPTH_BUFFER_BIT);
750 if (! (random() % 700)) /* Reverse the flow every now and then */
751 current_arg = -current_arg;
755 if (mi->fps_p) do_fps (mi);
758 glXSwapBuffers(dpy, window);
763 free_hydrostat (ModeInfo *mi)
765 hydrostat_configuration *bp = &bps[MI_SCREEN(mi)];
769 for (i = 0; i < MI_COUNT(mi); i++)
770 free_squid (bp->squids[i]);
774 XSCREENSAVER_MODULE ("Hydrostat", hydrostat)