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
19 #define countof(x) (sizeof((x))/sizeof((*x)))
21 #include "xlockmore.h"
24 #include "gltrackball.h"
27 #ifdef USE_GL /* whole file */
30 #define DEF_SPIN "False"
31 #define DEF_WANDER "True"
32 #define DEF_SPEED "1.0"
34 typedef struct tile tile;
48 GLXContext *glx_context;
51 trackball_state *trackball;
58 static ball_configuration *bps = NULL;
62 static Bool do_wander;
64 static XrmOptionDescRec opts[] = {
65 { "-spin", ".spin", XrmoptionNoArg, "True" },
66 { "+spin", ".spin", XrmoptionNoArg, "False" },
67 { "-speed", ".speed", XrmoptionSepArg, 0 },
68 { "-wander", ".wander", XrmoptionNoArg, "True" },
69 { "+wander", ".wander", XrmoptionNoArg, "False" }
72 static argtype vars[] = {
73 {&do_spin, "spin", "Spin", DEF_SPIN, t_Bool},
74 {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
75 {&speed, "speed", "Speed", DEF_SPEED, t_Float},
78 ENTRYPOINT ModeSpecOpt ball_opts = {countof(opts), opts, countof(vars), vars, NULL};
84 GLfloat d = sqrt(p.x*p.x + p.y*p.y * p.z*p.z);
99 build_texture (ModeInfo *mi)
104 unsigned char *data = malloc (bpl * size);
106 for (y = 0; y < size; y++)
108 for (x = 0; x < size; x++)
110 unsigned char *c = &data [y * bpl + x * 2];
111 GLfloat X = (x / (GLfloat) (size-1)) - 0.5;
112 GLfloat Y = (y / (GLfloat) (size-1)) - 0.5;
113 X = cos (X * X * 6.2);
114 Y = cos (Y * Y * 6.2);
122 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
123 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
124 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
125 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
126 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
127 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
128 check_gl_error ("texture param");
130 glTexImage2D (GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, size, size, 0,
131 GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, data);
132 check_gl_error ("light texture");
138 draw_rays (ModeInfo *mi)
140 ball_configuration *bp = &bps[MI_SCREEN(mi)];
141 int wire = MI_IS_WIREFRAME(mi);
145 glEnable (GL_TEXTURE_2D);
146 glDisable (GL_LIGHTING);
148 glDisable (GL_DEPTH_TEST);
149 glBlendFunc (GL_SRC_ALPHA, GL_ONE);
151 for (i = 0; i < bp->nrays; i++)
153 GLfloat x = bp->rays[i].normal.x;
154 GLfloat y = bp->rays[i].normal.y;
155 GLfloat z = bp->rays[i].normal.z;
158 /* Orient to direction of ray. */
159 glRotatef (-atan2 (x, y) * (180 / M_PI), 0, 0, 1);
160 glRotatef ( atan2 (z, sqrt(x*x + y*y)) * (180 / M_PI), 1, 0, 0);
163 glTranslatef(0, 0, 1.1);
164 glColor4fv (bp->rays[i].color);
165 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
166 glTexCoord2f (0, 0); glVertex3f (-0.5, 0, -1);
167 glTexCoord2f (1, 0); glVertex3f ( 0.5, 0, -1);
168 glTexCoord2f (1, 1); glVertex3f ( 0.5, 0, 1);
169 glTexCoord2f (0, 1); glVertex3f (-0.5, 0, 1);
175 glDisable (GL_TEXTURE_2D);
176 glEnable (GL_LIGHTING);
177 glDisable (GL_BLEND);
178 glEnable (GL_DEPTH_TEST);
186 draw_ball_1 (ModeInfo *mi)
188 ball_configuration *bp = &bps[MI_SCREEN(mi)];
189 int wire = MI_IS_WIREFRAME(mi);
194 glGetFloatv (GL_MODELVIEW_MATRIX, &m[0][0]);
199 /* Draw the back rays.
205 glMultMatrixf (&m[0][0]);
206 glTranslatef(0, 0, -4.1);
207 glRotatef (bp->th, 0, 0, 1);
208 polys += draw_rays (mi);
211 glClear(GL_DEPTH_BUFFER_BIT);
215 /* Instead of rendering polygons for the foam ball substrate, let's
216 just billboard a quad down the middle to mask out the back-facing
220 m[0][0] = 1; m[1][0] = 0; m[2][0] = 0;
221 m[0][1] = 0; m[1][1] = 1; m[2][1] = 0;
222 m[0][2] = 0; m[1][2] = 0; m[2][2] = 1;
224 glMultMatrixf (&m[0][0]);
225 glScalef (40, 40, 40);
226 glTranslatef (-0.5, -0.5, -0.01);
228 glDisable (GL_LIGHTING);
229 /* Draw into the depth buffer but not the frame buffer */
230 glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
233 glVertex3f (0, 0, 0);
234 glVertex3f (0, 1, 0);
235 glVertex3f (1, 1, 0);
236 glVertex3f (1, 0, 0);
239 glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
241 glEnable (GL_LIGHTING);
246 /* Draw all the tiles.
248 for (t = bp->tiles; t; t = t->next)
250 GLfloat x = t->normal.x;
251 GLfloat y = t->normal.y;
252 GLfloat z = t->normal.z;
253 GLfloat s = t->size / 2;
256 /* Move to location of tile. */
257 glTranslatef (t->position.x, t->position.y, t->position.z);
259 /* Orient to direction tile is facing. */
260 glRotatef (-atan2 (x, y) * (180 / M_PI), 0, 0, 1);
261 glRotatef ( atan2 (z, sqrt(x*x + y*y)) * (180 / M_PI), 1, 0, 0);
263 glRotatef (t->tilt, 0, 1, 0);
266 glNormal3f (0, 1, 0);
267 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
268 glVertex3f (-1, 0, -1);
269 glVertex3f ( 1, 0, -1);
270 glVertex3f ( 1, 0, 1);
271 glVertex3f (-1, 0, 1);
278 glNormal3f (0, 0, -1);
279 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
280 glVertex3f (-1, 0, -1);
281 glVertex3f (-1, -d, -1);
282 glVertex3f ( 1, -d, -1);
283 glVertex3f ( 1, 0, -1);
287 glNormal3f (0, 0, 1);
288 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
289 glVertex3f ( 1, 0, 1);
290 glVertex3f ( 1, -d, 1);
291 glVertex3f (-1, -d, 1);
292 glVertex3f (-1, 0, 1);
296 glNormal3f (1, 0, 0);
297 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
298 glVertex3f ( 1, 0, -1);
299 glVertex3f ( 1, -d, -1);
300 glVertex3f ( 1, -d, 1);
301 glVertex3f ( 1, 0, 1);
305 glNormal3f (-1, 0, 0);
306 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
307 glVertex3f (-1, 0, 1);
308 glVertex3f (-1, -d, 1);
309 glVertex3f (-1, -d, -1);
310 glVertex3f (-1, 0, -1);
318 /* Draw the front rays.
324 glMultMatrixf (&m[0][0]);
325 glTranslatef(0, 0, 4.1);
326 glRotatef (-bp->th, 0, 0, 1);
327 polys += draw_rays (mi);
336 vector_angle (XYZ a, XYZ b)
338 double La = sqrt (a.x*a.x + a.y*a.y + a.z*a.z);
339 double Lb = sqrt (b.x*b.x + b.y*b.y + b.z*b.z);
342 if (La == 0 || Lb == 0) return 0;
343 if (a.x == b.x && a.y == b.y && a.z == b.z) return 0;
345 /* dot product of two vectors is defined as:
346 La * Lb * cos(angle between vectors)
347 and is also defined as:
348 ax*bx + ay*by + az*bz
350 La * Lb * cos(angle) = ax*bx + ay*by + az*bz
351 cos(angle) = (ax*bx + ay*by + az*bz) / (La * Lb)
352 angle = acos ((ax*bx + ay*by + az*bz) / (La * Lb));
354 cc = (a.x*b.x + a.y*b.y + a.z*b.z) / (La * Lb);
355 if (cc > 1) cc = 1; /* avoid fp rounding error (1.000001 => sqrt error) */
363 #define RANDSIGN() ((random() & 1) ? 1 : -1)
366 #define BELLRAND(n) ((frand((n)) + frand((n)) + frand((n))) / 3)
369 build_ball (ModeInfo *mi)
371 ball_configuration *bp = &bps[MI_SCREEN(mi)];
372 int rows = MI_COUNT (mi);
374 GLfloat tile_size = M_PI / rows;
377 struct { XYZ position; GLfloat strength; } dents[5];
378 int dent_count = random() % countof(dents);
380 for (i = 0; i < dent_count; i++)
383 dents[i].position.x = RANDSIGN() * (2 - BELLRAND(0.2));
384 dents[i].position.y = RANDSIGN() * (2 - BELLRAND(0.2));
385 dents[i].position.z = RANDSIGN() * (2 - BELLRAND(0.2));
386 dist = sqrt (dents[i].position.x * dents[i].position.x +
387 dents[i].position.y * dents[i].position.y +
388 dents[i].position.z * dents[i].position.z);
389 dents[i].strength = dist - (1 - BELLRAND(0.3));
390 dents[i].strength = dist - (1 - BELLRAND(0.3));
394 for (th1 = M_PI/2; th1 > -(M_PI/2 + tile_size/2); th1 -= tile_size)
396 GLfloat x = cos (th1);
397 GLfloat y = sin (th1);
398 GLfloat x0 = cos (th1 - tile_size/2);
399 GLfloat x1 = cos (th1 + tile_size/2);
400 GLfloat circ0 = M_PI * x0 * 2;
401 GLfloat circ1 = M_PI * x1 * 2;
402 GLfloat circ = (circ0 < circ1 ? circ0 : circ1);
403 int row_tiles = floor ((circ < 0 ? 0 : circ) / tile_size);
405 GLfloat dropsy = 0.13 + frand(0.04);
407 if (row_tiles <= 0) row_tiles = 1;
408 spacing = M_PI*2 / row_tiles;
410 for (th0 = 0; th0 < M_PI*2; th0 += spacing)
412 tile *t = (tile *) calloc (1, sizeof(*t));
413 t->size = tile_size * 0.85;
414 t->position.x = cos (th0) * x;
415 t->position.y = sin (th0) * x;
418 t->normal = t->position;
420 /* Apply pressure on position from the dents. */
421 for (i = 0; i < dent_count; i++)
426 if (! (random() % 150)) /* Drop tiles randomly */
432 direction.x = t->position.x - dents[i].position.x;
433 direction.y = t->position.y - dents[i].position.y;
434 direction.z = t->position.z - dents[i].position.z;
435 dist = sqrt (direction.x * direction.x +
436 direction.y * direction.y +
437 direction.z * direction.z);
438 if (dist < dents[i].strength)
440 GLfloat s = 1 - (dents[i].strength - dist) * 0.66;
442 GLfloat angle = vector_angle (t->position, dents[i].position);
444 /* Drop out the tiles near the apex of the dent. */
455 direction = normalize (direction);
460 t->normal.x = (t->normal.x + n2.x) / 2;
461 t->normal.y = (t->normal.y + n2.y) / 2;
462 t->normal.z = (t->normal.z + n2.z) / 2;
466 /* Skew the direction the tile is facing slightly. */
467 t->normal.x += 0.12 - frand(0.06);
468 t->normal.y += 0.12 - frand(0.06);
469 t->normal.z += 0.12 - frand(0.06);
470 t->tilt = 4 - BELLRAND(8);
478 bp->nrays = 5 + BELLRAND(10);
479 bp->rays = (ray *) calloc (bp->nrays, sizeof(*bp->rays));
480 for (i = 0; i < bp->nrays; i++)
482 GLfloat th = frand(M_PI * 2);
483 bp->rays[i].normal.x = cos (th);
484 bp->rays[i].normal.y = sin (th);
485 bp->rays[i].normal.z = 1;
486 bp->rays[i].normal = normalize (bp->rays[i].normal);
487 bp->rays[i].color[0] = 0.9 + frand(0.1);
488 bp->rays[i].color[1] = 0.6 + frand(0.4);
489 bp->rays[i].color[2] = 0.6 + frand(0.2);
490 bp->rays[i].color[3] = 1;
495 /* Window management, etc
498 reshape_ball (ModeInfo *mi, int width, int height)
500 GLfloat h = (GLfloat) height / (GLfloat) width;
502 glViewport (0, 0, (GLint) width, (GLint) height);
504 glMatrixMode(GL_PROJECTION);
506 gluPerspective (30.0, 1/h, 1.0, 100.0);
508 glMatrixMode(GL_MODELVIEW);
510 gluLookAt( 0.0, 0.0, 30.0,
514 # ifdef HAVE_MOBILE /* Keep it the same relative size when rotated. */
516 int o = (int) current_device_rotation();
517 if (o != 0 && o != 180 && o != -180)
518 glScalef (1/h, 1/h, 1/h);
522 glClear(GL_COLOR_BUFFER_BIT);
527 ball_handle_event (ModeInfo *mi, XEvent *event)
529 ball_configuration *bp = &bps[MI_SCREEN(mi)];
531 if (gltrackball_event_handler (event, bp->trackball,
532 MI_WIDTH (mi), MI_HEIGHT (mi),
541 init_ball (ModeInfo *mi)
543 ball_configuration *bp;
544 int wire = MI_IS_WIREFRAME(mi);
547 bps = (ball_configuration *)
548 calloc (MI_NUM_SCREENS(mi), sizeof (ball_configuration));
550 fprintf(stderr, "%s: out of memory\n", progname);
555 bp = &bps[MI_SCREEN(mi)];
557 bp->glx_context = init_GL(mi);
562 reshape_ball (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
564 bp->th = 180 - frand(360);
566 if (MI_COUNT(mi) < 10)
568 if (MI_COUNT(mi) > 200)
572 double spin_speed = 0.1;
573 double wander_speed = 0.003;
574 double spin_accel = 1;
576 bp->rot = make_rotator (do_spin ? spin_speed : 0,
577 do_spin ? spin_speed : 0,
578 do_spin ? spin_speed : 0,
580 do_wander ? wander_speed : 0,
582 bp->trackball = gltrackball_init (True);
589 GLfloat color[4] = {0.5, 0.5, 0.5, 1};
590 GLfloat cspec[4] = {1, 1, 1, 1};
591 static const GLfloat shiny = 10;
593 static GLfloat pos0[4] = { 0.5, -1, -0.5, 0};
594 static GLfloat pos1[4] = {-0.75, -1, 0, 0};
595 static GLfloat amb[4] = {0, 0, 0, 1};
596 static GLfloat dif[4] = {1, 1, 1, 1};
597 static GLfloat spc[4] = {1, 1, 1, 1};
599 glEnable(GL_LIGHTING);
602 glEnable(GL_DEPTH_TEST);
603 glEnable(GL_CULL_FACE);
605 color[0] += frand(0.2);
606 color[1] += frand(0.2);
607 color[2] += frand(0.2);
609 cspec[0] -= frand(0.2);
610 cspec[1] -= frand(0.2);
611 cspec[2] -= frand(0.2);
613 glLightfv(GL_LIGHT0, GL_POSITION, pos0);
614 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
615 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
616 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
618 glLightfv(GL_LIGHT1, GL_POSITION, pos1);
619 glLightfv(GL_LIGHT1, GL_AMBIENT, amb);
620 glLightfv(GL_LIGHT1, GL_DIFFUSE, dif);
621 glLightfv(GL_LIGHT1, GL_SPECULAR, spc);
623 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
624 glMaterialfv (GL_FRONT, GL_SPECULAR, cspec);
625 glMateriali (GL_FRONT, GL_SHININESS, shiny);
631 draw_ball (ModeInfo *mi)
633 ball_configuration *bp = &bps[MI_SCREEN(mi)];
634 Display *dpy = MI_DISPLAY(mi);
635 Window window = MI_WINDOW(mi);
637 if (!bp->glx_context)
640 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
642 glShadeModel(GL_SMOOTH);
644 glEnable(GL_DEPTH_TEST);
645 glEnable(GL_NORMALIZE);
646 glEnable(GL_CULL_FACE);
648 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
652 glRotatef(current_device_rotation(), 0, 0, 1);
656 get_position (bp->rot, &x, &y, &z, !bp->button_down_p);
657 glTranslatef((x - 0.5) * 6,
661 gltrackball_rotate (bp->trackball);
663 get_rotation (bp->rot, &x, &y, &z, !bp->button_down_p);
664 glRotatef (x * 360, 1.0, 0.0, 0.0);
665 glRotatef (y * 360, 0.0, 1.0, 0.0);
666 glRotatef (z * 360, 0.0, 0.0, 1.0);
669 mi->polygon_count = 0;
671 glRotatef (50, 1, 0, 0);
674 glRotatef (bp->th, 0, 0, 1);
675 if (! bp->button_down_p)
677 bp->th += (bp->th > 0 ? speed : -speed);
678 while (bp->th > 360) bp->th -= 360;
679 while (bp->th < -360) bp->th += 360;
682 mi->polygon_count += draw_ball_1 (mi);
685 if (mi->fps_p) do_fps (mi);
688 glXSwapBuffers(dpy, window);
693 release_ball (ModeInfo *mi)
695 ball_configuration *bp = &bps[MI_SCREEN(mi)];
698 tile *t = bp->tiles->next;
705 XSCREENSAVER_MODULE_2 ("Discoball", discoball, ball)