1 /* dangerball, Copyright (c) 2001 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 #include <X11/Intrinsic.h>
14 extern XtAppContext app;
16 #define PROGCLASS "DangerBall"
17 #define HACK_INIT init_ball
18 #define HACK_DRAW draw_ball
19 #define HACK_RESHAPE reshape_ball
20 #define sws_opts xlockmore_opts
22 #define DEF_SPIN "True"
23 #define DEF_WANDER "True"
24 #define DEF_SPEED "0.05"
26 #define DEFAULTS "*delay: 30000 \n" \
28 "*showFPS: False \n" \
29 "*wireframe: False \n" \
30 "*speed: " DEF_SPEED " \n" \
31 "*spin: " DEF_SPIN "\n" \
32 "*wander: " DEF_WANDER "\n" \
35 #define SPHERE_SLICES 32 /* how densely to render spheres */
36 #define SPHERE_STACKS 16
39 #define SPIKE_FACES 12 /* how densely to render spikes */
43 #define countof(x) (sizeof((x))/sizeof((*x)))
45 #include "xlockmore.h"
49 #ifdef USE_GL /* whole file */
52 # include <sys/utsname.h>
53 #endif /* HAVE_UNAME */
57 #include "glutstroke.h"
58 #include "glut_roman.h"
59 #define GLUT_FONT (&glutStrokeRoman)
63 GLXContext *glx_context;
65 GLfloat rotx, roty, rotz; /* current object rotation */
66 GLfloat dx, dy, dz; /* current rotational velocity */
67 GLfloat ddx, ddy, ddz; /* current rotational acceleration */
68 GLfloat d_max; /* max velocity */
83 static ball_configuration *bps = NULL;
87 static Bool do_wander;
89 static XrmOptionDescRec opts[] = {
90 { "-spin", ".spin", XrmoptionNoArg, "True" },
91 { "+spin", ".spin", XrmoptionNoArg, "False" },
92 { "-speed", ".speed", XrmoptionSepArg, 0 },
93 { "-wander", ".wander", XrmoptionNoArg, "True" },
94 { "+wander", ".wander", XrmoptionNoArg, "False" }
97 static argtype vars[] = {
98 {(caddr_t *) &do_spin, "spin", "Spin", DEF_SPIN, t_Bool},
99 {(caddr_t *) &do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
100 {(caddr_t *) &speed, "speed", "Speed", DEF_SPEED, t_Float},
103 ModeSpecOpt sws_opts = {countof(opts), opts, countof(vars), vars, NULL};
108 unit_spike (Bool wire)
111 int faces = SPIKE_FACES;
112 GLfloat step = M_PI * 2 / faces;
116 glBegin(wire ? GL_LINE_STRIP : GL_TRIANGLE_FAN);
120 for (i = 0, th = 0; i <= faces; i++)
122 GLfloat x = cos (th);
123 GLfloat y = sin (th);
126 if (wire) glVertex3f(0, 1, 0);
133 /* lifted from glplanet */
134 /* Function for determining points on the surface of the sphere */
136 parametric_sphere (float theta, float rho, GLfloat *vector)
138 vector[0] = -sin(theta) * sin(rho);
139 vector[1] = cos(theta) * sin(rho);
140 vector[2] = cos(rho);
143 /* lifted from glplanet */
145 unit_sphere (Bool wire)
147 int stacks = SPHERE_STACKS;
148 int slices = SPHERE_SLICES;
154 GLfloat ds, dt, t, s;
157 glShadeModel(GL_SMOOTH);
159 /* Generate a sphere with quadrilaterals.
160 * Quad vertices are determined using a parametric sphere function.
161 * For fun, you could generate practically any parameteric surface and
162 * map an image onto it.
164 drho = M_PI / stacks;
165 dtheta = 2.0 * M_PI / slices;
170 glBegin( wire ? GL_LINE_LOOP : GL_QUADS );
173 for (i=0; i < stacks; i++) {
176 for (j=0; j < slices; j++) {
180 parametric_sphere (theta, rho, vector);
181 glNormal3fv (vector);
182 parametric_sphere (theta, rho, vector);
183 glVertex3f (vector[0], vector[1], vector[2]);
185 glTexCoord2f (s,t+dt);
186 parametric_sphere (theta, rho+drho, vector);
187 glNormal3fv (vector);
188 parametric_sphere (theta, rho+drho, vector);
189 glVertex3f (vector[0], vector[1], vector[2]);
191 glTexCoord2f (s+ds,t+dt);
192 parametric_sphere (theta + dtheta, rho+drho, vector);
193 glNormal3fv (vector);
194 parametric_sphere (theta + dtheta, rho+drho, vector);
195 glVertex3f (vector[0], vector[1], vector[2]);
197 glTexCoord2f (s+ds, t);
198 parametric_sphere (theta + dtheta, rho, vector);
199 glNormal3fv (vector);
200 parametric_sphere (theta + dtheta, rho, vector);
201 glVertex3f (vector[0], vector[1], vector[2]);
212 /* Window management, etc
215 reshape_ball (ModeInfo *mi, int width, int height)
217 GLfloat h = (GLfloat) height / (GLfloat) width;
219 glViewport (0, 0, (GLint) width, (GLint) height);
221 glMatrixMode(GL_PROJECTION);
224 gluPerspective( 30.0, 1/h, 1.0, 100.0 );
225 gluLookAt( 0.0, 0.0, 15.0,
228 glMatrixMode(GL_MODELVIEW);
230 glTranslatef(0.0, 0.0, -15.0);
232 glClear(GL_COLOR_BUFFER_BIT);
237 gl_init (ModeInfo *mi)
239 /* ball_configuration *bp = &bps[MI_SCREEN(mi)]; */
240 int wire = MI_IS_WIREFRAME(mi);
242 static GLfloat pos[4] = {5.0, 5.0, 10.0, 1.0};
246 glLightfv(GL_LIGHT0, GL_POSITION, pos);
247 glEnable(GL_CULL_FACE);
248 glEnable(GL_LIGHTING);
250 glEnable(GL_DEPTH_TEST);
255 /* lifted from lament.c */
256 #define RAND(n) ((long) ((random() & 0x7fffffff) % ((long) (n))))
257 #define RANDSIGN() ((random() & 1) ? 1 : -1)
260 rotate(GLfloat *pos, GLfloat *v, GLfloat *dv, GLfloat max_v)
275 if (ppos < 0) abort();
276 if (ppos > 1.0) abort();
277 *pos = (*pos > 0 ? ppos : -ppos);
283 if (*v > max_v || *v < -max_v)
287 /* If it stops, start it going in the other direction. */
294 /* keep going in the same direction */
309 /* Alter direction of rotational acceleration randomly. */
310 if (! (random() % 120))
313 /* Change acceleration very occasionally. */
314 if (! (random() % 200))
318 else if (random() & 1)
327 randomize_spikes (ModeInfo *mi)
329 ball_configuration *bp = &bps[MI_SCREEN(mi)];
332 for (i = 0; i < MI_COUNT(mi); i++)
334 bp->spikes[i*2] = (random() % 360) - 180;
335 bp->spikes[i*2+1] = (random() % 180) - 90;
338 # define ROT_SCALE 22
339 for (i = 0; i < MI_COUNT(mi) * 2; i++)
340 bp->spikes[i] = (bp->spikes[i] / ROT_SCALE) * ROT_SCALE;
342 if ((random() % 3) == 0)
343 bp->color_shift = random() % (bp->ncolors / 2);
349 draw_spikes (ModeInfo *mi)
351 ball_configuration *bp = &bps[MI_SCREEN(mi)];
353 GLfloat pos = bp->pos;
356 if (pos < 0) pos = -pos;
358 pos = (asin (0.5 + pos/2) - 0.5) * 2;
360 for (i = 0; i < MI_COUNT(mi); i++)
363 glRotatef(bp->spikes[i*2], 0, 1, 0);
364 glRotatef(bp->spikes[i*2+1], 0, 0, 1);
365 glTranslatef(0.7, 0, 0);
366 glRotatef(-90, 0, 0, 1);
367 glScalef (diam, pos, diam);
368 glCallList (bp->spike_list);
375 move_spikes (ModeInfo *mi)
377 ball_configuration *bp = &bps[MI_SCREEN(mi)];
379 if (bp->pos >= 0) /* moving outward */
382 if (bp->pos >= 1) /* reverse gears at apex */
385 else /* moving inward */
388 if (bp->pos >= 0) /* stop at end */
389 randomize_spikes (mi);
395 init_ball (ModeInfo *mi)
397 ball_configuration *bp;
398 int wire = MI_IS_WIREFRAME(mi);
401 bps = (ball_configuration *)
402 calloc (MI_NUM_SCREENS(mi), sizeof (ball_configuration));
404 fprintf(stderr, "%s: out of memory\n", progname);
408 bp = &bps[MI_SCREEN(mi)];
411 bp = &bps[MI_SCREEN(mi)];
413 if ((bp->glx_context = init_GL(mi)) != NULL) {
415 reshape_ball (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
418 bp->rotx = frand(1.0) * RANDSIGN();
419 bp->roty = frand(1.0) * RANDSIGN();
420 bp->rotz = frand(1.0) * RANDSIGN();
422 /* bell curve from 0-6 degrees, avg 3 */
423 bp->dx = (frand(2) + frand(2) + frand(2)) / (360/2);
424 bp->dy = (frand(2) + frand(2) + frand(2)) / (360/2);
425 bp->dz = (frand(2) + frand(2) + frand(2)) / (360/2);
427 bp->d_max = bp->dx * 2;
429 bp->ddx = 0.00006 + frand(0.00003);
430 bp->ddy = 0.00006 + frand(0.00003);
431 bp->ddz = 0.00006 + frand(0.00003);
434 bp->colors = (XColor *) calloc(bp->ncolors, sizeof(XColor));
435 make_smooth_colormap (0, 0, 0,
436 bp->colors, &bp->ncolors,
439 bp->spikes = (int *) calloc(MI_COUNT(mi), sizeof(*bp->spikes) * 2);
441 bp->ball_list = glGenLists (1);
442 bp->spike_list = glGenLists (1);
444 glNewList (bp->ball_list, GL_COMPILE);
448 glNewList (bp->spike_list, GL_COMPILE);
452 randomize_spikes (mi);
457 draw_ball (ModeInfo *mi)
459 ball_configuration *bp = &bps[MI_SCREEN(mi)];
460 Display *dpy = MI_DISPLAY(mi);
461 Window window = MI_WINDOW(mi);
464 static GLfloat color1[4] = {0.0, 0.0, 0.0, 1.0};
465 static GLfloat color2[4] = {0.0, 0.0, 0.0, 1.0};
467 if (!bp->glx_context)
470 glShadeModel(GL_SMOOTH);
472 glEnable(GL_DEPTH_TEST);
473 glEnable(GL_NORMALIZE);
474 glEnable(GL_CULL_FACE);
476 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
480 glScalef(1.1, 1.1, 1.1);
487 static int frame = 0;
489 # define SINOID(SCALE,SIZE) \
490 ((((1 + sin((frame * (SCALE)) / 2 * M_PI)) / 2.0) * (SIZE)) - (SIZE)/2)
492 x = SINOID(0.051, 8.0);
493 y = SINOID(0.037, 8.0);
494 z = SINOID(0.131, 13.0);
496 glTranslatef(x, y, z);
504 if (x < 0) x = 1 - (x + 1);
505 if (y < 0) y = 1 - (y + 1);
506 if (z < 0) z = 1 - (z + 1);
508 glRotatef(x * 360, 1.0, 0.0, 0.0);
509 glRotatef(y * 360, 0.0, 1.0, 0.0);
510 glRotatef(z * 360, 0.0, 0.0, 1.0);
512 rotate(&bp->rotx, &bp->dx, &bp->ddx, bp->d_max);
513 rotate(&bp->roty, &bp->dy, &bp->ddy, bp->d_max);
514 rotate(&bp->rotz, &bp->dz, &bp->ddz, bp->d_max);
518 color1[0] = bp->colors[bp->ccolor].red / 65536.0;
519 color1[1] = bp->colors[bp->ccolor].green / 65536.0;
520 color1[2] = bp->colors[bp->ccolor].blue / 65536.0;
522 c2 = (bp->ccolor + bp->color_shift) % bp->ncolors;
523 color2[0] = bp->colors[c2].red / 65536.0;
524 color2[1] = bp->colors[c2].green / 65536.0;
525 color2[2] = bp->colors[c2].blue / 65536.0;
528 if (bp->ccolor >= bp->ncolors) bp->ccolor = 0;
530 glScalef (2.0, 2.0, 2.0);
534 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color1);
535 glCallList (bp->ball_list);
537 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color2);
541 if (mi->fps_p) do_fps (mi);
544 glXSwapBuffers(dpy, window);