1 /* cityflow, Copyright (c) 2014-2017 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" \
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;
104 if (width > height * 2) { /* tiny window: show middle */
107 h = height / (GLfloat) width;
110 glViewport (0, y, (GLint) width, (GLint) height);
112 glMatrixMode(GL_PROJECTION);
115 /* For this one it's really important to minimize the distance between
117 gluPerspective (30, 1/h, 10, 50);
118 glMatrixMode(GL_MODELVIEW);
120 gluLookAt( 0.0, 0.0, 30.0,
124 glClear(GL_COLOR_BUFFER_BIT);
129 reset_colors (ModeInfo *mi)
131 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
132 make_smooth_colormap (0, 0, 0,
133 cc->colors, &cc->ncolors,
135 if (! MI_IS_WIREFRAME(mi))
136 glClearColor (cc->colors[0].red / 65536.0,
137 cc->colors[0].green / 65536.0,
138 cc->colors[0].blue / 65536.0,
144 tweak_cubes (ModeInfo *mi)
146 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
148 for (i = 0; i < cc->ncubes; i++)
150 cube *cube = &cc->cubes[i];
151 cube->x += (frand(2)-1)*0.01;
152 cube->y += (frand(2)-1)*0.01;
153 cube->z += (frand(2)-1)*0.01;
159 cube_handle_event (ModeInfo *mi, XEvent *event)
161 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
163 /* Neutralize any vertical motion */
164 GLfloat rot = current_device_rotation();
165 Bool rotp = ((rot > 45 && rot < 135) ||
166 (rot < -45 && rot > -135));
168 if (event->xany.type == ButtonPress ||
169 event->xany.type == ButtonRelease)
172 event->xbutton.x = MI_WIDTH(mi) / 2;
174 event->xbutton.y = MI_HEIGHT(mi) / 2;
176 else if (event->xany.type == MotionNotify)
179 event->xmotion.x = MI_WIDTH(mi) / 2;
181 event->xmotion.y = MI_HEIGHT(mi) / 2;
184 if (gltrackball_event_handler (event, cc->trackball,
185 MI_WIDTH (mi), MI_HEIGHT (mi),
188 else if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
192 gltrackball_reset (cc->trackball, 0, 0);
201 Adapted from ../hacks/interference.c by Hannu Mallat.
205 init_wave (ModeInfo *mi)
207 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
210 cc->waves = ww = (waves *) calloc (sizeof(*cc->waves), 1);
211 ww->nwaves = wave_count;
212 ww->radius = wave_radius;
213 ww->speed = wave_speed;
214 ww->heights = (int *) calloc (sizeof(*ww->heights), ww->radius);
215 ww->srcs = (wave_src *) calloc (sizeof(*ww->srcs), ww->nwaves);
217 for (i = 0; i < ww->radius; i++)
219 float max = (cc->ncolors * (ww->radius - i) / (float) ww->radius);
220 ww->heights[i] = ((max + max * cos(i / 50.0)) / 2.0);
223 for (i = 0; i < ww->nwaves; i++)
225 ww->srcs[i].xth = frand(2.0) * M_PI;
226 ww->srcs[i].yth = frand(2.0) * M_PI;
229 cc->texture_width = texture_size;
230 cc->texture_height = texture_size;
235 interference_point (cube_configuration *cc, int x, int y)
237 /* Compute the effect of the waves on a pixel. */
239 waves *ww = cc->waves;
242 for (i = 0; i < ww->nwaves; i++)
244 int dx = x - ww->srcs[i].x;
245 int dy = y - ww->srcs[i].y;
246 int dist = sqrt (dx*dx + dy*dy);
247 result += (dist >= ww->radius ? 0 : ww->heights[dist]);
250 if (result > 255) result = 255;
256 interference (ModeInfo *mi)
258 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
259 waves *ww = cc->waves;
262 /* Move the wave origins around
264 for (i = 0; i < ww->nwaves; i++)
266 ww->srcs[i].xth += (ww->speed / 1000.0);
267 if (ww->srcs[i].xth > 2*M_PI)
268 ww->srcs[i].xth -= 2*M_PI;
269 ww->srcs[i].yth += (ww->speed / 1000.0);
270 if (ww->srcs[i].yth > 2*M_PI)
271 ww->srcs[i].yth -= 2*M_PI;
273 ww->srcs[i].x = (cc->texture_width/2 +
274 (cos (ww->srcs[i].xth) *
275 cc->texture_width / 2));
276 ww->srcs[i].y = (cc->texture_height/2 +
277 (cos (ww->srcs[i].yth) *
278 cc->texture_height / 2));
283 /* qsort comparator for sorting cubes by y position */
285 cmp_cubes (const void *aa, const void *bb)
287 const cube *a = (cube *) aa;
288 const cube *b = (cube *) bb;
289 return ((int) (b->y * 10000) -
290 (int) (a->y * 10000));
295 init_cube (ModeInfo *mi)
298 cube_configuration *cc;
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 (False);
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 double th = -(skew ? frand(skew) : 0) * M_PI / 180;
328 cube->x = (frand(1)-0.5);
329 cube->y = (frand(1)-0.5);
331 cube->z = frand(0.12);
335 cube->w = scale * (frand(1) + 0.2);
336 cube->d = scale * (frand(1) + 0.2);
338 if (cube->x < cc->min_x) cc->min_x = cube->x;
339 if (cube->y < cc->min_y) cc->min_y = cube->y;
340 if (cube->x > cc->max_x) cc->max_x = cube->x;
341 if (cube->y > cc->max_y) cc->max_y = cube->y;
344 /* Sorting by depth improves frame rate slightly. With 6000 polygons we get:
346 3.1 FPS back to front;
347 4.3 FPS front to back.
349 qsort (cc->cubes, cc->ncubes, sizeof(*cc->cubes), cmp_cubes);
354 animate_cubes (ModeInfo *mi)
356 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
358 for (i = 0; i < cc->ncubes; i++)
360 cube *cube = &cc->cubes[i];
361 GLfloat fx = (cube->x - cc->min_x) / (cc->max_x - cc->min_x);
362 GLfloat fy = (cube->y - cc->min_y) / (cc->max_y - cc->min_y);
363 int x = (int) (cc->texture_width * fx) % cc->texture_width;
364 int y = (int) (cc->texture_height * fy) % cc->texture_height;
365 unsigned char v = interference_point (cc, x, y);
366 cube->h = cube->z + (v / 256.0 / 2.5) + 0.1;
372 draw_cube (ModeInfo *mi)
374 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
375 int wire = MI_IS_WIREFRAME(mi);
376 Display *dpy = MI_DISPLAY(mi);
377 Window window = MI_WINDOW(mi);
380 if (!cc->glx_context)
383 mi->polygon_count = 0;
384 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cc->glx_context));
389 glShadeModel(GL_FLAT);
391 glEnable(GL_DEPTH_TEST);
392 glEnable(GL_NORMALIZE);
393 glEnable(GL_CULL_FACE);
394 /* glEnable (GL_POLYGON_OFFSET_FILL); */
396 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
400 glRotatef(current_device_rotation(), 0, 0, 1);
401 gltrackball_rotate (cc->trackball);
402 glRotatef (-180, 1, 0, 0);
408 glRotatef (-90, 1, 0, 0);
410 glTranslatef (-0.18, 0, -0.18);
411 glRotatef (37, 1, 0, 0);
412 glRotatef (20, 0, 0, 1);
414 glScalef (2.1, 2.1, 2.1);
416 /* Position lights after device rotation. */
419 static const GLfloat pos[4] = {0.0, 0.25, -1.0, 0.0};
420 static const GLfloat amb[4] = {0.2, 0.2, 0.2, 1.0};
421 static const GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
423 glLightfv(GL_LIGHT0, GL_POSITION, pos);
424 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
425 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
427 glEnable(GL_LIGHTING);
429 glEnable(GL_DEPTH_TEST);
430 glEnable(GL_CULL_FACE);
433 glBegin (wire ? GL_LINES : GL_QUADS);
435 for (i = 0; i < cc->ncubes; i++)
437 cube *cube = &cc->cubes[i];
438 GLfloat cth = cube->cth;
439 GLfloat sth = cube->sth;
440 GLfloat x = cth*cube->x + sth*cube->y;
441 GLfloat y = -sth*cube->x + cth*cube->y;
442 GLfloat w = cube->w/2;
443 GLfloat h = cube->h/2;
444 GLfloat d = cube->d/2;
447 GLfloat xw = cth*w, xd = sth*d;
448 GLfloat yw = -sth*w, yd = cth*d;
451 int c = cube->h * cc->ncolors * 0.7;
454 color[0] = cc->colors[c].red / 65536.0;
455 color[1] = cc->colors[c].green / 65536.0;
456 color[2] = cc->colors[c].blue / 65536.0;
458 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
460 /* Putting this in a display list makes no performance difference. */
464 glNormal3f (0, 0, -1); /* top */
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);
468 glVertex3f (x-xw+xd, y-yw+yd, -h);
471 glNormal3f (sth, cth, 0); /* front */
472 glVertex3f (x+xw+xd, y+yw+yd, bottom);
473 glVertex3f (x+xw+xd, y+yw+yd, -h);
474 glVertex3f (x-xw+xd, y-yw+yd, -h);
475 glVertex3f (x-xw+xd, y-yw+yd, bottom);
478 glNormal3f (cth, -sth, 0); /* right */
479 glVertex3f (x+xw-xd, y+yw-yd, -h);
480 glVertex3f (x+xw+xd, y+yw+yd, -h);
481 glVertex3f (x+xw+xd, y+yw+yd, bottom);
482 glVertex3f (x+xw-xd, y+yw-yd, bottom);
485 # if 0 /* Omitting these makes no performance difference. */
487 glNormal3f (-cth, sth, 0); /* left */
488 glVertex3f (x-xw+xd, y-yw+yd, -h);
489 glVertex3f (x-xw-xd, y-yw-yd, -h);
490 glVertex3f (x-xw-xd, y-yw-yd, bottom);
491 glVertex3f (x-xw+xd, y-yw+yd, bottom);
494 glNormal3f (-sth, -cth, 0); /* back */
495 glVertex3f (x-xw-xd, y-yw-yd, bottom);
496 glVertex3f (x-xw-xd, y-yw-yd, -h);
497 glVertex3f (x+xw-xd, y+yw-yd, -h);
498 glVertex3f (x+xw-xd, y+yw-yd, bottom);
504 glNormal3f (0, 0, -1); /* top */
505 glVertex3f (x+xw+xd, y+yw+yd, -h);
506 glVertex3f (x+xw-xd, y+yw-yd, -h);
508 glVertex3f (x+xw-xd, y+yw-yd, -h);
509 glVertex3f (x-xw-xd, y-yw-yd, -h);
511 glVertex3f (x-xw-xd, y-yw-yd, -h);
512 glVertex3f (x-xw+xd, y-yw+yd, -h);
514 glVertex3f (x-xw+xd, y-yw+yd, -h);
515 glVertex3f (x+xw+xd, y+yw+yd, -h);
521 glPolygonOffset (0, 0);
524 glDisable(GL_DEPTH_TEST); /* Outline the playfield */
526 glBegin(GL_LINE_LOOP);
527 glVertex3f (-0.5, -0.5, 0);
528 glVertex3f (-0.5, 0.5, 0);
529 glVertex3f ( 0.5, 0.5, 0);
530 glVertex3f ( 0.5, -0.5, 0);
536 if (mi->fps_p) do_fps (mi);
539 glXSwapBuffers(dpy, window);
543 XSCREENSAVER_MODULE_2 ("Cityflow", cityflow, cube)