1 /* unknownpleasures, Copyright (c) 2013-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
11 * Translated from Mathematica code by Archery:
12 * http://intothecontinuum.tumblr.com/post/27443100682/in-july-1967-astronomers-at-the-cavendish
14 * Interestingly, the original image is copyright-free:
15 * http://adamcap.com/2011/05/19/history-of-joy-division-unknown-pleasures-album-art/
16 * http://en.wikipedia.org/wiki/Unknown_Pleasures
20 * - Performance is not great. Spending half our time in compute_line()
21 * and half our time in glEnd(). It's a vast number of cos/pow calls,
22 * and a vast number of polygons. I'm not sure how much could be cached.
24 * - There's too low periodicity vertically on the screen.
26 * - There's too low periodicity in time.
28 * - Could take advantage of time periodicity for caching: just save every
29 * poly for an N second loop. That would be a huge amount of RAM though.
31 * - At low resolutions, GL_POLYGON_OFFSET_FILL seems to work poorly
32 * and the lines get blocky.
36 #define DEFAULTS "*delay: 30000 \n" \
38 "*resolution: 100 \n" \
40 "*showFPS: False \n" \
41 "*wireframe: False \n" \
42 "*geometry: =800x800" "\n" \
44 # define refresh_unk 0
45 # define release_unk 0
47 #define countof(x) (sizeof((x))/sizeof((*x)))
49 #include "xlockmore.h"
51 #include "gltrackball.h"
54 #ifdef USE_GL /* whole file */
56 #define DEF_SPEED "1.0"
60 GLXContext *glx_context;
61 trackball_state *trackball;
69 static unk_configuration *bps = NULL;
73 static XrmOptionDescRec opts[] = {
74 { "-speed", ".speed", XrmoptionSepArg, 0 },
75 { "-resolution", ".resolution", XrmoptionSepArg, 0 },
76 { "-ortho", ".ortho", XrmoptionNoArg, "True" },
77 { "-no-ortho", ".ortho", XrmoptionNoArg, "False" },
80 static argtype vars[] = {
81 {&speed, "speed", "Speed", DEF_SPEED, t_Float},
84 ENTRYPOINT ModeSpecOpt unk_opts = {countof(opts), opts, countof(vars), vars, NULL};
88 /* Window management, etc
91 reshape_unk (ModeInfo *mi, int width, int height)
93 unk_configuration *bp = &bps[MI_SCREEN(mi)];
94 GLfloat h = (GLfloat) height / (GLfloat) width;
96 glViewport (0, 0, (GLint) width, (GLint) height);
100 glMatrixMode(GL_PROJECTION);
102 gluPerspective (1.0, 1/h, 690, 710);
104 glMatrixMode(GL_MODELVIEW);
106 gluLookAt( 0, 0, 700,
110 glScalef (1/h, 1/h, 1);
111 glTranslatef (0, -0.5, 0);
115 glMatrixMode(GL_PROJECTION);
117 gluPerspective (30.0, 1/h, 20.0, 40.0);
119 glMatrixMode(GL_MODELVIEW);
121 gluLookAt( 0.0, 0.0, 30.0,
125 glScalef (1/h, 1/h, 1);
128 glClear(GL_COLOR_BUFFER_BIT);
133 unk_handle_event (ModeInfo *mi, XEvent *event)
135 unk_configuration *bp = &bps[MI_SCREEN(mi)];
137 if (event->xany.type == ButtonPress &&
138 (event->xbutton.button == Button4 ||
139 event->xbutton.button == Button5 ||
140 event->xbutton.button == Button6 ||
141 event->xbutton.button == Button7))
143 int b = event->xbutton.button;
145 if (b == Button6 || b == Button7)
148 switch (b) { /* swap axes */
149 case Button4: b = Button6; break;
150 case Button5: b = Button7; break;
151 case Button6: b = Button4; break;
152 case Button7: b = Button5; break;
154 gltrackball_mousewheel (bp->trackball, b, speed, !!event->xbutton.state);
157 else if (gltrackball_event_handler (event, bp->trackball,
158 MI_WIDTH (mi), MI_HEIGHT (mi),
161 else if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
163 bp->orthop = !bp->orthop;
164 reshape_unk (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
174 init_unk (ModeInfo *mi)
176 unk_configuration *bp;
179 bps = (unk_configuration *)
180 calloc (MI_NUM_SCREENS(mi), sizeof (unk_configuration));
182 fprintf(stderr, "%s: out of memory\n", progname);
187 bp = &bps[MI_SCREEN(mi)];
189 bp->glx_context = init_GL(mi);
191 bp->orthop = get_boolean_resource (MI_DISPLAY (mi), "ortho", "Ortho");
192 bp->resolution = get_float_resource (MI_DISPLAY (mi),
193 "resolution", "Resolution");
194 if (bp->resolution < 1) bp->resolution = 1;
195 if (bp->resolution > 300) bp->resolution = 300;
197 reshape_unk (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
199 bp->count = MI_COUNT(mi);
200 if (bp->count < 1) bp->count = 1;
202 bp->trackball = gltrackball_init (False);
204 if (MI_COUNT(mi) < 1) MI_COUNT(mi) = 1;
206 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
214 /* A simple, fast, deterministic PRNG.
215 ya_rand_init() is too slow for this, and the stream of numbers here
216 doesn't have to be high quality.
222 /* Kluge to let me pick a good seed factor by trial and error... */
223 static int seed0 = 0;
224 static int count = 0;
225 if (count++ > 150000000) seed0 = 0, count=0;
228 seed0 = (random() & 0xFFFFF);
229 fprintf(stderr, "seed0 = %8x %d\n", seed0, seed0);
233 long seed = seed0 * f;
234 seed = 48271 * (seed % 44488) - 3399 * (seed / 44488);
235 f = (double) seed / 0x7FFFFFFF;
242 compute_line (ModeInfo *mi,
243 int xmin, int xmax, GLfloat xinc,
248 unk_configuration *bp = &bps[MI_SCREEN(mi)];
253 /* Compute the points on the line */
255 for (fx = xmin; fx < xmax; fx += xinc)
259 GLfloat hsum = 0, vsum = 0;
261 if (x == lastx) continue;
264 for (n = 1; n <= 30; n++)
266 /* This sum affects crinkliness of the lines */
273 * exp (-pow ((.3 * (x / res_scale) + 30
274 - 1 * 100 * R (2 * n * y)),
279 for (n = 1; n <= 4; n++)
281 /* This sum affects amplitude of the peaks */
282 vsum += (3 * (1 + R (3 * n * y))
283 * fabs (sin (bp->t + R (n * y)
285 * exp (-pow (((x / res_scale) - 1 * 100 * R (n * y)),
290 /* Scale of this affects amplitude of the flat parts */
291 points[x - xmin] = (0.2 * sin (2 * M_PI * R (6 * y) + hsum) + vsum);
298 draw_unk (ModeInfo *mi)
300 unk_configuration *bp = &bps[MI_SCREEN(mi)];
301 Display *dpy = MI_DISPLAY(mi);
302 Window window = MI_WINDOW(mi);
303 int wire = MI_IS_WIREFRAME(mi);
304 GLfloat aspect = 1.5;
306 GLfloat res_scale = 4;
307 int xmin = -50 * res_scale;
308 int xmax = 150 * res_scale;
309 GLfloat xinc = 100.0 / (bp->resolution / res_scale);
311 int ytop = MI_COUNT(mi);
316 if (xinc < 0.25) xinc = 0.25;
318 if (!bp->glx_context)
321 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
323 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
325 mi->polygon_count = 0;
327 glShadeModel (GL_FLAT);
328 glEnable (GL_DEPTH_TEST);
329 glDisable (GL_CULL_FACE);
333 glRotatef(current_device_rotation(), 0, 0, 1);
335 gltrackball_rotate (bp->trackball);
336 glScalef (10, 10, 10);
338 glRotatef (-45, 1, 0, 0);
339 glTranslatef (-0.5, -0.5, 0);
341 glTranslatef (0, 0.05, 0);
343 glTranslatef (0, 0.15, 0);
345 points = (GLfloat *) malloc (sizeof(*points) * (xmax - xmin));
347 if (!bp->button_down_p)
349 double s = 6.3 / 19 / 3;
361 /* Lower the resolution to get a decent frame rate on iPhone 4s */
362 if (mi->xgwa.width <= 640 || mi->xgwa.height <= 640)
369 /* Lower it even further for iPhone 3 */
370 if (mi->xgwa.width <= 480 || mi->xgwa.height <= 480)
376 /* Performance just sucks on iPad 3, even with a very high xinc. WTF? */
378 if (mi->xgwa.width >= 2048 || mi->xgwa.height >= 2048)
382 # endif /* USE_IPHONE */
385 /* Make the image fill the screen a little more fully */
386 if (mi->xgwa.width <= 640 || mi->xgwa.height <= 640)
388 glScalef (1.2, 1.2, 1.2);
389 glTranslatef (-0.08, 0, 0);
392 if (mi->xgwa.width <= 480 || mi->xgwa.height <= 480)
402 glDisable (GL_POLYGON_OFFSET_FILL);
405 glTranslatef (0, (1-aspect)/2, -0.005);
406 glScalef (1, aspect, 1);
407 glTranslatef (0.5, 0.5, 0);
410 glVertex3f (-0.5, -0.5, 0);
411 glVertex3f ( 0.5, -0.5, 0);
412 glVertex3f ( 0.5, 0.5, 0);
413 glVertex3f (-0.5, 0.5, 0);
420 glEnable (GL_LINE_SMOOTH);
421 glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
422 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
425 /* So the masking quads don't interfere with the lines */
426 glEnable (GL_POLYGON_OFFSET_FILL);
427 glPolygonOffset (1.0, 1.0);
430 for (y = ymin; y <= ytop; y += yinc)
432 /* Compute all the verts on the line */
433 compute_line (mi, xmin, xmax, xinc, res_scale, y, points);
435 /* Draw the line segments; then draw the black shielding quads. */
437 GLfloat yy = y / (GLfloat) ytop;
440 yy = (yy * aspect) - ((aspect - 1) / 2);
442 for (linesp = 0; linesp <= 1; linesp++)
447 GLfloat c = (linesp || wire ? 1 : 0);
452 : wire ? GL_LINES : GL_QUAD_STRIP);
454 for (fx = xmin; fx < xmax; fx += xinc)
457 GLfloat xx = (x - xmin) / (GLfloat) (xmax - xmin);
458 GLfloat zz = points [x - xmin];
460 if (x == lastx) continue;
464 glVertex3f (xx, yy, zz);
466 glVertex3f (xx, yy, 0);
478 if (mi->fps_p) do_fps (mi);
481 glXSwapBuffers(dpy, window);
484 XSCREENSAVER_MODULE_2 ("UnknownPleasures", unknownpleasures, unk)