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
38 #define countof(x) (sizeof((x))/sizeof((*x)))
40 #include "xlockmore.h"
44 #include "gltrackball.h"
47 #ifdef USE_GL /* whole file */
49 /* It looks bad when you rotate it with the trackball, because it reveals
50 that the tentacles are moving in a 2d plane. But it's useful for
54 #define DEF_SPEED "1.0"
55 #define DEF_PULSE "True"
56 #define DEF_HEAD_RADIUS "60"
57 #define DEF_TENTACLES "35"
58 #define DEF_THICKNESS "18"
59 #define DEF_LENGTH "55"
60 #define DEF_GRAVITY "0.5"
61 #define DEF_CURRENT "0.25"
62 #define DEF_FRICTION "0.02"
63 #define DEF_OPACITY "0.8"
82 GLfloat ratio, pulse, rate;
91 GLXContext *glx_context;
96 trackball_state *trackball;
98 } hydrostat_configuration;
100 static hydrostat_configuration *bps = NULL;
102 static Bool do_pulse;
103 static GLfloat speed_arg;
104 static GLfloat head_radius_arg;
105 static GLfloat ntentacles_arg;
106 static GLfloat thickness_arg;
107 static GLfloat length_arg;
108 static GLfloat gravity_arg;
109 static GLfloat current_arg;
110 static GLfloat friction_arg;
111 static GLfloat opacity_arg;
113 static XrmOptionDescRec opts[] = {
114 { "-pulse", ".pulse", XrmoptionNoArg, "True" },
115 { "+pulse", ".pulse", XrmoptionNoArg, "False" },
116 { "-speed", ".speed", XrmoptionSepArg, 0 },
117 { "-head-radius", ".headRadius", XrmoptionSepArg, 0 },
118 { "-tentacles", ".tentacles", XrmoptionSepArg, 0 },
119 { "-thickness", ".thickness", XrmoptionSepArg, 0 },
120 { "-length", ".length", XrmoptionSepArg, 0 },
121 { "-gravity", ".gravity", XrmoptionSepArg, 0 },
122 { "-current", ".current", XrmoptionSepArg, 0 },
123 { "-friction", ".friction", XrmoptionSepArg, 0 },
124 { "-opacity", ".opacity", XrmoptionSepArg, 0 },
127 static argtype vars[] = {
128 { &do_pulse, "pulse", "Pulse", DEF_PULSE, t_Bool },
129 { &speed_arg, "speed", "Speed", DEF_SPEED, t_Float },
130 { &head_radius_arg, "headRadius", "HeadRadius", DEF_HEAD_RADIUS, t_Float },
131 { &ntentacles_arg, "tentacles", "Tentacles", DEF_TENTACLES, t_Float },
132 { &thickness_arg, "thickness", "Thickness", DEF_THICKNESS, t_Float },
133 { &length_arg, "length", "Length", DEF_LENGTH, t_Float },
134 { &gravity_arg, "gravity", "Gravity", DEF_GRAVITY, t_Float },
135 { ¤t_arg, "current", "Current", DEF_CURRENT, t_Float },
136 { &friction_arg, "friction", "Friction", DEF_FRICTION, t_Float },
137 { &opacity_arg, "opacity", "Opacity", DEF_OPACITY, t_Float },
140 ENTRYPOINT ModeSpecOpt hydrostat_opts = {countof(opts), opts,
141 countof(vars), vars, NULL};
145 move_tentacle (squid *sq, tentacle *t)
148 node *prev = &t->nodes[0];
149 int rot = (int) current_device_rotation();
151 for (i = 1, j = 0; i < t->length; i++, j++)
155 node *n = &t->nodes[i];
157 /* Sadly, this is still computing motion in a 2d plane, so the
158 tentacles look dumb if the scene is rotated. */
164 d.x = prev->pos.x - n->pos.x;
165 d.y = prev->pos.y - n->pos.y;
166 d.z = prev->pos.z - n->pos.z;
167 da = atan2 (d.z, d.x);
169 p.x = n->pos.x + cos (da) * t->spacing * t->length;
170 p.y = n->pos.y + cos (da) * t->spacing * t->length;
171 p.z = n->pos.z + sin (da) * t->spacing * t->length;
173 n->pos.x = prev->pos.x - (p.x - n->pos.x);
174 n->pos.y = prev->pos.y - (p.y - n->pos.y);
175 n->pos.z = prev->pos.z - (p.z - n->pos.z);
177 n->v.x = n->pos.x - n->opos.x;
178 n->v.y = n->pos.y - n->opos.y;
179 n->v.z = n->pos.z - n->opos.z;
181 n->v.x *= t->friction * (1 - friction_arg);
182 n->v.y *= t->friction * (1 - friction_arg);
183 n->v.z *= t->friction * (1 - friction_arg);
187 n->v.x += gravity_arg;
188 n->v.y -= current_arg;
189 n->v.z -= current_arg;
192 n->v.x -= gravity_arg;
193 n->v.y += current_arg;
194 n->v.z += current_arg;
197 n->v.x -= current_arg;
198 n->v.y -= current_arg;
199 n->v.z -= gravity_arg;
202 n->v.x += current_arg;
203 n->v.y += current_arg;
204 n->v.z += gravity_arg;
208 n->opos.x = n->pos.x;
209 n->opos.y = n->pos.y;
210 n->opos.z = n->pos.z;
220 return cos ((r/2 + 1) * M_PI) + 1; /* Smooth curve up, end at slope 1. */
224 /* Squirty motion: fast acceleration, then fade. */
226 ease_ratio (GLfloat r)
229 GLfloat ease2 = 1-ease;
231 else if (r >= 1) r = 1;
232 else if (r <= ease) r = ease * ease_fn (r / ease);
233 else r = 1 - ease2 * ease_fn ((1 - r) / ease2);
239 move_squid (ModeInfo *mi, squid *sq)
241 hydrostat_configuration *bp = &bps[MI_SCREEN(mi)];
242 GLfloat step = M_PI * 2 / sq->ntentacles;
244 GLfloat radius = head_radius_arg;
247 /* Move to a new position */
249 if (! bp->button_down_p)
251 sq->ratio += speed_arg * 0.01;
254 sq->ratio = -(frand(2.0) + frand(2.0) + frand(2.0));
255 sq->from.x = sq->to.x;
256 sq->from.y = sq->to.y;
257 sq->from.z = sq->to.z;
258 sq->to.x = 250 - frand(500);
259 sq->to.y = 250 - frand(500);
260 sq->to.z = 250 - frand(500);
263 r = sq->ratio > 0 ? ease_ratio (sq->ratio) : 0;
264 sq->pos.x = sq->from.x + r * (sq->to.x - sq->from.x);
265 sq->pos.y = sq->from.y + r * (sq->to.y - sq->from.y);
266 sq->pos.z = sq->from.z + r * (sq->to.z - sq->from.z);
271 GLfloat p = pow (sin (sq->pulse * M_PI), 18);
272 sq->head_radius = (head_radius_arg * 0.7 +
273 head_radius_arg * 0.3 * p);
274 radius = sq->head_radius * 0.25;
275 sq->pulse += sq->rate * speed_arg * 0.02;
276 if (sq->pulse > 1) sq->pulse = 0;
279 for (i = 0; i < sq->ntentacles; i++)
281 tentacle *tt = &sq->tentacles[i];
282 GLfloat th = i * step;
283 GLfloat px = cos (th) * radius;
284 GLfloat py = sin (th) * radius;
286 tt->nodes[0].pos.x = sq->pos.x + px;
287 tt->nodes[0].pos.y = sq->pos.y + py;
288 tt->nodes[0].pos.z = sq->pos.z;
289 move_tentacle (sq, tt);
294 /* Find the angle at which the head should be tilted in the XY plane.
297 head_angle (ModeInfo *mi, squid *sq)
302 for (i = 0; i < sq->ntentacles; i++)
304 tentacle *t = &sq->tentacles[i];
305 int j = t->length / 3; /* Pick a node toward the top */
306 node *n = &t->nodes[j];
312 sum.x /= sq->ntentacles;
313 sum.y /= sq->ntentacles;
314 sum.z /= sq->ntentacles;
320 return (-atan2 (sum.x, sum.z) * (180 / M_PI));
325 draw_head (ModeInfo *mi, squid *sq, GLfloat scale)
327 int wire = MI_IS_WIREFRAME(mi);
328 int i = wire ? 8 : 64;
330 GLfloat angle = head_angle (mi, sq);
335 glTranslatef (sq->pos.x, sq->pos.y, sq->pos.z);
336 glScalef (sq->head_radius, sq->head_radius, sq->head_radius);
337 glScalef (scale, scale, scale);
338 glRotatef (90, 1, 0, 0);
340 memcpy (c2, sq->color, sizeof(c2));
341 if (opacity_arg < 1.0 && scale >= 1.0)
344 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, c2);
346 glTranslatef (0, 0.3, 0);
347 glRotatef (angle, 0, 0, 1);
349 glScalef (1, 1.1, 1);
350 unit_dome (i, i/2, wire);
351 glRotatef (180, 0, 0, 1);
352 glScalef (1, 0.5, 1);
353 unit_dome (i, i/2, wire);
355 mi->polygon_count += i * i;
361 draw_squid (ModeInfo *mi, squid *sq)
363 int wire = MI_IS_WIREFRAME(mi);
366 glRotatef (90, 1, 0, 0);
368 if (opacity_arg < 1.0)
369 draw_head (mi, sq, 0.75);
371 for (i = 0; i < sq->ntentacles; i++)
373 tentacle *t = &sq->tentacles[i];
376 glColor4fv (t->color);
377 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, t->color);
381 glBegin (GL_LINE_STRIP);
382 for (j = 0; j < t->length; j++)
383 glVertex3f (t->nodes[j].pos.x,
387 mi->polygon_count += t->length;
391 GLfloat radius = t->radius * thickness_arg;
392 GLfloat rstep = radius / t->length;
394 glFrontFace (GL_CCW);
395 for (j = 0; j < t->length-1; j++)
398 node *n1 = &t->nodes[j];
399 node *n2 = &t->nodes[j+1];
400 GLfloat X = (n1->pos.x - n2->pos.x);
401 GLfloat Y = (n1->pos.y - n2->pos.y);
402 GLfloat Z = (n1->pos.z - n2->pos.z);
403 GLfloat L = sqrt (X*X + Y*Y + Z*Z);
404 GLfloat r2 = radius - rstep;
407 glTranslatef (n1->pos.x, n1->pos.y, n1->pos.z);
408 glRotatef (-atan2 (X, Y) * (180 / M_PI), 0, 0, 1);
409 glRotatef ( atan2 (Z, sqrt(X*X + Y*Y)) * (180 / M_PI), 1, 0, 0);
411 glBegin (wire ? GL_LINE_LOOP : GL_QUAD_STRIP);
412 for (k = 0; k <= faces; k++)
414 GLfloat th = k * M_PI * 2 / faces;
417 GLfloat x1 = radius * c;
418 GLfloat y1 = radius * s;
424 glNormal3f (x1, z1, y1);
425 glVertex3f (x1, z1, y1);
426 glVertex3f (x2, z2, y2);
436 draw_head (mi, sq, 1.0);
443 make_squid (ModeInfo *mi, int which)
445 squid *sq = calloc (1, sizeof(*sq));
448 sq->head_radius = head_radius_arg;
449 sq->thickness = thickness_arg;
450 sq->ntentacles = ntentacles_arg;
452 sq->color[0] = 0.1 + frand(0.7);
453 sq->color[1] = 0.5 + frand(0.5);
454 sq->color[2] = 0.1 + frand(0.7);
455 sq->color[3] = opacity_arg;
457 sq->from.x = sq->to.x = sq->pos.x = 200 - frand(400);
458 sq->from.y = sq->to.y = sq->pos.y = 200 - frand(400);
459 sq->from.z = sq->to.z = sq->pos.z = -frand(200);
461 sq->ratio = -frand(3);
463 if (which > 0) /* Start others off screen, and moving in */
465 sq->from.x = sq->to.x = sq->pos.x = 800 + frand(500)
466 * (random()&1 ? 1 : -1);
467 sq->from.y = sq->to.y = sq->pos.y = 800 + frand(500)
468 * (random()&1 ? 1 : -1);
473 sq->pulse = frand(1.0);
474 sq->rate = 0.8 + frand(0.2);
476 sq->tentacles = (tentacle *)
477 calloc (sq->ntentacles, sizeof(*sq->tentacles));
478 for (i = 0; i < sq->ntentacles; i++)
481 tentacle *t = &sq->tentacles[i];
482 GLfloat shade = 0.75 + frand(0.25);
484 t->length = 2 + length_arg * (0.8 + frand (0.4));
485 t->radius = 0.05 + frand (0.95);
486 t->spacing = 0.02 + frand (0.08);
487 t->friction = 0.7 + frand (0.18);
488 t->nodes = (node *) calloc (t->length + 1, sizeof (*t->nodes));
490 t->color[0] = shade * sq->color[0];
491 t->color[1] = shade * sq->color[1];
492 t->color[2] = shade * sq->color[2];
493 t->color[3] = sq->color[3];
495 for (j = 0; j < t->length; j++)
497 node *n = &t->nodes[j];
498 n->pos.x = sq->pos.x;
499 n->pos.y = sq->pos.y;
500 n->pos.z = sq->pos.z + j;
507 /* qsort comparator for sorting squid by depth */
509 cmp_squid (const void *aa, const void *bb)
511 squid * const *a = aa;
512 squid * const *b = bb;
513 return ((int) ((*b)->pos.y * 10000) -
514 (int) ((*a)->pos.y * 10000));
519 free_squid (squid *sq)
522 for (i = 0; i < sq->ntentacles; i++)
523 free (sq->tentacles[i].nodes);
524 free (sq->tentacles);
529 /* Window management, etc
532 reshape_hydrostat (ModeInfo *mi, int width, int height)
534 GLfloat h = (GLfloat) height / (GLfloat) width;
536 glViewport (0, 0, (GLint) width, (GLint) height);
538 glMatrixMode(GL_PROJECTION);
540 gluPerspective (30.0, 1/h, 1.0, 100.0);
542 glMatrixMode(GL_MODELVIEW);
544 gluLookAt( 0.0, 0.0, 30.0,
548 # ifdef HAVE_MOBILE /* Keep it the same relative size when rotated. */
550 int o = (int) current_device_rotation();
551 if (o != 0 && o != 180 && o != -180)
552 glScalef (1/h, 1/h, 1/h);
556 glClear(GL_COLOR_BUFFER_BIT);
562 hydrostat_handle_event (ModeInfo *mi, XEvent *event)
564 hydrostat_configuration *bp = &bps[MI_SCREEN(mi)];
565 int w = MI_WIDTH(mi);
566 int h = MI_HEIGHT(mi);
569 # ifdef USE_TRACKBALL
570 if (gltrackball_event_handler (event, bp->trackball,
571 MI_WIDTH (mi), MI_HEIGHT (mi),
576 switch (event->xany.type) {
577 case ButtonPress: case ButtonRelease:
578 x = event->xbutton.x;
579 y = event->xbutton.y;
582 x = event->xmotion.x;
583 y = event->xmotion.y;
594 if (event->xany.type == ButtonPress)
599 /* This is pretty halfassed hit detection, but it works ok... */
600 for (i = 0; i < MI_COUNT(mi); i++)
602 squid *s = bp->squids[i];
603 GLfloat X = s->pos.x - x;
604 GLfloat Y = s->pos.z - y;
605 GLfloat D = sqrt(X*X + Y*Y);
613 if (D0 > 300) /* Too far away, missed hit */
619 bp->squids[bp->dragging]->ratio = -3;
620 bp->button_down_p = True;
624 else if (event->xany.type == ButtonRelease && bp->dragging >= 0)
626 bp->button_down_p = False;
630 else if (event->xany.type == MotionNotify && bp->dragging >= 0)
632 squid *s = bp->squids[bp->dragging];
633 s->from.x = s->to.x = s->pos.x = x;
634 s->from.z = s->to.z = s->pos.z = y;
635 s->from.y = s->to.y = s->pos.y;
644 init_hydrostat (ModeInfo *mi)
646 int wire = MI_IS_WIREFRAME(mi);
647 hydrostat_configuration *bp;
651 bps = (hydrostat_configuration *)
652 calloc (MI_NUM_SCREENS(mi), sizeof (hydrostat_configuration));
654 fprintf(stderr, "%s: out of memory\n", progname);
659 bp = &bps[MI_SCREEN(mi)];
661 bp->glx_context = init_GL(mi);
663 reshape_hydrostat (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
667 GLfloat pos[4] = {1.0, 1.0, 1.0, 0.0};
668 GLfloat amb[4] = {0.0, 0.0, 0.0, 1.0};
669 GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
670 GLfloat spc[4] = {0.0, 1.0, 1.0, 1.0};
672 glEnable(GL_LIGHTING);
674 glEnable(GL_DEPTH_TEST);
675 glEnable(GL_CULL_FACE);
677 glLightfv(GL_LIGHT0, GL_POSITION, pos);
678 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
679 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
680 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
683 glShadeModel(GL_SMOOTH);
685 glEnable(GL_DEPTH_TEST);
686 glEnable(GL_NORMALIZE);
688 if (MI_COUNT(mi) <= 0)
692 current_arg = -current_arg;
694 if (MI_COUNT(mi) == 1 || wire)
696 if (opacity_arg < 0.1) opacity_arg = 0.1;
697 if (opacity_arg > 1.0) opacity_arg = 1.0;
699 bp->squids = (squid **) calloc (MI_COUNT(mi), sizeof(*bp->squids));
700 for (i = 0; i < MI_COUNT(mi); i++)
701 bp->squids[i] = make_squid (mi, i);
705 if (opacity_arg < 1.0)
708 glBlendFunc (GL_SRC_ALPHA, GL_ONE);
711 # ifdef USE_TRACKBALL
712 bp->trackball = gltrackball_init (True);
718 draw_hydrostat (ModeInfo *mi)
720 hydrostat_configuration *bp = &bps[MI_SCREEN(mi)];
721 Display *dpy = MI_DISPLAY(mi);
722 Window window = MI_WINDOW(mi);
725 if (!bp->glx_context)
728 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
730 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
734 glScalef (0.03, 0.03, 0.03);
736 # ifdef USE_TRACKBALL
737 gltrackball_rotate (bp->trackball);
740 mi->polygon_count = 0;
742 if (opacity_arg < 1.0)
743 qsort (bp->squids, MI_COUNT(mi), sizeof(*bp->squids), cmp_squid);
745 for (i = 0; i < MI_COUNT(mi); i++)
747 squid *sq = bp->squids[i];
750 if (opacity_arg < 1.0)
751 glClear (GL_DEPTH_BUFFER_BIT);
754 if (! (random() % 700)) /* Reverse the flow every now and then */
755 current_arg = -current_arg;
759 if (mi->fps_p) do_fps (mi);
762 glXSwapBuffers(dpy, window);
767 release_hydrostat (ModeInfo *mi)
769 hydrostat_configuration *bp = &bps[MI_SCREEN(mi)];
771 for (i = 0; i < MI_COUNT(mi); i++)
772 free_squid (bp->squids[i]);
776 XSCREENSAVER_MODULE ("Hydrostat", hydrostat)