1 /* voronoi, Copyright (c) 2007, 2008 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: 20000 \n" \
13 "*showFPS: False \n" \
16 # define refresh_voronoi 0
17 # define release_voronoi 0
19 #define countof(x) (sizeof((x))/sizeof((*x)))
21 #define BELLRAND(n) ((frand((n)) + frand((n)) + frand((n))) / 3)
24 #include "xlockmore.h"
27 #ifdef USE_GL /* whole file */
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"
41 GLfloat color[4], color2[4];
46 GLXContext *glx_context;
53 enum { MODE_WAITING, MODE_ADDING, MODE_ZOOMING } mode;
57 GLfloat zooming; /* 1.0 starting zoom, 0.0 no longer zooming. */
58 GLfloat zoom_toward[2];
60 } voronoi_configuration;
62 static voronoi_configuration *vps = NULL;
64 /* command line arguments */
66 static GLfloat point_size, point_speed, point_delay;
67 static GLfloat zoom_speed, zoom_delay;
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 },
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},
87 ENTRYPOINT ModeSpecOpt voronoi_opts =
88 {countof(opts), opts, countof(vars), vars, NULL};
91 /* Returns the current time in seconds as a double.
97 # ifdef GETTIMEOFDAY_TWO_ARGS
99 gettimeofday(&now, &tzp);
104 return (now.tv_sec + ((double) now.tv_usec * 0.000001));
109 add_node (voronoi_configuration *vp, GLfloat x, GLfloat y)
111 node *nn = (node *) calloc (1, sizeof (*nn));
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;
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;
127 nn->ddx = frand (0.000001 * point_speed) * (random() & 1 ? 1 : -1);
128 nn->ddy = frand (0.000001 * point_speed) * (random() & 1 ? 1 : -1);
130 nn->rot = (random() % 360) * (random() & 1 ? 1 : -1);
132 nn->next = vp->nodes;
144 GLfloat step = M_PI * 2 / faces;
149 glBegin(GL_TRIANGLE_FAN);
150 glVertex3f (0, 0, 1);
151 for (i = 0; i < faces; i++)
153 glVertex3f (x, y, 0);
158 glVertex3f (1, 0, 0);
165 move_points (voronoi_configuration *vp)
168 for (nn = vp->nodes; nn; nn = nn->next)
170 if (nn == vp->dragging) continue;
174 if (vp->mode == MODE_WAITING)
184 prune_points (voronoi_configuration *vp)
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)
195 prev->next = nn->next;
197 vp->nodes = nn->next;
206 zoom_points (voronoi_configuration *vp)
210 GLfloat tick = sin (vp->zooming * M_PI);
211 GLfloat scale = 1 + (tick * 0.02 * zoom_speed);
213 vp->zooming -= (0.01 * zoom_speed);
214 if (vp->zooming < 0) vp->zooming = 0;
216 if (vp->zooming <= 0) return;
218 if (scale < 1) scale = 1;
220 for (nn = vp->nodes; nn; nn = nn->next)
222 GLfloat x = nn->x - vp->zoom_toward[0];
223 GLfloat y = nn->y - vp->zoom_toward[1];
226 nn->x = x + vp->zoom_toward[0];
227 nn->y = y + vp->zoom_toward[1];
234 draw_cells (ModeInfo *mi)
236 voronoi_configuration *vp = &vps[MI_SCREEN(mi)];
240 for (nn = vp->nodes; nn; nn = nn->next)
242 if (nn->x < -lim || nn->x > lim ||
243 nn->y < -lim || nn->y > lim)
247 glTranslatef (nn->x, nn->y, 0);
248 glScalef (lim*2, lim*2, 1);
249 glColor4fv (nn->color);
250 mi->polygon_count += cone ();
254 glClear (GL_DEPTH_BUFFER_BIT);
258 else if (point_size < 3)
260 glPointSize (point_size);
261 for (nn = vp->nodes; nn; nn = nn->next)
264 glColor4fv (nn->color2);
265 glVertex2f (nn->x, nn->y);
272 for (nn = vp->nodes; nn; nn = nn->next)
274 int w = MI_WIDTH (mi);
275 int h = MI_HEIGHT (mi);
279 glColor4fv (nn->color2);
281 glTranslatef (nn->x, nn->y, 0);
282 glScalef (1.0 / w * s, 1.0 / h * s, 1);
284 glLineWidth (point_size / 10);
285 nn->rot += (nn->rot < 0 ? -1 : 1);
286 glRotatef (nn->rot, 0, 0, 1);
288 glRotatef (180, 0, 0, 1);
289 for (i = 0; i < 5; i++)
291 glBegin (GL_TRIANGLES);
293 glVertex2f (-0.2, 0);
294 glVertex2f ( 0.2, 0);
296 glRotatef (360.0/5, 0, 0, 1);
305 /* Window management, etc
308 reshape_voronoi (ModeInfo *mi, int width, int height)
310 /* voronoi_configuration *vp = &vps[MI_SCREEN(mi)];*/
312 glViewport (0, 0, (GLint) width, (GLint) height);
314 glMatrixMode(GL_PROJECTION);
316 glOrtho (0, 1, 1, 0, -1, 1);
318 glMatrixMode(GL_MODELVIEW);
321 glClear(GL_COLOR_BUFFER_BIT);
326 find_node (ModeInfo *mi, GLfloat x, GLfloat y)
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;
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)
341 voronoi_handle_event (ModeInfo *mi, XEvent *event)
343 voronoi_configuration *vp = &vps[MI_SCREEN(mi)];
345 if (event->xany.type == ButtonPress)
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);
351 nn = add_node (vp, x, y);
356 else if (event->xany.type == ButtonRelease && vp->dragging)
361 else if (event->xany.type == MotionNotify && vp->dragging)
363 vp->dragging->x = (GLfloat) event->xmotion.x / MI_WIDTH (mi);
364 vp->dragging->y = (GLfloat) event->xmotion.y / MI_HEIGHT (mi);
372 state_change (ModeInfo *mi)
374 voronoi_configuration *vp = &vps[MI_SCREEN(mi)];
375 double now = double_time();
388 if (vp->last_time + zoom_delay <= now)
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);
394 vp->mode = MODE_ZOOMING;
402 if (vp->last_time + point_delay <= now)
405 BELLRAND(0.5) + 0.25,
406 BELLRAND(0.5) + 0.25);
412 vp->mode = MODE_WAITING;
421 if (vp->zooming <= 0)
423 vp->mode = MODE_ADDING;
424 vp->adding = npoints;
437 init_voronoi (ModeInfo *mi)
439 voronoi_configuration *vp;
442 vps = (voronoi_configuration *)
443 calloc (MI_NUM_SCREENS(mi), sizeof (voronoi_configuration));
445 fprintf(stderr, "%s: out of memory\n", progname);
450 vp = &vps[MI_SCREEN(mi)];
452 vp->glx_context = init_GL(mi);
454 if (point_size < 0) point_size = 10;
457 vp->colors = (XColor *) calloc (vp->ncolors, sizeof(XColor));
458 make_smooth_colormap (0, 0, 0,
459 vp->colors, &vp->ncolors,
460 False, False, False);
462 reshape_voronoi (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
464 vp->mode = MODE_ADDING;
465 vp->adding = npoints * 2;
471 draw_voronoi (ModeInfo *mi)
473 voronoi_configuration *vp = &vps[MI_SCREEN(mi)];
474 Display *dpy = MI_DISPLAY(mi);
475 Window window = MI_WINDOW(mi);
477 if (!vp->glx_context)
480 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(vp->glx_context));
482 glShadeModel(GL_FLAT);
483 glEnable(GL_POINT_SMOOTH);
484 /* glEnable(GL_LINE_SMOOTH);*/
485 /* glEnable(GL_POLYGON_SMOOTH);*/
487 glEnable (GL_DEPTH_TEST);
488 glDepthFunc (GL_LEQUAL);
490 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
492 mi->polygon_count = 0;
498 if (mi->fps_p) do_fps (mi);
501 glXSwapBuffers(dpy, window);
504 XSCREENSAVER_MODULE ("Voronoi", voronoi)