http://www.jwz.org/xscreensaver/xscreensaver-5.09.tar.gz
[xscreensaver] / hacks / glx / voronoi.c
1 /* voronoi, Copyright (c) 2007, 2008 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:        20000              \n" \
13                         "*showFPS:      False              \n" \
14
15
16 # define refresh_voronoi 0
17 # define release_voronoi 0
18 #undef countof
19 #define countof(x) (sizeof((x))/sizeof((*x)))
20
21 #define BELLRAND(n) ((frand((n)) + frand((n)) + frand((n))) / 3)
22
23
24 #include "xlockmore.h"
25 #include <ctype.h>
26
27 #ifdef USE_GL /* whole file */
28
29 #define DEF_POINTS      "25"
30 #define DEF_POINT_SIZE  "9"
31 #define DEF_POINT_SPEED "1.0"
32 #define DEF_POINT_DELAY "0.05"
33 #define DEF_ZOOM_SPEED  "1.0"
34 #define DEF_ZOOM_DELAY  "15"
35
36 typedef struct node {
37   GLfloat x, y;
38   GLfloat dx, dy;
39   GLfloat ddx, ddy;
40   struct node *next;
41   GLfloat color[4], color2[4];
42   int rot;
43 } node;
44
45 typedef struct {
46   GLXContext *glx_context;
47   node *nodes;
48   int nnodes;
49   node *dragging;
50   int ncolors;
51   XColor *colors;
52
53   enum { MODE_WAITING, MODE_ADDING, MODE_ZOOMING } mode;
54   int adding;
55   double last_time;
56
57   GLfloat zooming;         /* 1.0 starting zoom, 0.0 no longer zooming. */
58   GLfloat zoom_toward[2];
59
60 } voronoi_configuration;
61
62 static voronoi_configuration *vps = NULL;
63
64 /* command line arguments */
65 static int npoints;
66 static GLfloat point_size, point_speed, point_delay;
67 static GLfloat zoom_speed, zoom_delay;
68
69 static XrmOptionDescRec opts[] = {
70   { "-points",       ".points",      XrmoptionSepArg, 0 },
71   { "-point-size",   ".pointSize",   XrmoptionSepArg, 0 },
72   { "-point-speed",  ".pointSpeed",  XrmoptionSepArg, 0 },
73   { "-point-delay",  ".pointDelay",  XrmoptionSepArg, 0 },
74   { "-zoom-speed",   ".zoomSpeed",   XrmoptionSepArg, 0 },
75   { "-zoom-delay",   ".zoomDelay",   XrmoptionSepArg, 0 },
76 };
77
78 static argtype vars[] = {
79   {&npoints,      "points",      "Points",      DEF_POINTS,       t_Int},
80   {&point_size,   "pointSize",   "PointSize",   DEF_POINT_SIZE,   t_Float},
81   {&point_speed,  "pointSpeed",  "PointSpeed",  DEF_POINT_SPEED,  t_Float},
82   {&point_delay,  "pointDelay",  "PointDelay",  DEF_POINT_DELAY,  t_Float},
83   {&zoom_speed,   "zoomSpeed",   "ZoomSpeed",   DEF_ZOOM_SPEED,   t_Float},
84   {&zoom_delay,   "zoomDelay",   "ZoomDelay",   DEF_ZOOM_DELAY,   t_Float},
85 };
86
87 ENTRYPOINT ModeSpecOpt voronoi_opts =
88   {countof(opts), opts, countof(vars), vars, NULL};
89
90
91 /* Returns the current time in seconds as a double.
92  */
93 static double
94 double_time (void)
95 {
96   struct timeval now;
97 # ifdef GETTIMEOFDAY_TWO_ARGS
98   struct timezone tzp;
99   gettimeofday(&now, &tzp);
100 # else
101   gettimeofday(&now);
102 # endif
103
104   return (now.tv_sec + ((double) now.tv_usec * 0.000001));
105 }
106
107
108 static node *
109 add_node (voronoi_configuration *vp, GLfloat x, GLfloat y)
110 {
111   node *nn = (node *) calloc (1, sizeof (*nn));
112   int i;
113   nn->x = x;
114   nn->y = y;
115
116   i = random() % vp->ncolors;
117   nn->color[0] = vp->colors[i].red   / 65536.0;
118   nn->color[1] = vp->colors[i].green / 65536.0;
119   nn->color[2] = vp->colors[i].blue  / 65536.0;
120   nn->color[3] = 1.0;
121
122   nn->color2[0] = nn->color[0] * 0.7;
123   nn->color2[1] = nn->color[1] * 0.7;
124   nn->color2[2] = nn->color[2] * 0.7;
125   nn->color2[3] = 1.0;
126
127   nn->ddx = frand (0.000001 * point_speed) * (random() & 1 ? 1 : -1);
128   nn->ddy = frand (0.000001 * point_speed) * (random() & 1 ? 1 : -1);
129
130   nn->rot = (random() % 360) * (random() & 1 ? 1 : -1);
131
132   nn->next = vp->nodes;
133   vp->nodes = nn;
134   vp->nnodes++;
135   return nn;
136 }
137
138
139 static int
140 cone (void)
141 {
142   int i;
143   int faces = 64;
144   GLfloat step = M_PI * 2 / faces;
145   GLfloat th = 0;
146   GLfloat x = 1;
147   GLfloat y = 0;
148
149   glBegin(GL_TRIANGLE_FAN);
150   glVertex3f (0, 0, 1);
151   for (i = 0; i < faces; i++)
152     {
153       glVertex3f (x, y, 0);
154       th += step;
155       x = cos (th);
156       y = sin (th);
157     }
158   glVertex3f (1, 0, 0);
159   glEnd();
160   return faces;
161 }
162
163
164 static void
165 move_points (voronoi_configuration *vp)
166 {
167   node *nn;
168   for (nn = vp->nodes; nn; nn = nn->next)
169     {
170       if (nn == vp->dragging) continue;
171       nn->x  += nn->dx;
172       nn->y  += nn->dy;
173
174       if (vp->mode == MODE_WAITING)
175         {
176           nn->dx += nn->ddx;
177           nn->dy += nn->ddy;
178         }
179     }
180 }
181
182
183 static void
184 prune_points (voronoi_configuration *vp)
185 {
186   node *nn;
187   node *prev = 0;
188   int lim = 5;
189
190   for (nn = vp->nodes; nn; prev = nn, nn = (nn ? nn->next : 0))
191     if (nn->x < -lim || nn->x > lim ||
192         nn->y < -lim || nn->y > lim)
193       {
194         if (prev)
195           prev->next = nn->next;
196         else
197           vp->nodes = nn->next;
198         free (nn);
199         vp->nnodes--;
200         nn = prev;
201      }
202 }
203
204
205 static void
206 zoom_points (voronoi_configuration *vp)
207 {
208   node *nn;
209
210   GLfloat tick = sin (vp->zooming * M_PI);
211   GLfloat scale = 1 + (tick * 0.02 * zoom_speed);
212
213   vp->zooming -= (0.01 * zoom_speed);
214   if (vp->zooming < 0) vp->zooming = 0;
215
216   if (vp->zooming <= 0) return;
217
218   if (scale < 1) scale = 1;
219
220   for (nn = vp->nodes; nn; nn = nn->next)
221     {
222       GLfloat x = nn->x - vp->zoom_toward[0];
223       GLfloat y = nn->y - vp->zoom_toward[1];
224       x *= scale;
225       y *= scale;
226       nn->x = x + vp->zoom_toward[0];
227       nn->y = y + vp->zoom_toward[1];
228     }
229 }
230
231
232
233 static void
234 draw_cells (ModeInfo *mi)
235 {
236   voronoi_configuration *vp = &vps[MI_SCREEN(mi)];
237   node *nn;
238   int lim = 5;
239
240   for (nn = vp->nodes; nn; nn = nn->next)
241     {
242       if (nn->x < -lim || nn->x > lim ||
243           nn->y < -lim || nn->y > lim)
244         continue;
245
246       glPushMatrix();
247       glTranslatef (nn->x, nn->y, 0);
248       glScalef (lim*2, lim*2, 1);
249       glColor4fv (nn->color);
250       mi->polygon_count += cone ();
251       glPopMatrix();
252     }
253
254   glClear (GL_DEPTH_BUFFER_BIT);
255
256   if (point_size <= 0)
257     ;
258   else if (point_size < 3)
259     {
260       glPointSize (point_size);
261       for (nn = vp->nodes; nn; nn = nn->next)
262         {
263           glBegin (GL_POINTS);
264           glColor4fv (nn->color2);
265           glVertex2f (nn->x, nn->y);
266           glEnd();
267           mi->polygon_count++;
268         }
269     }
270   else
271     {
272       for (nn = vp->nodes; nn; nn = nn->next)
273         {
274           int w = MI_WIDTH (mi);
275           int h = MI_HEIGHT (mi);
276           int s = point_size;
277           int i;
278
279           glColor4fv (nn->color2);
280           glPushMatrix();
281           glTranslatef (nn->x, nn->y, 0);
282           glScalef (1.0 / w * s, 1.0 / h * s, 1);
283
284           glLineWidth (point_size / 10);
285           nn->rot += (nn->rot < 0 ? -1 : 1);
286           glRotatef (nn->rot, 0, 0, 1);
287
288           glRotatef (180, 0, 0, 1);
289           for (i = 0; i < 5; i++)
290             {
291               glBegin (GL_TRIANGLES);
292               glVertex2f (0, 1);
293               glVertex2f (-0.2, 0);
294               glVertex2f ( 0.2, 0);
295               glEnd ();
296               glRotatef (360.0/5, 0, 0, 1);
297               mi->polygon_count++;
298             }
299           glPopMatrix();
300         }
301     }
302 }
303
304
305 /* Window management, etc
306  */
307 ENTRYPOINT void
308 reshape_voronoi (ModeInfo *mi, int width, int height)
309 {
310 /*  voronoi_configuration *vp = &vps[MI_SCREEN(mi)];*/
311
312   glViewport (0, 0, (GLint) width, (GLint) height);
313
314   glMatrixMode(GL_PROJECTION);
315   glLoadIdentity();
316   glOrtho (0, 1, 1, 0, -1, 1);
317
318   glMatrixMode(GL_MODELVIEW);
319   glLoadIdentity();
320
321   glClear(GL_COLOR_BUFFER_BIT);
322 }
323
324
325 static node *
326 find_node (ModeInfo *mi, GLfloat x, GLfloat y)
327 {
328   voronoi_configuration *vp = &vps[MI_SCREEN(mi)];
329   int ps = (point_size < 5 ? 5 : point_size);
330   GLfloat hysteresis = (1.0 / MI_WIDTH (mi)) * ps;
331   node *nn;
332   for (nn = vp->nodes; nn; nn = nn->next)
333     if (nn->x > x - hysteresis && nn->x < x + hysteresis &&
334         nn->y > y - hysteresis && nn->y < y + hysteresis)
335       return nn;
336   return 0;
337 }
338
339
340 ENTRYPOINT Bool
341 voronoi_handle_event (ModeInfo *mi, XEvent *event)
342 {
343   voronoi_configuration *vp = &vps[MI_SCREEN(mi)];
344
345   if (event->xany.type == ButtonPress)
346     {
347       GLfloat x = (GLfloat) event->xbutton.x / MI_WIDTH (mi);
348       GLfloat y = (GLfloat) event->xbutton.y / MI_HEIGHT (mi);
349       node *nn = find_node (mi, x, y);
350       if (!nn)
351         nn = add_node (vp, x, y);
352       vp->dragging = nn;
353
354       return True;
355     }
356   else if (event->xany.type == ButtonRelease && vp->dragging)
357     {
358       vp->dragging = 0;
359       return True;
360     }
361   else if (event->xany.type == MotionNotify && vp->dragging)
362     {
363       vp->dragging->x = (GLfloat) event->xmotion.x / MI_WIDTH (mi);
364       vp->dragging->y = (GLfloat) event->xmotion.y / MI_HEIGHT (mi);
365       return True;
366     }
367
368   return False;
369 }
370
371 static void
372 state_change (ModeInfo *mi)
373 {
374   voronoi_configuration *vp = &vps[MI_SCREEN(mi)];
375   double now = double_time();
376
377   if (vp->dragging)
378     {
379       vp->last_time = now;
380       vp->adding = 0;
381       vp->zooming = 0;
382       return;
383     }
384
385   switch (vp->mode)
386     {
387     case MODE_WAITING:
388       if (vp->last_time + zoom_delay <= now)
389         {
390           node *tn = vp->nodes;
391           vp->zoom_toward[0] = (tn ? tn->x : 0.5);
392           vp->zoom_toward[1] = (tn ? tn->y : 0.5);
393
394           vp->mode = MODE_ZOOMING;
395           vp->zooming = 1;
396
397           vp->last_time = now;
398         }
399       break;
400
401     case MODE_ADDING:
402       if (vp->last_time + point_delay <= now)
403         {
404           add_node (vp, 
405                     BELLRAND(0.5) + 0.25, 
406                     BELLRAND(0.5) + 0.25);
407           vp->last_time = now;
408           vp->adding--;
409           if (vp->adding <= 0)
410             {
411               vp->adding = 0;
412               vp->mode = MODE_WAITING;
413               vp->last_time = now;
414             }
415         }
416       break;
417
418     case MODE_ZOOMING:
419       {
420         zoom_points (vp);
421         if (vp->zooming <= 0)
422           {
423             vp->mode = MODE_ADDING;
424             vp->adding = npoints;
425             vp->last_time = now;
426           }
427       }
428       break;
429
430     default:
431       abort();
432     }
433 }
434
435
436 ENTRYPOINT void 
437 init_voronoi (ModeInfo *mi)
438 {
439   voronoi_configuration *vp;
440
441   if (!vps) {
442     vps = (voronoi_configuration *)
443       calloc (MI_NUM_SCREENS(mi), sizeof (voronoi_configuration));
444     if (!vps) {
445       fprintf(stderr, "%s: out of memory\n", progname);
446       exit(1);
447     }
448
449     vp = &vps[MI_SCREEN(mi)];
450   }
451
452   vp = &vps[MI_SCREEN(mi)];
453
454   vp->glx_context = init_GL(mi);
455
456   if (point_size < 0) point_size = 10;
457
458   vp->ncolors = 128;
459   vp->colors = (XColor *) calloc (vp->ncolors, sizeof(XColor));
460   make_smooth_colormap (0, 0, 0,
461                         vp->colors, &vp->ncolors,
462                         False, False, False);
463
464   reshape_voronoi (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
465
466   vp->mode = MODE_ADDING;
467   vp->adding = npoints * 2;
468   vp->last_time = 0;
469 }
470
471
472 ENTRYPOINT void
473 draw_voronoi (ModeInfo *mi)
474 {
475   voronoi_configuration *vp = &vps[MI_SCREEN(mi)];
476   Display *dpy = MI_DISPLAY(mi);
477   Window window = MI_WINDOW(mi);
478
479   if (!vp->glx_context)
480     return;
481
482   glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(vp->glx_context));
483
484   glShadeModel(GL_FLAT);
485   glEnable(GL_POINT_SMOOTH);
486 /*  glEnable(GL_LINE_SMOOTH);*/
487 /*  glEnable(GL_POLYGON_SMOOTH);*/
488
489   glEnable (GL_DEPTH_TEST);
490   glDepthFunc (GL_LEQUAL);
491
492   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
493
494   mi->polygon_count = 0;
495   draw_cells (mi);
496   move_points (vp);
497   prune_points (vp);
498   state_change (mi);
499
500   if (mi->fps_p) do_fps (mi);
501   glFinish();
502
503   glXSwapBuffers(dpy, window);
504 }
505
506 XSCREENSAVER_MODULE ("Voronoi", voronoi)
507
508 #endif /* USE_GL */