1 /* voronoi, Copyright (c) 2007 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 "10"
30 #define DEF_POINT_SIZE "9"
31 #define DEF_POINT_SPEED "1"
32 #define DEF_POINT_DELAY "0.1"
33 #define DEF_ZOOM_SPEED "1"
34 #define DEF_ZOOM_DELAY "10"
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;
147 GLfloat x, y, x0, y0;
149 glBegin(GL_TRIANGLES);
157 for (i = 0; i < faces; i++)
176 move_points (voronoi_configuration *vp)
179 for (nn = vp->nodes; nn; nn = nn->next)
181 if (nn == vp->dragging) continue;
185 if (vp->mode == MODE_WAITING)
195 prune_points (voronoi_configuration *vp)
201 for (nn = vp->nodes; nn; prev = nn, nn = (nn ? nn->next : 0))
202 if (nn->x < -lim || nn->x > lim ||
203 nn->y < -lim || nn->y > lim)
206 prev->next = nn->next;
208 vp->nodes = nn->next;
217 zoom_points (voronoi_configuration *vp)
221 GLfloat tick = sin (vp->zooming * M_PI);
222 GLfloat scale = 1 + (tick * 0.02 * zoom_speed);
224 vp->zooming -= (0.01 * zoom_speed);
225 if (vp->zooming < 0) vp->zooming = 0;
227 if (vp->zooming <= 0) return;
229 if (scale < 1) scale = 1;
231 for (nn = vp->nodes; nn; nn = nn->next)
233 GLfloat x = nn->x - vp->zoom_toward[0];
234 GLfloat y = nn->y - vp->zoom_toward[1];
237 nn->x = x + vp->zoom_toward[0];
238 nn->y = y + vp->zoom_toward[1];
245 draw_cells (ModeInfo *mi)
247 voronoi_configuration *vp = &vps[MI_SCREEN(mi)];
251 for (nn = vp->nodes; nn; nn = nn->next)
253 if (nn->x < -lim || nn->x > lim ||
254 nn->y < -lim || nn->y > lim)
258 glTranslatef (nn->x, nn->y, 0);
259 glScalef (lim*2, lim*2, 1);
260 glColor4fv (nn->color);
261 mi->polygon_count += cone ();
265 glClear (GL_DEPTH_BUFFER_BIT);
269 else if (point_size < 3)
271 glPointSize (point_size);
272 for (nn = vp->nodes; nn; nn = nn->next)
275 glColor4fv (nn->color2);
276 glVertex2f (nn->x, nn->y);
282 for (nn = vp->nodes; nn; nn = nn->next)
284 int w = MI_WIDTH (mi);
285 int h = MI_HEIGHT (mi);
289 glColor4fv (nn->color2);
291 glTranslatef (nn->x, nn->y, 0);
292 glScalef (1.0 / w * s, 1.0 / h * s, 1);
294 glLineWidth (point_size / 10);
295 nn->rot += (nn->rot < 0 ? -1 : 1);
296 glRotatef (nn->rot, 0, 0, 1);
298 glRotatef (180, 0, 0, 1);
299 for (i = 0; i < 5; i++)
301 glBegin (GL_TRIANGLES);
303 glVertex2f (-0.2, 0);
304 glVertex2f ( 0.2, 0);
306 glRotatef (360.0/5, 0, 0, 1);
314 /* Window management, etc
317 reshape_voronoi (ModeInfo *mi, int width, int height)
319 /* voronoi_configuration *vp = &vps[MI_SCREEN(mi)];*/
321 glViewport (0, 0, (GLint) width, (GLint) height);
323 glMatrixMode(GL_PROJECTION);
325 glOrtho (0, 1, 1, 0, -1, 1);
327 glMatrixMode(GL_MODELVIEW);
330 glClear(GL_COLOR_BUFFER_BIT);
335 find_node (ModeInfo *mi, GLfloat x, GLfloat y)
337 voronoi_configuration *vp = &vps[MI_SCREEN(mi)];
338 int ps = (point_size < 5 ? 5 : point_size);
339 GLfloat hysteresis = (1.0 / MI_WIDTH (mi)) * ps;
341 for (nn = vp->nodes; nn; nn = nn->next)
342 if (nn->x > x - hysteresis && nn->x < x + hysteresis &&
343 nn->y > y - hysteresis && nn->y < y + hysteresis)
350 voronoi_handle_event (ModeInfo *mi, XEvent *event)
352 voronoi_configuration *vp = &vps[MI_SCREEN(mi)];
354 if (event->xany.type == ButtonPress)
356 GLfloat x = (GLfloat) event->xbutton.x / MI_WIDTH (mi);
357 GLfloat y = (GLfloat) event->xbutton.y / MI_HEIGHT (mi);
358 node *nn = find_node (mi, x, y);
360 nn = add_node (vp, x, y);
365 else if (event->xany.type == ButtonRelease && vp->dragging)
370 else if (event->xany.type == MotionNotify && vp->dragging)
372 vp->dragging->x = (GLfloat) event->xmotion.x / MI_WIDTH (mi);
373 vp->dragging->y = (GLfloat) event->xmotion.y / MI_HEIGHT (mi);
381 state_change (ModeInfo *mi)
383 voronoi_configuration *vp = &vps[MI_SCREEN(mi)];
384 double now = double_time();
397 if (vp->last_time + zoom_delay <= now)
399 node *tn = vp->nodes;
400 vp->zoom_toward[0] = (tn ? tn->x : 0.5);
401 vp->zoom_toward[1] = (tn ? tn->y : 0.5);
403 vp->mode = MODE_ZOOMING;
411 if (vp->last_time + point_delay <= now)
414 BELLRAND(0.5) + 0.25,
415 BELLRAND(0.5) + 0.25);
421 vp->mode = MODE_WAITING;
430 if (vp->zooming <= 0)
432 vp->mode = MODE_ADDING;
433 vp->adding = npoints;
446 init_voronoi (ModeInfo *mi)
448 voronoi_configuration *vp;
451 vps = (voronoi_configuration *)
452 calloc (MI_NUM_SCREENS(mi), sizeof (voronoi_configuration));
454 fprintf(stderr, "%s: out of memory\n", progname);
458 vp = &vps[MI_SCREEN(mi)];
461 vp = &vps[MI_SCREEN(mi)];
463 vp->glx_context = init_GL(mi);
465 if (point_size < 0) point_size = 10;
468 vp->colors = (XColor *) calloc (vp->ncolors, sizeof(XColor));
470 make_random_colormap (0, 0, 0,
471 vp->colors, &vp->ncolors,
472 True, False, 0, False);
474 make_smooth_colormap (0, 0, 0,
475 vp->colors, &vp->ncolors,
476 False, False, False);
479 reshape_voronoi (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
481 vp->mode = MODE_ADDING;
482 vp->adding = npoints * 2;
488 draw_voronoi (ModeInfo *mi)
490 voronoi_configuration *vp = &vps[MI_SCREEN(mi)];
491 Display *dpy = MI_DISPLAY(mi);
492 Window window = MI_WINDOW(mi);
494 if (!vp->glx_context)
497 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(vp->glx_context));
499 glShadeModel(GL_SMOOTH);
500 glEnable(GL_POINT_SMOOTH);
501 glEnable(GL_LINE_SMOOTH);
502 glEnable(GL_POLYGON_SMOOTH);
504 glEnable (GL_DEPTH_TEST);
505 glDepthFunc (GL_LEQUAL);
507 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
509 mi->polygon_count = 0;
515 if (mi->fps_p) do_fps (mi);
518 glXSwapBuffers(dpy, window);
521 XSCREENSAVER_MODULE ("Voronoi", voronoi)