1 /* cityflow, Copyright (c) 2014 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" \
14 "*showFPS: False \n" \
15 "*wireframe: False \n" \
17 # define refresh_cube 0
18 # define release_cube 0
20 #define countof(x) (sizeof((x))/sizeof((*x)))
22 #include "xlockmore.h"
24 #include "gltrackball.h"
27 #ifdef USE_GL /* whole file */
33 #define DEF_WAVE_SPEED "25"
34 #define DEF_WAVE_RADIUS "256"
35 static int texture_size = 512;
58 GLXContext *glx_context;
59 trackball_state *trackball;
66 GLfloat min_x, max_x, min_y, max_y;
67 int texture_width, texture_height;
73 static cube_configuration *ccs = NULL;
75 static int wave_count;
76 static int wave_speed;
77 static int wave_radius;
80 static XrmOptionDescRec opts[] = {
81 {"-waves", ".waves", XrmoptionSepArg, 0 },
82 {"-wave-speed", ".waveSpeed", XrmoptionSepArg, 0 },
83 {"-wave-radius", ".waveRadius", XrmoptionSepArg, 0 },
84 {"-skew", ".skew", XrmoptionSepArg, 0 },
87 static argtype vars[] = {
88 {&wave_count, "waves", "Waves", DEF_WAVES, t_Int},
89 {&wave_speed, "waveSpeed", "WaveSpeed", DEF_WAVE_SPEED, t_Int},
90 {&wave_radius,"waveRadius","WaveRadius", DEF_WAVE_RADIUS,t_Int},
91 {&skew, "skew", "Skew", DEF_SKEW,t_Int},
94 ENTRYPOINT ModeSpecOpt cube_opts = {
95 countof(opts), opts, countof(vars), vars, NULL};
99 reshape_cube (ModeInfo *mi, int width, int height)
101 GLfloat h = (GLfloat) height / (GLfloat) width;
103 glViewport (0, 0, (GLint) width, (GLint) height);
105 glMatrixMode(GL_PROJECTION);
108 /* For this one it's really important to minimize the distance between
110 gluPerspective (30, 1/h, 10, 50);
111 glMatrixMode(GL_MODELVIEW);
113 gluLookAt( 0.0, 0.0, 30.0,
117 glClear(GL_COLOR_BUFFER_BIT);
122 reset_colors (ModeInfo *mi)
124 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
125 make_smooth_colormap (0, 0, 0,
126 cc->colors, &cc->ncolors,
128 if (! MI_IS_WIREFRAME(mi))
129 glClearColor (cc->colors[0].red / 65536.0,
130 cc->colors[0].green / 65536.0,
131 cc->colors[0].blue / 65536.0,
137 tweak_cubes (ModeInfo *mi)
139 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
141 for (i = 0; i < cc->ncubes; i++)
143 cube *cube = &cc->cubes[i];
144 cube->x += (frand(2)-1)*0.01;
145 cube->y += (frand(2)-1)*0.01;
146 cube->z += (frand(2)-1)*0.01;
152 cube_handle_event (ModeInfo *mi, XEvent *event)
154 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
156 /* Neutralize any vertical motion */
157 GLfloat rot = current_device_rotation();
158 Bool rotp = ((rot > 45 && rot < 135) ||
159 (rot < -45 && rot > -135));
161 if (event->xany.type == ButtonPress ||
162 event->xany.type == ButtonRelease)
165 event->xbutton.x = MI_WIDTH(mi) / 2;
167 event->xbutton.y = MI_HEIGHT(mi) / 2;
169 else if (event->xany.type == MotionNotify)
172 event->xmotion.x = MI_WIDTH(mi) / 2;
174 event->xmotion.y = MI_HEIGHT(mi) / 2;
177 if (gltrackball_event_handler (event, cc->trackball,
178 MI_WIDTH (mi), MI_HEIGHT (mi),
181 else if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
185 gltrackball_reset (cc->trackball);
194 Adapted from ../hacks/interference.c by Hannu Mallat.
198 init_wave (ModeInfo *mi)
200 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
203 cc->waves = ww = (waves *) calloc (sizeof(*cc->waves), 1);
204 ww->nwaves = wave_count;
205 ww->radius = wave_radius;
206 ww->speed = wave_speed;
207 ww->heights = (int *) calloc (sizeof(*ww->heights), ww->radius);
208 ww->srcs = (wave_src *) calloc (sizeof(*ww->srcs), ww->nwaves);
210 for (i = 0; i < ww->radius; i++)
212 float max = (cc->ncolors * (ww->radius - i) / (float) ww->radius);
213 ww->heights[i] = ((max + max * cos(i / 50.0)) / 2.0);
216 for (i = 0; i < ww->nwaves; i++)
218 ww->srcs[i].xth = frand(2.0) * M_PI;
219 ww->srcs[i].yth = frand(2.0) * M_PI;
222 cc->texture_width = texture_size;
223 cc->texture_height = texture_size;
228 interference_point (cube_configuration *cc, int x, int y)
230 /* Compute the effect of the waves on a pixel. */
232 waves *ww = cc->waves;
235 for (i = 0; i < ww->nwaves; i++)
237 int dx = x - ww->srcs[i].x;
238 int dy = y - ww->srcs[i].y;
239 int dist = sqrt (dx*dx + dy*dy);
240 result += (dist >= ww->radius ? 0 : ww->heights[dist]);
243 if (result > 255) result = 255;
249 interference (ModeInfo *mi)
251 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
252 waves *ww = cc->waves;
255 /* Move the wave origins around
257 for (i = 0; i < ww->nwaves; i++)
259 ww->srcs[i].xth += (ww->speed / 1000.0);
260 if (ww->srcs[i].xth > 2*M_PI)
261 ww->srcs[i].xth -= 2*M_PI;
262 ww->srcs[i].yth += (ww->speed / 1000.0);
263 if (ww->srcs[i].yth > 2*M_PI)
264 ww->srcs[i].yth -= 2*M_PI;
266 ww->srcs[i].x = (cc->texture_width/2 +
267 (cos (ww->srcs[i].xth) *
268 cc->texture_width / 2));
269 ww->srcs[i].y = (cc->texture_height/2 +
270 (cos (ww->srcs[i].yth) *
271 cc->texture_height / 2));
276 /* qsort comparator for sorting cubes by y position */
278 cmp_cubes (const void *aa, const void *bb)
280 const cube *a = (cube *) aa;
281 const cube *b = (cube *) bb;
282 return ((int) (b->y * 10000) -
283 (int) (a->y * 10000));
288 init_cube (ModeInfo *mi)
291 cube_configuration *cc;
294 ccs = (cube_configuration *)
295 calloc (MI_NUM_SCREENS(mi), sizeof (cube_configuration));
297 fprintf(stderr, "%s: out of memory\n", progname);
302 cc = &ccs[MI_SCREEN(mi)];
304 if ((cc->glx_context = init_GL(mi)) != NULL) {
305 reshape_cube (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
308 cc->trackball = gltrackball_init (True);
311 cc->colors = (XColor *) calloc(cc->ncolors, sizeof(XColor));
316 cc->ncubes = MI_COUNT (mi);
318 if (cc->ncubes < 1) cc->ncubes = 1;
320 cc->cubes = (cube *) calloc (sizeof(cube), cc->ncubes);
321 for (i = 0; i < cc->ncubes; i++)
323 /* Set the size to roughly cover a 2x2 square on average. */
324 GLfloat scale = 1.8 / sqrt (cc->ncubes);
325 cube *cube = &cc->cubes[i];
326 cube->x = (frand(1)-0.5);
327 cube->y = (frand(1)-0.5);
329 cube->z = frand(0.12);
330 double th = -(skew ? frand(skew) : 0) * M_PI / 180;
334 cube->w = scale * (frand(1) + 0.2);
335 cube->d = scale * (frand(1) + 0.2);
337 if (cube->x < cc->min_x) cc->min_x = cube->x;
338 if (cube->y < cc->min_y) cc->min_y = cube->y;
339 if (cube->x > cc->max_x) cc->max_x = cube->x;
340 if (cube->y > cc->max_y) cc->max_y = cube->y;
343 /* Sorting by depth improves frame rate slightly. With 6000 polygons we get:
345 3.1 FPS back to front;
346 4.3 FPS front to back.
348 qsort (cc->cubes, cc->ncubes, sizeof(*cc->cubes), cmp_cubes);
353 animate_cubes (ModeInfo *mi)
355 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
357 for (i = 0; i < cc->ncubes; i++)
359 cube *cube = &cc->cubes[i];
360 GLfloat fx = (cube->x - cc->min_x) / (cc->max_x - cc->min_x);
361 GLfloat fy = (cube->y - cc->min_y) / (cc->max_y - cc->min_y);
362 int x = (int) (cc->texture_width * fx) % cc->texture_width;
363 int y = (int) (cc->texture_height * fy) % cc->texture_height;
364 unsigned char v = interference_point (cc, x, y);
365 cube->h = cube->z + (v / 256.0 / 2.5) + 0.1;
371 draw_cube (ModeInfo *mi)
373 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
374 int wire = MI_IS_WIREFRAME(mi);
375 Display *dpy = MI_DISPLAY(mi);
376 Window window = MI_WINDOW(mi);
379 if (!cc->glx_context)
382 mi->polygon_count = 0;
383 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cc->glx_context));
388 glShadeModel(GL_FLAT);
390 glEnable(GL_DEPTH_TEST);
391 glEnable(GL_NORMALIZE);
392 glEnable(GL_CULL_FACE);
393 /* glEnable (GL_POLYGON_OFFSET_FILL); */
395 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
399 gltrackball_rotate (cc->trackball);
400 glRotatef (-180, 1, 0, 0);
406 glRotatef (-90, 1, 0, 0);
407 glRotatef(current_device_rotation(), 0, 1, 0);
409 glTranslatef (-0.18, 0, -0.18);
410 glRotatef (37, 1, 0, 0);
411 glRotatef (20, 0, 0, 1);
413 glScalef (2.1, 2.1, 2.1);
415 /* Position lights after device rotation. */
418 static const GLfloat pos[4] = {0.0, 0.25, -1.0, 0.0};
419 static const GLfloat amb[4] = {0.2, 0.2, 0.2, 1.0};
420 static const GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
422 glLightfv(GL_LIGHT0, GL_POSITION, pos);
423 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
424 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
426 glEnable(GL_LIGHTING);
428 glEnable(GL_DEPTH_TEST);
429 glEnable(GL_CULL_FACE);
432 glBegin (wire ? GL_LINES : GL_QUADS);
434 for (i = 0; i < cc->ncubes; i++)
436 cube *cube = &cc->cubes[i];
437 GLfloat cth = cube->cth;
438 GLfloat sth = cube->sth;
439 GLfloat x = cth*cube->x + sth*cube->y;
440 GLfloat y = -sth*cube->x + cth*cube->y;
441 GLfloat w = cube->w/2;
442 GLfloat h = cube->h/2;
443 GLfloat d = cube->d/2;
446 GLfloat xw = cth*w, xd = sth*d;
447 GLfloat yw = -sth*w, yd = cth*d;
450 int c = cube->h * cc->ncolors * 0.7;
453 color[0] = cc->colors[c].red / 65536.0;
454 color[1] = cc->colors[c].green / 65536.0;
455 color[2] = cc->colors[c].blue / 65536.0;
457 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
459 /* Putting this in a display list makes no performance difference. */
463 glNormal3f (0, 0, -1); /* top */
464 glVertex3f (x+xw+xd, y+yw+yd, -h);
465 glVertex3f (x+xw-xd, y+yw-yd, -h);
466 glVertex3f (x-xw-xd, y-yw-yd, -h);
467 glVertex3f (x-xw+xd, y-yw+yd, -h);
470 glNormal3f (sth, cth, 0); /* front */
471 glVertex3f (x+xw+xd, y+yw+yd, bottom);
472 glVertex3f (x+xw+xd, y+yw+yd, -h);
473 glVertex3f (x-xw+xd, y-yw+yd, -h);
474 glVertex3f (x-xw+xd, y-yw+yd, bottom);
477 glNormal3f (cth, -sth, 0); /* right */
478 glVertex3f (x+xw-xd, y+yw-yd, -h);
479 glVertex3f (x+xw+xd, y+yw+yd, -h);
480 glVertex3f (x+xw+xd, y+yw+yd, bottom);
481 glVertex3f (x+xw-xd, y+yw-yd, bottom);
484 # if 0 /* Omitting these makes no performance difference. */
486 glNormal3f (-cth, sth, 0); /* left */
487 glVertex3f (x-xw+xd, y-yw+yd, -h);
488 glVertex3f (x-xw-xd, y-yw-yd, -h);
489 glVertex3f (x-xw-xd, y-yw-yd, bottom);
490 glVertex3f (x-xw+xd, y-yw+yd, bottom);
493 glNormal3f (-sth, -cth, 0); /* back */
494 glVertex3f (x-xw-xd, y-yw-yd, bottom);
495 glVertex3f (x-xw-xd, y-yw-yd, -h);
496 glVertex3f (x+xw-xd, y+yw-yd, -h);
497 glVertex3f (x+xw-xd, y+yw-yd, bottom);
503 glNormal3f (0, 0, -1); /* top */
504 glVertex3f (x+xw+xd, y+yw+yd, -h);
505 glVertex3f (x+xw-xd, y+yw-yd, -h);
507 glVertex3f (x+xw-xd, y+yw-yd, -h);
508 glVertex3f (x-xw-xd, y-yw-yd, -h);
510 glVertex3f (x-xw-xd, y-yw-yd, -h);
511 glVertex3f (x-xw+xd, y-yw+yd, -h);
513 glVertex3f (x-xw+xd, y-yw+yd, -h);
514 glVertex3f (x+xw+xd, y+yw+yd, -h);
520 glPolygonOffset (0, 0);
523 glDisable(GL_DEPTH_TEST); /* Outline the playfield */
525 glBegin(GL_LINE_LOOP);
526 glVertex3f (-0.5, -0.5, 0);
527 glVertex3f (-0.5, 0.5, 0);
528 glVertex3f ( 0.5, 0.5, 0);
529 glVertex3f ( 0.5, -0.5, 0);
535 if (mi->fps_p) do_fps (mi);
538 glXSwapBuffers(dpy, window);
542 XSCREENSAVER_MODULE_2 ("Cityflow", cityflow, cube)