1 /* geodesic, 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
12 #define DEFAULTS "*delay: 30000 \n" \
16 # define refresh_geodesic 0
17 # define release_geodesic 0
19 #define countof(x) (sizeof((x))/sizeof((*x)))
21 #include "xlockmore.h"
25 #include "gltrackball.h"
28 #ifdef USE_GL /* whole file */
32 #define DEF_SPIN "True"
33 #define DEF_WANDER "True"
34 #define DEF_SPEED "1.0"
35 #define DEF_MODE "mesh"
37 typedef struct { double a, o; } LL; /* latitude + longitude */
40 GLXContext *glx_context;
42 trackball_state *trackball;
47 GLfloat color1[4], color2[4];
58 enum { WIRE, MESH, SOLID, STELLATED, STELLATED2 } mode;
60 } geodesic_configuration;
62 static geodesic_configuration *bps = NULL;
66 static Bool do_wander;
67 static char *mode_str;
69 static XrmOptionDescRec opts[] = {
70 { "-spin", ".spin", XrmoptionNoArg, "True" },
71 { "+spin", ".spin", XrmoptionNoArg, "False" },
72 { "-speed", ".speed", XrmoptionSepArg, 0 },
73 { "-wander", ".wander", XrmoptionNoArg, "True" },
74 { "+wander", ".wander", XrmoptionNoArg, "False" },
75 { "-mode", ".mode", XrmoptionSepArg, 0 },
76 { "-wireframe", ".mode", XrmoptionNoArg, "wire" },
79 static argtype vars[] = {
80 {&do_spin, "spin", "Spin", DEF_SPIN, t_Bool},
81 {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
82 {&speed, "speed", "Speed", DEF_SPEED, t_Float},
83 {&mode_str, "mode", "Mode", DEF_MODE, t_String},
86 ENTRYPOINT ModeSpecOpt geodesic_opts = {
87 countof(opts), opts, countof(vars), vars, NULL};
90 /* Renders a triangle specified by 3 cartesian endpoints.
93 triangle0 (ModeInfo *mi, XYZ p1, XYZ p2, XYZ p3)
95 geodesic_configuration *bp = &bps[MI_SCREEN(mi)];
96 int wire = MI_IS_WIREFRAME(mi);
97 GLfloat r = bp->thickness;
99 if (bp->mode == SOLID || bp->mode == STELLATED || bp->mode == STELLATED2)
102 if (r <= 0.001) r = 0.001;
104 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, bp->color1);
110 else if (r >= 1) /* solid triangular face */
112 glFrontFace (GL_CCW);
113 glBegin (wire ? GL_LINE_LOOP : GL_TRIANGLES);
114 do_normal (p1.x, p1.y, p1.z,
117 glVertex3f (p1.x, p1.y, p1.z);
118 glVertex3f (p2.x, p2.y, p2.z);
119 glVertex3f (p3.x, p3.y, p3.z);
123 else /* mesh: triangular face with a triangular hole */
125 XYZ p1b, p2b, p3b, c;
128 c.x = (p1.x + p2.x + p3.x) / 3;
129 c.y = (p1.y + p2.y + p3.y) / 3;
130 c.z = (p1.z + p2.z + p3.z) / 3;
132 p1b.x = p1.x + (r * (c.x - p1.x));
133 p1b.y = p1.y + (r * (c.y - p1.y));
134 p1b.z = p1.z + (r * (c.z - p1.z));
136 p2b.x = p2.x + (r * (c.x - p2.x));
137 p2b.y = p2.y + (r * (c.y - p2.y));
138 p2b.z = p2.z + (r * (c.z - p2.z));
140 p3b.x = p3.x + (r * (c.x - p3.x));
141 p3b.y = p3.y + (r * (c.y - p3.y));
142 p3b.z = p3.z + (r * (c.z - p3.z));
146 do_normal (p1.x, p1.y, p1.z,
150 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
151 glVertex3f (p1.x, p1.y, p1.z);
152 glVertex3f (p1b.x, p1b.y, p1b.z);
153 glVertex3f (p3b.x, p3b.y, p3b.z);
154 glVertex3f (p3.x, p3.y, p3.z);
157 glVertex3f (p1.x, p1.y, p1.z);
158 glVertex3f (p2.x, p2.y, p2.z);
159 glVertex3f (p2b.x, p2b.y, p2b.z);
160 glVertex3f (p1b.x, p1b.y, p1b.z);
163 glVertex3f (p2.x, p2.y, p2.z);
164 glVertex3f (p3.x, p3.y, p3.z);
165 glVertex3f (p3b.x, p3b.y, p3b.z);
166 glVertex3f (p2b.x, p2b.y, p2b.z);
172 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, bp->color2);
174 do_normal (p3.x, p3.y, p3.z,
176 p1b.x, p1b.y, p1b.z);
178 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
179 glVertex3f (d * p3.x, d * p3.y, d * p3.z);
180 glVertex3f (d * p3b.x, d * p3b.y, d * p3b.z);
181 glVertex3f (d * p1b.x, d * p1b.y, d * p1b.z);
182 glVertex3f (d * p1.x, d * p1.y, d * p1.z);
185 glVertex3f (d * p1b.x, d * p1b.y, d * p1b.z);
186 glVertex3f (d * p2b.x, d * p2b.y, d * p2b.z);
187 glVertex3f (d * p2.x, d * p2.y, d * p2.z);
188 glVertex3f (d * p1.x, d * p1.y, d * p1.z);
191 glVertex3f (d * p2b.x, d * p2b.y, d * p2b.z);
192 glVertex3f (d * p3b.x, d * p3b.y, d * p3b.z);
193 glVertex3f (d * p3.x, d * p3.y, d * p3.z);
194 glVertex3f (d * p2.x, d * p2.y, d * p2.z);
199 /* Connecting edges */
201 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, bp->color1);
203 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
205 do_normal (p1b.x, p1b.y, p1b.z,
207 p2b.x * d, p2b.y * d, p2b.z * d);
208 glVertex3f (p1b.x, p1b.y, p1b.z);
209 glVertex3f (p2b.x, p2b.y, p2b.z);
210 glVertex3f (p2b.x * d, p2b.y * d, p2b.z * d);
211 glVertex3f (p1b.x * d, p1b.y * d, p1b.z * d);
214 do_normal (p2b.x, p2b.y, p2b.z,
216 p3b.x * d, p3b.y * d, p3b.z * d);
217 glVertex3f (p2b.x, p2b.y, p2b.z);
218 glVertex3f (p3b.x, p3b.y, p3b.z);
219 glVertex3f (p3b.x * d, p3b.y * d, p3b.z * d);
220 glVertex3f (p2b.x * d, p2b.y * d, p2b.z * d);
223 do_normal (p3b.x, p3b.y, p3b.z,
225 p1b.x * d, p1b.y * d, p1b.z * d);
226 glVertex3f (p3b.x, p3b.y, p3b.z);
227 glVertex3f (p1b.x, p1b.y, p1b.z);
228 glVertex3f (p1b.x * d, p1b.y * d, p1b.z * d);
229 glVertex3f (p3b.x * d, p3b.y * d, p3b.z * d);
236 /* Renders a triangle specified by 3 polar endpoints.
239 triangle1 (ModeInfo *mi, LL v1, LL v2, LL v3)
243 p1.x = cos (v1.a) * cos (v1.o);
244 p1.y = cos (v1.a) * sin (v1.o);
247 p2.x = cos (v2.a) * cos (v2.o);
248 p2.y = cos (v2.a) * sin (v2.o);
251 p3.x = cos (v3.a) * cos (v3.o);
252 p3.y = cos (v3.a) * sin (v3.o);
255 triangle0 (mi, p1, p2, p3);
259 /* Computes the midpoint of a line between two polar coords.
262 midpoint2 (LL v1, LL v2, LL *vm_ret,
263 XYZ *p1_ret, XYZ *p2_ret, XYZ *pm_ret)
269 p1.x = cos (v1.a) * cos (v1.o);
270 p1.y = cos (v1.a) * sin (v1.o);
273 p2.x = cos (v2.a) * cos (v2.o);
274 p2.y = cos (v2.a) * sin (v2.o);
277 pm.x = (p1.x + p2.x) / 2;
278 pm.y = (p1.y + p2.y) / 2;
279 pm.z = (p1.z + p2.z) / 2;
281 vm.o = atan2 (pm.y, pm.x);
282 hyp = sqrt (pm.x * pm.x + pm.y * pm.y);
283 vm.a = atan2 (pm.z, hyp);
292 /* Computes the midpoint of a triangle specified in polar coords.
295 midpoint3 (LL v1, LL v2, LL v3, LL *vm_ret,
296 XYZ *p1_ret, XYZ *p2_ret, XYZ *p3_ret, XYZ *pm_ret)
302 p1.x = cos (v1.a) * cos (v1.o);
303 p1.y = cos (v1.a) * sin (v1.o);
306 p2.x = cos (v2.a) * cos (v2.o);
307 p2.y = cos (v2.a) * sin (v2.o);
310 p3.x = cos (v3.a) * cos (v3.o);
311 p3.y = cos (v3.a) * sin (v3.o);
314 pm.x = (p1.x + p2.x + p3.x) / 3;
315 pm.y = (p1.y + p2.y + p3.y) / 3;
316 pm.z = (p1.z + p2.z + p3.z) / 3;
318 vm.o = atan2 (pm.y, pm.x);
319 hyp = sqrt (pm.x * pm.x + pm.y * pm.y);
320 vm.a = atan2 (pm.z, hyp);
330 /* Renders a triangular geodesic facet to the given depth.
333 triangle (ModeInfo *mi, LL v1, LL v2, LL v3, int depth)
335 geodesic_configuration *bp = &bps[MI_SCREEN(mi)];
338 triangle1 (mi, v1, v2, v3);
342 XYZ p1, p2, p3, p12, p23, p13;
343 GLfloat r = bp->morph_ratio;
345 midpoint2 (v1, v2, &v12, &p1, &p2, &p12);
346 midpoint2 (v2, v3, &v23, &p2, &p3, &p23);
347 midpoint2 (v1, v3, &v13, &p1, &p3, &p13);
352 (bp->mode == STELLATED || bp->mode == STELLATED2))
353 { /* morph between flat and stellated faces */
356 midpoint3 (v1, v2, v3, &vc, &p1, &p2, &p3, &pc);
358 pc2.x = cos (vc.a) * cos (vc.o);
359 pc2.y = cos (vc.a) * sin (vc.o);
362 pc.x = pc.x + r * (pc2.x - pc.x);
363 pc.y = pc.y + r * (pc2.y - pc.y);
364 pc.z = pc.z + r * (pc2.z - pc.z);
366 triangle0 (mi, p1, p2, pc);
367 triangle0 (mi, p2, p3, pc);
368 triangle0 (mi, p3, p1, pc);
370 else if (depth == 0 && r < 1)
371 { /* morph between flat and sphere-oid faces */
372 XYZ p12b, p23b, p13b;
374 p12b.x = cos (v12.a) * cos (v12.o);
375 p12b.y = cos (v12.a) * sin (v12.o);
376 p12b.z = sin (v12.a);
378 p23b.x = cos (v23.a) * cos (v23.o);
379 p23b.y = cos (v23.a) * sin (v23.o);
380 p23b.z = sin (v23.a);
382 p13b.x = cos (v13.a) * cos (v13.o);
383 p13b.y = cos (v13.a) * sin (v13.o);
384 p13b.z = sin (v13.a);
386 p12.x = p12.x + r * (p12b.x - p12.x);
387 p12.y = p12.y + r * (p12b.y - p12.y);
388 p12.z = p12.z + r * (p12b.z - p12.z);
390 p23.x = p23.x + r * (p23b.x - p23.x);
391 p23.y = p23.y + r * (p23b.y - p23.y);
392 p23.z = p23.z + r * (p23b.z - p23.z);
394 p13.x = p13.x + r * (p13b.x - p13.x);
395 p13.y = p13.y + r * (p13b.y - p13.y);
396 p13.z = p13.z + r * (p13b.z - p13.z);
398 triangle0 (mi, p1, p12, p13);
399 triangle0 (mi, p12, p2, p23);
400 triangle0 (mi, p13, p23, p3);
401 triangle0 (mi, p12, p23, p13);
405 triangle (mi, v1, v12, v13, depth);
406 triangle (mi, v12, v2, v23, depth);
407 triangle (mi, v13, v23, v3, depth);
408 triangle (mi, v12, v23, v13, depth);
414 /* Renders a geodesic sphere to the given depth (frequency).
417 make_geodesic (ModeInfo *mi, int depth)
419 GLfloat th0 = atan (0.5); /* lat division: 26.57 deg */
420 GLfloat s = M_PI / 5; /* lon division: 72 deg */
423 for (i = 0; i < 10; i++)
426 GLfloat th2 = s * (i+1);
427 GLfloat th3 = s * (i+2);
429 v1.a = th0; v1.o = th1;
430 v2.a = th0; v2.o = th3;
431 v3.a = -th0; v3.o = th2;
432 vc.a = M_PI/2; vc.o = th2;
434 if (i & 1) /* north */
436 triangle (mi, v1, v2, vc, depth);
437 triangle (mi, v2, v1, v3, depth);
445 triangle (mi, v2, v1, vc, depth);
446 triangle (mi, v1, v2, v3, depth);
452 /* Window management, etc
455 reshape_geodesic (ModeInfo *mi, int width, int height)
457 GLfloat h = (GLfloat) height / (GLfloat) width;
459 glViewport (0, 0, (GLint) width, (GLint) height);
461 glMatrixMode(GL_PROJECTION);
463 gluPerspective (30.0, 1/h, 1.0, 100.0);
465 glMatrixMode(GL_MODELVIEW);
467 gluLookAt( 0.0, 0.0, 30.0,
471 glClear(GL_COLOR_BUFFER_BIT);
477 geodesic_handle_event (ModeInfo *mi, XEvent *event)
479 geodesic_configuration *bp = &bps[MI_SCREEN(mi)];
481 if (event->xany.type == ButtonPress &&
482 event->xbutton.button == Button1)
484 bp->button_down_p = True;
485 gltrackball_start (bp->trackball,
486 event->xbutton.x, event->xbutton.y,
487 MI_WIDTH (mi), MI_HEIGHT (mi));
490 else if (event->xany.type == ButtonRelease &&
491 event->xbutton.button == Button1)
493 bp->button_down_p = False;
496 else if (event->xany.type == ButtonPress &&
497 (event->xbutton.button == Button4 ||
498 event->xbutton.button == Button5 ||
499 event->xbutton.button == Button6 ||
500 event->xbutton.button == Button7))
502 gltrackball_mousewheel (bp->trackball, event->xbutton.button, 3,
503 !!event->xbutton.state);
506 else if (event->xany.type == MotionNotify &&
509 gltrackball_track (bp->trackball,
510 event->xmotion.x, event->xmotion.y,
511 MI_WIDTH (mi), MI_HEIGHT (mi));
520 init_geodesic (ModeInfo *mi)
522 geodesic_configuration *bp;
523 int wire = MI_IS_WIREFRAME(mi);
526 bps = (geodesic_configuration *)
527 calloc (MI_NUM_SCREENS(mi), sizeof (geodesic_configuration));
529 fprintf(stderr, "%s: out of memory\n", progname);
534 bp = &bps[MI_SCREEN(mi)];
536 bp->glx_context = init_GL(mi);
538 reshape_geodesic (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
540 /* This comes first because it sets wire. */
541 if (!mode_str || !*mode_str)
543 if (!strcasecmp(mode_str, "random")) {
545 bp->mode = MESH + (random() % (STELLATED2 - MESH + 1));
546 } else if (!strcasecmp(mode_str, "mesh")) {
548 } else if (!strcasecmp(mode_str, "solid")) {
550 } else if (!strcasecmp(mode_str, "stellated")) {
551 bp->mode = STELLATED;
552 } else if (!strcasecmp(mode_str, "stellated2")) {
553 bp->mode = STELLATED2;
554 } else if (!strcasecmp(mode_str, "wire")) {
556 MI_IS_WIREFRAME(mi) = wire = 1;
558 fprintf (stderr, "%s: unknown mode: %s\n", progname, mode_str);
564 static GLfloat cspec[4] = {1.0, 1.0, 1.0, 1.0};
565 static const GLfloat shiny = 128.0;
567 static GLfloat pos[4] = {1.0, 1.0, 1.0, 0.0};
568 static GLfloat amb[4] = {0.0, 0.0, 0.0, 1.0};
569 static GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
570 static GLfloat spc[4] = {0.0, 1.0, 1.0, 1.0};
572 glLightfv(GL_LIGHT0, GL_POSITION, pos);
573 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
574 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
575 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
577 glMaterialfv (GL_FRONT, GL_SPECULAR, cspec);
578 glMateriali (GL_FRONT, GL_SHININESS, shiny);
585 glEnable (GL_DEPTH_TEST);
587 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
590 /* Actually this looks pretty good in -wire with lighting! */
591 glEnable (GL_LIGHTING);
592 glEnable (GL_LIGHT0);
595 double spin_speed = 0.25 * speed;
596 double wander_speed = 0.01 * speed;
597 double spin_accel = 0.2;
599 bp->rot = make_rotator (do_spin ? spin_speed : 0,
600 do_spin ? spin_speed : 0,
601 do_spin ? spin_speed : 0,
603 do_wander ? wander_speed : 0,
605 bp->trackball = gltrackball_init ();
608 if (MI_COUNT(mi) < 1) MI_COUNT(mi) = 1;
611 bp->colors = (XColor *) calloc(bp->ncolors, sizeof(XColor));
612 make_smooth_colormap (0, 0, 0,
613 bp->colors, &bp->ncolors,
616 bp->depth = 1; /* start 1 up from the icosahedron */
621 bp->thickdelta = 0.0007;
630 draw_geodesic (ModeInfo *mi)
632 int wire = MI_IS_WIREFRAME(mi);
633 geodesic_configuration *bp = &bps[MI_SCREEN(mi)];
634 Display *dpy = MI_DISPLAY(mi);
635 Window window = MI_WINDOW(mi);
637 if (!bp->glx_context)
640 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
643 glShadeModel(GL_SMOOTH);
645 glEnable(GL_DEPTH_TEST);
646 glEnable(GL_NORMALIZE);
647 glEnable(GL_CULL_FACE);
649 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
655 get_position (bp->rot, &x, &y, &z, !bp->button_down_p);
656 glTranslatef((x - 0.5) * 8,
660 /* Do it twice because we don't track the device's orientation. */
661 glRotatef( current_device_rotation(), 0, 0, 1);
662 gltrackball_rotate (bp->trackball);
663 glRotatef(-current_device_rotation(), 0, 0, 1);
665 get_rotation (bp->rot, &x, &y, &z, !bp->button_down_p);
666 glRotatef (x * 360, 1.0, 0.0, 0.0);
667 glRotatef (y * 360, 0.0, 1.0, 0.0);
668 glRotatef (z * 360, 0.0, 0.0, 1.0);
671 bp->color1[0] = bp->colors[bp->ccolor].red / 65536.0;
672 bp->color1[1] = bp->colors[bp->ccolor].green / 65536.0;
673 bp->color1[2] = bp->colors[bp->ccolor].blue / 65536.0;
676 bp->color2[0] = bp->colors[bp->ccolor2].red / 65536.0;
677 bp->color2[1] = bp->colors[bp->ccolor2].green / 65536.0;
678 bp->color2[2] = bp->colors[bp->ccolor2].blue / 65536.0;
681 bp->ccolor = (bp->ccolor + 1) % bp->ncolors;
682 bp->ccolor2 = (bp->ccolor + bp->ncolors / 2) % bp->ncolors;
684 mi->polygon_count = 0;
686 glScalef (10, 10, 10);
689 GLfloat r = bp->depth - floor(bp->depth);
690 GLfloat alpha, morph1, morph2;
693 /* Two ranges: first for fading in the new segments.
694 Second for morphing the segments into position.
696 GLfloat range = 0.15;
697 GLfloat min1 = (0.5 - range) / 2;
698 GLfloat max1 = 0.5 - min1;
699 GLfloat min2 = 0.5 + min1;
700 GLfloat max2 = 0.5 + max1;
702 if (r < min1) /* old alone */
704 d1 = d2 = floor (bp->depth);
708 else if (r < max1 && /* fade to new flat */
710 bp->mode == STELLATED ||
711 bp->mode == STELLATED2))
713 d1 = floor (bp->depth);
714 d2 = ceil (bp->depth);
717 alpha = (r - min1) / (max1 - min1);
719 if (bp->mode == STELLATED || bp->mode == STELLATED2)
721 morph1 = 1 - alpha; /* de-stellate while fading out */
722 morph1 = 2 * (morph1 - 0.5); /* do it faster */
723 if (morph1 < 0) morph1 = 0;
726 else if (r < min2) /* new flat */
728 d1 = d2 = ceil (bp->depth);
732 else if (r < max2) /* morph */
734 d1 = d2 = ceil (bp->depth);
735 morph1 = morph2 = (r - min2) / (max2 - min2);
740 d1 = d2 = ceil (bp->depth);
745 mi->recursion_depth = d2 + r;
747 if (bp->mode == STELLATED2)
755 if (alpha > 0.5) /* always draw the more transparent one first */
758 s1 = d1; d1 = d2; d2 = s1;
759 s2 = morph1; morph1 = morph2; morph2 = s2;
762 bp->color1[3] = 1 - alpha;
763 bp->color2[3] = 1 - alpha;
766 glDisable (GL_POLYGON_OFFSET_FILL);
768 bp->morph_ratio = morph1;
769 make_geodesic (mi, d1);
771 /* Make the less-transparent object take precedence */
774 glEnable (GL_POLYGON_OFFSET_FILL);
775 glPolygonOffset (1.0, 1.0);
779 bp->color1[3] = alpha;
780 bp->color2[3] = alpha;
782 bp->morph_ratio = morph2;
783 make_geodesic (mi, d2);
788 if (mi->fps_p) do_fps (mi);
791 glXSwapBuffers(dpy, window);
794 if (! bp->button_down_p)
796 bp->depth += speed * bp->delta;
797 bp->thickness += speed * bp->thickdelta;
799 if (bp->depth > MI_COUNT(mi)-1)
801 bp->depth = MI_COUNT(mi)-1;
802 bp->delta = -fabs (bp->delta);
804 else if (bp->depth < 0)
807 bp->delta = fabs (bp->delta);
809 /* Randomize the mode again when we hit the bottom state.
810 #### I wish this did a fade instead of a jump-cut.
813 bp->mode = MESH + (random() % (STELLATED2 - MESH + 1));
816 if (bp->thickness > 1)
819 bp->thickdelta = -fabs (bp->thickdelta);
821 else if (bp->thickness < 0)
824 bp->thickdelta = fabs (bp->thickdelta);
829 XSCREENSAVER_MODULE ("Geodesic", geodesic)