1 /* discoball, Copyright (c) 2016 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: 30000 \n" \
14 "*showFPS: False \n" \
15 "*wireframe: False \n" \
17 # define refresh_ball 0
18 # define release_ball 0
20 #define countof(x) (sizeof((x))/sizeof((*x)))
22 #include "xlockmore.h"
25 #include "gltrackball.h"
28 #ifdef USE_GL /* whole file */
31 #define DEF_SPIN "False"
32 #define DEF_WANDER "True"
33 #define DEF_SPEED "1.0"
35 typedef struct tile tile;
49 GLXContext *glx_context;
52 trackball_state *trackball;
59 static ball_configuration *bps = NULL;
63 static Bool do_wander;
65 static XrmOptionDescRec opts[] = {
66 { "-spin", ".spin", XrmoptionNoArg, "True" },
67 { "+spin", ".spin", XrmoptionNoArg, "False" },
68 { "-speed", ".speed", XrmoptionSepArg, 0 },
69 { "-wander", ".wander", XrmoptionNoArg, "True" },
70 { "+wander", ".wander", XrmoptionNoArg, "False" }
73 static argtype vars[] = {
74 {&do_spin, "spin", "Spin", DEF_SPIN, t_Bool},
75 {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
76 {&speed, "speed", "Speed", DEF_SPEED, t_Float},
79 ENTRYPOINT ModeSpecOpt ball_opts = {countof(opts), opts, countof(vars), vars, NULL};
85 GLfloat d = sqrt(p.x*p.x + p.y*p.y * p.z*p.z);
100 build_texture (ModeInfo *mi)
105 unsigned char *data = malloc (bpl * size);
107 for (y = 0; y < size; y++)
109 for (x = 0; x < size; x++)
111 unsigned char *c = &data [y * bpl + x * 2];
112 GLfloat X = (x / (GLfloat) (size-1)) - 0.5;
113 GLfloat Y = (y / (GLfloat) (size-1)) - 0.5;
114 X = cos (X * X * 6.2);
115 Y = cos (Y * Y * 6.2);
123 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
124 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
125 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
126 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
127 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
128 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
129 check_gl_error ("texture param");
131 glTexImage2D (GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, size, size, 0,
132 GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, data);
133 check_gl_error ("light texture");
139 draw_rays (ModeInfo *mi)
141 ball_configuration *bp = &bps[MI_SCREEN(mi)];
142 int wire = MI_IS_WIREFRAME(mi);
146 glEnable (GL_TEXTURE_2D);
147 glDisable (GL_LIGHTING);
149 glDisable (GL_DEPTH_TEST);
150 glBlendFunc (GL_SRC_ALPHA, GL_ONE);
152 for (i = 0; i < bp->nrays; i++)
154 GLfloat x = bp->rays[i].normal.x;
155 GLfloat y = bp->rays[i].normal.y;
156 GLfloat z = bp->rays[i].normal.z;
159 /* Orient to direction of ray. */
160 glRotatef (-atan2 (x, y) * (180 / M_PI), 0, 0, 1);
161 glRotatef ( atan2 (z, sqrt(x*x + y*y)) * (180 / M_PI), 1, 0, 0);
164 glTranslatef(0, 0, 1.1);
165 glColor4fv (bp->rays[i].color);
166 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
167 glTexCoord2f (0, 0); glVertex3f (-0.5, 0, -1);
168 glTexCoord2f (1, 0); glVertex3f ( 0.5, 0, -1);
169 glTexCoord2f (1, 1); glVertex3f ( 0.5, 0, 1);
170 glTexCoord2f (0, 1); glVertex3f (-0.5, 0, 1);
176 glDisable (GL_TEXTURE_2D);
177 glEnable (GL_LIGHTING);
178 glDisable (GL_BLEND);
179 glEnable (GL_DEPTH_TEST);
187 draw_ball_1 (ModeInfo *mi)
189 ball_configuration *bp = &bps[MI_SCREEN(mi)];
190 int wire = MI_IS_WIREFRAME(mi);
195 glGetFloatv (GL_MODELVIEW_MATRIX, &m[0][0]);
200 /* Draw the back rays.
206 glMultMatrixf (&m[0][0]);
207 glTranslatef(0, 0, -4.1);
208 glRotatef (bp->th, 0, 0, 1);
209 polys += draw_rays (mi);
212 glClear(GL_DEPTH_BUFFER_BIT);
216 /* Instead of rendering polygons for the foam ball substrate, let's
217 just billboard a quad down the middle to mask out the back-facing
221 m[0][0] = 1; m[1][0] = 0; m[2][0] = 0;
222 m[0][1] = 0; m[1][1] = 1; m[2][1] = 0;
223 m[0][2] = 0; m[1][2] = 0; m[2][2] = 1;
225 glMultMatrixf (&m[0][0]);
226 glScalef (40, 40, 40);
227 glTranslatef (-0.5, -0.5, -0.01);
229 glDisable (GL_LIGHTING);
230 /* Draw into the depth buffer but not the frame buffer */
231 glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
234 glVertex3f (0, 0, 0);
235 glVertex3f (0, 1, 0);
236 glVertex3f (1, 1, 0);
237 glVertex3f (1, 0, 0);
240 glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
242 glEnable (GL_LIGHTING);
247 /* Draw all the tiles.
249 for (t = bp->tiles; t; t = t->next)
251 GLfloat x = t->normal.x;
252 GLfloat y = t->normal.y;
253 GLfloat z = t->normal.z;
254 GLfloat s = t->size / 2;
257 /* Move to location of tile. */
258 glTranslatef (t->position.x, t->position.y, t->position.z);
260 /* Orient to direction tile is facing. */
261 glRotatef (-atan2 (x, y) * (180 / M_PI), 0, 0, 1);
262 glRotatef ( atan2 (z, sqrt(x*x + y*y)) * (180 / M_PI), 1, 0, 0);
264 glRotatef (t->tilt, 0, 1, 0);
267 glNormal3f (0, 1, 0);
268 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
269 glVertex3f (-1, 0, -1);
270 glVertex3f ( 1, 0, -1);
271 glVertex3f ( 1, 0, 1);
272 glVertex3f (-1, 0, 1);
279 glNormal3f (0, 0, -1);
280 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
281 glVertex3f (-1, 0, -1);
282 glVertex3f (-1, -d, -1);
283 glVertex3f ( 1, -d, -1);
284 glVertex3f ( 1, 0, -1);
288 glNormal3f (0, 0, 1);
289 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
290 glVertex3f ( 1, 0, 1);
291 glVertex3f ( 1, -d, 1);
292 glVertex3f (-1, -d, 1);
293 glVertex3f (-1, 0, 1);
297 glNormal3f (1, 0, 0);
298 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
299 glVertex3f ( 1, 0, -1);
300 glVertex3f ( 1, -d, -1);
301 glVertex3f ( 1, -d, 1);
302 glVertex3f ( 1, 0, 1);
306 glNormal3f (-1, 0, 0);
307 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
308 glVertex3f (-1, 0, 1);
309 glVertex3f (-1, -d, 1);
310 glVertex3f (-1, -d, -1);
311 glVertex3f (-1, 0, -1);
319 /* Draw the front rays.
325 glMultMatrixf (&m[0][0]);
326 glTranslatef(0, 0, 4.1);
327 glRotatef (-bp->th, 0, 0, 1);
328 polys += draw_rays (mi);
337 vector_angle (XYZ a, XYZ b)
339 double La = sqrt (a.x*a.x + a.y*a.y + a.z*a.z);
340 double Lb = sqrt (b.x*b.x + b.y*b.y + b.z*b.z);
343 if (La == 0 || Lb == 0) return 0;
344 if (a.x == b.x && a.y == b.y && a.z == b.z) return 0;
346 /* dot product of two vectors is defined as:
347 La * Lb * cos(angle between vectors)
348 and is also defined as:
349 ax*bx + ay*by + az*bz
351 La * Lb * cos(angle) = ax*bx + ay*by + az*bz
352 cos(angle) = (ax*bx + ay*by + az*bz) / (La * Lb)
353 angle = acos ((ax*bx + ay*by + az*bz) / (La * Lb));
355 cc = (a.x*b.x + a.y*b.y + a.z*b.z) / (La * Lb);
356 if (cc > 1) cc = 1; /* avoid fp rounding error (1.000001 => sqrt error) */
364 #define RANDSIGN() ((random() & 1) ? 1 : -1)
367 #define BELLRAND(n) ((frand((n)) + frand((n)) + frand((n))) / 3)
370 build_ball (ModeInfo *mi)
372 ball_configuration *bp = &bps[MI_SCREEN(mi)];
373 int rows = MI_COUNT (mi);
375 GLfloat tile_size = M_PI / rows;
378 struct { XYZ position; GLfloat strength; } dents[5];
379 int dent_count = random() % countof(dents);
381 for (i = 0; i < dent_count; i++)
384 dents[i].position.x = RANDSIGN() * (2 - BELLRAND(0.2));
385 dents[i].position.y = RANDSIGN() * (2 - BELLRAND(0.2));
386 dents[i].position.z = RANDSIGN() * (2 - BELLRAND(0.2));
387 dist = sqrt (dents[i].position.x * dents[i].position.x +
388 dents[i].position.y * dents[i].position.y +
389 dents[i].position.z * dents[i].position.z);
390 dents[i].strength = dist - (1 - BELLRAND(0.3));
391 dents[i].strength = dist - (1 - BELLRAND(0.3));
395 for (th1 = M_PI/2; th1 > -(M_PI/2 + tile_size/2); th1 -= tile_size)
397 GLfloat x = cos (th1);
398 GLfloat y = sin (th1);
399 GLfloat x0 = cos (th1 - tile_size/2);
400 GLfloat x1 = cos (th1 + tile_size/2);
401 GLfloat circ0 = M_PI * x0 * 2;
402 GLfloat circ1 = M_PI * x1 * 2;
403 GLfloat circ = (circ0 < circ1 ? circ0 : circ1);
404 int row_tiles = floor ((circ < 0 ? 0 : circ) / tile_size);
406 GLfloat dropsy = 0.13 + frand(0.04);
408 if (row_tiles <= 0) row_tiles = 1;
409 spacing = M_PI*2 / row_tiles;
411 for (th0 = 0; th0 < M_PI*2; th0 += spacing)
413 tile *t = (tile *) calloc (1, sizeof(*t));
414 t->size = tile_size * 0.85;
415 t->position.x = cos (th0) * x;
416 t->position.y = sin (th0) * x;
419 t->normal = t->position;
421 /* Apply pressure on position from the dents. */
422 for (i = 0; i < dent_count; i++)
427 if (! (random() % 150)) /* Drop tiles randomly */
433 direction.x = t->position.x - dents[i].position.x;
434 direction.y = t->position.y - dents[i].position.y;
435 direction.z = t->position.z - dents[i].position.z;
436 dist = sqrt (direction.x * direction.x +
437 direction.y * direction.y +
438 direction.z * direction.z);
439 if (dist < dents[i].strength)
441 GLfloat s = 1 - (dents[i].strength - dist) * 0.66;
443 GLfloat angle = vector_angle (t->position, dents[i].position);
445 /* Drop out the tiles near the apex of the dent. */
456 direction = normalize (direction);
461 t->normal.x = (t->normal.x + n2.x) / 2;
462 t->normal.y = (t->normal.y + n2.y) / 2;
463 t->normal.z = (t->normal.z + n2.z) / 2;
467 /* Skew the direction the tile is facing slightly. */
468 t->normal.x += 0.12 - frand(0.06);
469 t->normal.y += 0.12 - frand(0.06);
470 t->normal.z += 0.12 - frand(0.06);
471 t->tilt = 4 - BELLRAND(8);
479 bp->nrays = 5 + BELLRAND(10);
480 bp->rays = (ray *) calloc (bp->nrays, sizeof(*bp->rays));
481 for (i = 0; i < bp->nrays; i++)
483 GLfloat th = frand(M_PI * 2);
484 bp->rays[i].normal.x = cos (th);
485 bp->rays[i].normal.y = sin (th);
486 bp->rays[i].normal.z = 1;
487 bp->rays[i].normal = normalize (bp->rays[i].normal);
488 bp->rays[i].color[0] = 0.9 + frand(0.1);
489 bp->rays[i].color[1] = 0.6 + frand(0.4);
490 bp->rays[i].color[2] = 0.6 + frand(0.2);
491 bp->rays[i].color[3] = 1;
496 /* Window management, etc
499 reshape_ball (ModeInfo *mi, int width, int height)
501 GLfloat h = (GLfloat) height / (GLfloat) width;
503 glViewport (0, 0, (GLint) width, (GLint) height);
505 glMatrixMode(GL_PROJECTION);
507 gluPerspective (30.0, 1/h, 1.0, 100.0);
509 glMatrixMode(GL_MODELVIEW);
511 gluLookAt( 0.0, 0.0, 30.0,
515 # ifdef HAVE_MOBILE /* Keep it the same relative size when rotated. */
517 int o = (int) current_device_rotation();
518 if (o != 0 && o != 180 && o != -180)
519 glScalef (1/h, 1/h, 1/h);
523 glClear(GL_COLOR_BUFFER_BIT);
528 ball_handle_event (ModeInfo *mi, XEvent *event)
530 ball_configuration *bp = &bps[MI_SCREEN(mi)];
532 if (gltrackball_event_handler (event, bp->trackball,
533 MI_WIDTH (mi), MI_HEIGHT (mi),
541 static void free_ball (ModeInfo *mi);
545 init_ball (ModeInfo *mi)
547 ball_configuration *bp;
548 int wire = MI_IS_WIREFRAME(mi);
550 MI_INIT (mi, bps, free_ball);
552 bp = &bps[MI_SCREEN(mi)];
554 bp->glx_context = init_GL(mi);
559 reshape_ball (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
561 bp->th = 180 - frand(360);
563 if (MI_COUNT(mi) < 10)
565 if (MI_COUNT(mi) > 200)
569 double spin_speed = 0.1;
570 double wander_speed = 0.003;
571 double spin_accel = 1;
573 bp->rot = make_rotator (do_spin ? spin_speed : 0,
574 do_spin ? spin_speed : 0,
575 do_spin ? spin_speed : 0,
577 do_wander ? wander_speed : 0,
579 bp->trackball = gltrackball_init (True);
586 GLfloat color[4] = {0.5, 0.5, 0.5, 1};
587 GLfloat cspec[4] = {1, 1, 1, 1};
588 static const GLfloat shiny = 10;
590 static GLfloat pos0[4] = { 0.5, -1, -0.5, 0};
591 static GLfloat pos1[4] = {-0.75, -1, 0, 0};
592 static GLfloat amb[4] = {0, 0, 0, 1};
593 static GLfloat dif[4] = {1, 1, 1, 1};
594 static GLfloat spc[4] = {1, 1, 1, 1};
596 glEnable(GL_LIGHTING);
599 glEnable(GL_DEPTH_TEST);
600 glEnable(GL_CULL_FACE);
602 color[0] += frand(0.2);
603 color[1] += frand(0.2);
604 color[2] += frand(0.2);
606 cspec[0] -= frand(0.2);
607 cspec[1] -= frand(0.2);
608 cspec[2] -= frand(0.2);
610 glLightfv(GL_LIGHT0, GL_POSITION, pos0);
611 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
612 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
613 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
615 glLightfv(GL_LIGHT1, GL_POSITION, pos1);
616 glLightfv(GL_LIGHT1, GL_AMBIENT, amb);
617 glLightfv(GL_LIGHT1, GL_DIFFUSE, dif);
618 glLightfv(GL_LIGHT1, GL_SPECULAR, spc);
620 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
621 glMaterialfv (GL_FRONT, GL_SPECULAR, cspec);
622 glMateriali (GL_FRONT, GL_SHININESS, shiny);
628 draw_ball (ModeInfo *mi)
630 ball_configuration *bp = &bps[MI_SCREEN(mi)];
631 Display *dpy = MI_DISPLAY(mi);
632 Window window = MI_WINDOW(mi);
634 if (!bp->glx_context)
637 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
639 glShadeModel(GL_SMOOTH);
641 glEnable(GL_DEPTH_TEST);
642 glEnable(GL_NORMALIZE);
643 glEnable(GL_CULL_FACE);
645 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
649 glRotatef(current_device_rotation(), 0, 0, 1);
653 get_position (bp->rot, &x, &y, &z, !bp->button_down_p);
654 glTranslatef((x - 0.5) * 6,
658 gltrackball_rotate (bp->trackball);
660 get_rotation (bp->rot, &x, &y, &z, !bp->button_down_p);
661 glRotatef (x * 360, 1.0, 0.0, 0.0);
662 glRotatef (y * 360, 0.0, 1.0, 0.0);
663 glRotatef (z * 360, 0.0, 0.0, 1.0);
666 mi->polygon_count = 0;
668 glRotatef (50, 1, 0, 0);
671 glRotatef (bp->th, 0, 0, 1);
672 if (! bp->button_down_p)
674 bp->th += (bp->th > 0 ? speed : -speed);
675 while (bp->th > 360) bp->th -= 360;
676 while (bp->th < -360) bp->th += 360;
679 mi->polygon_count += draw_ball_1 (mi);
682 if (mi->fps_p) do_fps (mi);
685 glXSwapBuffers(dpy, window);
690 free_ball (ModeInfo *mi)
692 ball_configuration *bp = &bps[MI_SCREEN(mi)];
695 tile *t = bp->tiles->next;
702 XSCREENSAVER_MODULE_2 ("Discoball", discoball, ball)