From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / glx / splodesic.c
1 /* splodesic, Copyright (c) 2016 Jamie Zawinski <jwz@jwz.org>
2  *
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 
9  * implied warranty.
10  */
11
12 #define DEFAULTS        "*delay:        30000       \n" \
13                         "*showFPS:      False       \n" \
14                         "*wireframe:    False       \n" \
15                         "*suppressRotationAnimation: True\n" \
16
17 # define refresh_splodesic 0
18 # define release_splodesic 0
19 #undef countof
20 #define countof(x) (sizeof((x))/sizeof((*x)))
21
22 #include "xlockmore.h"
23 #include "colors.h"
24 #include "normals.h"
25 #include "rotator.h"
26 #include "gltrackball.h"
27 #include <ctype.h>
28
29 #ifdef USE_GL /* whole file */
30
31
32 #define DEF_SPIN        "True"
33 #define DEF_WANDER      "True"
34 #define DEF_SPEED       "1.0"
35 #define DEF_DEPTH       "4"
36
37 #define BELLRAND(n) ((frand((n)) + frand((n)) + frand((n))) / 3)
38
39 typedef struct { double a, o; } LL;     /* latitude + longitude */
40
41 typedef struct triangle triangle;
42 struct triangle {
43   XYZ p[3];
44   triangle *next;
45   triangle *neighbors[3];
46   GLfloat altitude;
47   GLfloat velocity;
48   GLfloat thrust;
49   int thrust_duration;
50   int refcount;
51 };
52
53 typedef struct {
54   GLXContext *glx_context;
55   rotator *rot;
56   trackball_state *trackball;
57   Bool button_down_p;
58
59   int count;
60   triangle *triangles;
61
62   int ncolors;
63   XColor *colors;
64   int ccolor;
65
66 } splodesic_configuration;
67
68 static splodesic_configuration *bps = NULL;
69
70 static Bool do_spin;
71 static GLfloat speed;
72 static int depth_arg;
73 static Bool do_wander;
74
75 static XrmOptionDescRec opts[] = {
76   { "-spin",   ".spin",   XrmoptionNoArg, "True" },
77   { "+spin",   ".spin",   XrmoptionNoArg, "False" },
78   { "-speed",  ".speed",  XrmoptionSepArg, 0 },
79   { "-depth",  ".freq",   XrmoptionSepArg, 0 },
80   { "-wander", ".wander", XrmoptionNoArg, "True" },
81   { "+wander", ".wander", XrmoptionNoArg, "False" }
82 };
83
84 static argtype vars[] = {
85   {&do_spin,   "spin",   "Spin",   DEF_SPIN,   t_Bool},
86   {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
87   {&speed,     "speed",  "Speed",  DEF_SPEED,  t_Float},
88   {&depth_arg, "freq",   "Depth",  DEF_DEPTH,  t_Int},
89 };
90
91 ENTRYPOINT ModeSpecOpt splodesic_opts = {countof(opts), opts, countof(vars), vars, NULL};
92
93
94 /* Creates a triangle specified by 3 polar endpoints.
95  */
96 static void
97 make_triangle1 (ModeInfo *mi, LL v1, LL v2, LL v3)
98 {
99   splodesic_configuration *bp = &bps[MI_SCREEN(mi)];
100   triangle *t = (triangle *) calloc (1, sizeof(*t));
101
102   t->p[0].x = cos (v1.a) * cos (v1.o);
103   t->p[0].y = cos (v1.a) * sin (v1.o);
104   t->p[0].z = sin (v1.a);
105
106   t->p[1].x = cos (v2.a) * cos (v2.o);
107   t->p[1].y = cos (v2.a) * sin (v2.o);
108   t->p[1].z = sin (v2.a);
109
110   t->p[2].x = cos (v3.a) * cos (v3.o);
111   t->p[2].y = cos (v3.a) * sin (v3.o);
112   t->p[2].z = sin (v3.a);
113
114   t->next = bp->triangles;
115   bp->triangles = t;
116   bp->count++;
117 }
118
119
120 /* Computes the midpoint of a line between two polar coords.
121  */
122 static void
123 midpoint2 (LL v1, LL v2, LL *vm_ret,
124            XYZ *p1_ret, XYZ *p2_ret, XYZ *pm_ret)
125 {
126   XYZ p1, p2, pm;
127   LL vm;
128   GLfloat hyp;
129
130   p1.x = cos (v1.a) * cos (v1.o);
131   p1.y = cos (v1.a) * sin (v1.o);
132   p1.z = sin (v1.a);
133
134   p2.x = cos (v2.a) * cos (v2.o);
135   p2.y = cos (v2.a) * sin (v2.o);
136   p2.z = sin (v2.a);
137
138   pm.x = (p1.x + p2.x) / 2;
139   pm.y = (p1.y + p2.y) / 2;
140   pm.z = (p1.z + p2.z) / 2;
141
142   vm.o = atan2 (pm.y, pm.x);
143   hyp = sqrt (pm.x * pm.x + pm.y * pm.y);
144   vm.a = atan2 (pm.z, hyp);
145
146   *p1_ret = p1;
147   *p2_ret = p2;
148   *pm_ret = pm;
149   *vm_ret = vm;
150 }
151
152
153 /* Creates triangular geodesic facets to the given depth.
154  */
155 static void
156 make_triangle (ModeInfo *mi, LL v1, LL v2, LL v3, int depth)
157 {
158   if (depth <= 0)
159     make_triangle1 (mi, v1, v2, v3);
160   else
161     {
162       LL v12, v23, v13;
163       XYZ p1, p2, p3, p12, p23, p13;
164
165       midpoint2 (v1, v2, &v12, &p1, &p2, &p12);
166       midpoint2 (v2, v3, &v23, &p2, &p3, &p23);
167       midpoint2 (v1, v3, &v13, &p1, &p3, &p13);
168       depth--;
169
170       make_triangle (mi, v1,  v12, v13, depth);
171       make_triangle (mi, v12, v2,  v23, depth);
172       make_triangle (mi, v13, v23, v3,  depth);
173       make_triangle (mi, v12, v23, v13, depth);
174     }
175 }
176
177
178 /* Creates triangles of a geodesic to the given depth (frequency).
179  */
180 static void
181 make_geodesic (ModeInfo *mi)
182 {
183   int depth = depth_arg;
184   GLfloat th0 = atan (0.5);  /* lat division: 26.57 deg */
185   GLfloat s = M_PI / 5;      /* lon division: 72 deg    */
186   int i;
187
188   for (i = 0; i < 10; i++)
189     {
190       GLfloat th1 = s * i;
191       GLfloat th2 = s * (i+1);
192       GLfloat th3 = s * (i+2);
193       LL v1, v2, v3, vc;
194       v1.a = th0;    v1.o = th1;
195       v2.a = th0;    v2.o = th3;
196       v3.a = -th0;   v3.o = th2;
197       vc.a = M_PI/2; vc.o = th2;
198
199       if (i & 1)                        /* north */
200         {
201           make_triangle (mi, v1, v2, vc, depth);
202           make_triangle (mi, v2, v1, v3, depth);
203         }
204       else                              /* south */
205         {
206           v1.a = -v1.a;
207           v2.a = -v2.a;
208           v3.a = -v3.a;
209           vc.a = -vc.a;
210           make_triangle (mi, v2, v1, vc, depth);
211           make_triangle (mi, v1, v2, v3, depth);
212         }
213     }
214 }
215
216
217 /* Add t1 to the neighbor list of t0. */
218 static void
219 link_neighbor (int i, int j, triangle *t0, triangle *t1)
220 {
221   int k;
222   if (t0 == t1)
223     return;
224   for (k = 0; k < countof(t0->neighbors); k++)
225     {
226       if (t0->neighbors[k] == t1 ||
227           t0->neighbors[k] == 0)
228         {
229           t0->neighbors[k] = t1;
230           return;
231         }
232     }
233   fprintf (stderr, "%d %d: too many neighbors\n", i, j);
234   abort();
235 }
236
237
238 static int
239 feq (GLfloat a, GLfloat b)      /* Oh for fuck's sake */
240 {
241   const GLfloat e = 0.00001;
242   GLfloat d = a - b;
243   return (d > -e && d < e);
244 }
245
246
247 /* Link each triangle to its three neighbors.
248  */
249 static void
250 link_neighbors (ModeInfo *mi)
251 {
252   splodesic_configuration *bp = &bps[MI_SCREEN(mi)];
253   triangle *t0 = bp->triangles;
254   int i;
255
256   /* Triangles are neighbors if they share an edge (exactly 2 points).
257      (There must be a faster than N! way to do this...)
258    */
259   for (i = 0, t0 = bp->triangles; t0; t0 = t0->next, i++)
260     {
261       triangle *t1;
262       int j;
263
264       for (j = i+1, t1 = t0->next; t1; t1 = t1->next, j++)
265         {
266           int count = 0;
267           int ii, jj;
268           for (ii = 0; ii < 3; ii++)
269             for (jj = 0; jj < 3; jj++)
270               if (feq (t0->p[ii].x, t1->p[jj].x) &&
271                   feq (t0->p[ii].y, t1->p[jj].y) &&
272                   feq (t0->p[ii].z, t1->p[jj].z))
273                 count++;
274           if (count >= 3)
275             {
276               fprintf (stderr, "%d %d: too many matches: %d\n", i, j, count);
277               abort();
278             }
279           if (count == 2)
280             {
281               link_neighbor (i, j, t0, t1);
282               link_neighbor (j, i, t1, t0);
283             }
284         }
285
286       if (! (t0->neighbors[0] && t0->neighbors[1] && t0->neighbors[2]))
287         {
288           fprintf (stderr, "%d: missing neighbors\n", i);
289           abort();
290         }
291
292       t0->altitude = 60;  /* Fall in from space */
293     }
294 }
295
296
297 /* Add thrust to the triangle, and propagate some of that to its neighbors.
298  */
299 static void
300 add_thrust (triangle *t, GLfloat thrust)
301 {
302   GLfloat dampen = 0;
303   if (t->refcount)
304     return;
305   t->refcount++;
306   t->velocity += thrust;
307
308   /* Eyeballed this to look roughly the same at various depths. Eh. */
309   switch (depth_arg) {
310   case 0: dampen = 0.5;    break;
311   case 1: dampen = 0.7;    break;
312   case 2: dampen = 0.9;    break;
313   case 3: dampen = 0.98;   break;
314   case 4: dampen = 0.985;  break;
315   default: dampen = 0.993; break;
316   }
317
318   thrust *= dampen;
319   if (thrust > 0.1)
320     {
321       add_thrust (t->neighbors[0], thrust);
322       add_thrust (t->neighbors[1], thrust);
323       add_thrust (t->neighbors[2], thrust);
324     }
325 }
326
327
328 static void
329 tick_triangles (ModeInfo *mi)
330 {
331   splodesic_configuration *bp = &bps[MI_SCREEN(mi)];
332   GLfloat gravity = 0.1;
333   triangle *t;
334   int i;
335
336   /* Compute new velocities. */
337   for (i = 0, t = bp->triangles; t; t = t->next, i++)
338     {
339       if (t->thrust > 0)
340         {
341           add_thrust (t, t->thrust);
342           t->thrust_duration--;
343           if (t->thrust_duration <= 0)
344             {
345               t->thrust_duration = 0;
346               t->thrust = 0;
347             }
348         }
349     }
350
351   /* Apply new velocities. */
352   for (i = 0, t = bp->triangles; t; t = t->next, i++)
353     {
354       t->altitude += t->velocity;
355       t->velocity -= gravity;
356       if (t->altitude < 0)
357         {
358           t->velocity = 0;
359           t->altitude = 0;
360         }
361       t->refcount = 0;  /* Clear for next time */
362     }
363
364   /* Add eruptions. */
365   if (frand(1 / speed) < 0.2)
366     {
367       int n = random() % bp->count;
368       for (i = 0, t = bp->triangles; t; t = t->next, i++)
369         if (i == n)
370           break;
371       t->thrust += gravity * 1.5;
372       t->thrust_duration = 1 + BELLRAND(16);
373     }
374
375   bp->ccolor++;
376   if (bp->ccolor >= bp->ncolors)
377     bp->ccolor = 0;
378 }
379
380
381 static void
382 draw_triangles (ModeInfo *mi)
383 {
384   splodesic_configuration *bp = &bps[MI_SCREEN(mi)];
385   int wire = MI_IS_WIREFRAME(mi);
386   triangle *t;
387   GLfloat c[4];
388   int c0 = bp->ccolor;
389   int c1 = (c0 + bp->ncolors / 2) % bp->ncolors;
390
391   c[0] = bp->colors[c0].red    / 65536.0;
392   c[1] = bp->colors[c0].green  / 65536.0;
393   c[2] = bp->colors[c0].blue   / 65536.0;
394   c[3] = 1;
395
396   if (wire)
397     glColor4fv (c);
398   else
399     {
400       glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, c);
401
402       c[0] = bp->colors[c1].red    / 65536.0;
403       c[1] = bp->colors[c1].green  / 65536.0;
404       c[2] = bp->colors[c1].blue   / 65536.0;
405       c[3] = 1;
406       glMaterialfv (GL_BACK, GL_AMBIENT_AND_DIFFUSE, c);
407     }
408
409   glFrontFace (GL_CCW);
410   for (t = bp->triangles; t; t = t->next)
411     {
412       GLfloat a = t->altitude * 0.25;
413       XYZ c;
414       glPushMatrix();
415
416       c.x = t->p[0].x + t->p[1].x + t->p[2].x;
417       c.y = t->p[0].y + t->p[1].y + t->p[2].y;
418       c.z = t->p[0].z + t->p[1].z + t->p[2].z;
419       if (a > 0)
420         glTranslatef (a * c.x / 3, a * c.y / 3, a * c.z / 3);
421       glBegin (wire ? GL_LINE_LOOP : GL_TRIANGLES);
422       glNormal3f (c.x, c.y, c.z);
423       glVertex3f (t->p[0].x, t->p[0].y, t->p[0].z);
424       glVertex3f (t->p[1].x, t->p[1].y, t->p[1].z);
425       glVertex3f (t->p[2].x, t->p[2].y, t->p[2].z);
426       glEnd();
427       mi->polygon_count++;
428       glPopMatrix();
429     }
430 }
431
432
433 /* Window management, etc
434  */
435 ENTRYPOINT void
436 reshape_splodesic (ModeInfo *mi, int width, int height)
437 {
438   GLfloat h = (GLfloat) height / (GLfloat) width;
439
440   glViewport (0, 0, (GLint) width, (GLint) height);
441
442   glMatrixMode(GL_PROJECTION);
443   glLoadIdentity();
444   gluPerspective (30.0, 1/h, 1.0, 100.0);
445
446   glMatrixMode(GL_MODELVIEW);
447   glLoadIdentity();
448   gluLookAt( 0.0, 0.0, 30.0,
449              0.0, 0.0, 0.0,
450              0.0, 1.0, 0.0);
451
452 # ifdef HAVE_MOBILE     /* Keep it the same relative size when rotated. */
453   {
454     int o = (int) current_device_rotation();
455     if (o != 0 && o != 180 && o != -180)
456       glScalef (1/h, 1/h, 1/h);
457   }
458 # endif
459
460   glClear(GL_COLOR_BUFFER_BIT);
461 }
462
463
464 ENTRYPOINT Bool
465 splodesic_handle_event (ModeInfo *mi, XEvent *event)
466 {
467   splodesic_configuration *bp = &bps[MI_SCREEN(mi)];
468
469   if (gltrackball_event_handler (event, bp->trackball,
470                                  MI_WIDTH (mi), MI_HEIGHT (mi),
471                                  &bp->button_down_p))
472     return True;
473   else if (event->xany.type == KeyPress)
474     {
475       KeySym keysym;
476       char c = 0;
477       XLookupString (&event->xkey, &c, 1, &keysym, 0);
478       if (c == ' ' || c == '\t')
479         {
480           bp->ncolors = 1024;
481           make_smooth_colormap (0, 0, 0,
482                                 bp->colors, &bp->ncolors,
483                                 False, 0, False);
484           return True;
485         }
486     }
487
488   return False;
489 }
490
491
492 static void free_splodesic (ModeInfo *mi);
493
494 ENTRYPOINT void 
495 init_splodesic (ModeInfo *mi)
496 {
497   splodesic_configuration *bp;
498   int wire = MI_IS_WIREFRAME(mi);
499
500   MI_INIT (mi, bps, free_splodesic);
501
502   bp = &bps[MI_SCREEN(mi)];
503
504   bp->glx_context = init_GL(mi);
505
506   reshape_splodesic (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
507
508   if (!wire)
509     {
510       GLfloat pos[4] = {4.0, 1.4, 1.1, 0.0};
511       GLfloat amb[4] = {0.0, 0.0, 0.0, 1.0};
512       GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
513       GLfloat spc[4] = {1.0, 0.2, 0.2, 1.0};
514       GLfloat cspec[4] = {1, 1, 1, 1};
515       static const GLfloat shiny = 10;
516       int lightmodel = 1;
517
518       glEnable(GL_LIGHTING);
519       glEnable(GL_LIGHT0);
520       glEnable(GL_DEPTH_TEST);
521       glEnable(GL_CULL_FACE);
522
523       glLightfv(GL_LIGHT0, GL_POSITION, pos);
524       glLightfv(GL_LIGHT0, GL_AMBIENT,  amb);
525       glLightfv(GL_LIGHT0, GL_DIFFUSE,  dif);
526       glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
527       glLightModeliv (GL_LIGHT_MODEL_TWO_SIDE, &lightmodel);
528       glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, cspec);
529       glMateriali (GL_FRONT_AND_BACK, GL_SHININESS, shiny);
530     }
531
532   {
533     double spin_speed   = 0.5;
534     double wander_speed = 0.005;
535     double spin_accel   = 1.0;
536
537     bp->rot = make_rotator (do_spin ? spin_speed : 0,
538                             do_spin ? spin_speed : 0,
539                             do_spin ? spin_speed : 0,
540                             spin_accel,
541                             do_wander ? wander_speed : 0,
542                             True);
543     bp->trackball = gltrackball_init (True);
544   }
545
546   bp->ncolors = 1024;
547   bp->colors = (XColor *) calloc(bp->ncolors, sizeof(XColor));
548   make_smooth_colormap (0, 0, 0,
549                         bp->colors, &bp->ncolors,
550                         False, 0, False);
551
552 # ifdef HAVE_MOBILE
553   depth_arg--;
554 # endif
555
556   if (depth_arg < 0)  depth_arg = 0;
557   if (depth_arg > 10) depth_arg = 10;
558
559   make_geodesic (mi);
560   link_neighbors (mi);
561 }
562
563
564 ENTRYPOINT void
565 draw_splodesic (ModeInfo *mi)
566 {
567   splodesic_configuration *bp = &bps[MI_SCREEN(mi)];
568   Display *dpy = MI_DISPLAY(mi);
569   Window window = MI_WINDOW(mi);
570
571   if (!bp->glx_context)
572     return;
573
574   glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
575
576   glShadeModel(GL_SMOOTH);
577
578   glEnable(GL_DEPTH_TEST);
579   glEnable(GL_NORMALIZE);
580   glDisable(GL_CULL_FACE);
581
582   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
583
584   glPushMatrix ();
585
586   {
587     double x, y, z;
588     get_position (bp->rot, &x, &y, &z, !bp->button_down_p);
589     glTranslatef((x - 0.5) * 6,
590                  (y - 0.5) * 6,
591                  (z - 0.5) * 8);
592
593     gltrackball_rotate (bp->trackball);
594
595     get_rotation (bp->rot, &x, &y, &z, !bp->button_down_p);
596     glRotatef (x * 360, 1.0, 0.0, 0.0);
597     glRotatef (y * 360, 0.0, 1.0, 0.0);
598     glRotatef (z * 360, 0.0, 0.0, 1.0);
599   }
600
601   mi->polygon_count = 0;
602
603 # ifdef HAVE_MOBILE
604   glScalef (3, 3, 3);
605 #else
606   glScalef (4, 4, 4);
607 # endif
608
609   if (! bp->button_down_p)
610     tick_triangles (mi);
611   draw_triangles (mi);
612
613   glPopMatrix ();
614
615   if (mi->fps_p) do_fps (mi);
616   glFinish();
617
618   glXSwapBuffers(dpy, window);
619 }
620
621
622 static void
623 free_splodesic (ModeInfo *mi)
624 {
625   splodesic_configuration *bp = &bps[MI_SCREEN(mi)];
626   while (bp->triangles)
627     {
628       triangle *t = bp->triangles->next;
629       free (bp->triangles);
630       bp->triangles = t;
631     }
632 }
633
634 XSCREENSAVER_MODULE ("Splodesic", splodesic)
635
636 #endif /* USE_GL */