1 /* boing, Copyright (c) 2005-2008 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 * A clone of the Amiga 1000 "Boing" demo. This was the first graphics demo
12 * for the Amiga, written by Dale Luck and RJ Mical during a break at the 1984
13 * Consumer Electronics Show (or so the legend goes.) The boing ball was
14 * briefly the official logo of Amiga Inc., until they were bought by
15 * Commodore later that year.
17 * With no arguments, this program looks a lot like the original Amiga demo.
18 * With "-smooth -lighting", it looks... less old.
20 * The amiga version made noise when the ball hit the walls. This version
21 * does not, obviously.
24 #define DEFAULTS "*delay: 30000 \n" \
25 "*showFPS: False \n" \
26 "*wireframe: False \n" \
28 # define refresh_boing 0
29 # define release_boing 0
31 #define countof(x) (sizeof((x))/sizeof((*x)))
33 #include "xlockmore.h"
34 #include "gltrackball.h"
37 #ifdef USE_GL /* whole file */
40 #define DEF_SPIN "True"
41 #define DEF_LIGHTING "False"
42 #define DEF_SMOOTH "False"
43 #define DEF_SCANLINES "True"
44 #define DEF_SPEED "1.0"
45 #define DEF_BALL_SIZE "0.5"
46 #define DEF_ANGLE "15"
47 #define DEF_MERIDIANS "16"
48 #define DEF_PARALLELS "8"
49 #define DEF_TILES "12"
50 #define DEF_THICKNESS "0.05"
52 #define DEF_BALL_COLOR1 "#CC1919"
53 #define DEF_BALL_COLOR2 "#F2F2F2"
54 #define DEF_GRID_COLOR "#991999"
55 #define DEF_SHADOW_COLOR "#303030"
56 #define DEF_BACKGROUND "#8C8C8C"
58 typedef struct { GLfloat x, y, z; } XYZ;
61 GLXContext *glx_context;
62 trackball_state *trackball;
66 double ball_x, ball_y, ball_z, ball_th;
67 double ball_dx, ball_dy, ball_dz, ball_dth;
68 double ball_ddx, ball_ddy, ball_ddz;
70 GLfloat ball_color1[4], ball_color2[4], grid_color[4];
71 GLfloat bg_color[4], shadow_color[4];
74 } boing_configuration;
76 static boing_configuration *bps = NULL;
79 static Bool lighting_p;
81 static Bool scanlines_p;
84 static GLfloat ball_size;
85 static unsigned int meridians;
86 static unsigned int parallels;
87 static unsigned int tiles;
88 static GLfloat thickness;
89 static char *ball_color1_str, *ball_color2_str, *grid_color_str,
92 static XrmOptionDescRec opts[] = {
93 { "-spin", ".spin", XrmoptionNoArg, "True" },
94 { "+spin", ".spin", XrmoptionNoArg, "False" },
95 { "-lighting", ".lighting", XrmoptionNoArg, "True" },
96 { "+lighting", ".lighting", XrmoptionNoArg, "False" },
97 { "-smooth", ".smooth", XrmoptionNoArg, "True" },
98 { "+smooth", ".smooth", XrmoptionNoArg, "False" },
99 { "-scanlines", ".scanlines", XrmoptionNoArg, "True" },
100 { "+scanlines", ".scanlines", XrmoptionNoArg, "False" },
101 { "-speed", ".speed", XrmoptionSepArg, 0 },
102 { "-angle", ".angle", XrmoptionSepArg, 0 },
103 { "-size", ".ballSize", XrmoptionSepArg, 0 },
104 { "-meridians", ".meridians", XrmoptionSepArg, 0 },
105 { "-parallels", ".parallels", XrmoptionSepArg, 0 },
106 { "-tiles", ".tiles", XrmoptionSepArg, 0 },
107 { "-thickness", ".thickness", XrmoptionSepArg, 0 },
108 { "-ball-color1",".ballColor1",XrmoptionSepArg, 0 },
109 { "-ball-color2",".ballColor2",XrmoptionSepArg, 0 },
110 { "-grid-color", ".gridColor", XrmoptionSepArg, 0 },
111 { "-shadow-color",".shadowColor",XrmoptionSepArg, 0 },
114 static argtype vars[] = {
115 {&spin, "spin", "Spin", DEF_SPIN, t_Bool},
116 {&lighting_p,"lighting", "Lighting", DEF_LIGHTING, t_Bool},
117 {&smooth_p, "smooth", "Smooth", DEF_SMOOTH, t_Bool},
118 {&scanlines_p,"scanlines","Scanlines", DEF_SCANLINES, t_Bool},
119 {&speed, "speed", "Speed", DEF_SPEED, t_Float},
120 {&angle, "angle", "Angle", DEF_ANGLE, t_Int},
121 {&ball_size, "ballSize", "BallSize", DEF_BALL_SIZE, t_Float},
122 {&meridians, "meridians", "meridians", DEF_MERIDIANS, t_Int},
123 {¶llels, "parallels", "parallels", DEF_PARALLELS, t_Int},
124 {&tiles, "tiles", "Tiles", DEF_TILES, t_Int},
125 {&thickness, "thickness", "Thickness", DEF_THICKNESS, t_Float},
126 {&ball_color1_str, "ballColor1", "BallColor1", DEF_BALL_COLOR1, t_String},
127 {&ball_color2_str, "ballColor2", "BallColor2", DEF_BALL_COLOR2, t_String},
128 {&grid_color_str, "gridColor", "GridColor", DEF_GRID_COLOR, t_String},
129 {&shadow_str, "shadowColor","ShadowColor",DEF_SHADOW_COLOR,t_String},
130 /* dammit, -background is too magic... */
131 {&bg_str, "boingBackground", "Background", DEF_BACKGROUND, t_String},
134 ENTRYPOINT ModeSpecOpt boing_opts = {countof(opts), opts, countof(vars), vars, NULL};
137 parse_color (ModeInfo *mi, const char *name, const char *s, GLfloat *a)
140 a[3] = 1.0; /* alpha */
142 if (! XParseColor (MI_DISPLAY(mi), MI_COLORMAP(mi), s, &c))
144 fprintf (stderr, "%s: can't parse %s color %s", progname, name, s);
147 a[0] = c.red / 65536.0;
148 a[1] = c.green / 65536.0;
149 a[2] = c.blue / 65536.0;
154 draw_grid (ModeInfo *mi)
156 boing_configuration *bp = &bps[MI_SCREEN(mi)];
158 GLfloat t2 = (GLfloat) tiles / 2;
159 GLfloat s = 1.0 / (tiles + thickness);
162 GLfloat lw = MI_HEIGHT(mi) * 0.06 * thickness;
164 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, bp->grid_color);
165 glColor3fv (bp->grid_color);
169 glTranslatef (-t2, -t2, 0);
173 for (y = 0; y <= tiles; y++)
175 glVertex3f (0, y, z);
176 glVertex3f (tiles, y, z);
177 /*mi->polygon_count++;*/
179 for (x = 0; x <= tiles; x++)
181 glVertex3f (x, tiles, z);
182 glVertex3f (x, 0, z);
183 /*mi->polygon_count++;*/
192 draw_box (ModeInfo *mi)
194 /* boing_configuration *bp = &bps[MI_SCREEN(mi)]; */
196 glTranslatef (0, 0, -0.5);
197 /* glFrontFace (GL_CCW);*/
202 glRotatef (90, 1, 0, 0);
203 glTranslatef (0, 0, 0.5);
204 /* glFrontFace (GL_CW);*/
211 draw_ball (ModeInfo *mi)
213 boing_configuration *bp = &bps[MI_SCREEN(mi)];
214 int wire = MI_IS_WIREFRAME(mi);
218 int scale = (smooth_p ? 5 : 1);
220 if (lighting_p && !wire)
221 glEnable (GL_LIGHTING);
232 glTranslatef (bp->ball_x, bp->ball_y, bp->ball_z);
233 glScalef (ball_size, ball_size, ball_size);
234 glRotatef (-angle, 0, 0, 1);
235 glRotatef (bp->ball_th, 0, 1, 0);
237 for (y = 0; y < yy; y++)
239 GLfloat thy0 = y * (M_PI * 2) / (yy * 2) + M_PI_2;
240 GLfloat thy1 = (y+1) * (M_PI * 2) / (yy * 2) + M_PI_2;
242 for (x = 0; x < xx; x++)
244 GLfloat thx0 = x * (M_PI * 2) / xx;
245 GLfloat thx1 = (x+1) * (M_PI * 2) / xx;
247 Bool bgp = ((x/scale) & 1) ^ ((y/scale) & 1);
249 if (wire && bgp) continue;
251 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE,
252 (bgp ? bp->ball_color2 : bp->ball_color1));
253 glColor3fv (bgp ? bp->ball_color2 : bp->ball_color1);
255 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
259 p.x = cos((thy0+thy1)/2) * cos((thx0+thx1)/2);
260 p.y = sin((thy0+thy1)/2);
261 p.z = cos((thy0+thy1)/2) * sin((thx0+thx1)/2);
262 glNormal3f (-p.x, -p.y, -p.z);
265 p.x = cos(thy0) * cos(thx0) / 2;
267 p.z = cos(thy0) * sin(thx0) / 2;
269 glNormal3f (-p.x, -p.y, -p.z);
270 glVertex3f (p.x, p.y, p.z);
272 p.x = cos(thy1) * cos(thx0) / 2;
274 p.z = cos(thy1) * sin(thx0) / 2;
276 glNormal3f (-p.x, -p.y, -p.z);
277 glVertex3f (p.x, p.y, p.z);
279 p.x = cos(thy1) * cos(thx1) / 2;
281 p.z = cos(thy1) * sin(thx1) / 2;
283 glNormal3f (-p.x, -p.y, -p.z);
284 glVertex3f (p.x, p.y, p.z);
286 p.x = cos(thy0) * cos(thx1) / 2;
288 p.z = cos(thy0) * sin(thx1) / 2;
290 glNormal3f (-p.x, -p.y, -p.z);
291 glVertex3f (p.x, p.y, p.z);
300 if (lighting_p && !wire)
301 glDisable(GL_LIGHTING);
306 draw_shadow (ModeInfo *mi)
308 boing_configuration *bp = &bps[MI_SCREEN(mi)];
309 int wire = MI_IS_WIREFRAME(mi);
314 int scale = (smooth_p ? 5 : 1);
316 if (lighting_p && !wire)
325 glTranslatef (bp->ball_x + xoff, bp->ball_y + yoff, -0.49);
326 glScalef (ball_size, ball_size, ball_size);
327 glRotatef (-angle, 0, 0, 1);
329 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, bp->shadow_color);
330 glColor4fv (bp->shadow_color);
332 glFrontFace (GL_CCW);
333 glNormal3f (0, 0, 1);
334 glBegin (wire ? GL_LINE_LOOP : GL_TRIANGLE_FAN);
335 if (!wire) glVertex3f (0, 0, 0);
337 for (y = 0; y < yy*2+1; y++)
339 GLfloat thy0 = y * (M_PI * 2) / (yy * 2) + M_PI_2;
340 glVertex3f (cos(thy0) / 2, sin(thy0) / 2, 0);
348 if (lighting_p && !wire)
349 glDisable (GL_BLEND);
354 draw_scanlines (ModeInfo *mi)
356 /* boing_configuration *bp = &bps[MI_SCREEN(mi)]; */
357 int wire = MI_IS_WIREFRAME(mi);
358 int w = MI_WIDTH(mi);
359 int h = MI_HEIGHT(mi);
361 if (h <= 300) return;
366 glDisable (GL_DEPTH_TEST);
369 glMatrixMode(GL_PROJECTION);
373 glMatrixMode(GL_MODELVIEW);
379 gluOrtho2D (0, w, 0, h);
381 if (h > 500) lh = 4, ls = 4;
382 else if (h > 300) lh = 2, ls = 1;
386 glDisable (GL_BLEND);
389 glColor4f (0, 0, 0, 0.3);
392 for (y = 0; y < h; y += lh + ls)
394 glVertex3f (0, y, 0);
395 glVertex3f (w, y, 0);
401 glMatrixMode(GL_PROJECTION);
403 glMatrixMode(GL_MODELVIEW);
407 glDisable (GL_BLEND);
408 glEnable (GL_DEPTH_TEST);
415 tick_physics (ModeInfo *mi)
417 boing_configuration *bp = &bps[MI_SCREEN(mi)];
418 GLfloat s2 = ball_size / 2;
419 GLfloat max = 0.5 - s2;
422 bp->ball_th += bp->ball_dth;
423 while (bp->ball_th > 360) bp->ball_th -= 360;
424 while (bp->ball_th < 0) bp->ball_th += 360;
426 bp->ball_dx += bp->ball_ddx;
427 bp->ball_x += bp->ball_dx;
428 if (bp->ball_x < min) bp->ball_x = min, bp->ball_dx = -bp->ball_dx,
429 bp->ball_dth = -bp->ball_dth,
430 bp->ball_dx += (frand(speed/2) - speed);
431 else if (bp->ball_x > max) bp->ball_x = max, bp->ball_dx = -bp->ball_dx,
432 bp->ball_dth = -bp->ball_dth,
433 bp->ball_dx += (frand(speed/2) - speed);
435 bp->ball_dy += bp->ball_ddy;
436 bp->ball_y += bp->ball_dy;
437 if (bp->ball_y < min) bp->ball_y = min, bp->ball_dy = -bp->ball_dy;
438 else if (bp->ball_y > max) bp->ball_y = max, bp->ball_dy = -bp->ball_dy;
440 bp->ball_dz += bp->ball_ddz;
441 bp->ball_z += bp->ball_dz;
442 if (bp->ball_z < min) bp->ball_z = min, bp->ball_dz = -bp->ball_dz;
443 else if (bp->ball_z > max) bp->ball_z = max, bp->ball_dz = -bp->ball_dz;
448 /* Window management, etc
451 reshape_boing (ModeInfo *mi, int width, int height)
453 GLfloat h = (GLfloat) height / (GLfloat) width;
455 h *= 4.0 / 3.0; /* Back in the caveman days we couldn't even afford
458 glViewport (0, 0, (GLint) width, (GLint) height);
460 glMatrixMode(GL_PROJECTION);
462 gluPerspective (8.0, 1/h, 1.0, 10.0);
464 glMatrixMode(GL_MODELVIEW);
466 gluLookAt (0.0, 0.0, 8.0,
470 glClear(GL_COLOR_BUFFER_BIT);
475 boing_handle_event (ModeInfo *mi, XEvent *event)
477 boing_configuration *bp = &bps[MI_SCREEN(mi)];
479 if (event->xany.type == ButtonPress &&
480 event->xbutton.button == Button1)
482 bp->button_down_p = True;
483 gltrackball_start (bp->trackball,
484 event->xbutton.x, event->xbutton.y,
485 MI_WIDTH (mi), MI_HEIGHT (mi));
488 else if (event->xany.type == ButtonRelease &&
489 event->xbutton.button == Button1)
491 bp->button_down_p = False;
494 else if (event->xany.type == ButtonPress &&
495 (event->xbutton.button == Button4 ||
496 event->xbutton.button == Button5 ||
497 event->xbutton.button == Button6 ||
498 event->xbutton.button == Button7))
500 gltrackball_mousewheel (bp->trackball, event->xbutton.button, 10,
501 !!event->xbutton.state);
504 else if (event->xany.type == MotionNotify &&
507 gltrackball_track (bp->trackball,
508 event->xmotion.x, event->xmotion.y,
509 MI_WIDTH (mi), MI_HEIGHT (mi));
518 init_boing (ModeInfo *mi)
520 boing_configuration *bp;
521 int wire = MI_IS_WIREFRAME(mi);
524 bps = (boing_configuration *)
525 calloc (MI_NUM_SCREENS(mi), sizeof (boing_configuration));
527 fprintf(stderr, "%s: out of memory\n", progname);
532 bp = &bps[MI_SCREEN(mi)];
534 bp->glx_context = init_GL(mi);
536 if (tiles < 1) tiles = 1;
540 if (meridians < 1) meridians = 1;
541 if (parallels < 1) parallels = 1;
545 if (meridians < 3) meridians = 3;
546 if (parallels < 2) parallels = 2;
549 if (meridians > 1 && meridians & 1) meridians++; /* odd numbers look bad */
552 if (thickness <= 0) thickness = 0.001;
553 else if (thickness > 1) thickness = 1;
555 reshape_boing (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
557 parse_color (mi, "ballColor1", ball_color1_str, bp->ball_color1);
558 parse_color (mi, "ballColor2", ball_color2_str, bp->ball_color2);
559 parse_color (mi, "gridColor", grid_color_str, bp->grid_color);
560 parse_color (mi, "shadowColor", shadow_str, bp->shadow_color);
561 parse_color (mi, "background", bg_str, bp->bg_color);
563 bp->shadow_color[3] = 0.9;
565 glClearColor (bp->bg_color[0], bp->bg_color[1], bp->bg_color[2], 1);
569 glEnable(GL_DEPTH_TEST);
570 glEnable(GL_CULL_FACE);
573 bp->lightpos[0] = 0.5;
574 bp->lightpos[1] = 0.5;
575 bp->lightpos[2] = -1;
578 if (lighting_p && !wire)
580 GLfloat amb[4] = {0, 0, 0, 1};
581 GLfloat dif[4] = {1, 1, 1, 1};
582 GLfloat spc[4] = {1, 1, 1, 1};
584 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
585 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
586 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
589 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
591 speed = speed / 800.0;
593 bp->ball_dth = (spin ? -speed * 7 * 360 : 0);
595 bp->ball_x = 0.5 - ((ball_size/2) + frand(1-ball_size));
597 bp->ball_dx = speed * 6 + frand(speed);
598 bp->ball_ddy = -speed;
600 bp->ball_dz = speed * 6 + frand(speed);
602 bp->trackball = gltrackball_init ();
607 draw_boing (ModeInfo *mi)
609 boing_configuration *bp = &bps[MI_SCREEN(mi)];
610 Display *dpy = MI_DISPLAY(mi);
611 Window window = MI_WINDOW(mi);
613 if (!bp->glx_context)
616 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
618 mi->polygon_count = 0;
620 glShadeModel(GL_SMOOTH);
622 glEnable(GL_NORMALIZE);
624 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
626 if (! bp->button_down_p)
630 gltrackball_rotate (bp->trackball);
632 glLightfv (GL_LIGHT0, GL_POSITION, bp->lightpos);
634 glDisable (GL_CULL_FACE);
635 glDisable (GL_DEPTH_TEST);
637 glEnable (GL_LINE_SMOOTH);
638 glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
639 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
645 glEnable (GL_CULL_FACE);
646 glEnable (GL_DEPTH_TEST);
654 if (mi->fps_p) do_fps (mi);
657 glXSwapBuffers(dpy, window);
660 XSCREENSAVER_MODULE ("Boing", boing)