1 /* dangerball, Copyright (c) 2001-2004 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 HACK_HANDLE_EVENT ball_handle_event
21 #define EVENT_MASK PointerMotionMask
22 #define sws_opts xlockmore_opts
24 #define DEF_SPIN "True"
25 #define DEF_WANDER "True"
26 #define DEF_SPEED "0.05"
28 #define DEFAULTS "*delay: 30000 \n" \
30 "*showFPS: False \n" \
31 "*wireframe: False \n" \
33 #define SPIKE_FACES 12 /* how densely to render spikes */
34 #define SMOOTH_SPIKES True
35 #define SPHERE_SLICES 32 /* how densely to render spheres */
36 #define SPHERE_STACKS 16
40 #define countof(x) (sizeof((x))/sizeof((*x)))
42 #include "xlockmore.h"
47 #include "gltrackball.h"
50 #ifdef USE_GL /* whole file */
55 GLXContext *glx_context;
57 trackball_state *trackball;
73 static ball_configuration *bps = NULL;
77 static Bool do_wander;
79 static XrmOptionDescRec opts[] = {
80 { "-spin", ".spin", XrmoptionNoArg, "True" },
81 { "+spin", ".spin", XrmoptionNoArg, "False" },
82 { "-speed", ".speed", XrmoptionSepArg, 0 },
83 { "-wander", ".wander", XrmoptionNoArg, "True" },
84 { "+wander", ".wander", XrmoptionNoArg, "False" }
87 static argtype vars[] = {
88 {&do_spin, "spin", "Spin", DEF_SPIN, t_Bool},
89 {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
90 {&speed, "speed", "Speed", DEF_SPEED, t_Float},
93 ModeSpecOpt sws_opts = {countof(opts), opts, countof(vars), vars, NULL};
96 /* Window management, etc
99 reshape_ball (ModeInfo *mi, int width, int height)
101 GLfloat h = (GLfloat) height / (GLfloat) width;
103 glViewport (0, 0, (GLint) width, (GLint) height);
105 glMatrixMode(GL_PROJECTION);
107 gluPerspective (30.0, 1/h, 1.0, 100.0);
109 glMatrixMode(GL_MODELVIEW);
111 gluLookAt( 0.0, 0.0, 30.0,
115 glClear(GL_COLOR_BUFFER_BIT);
120 randomize_spikes (ModeInfo *mi)
122 ball_configuration *bp = &bps[MI_SCREEN(mi)];
125 for (i = 0; i < MI_COUNT(mi); i++)
127 bp->spikes[i*2] = (random() % 360) - 180;
128 bp->spikes[i*2+1] = (random() % 180) - 90;
131 # define ROT_SCALE 22
132 for (i = 0; i < MI_COUNT(mi) * 2; i++)
133 bp->spikes[i] = (bp->spikes[i] / ROT_SCALE) * ROT_SCALE;
135 if ((random() % 3) == 0)
136 bp->color_shift = random() % (bp->ncolors / 2);
142 draw_spikes (ModeInfo *mi)
144 ball_configuration *bp = &bps[MI_SCREEN(mi)];
146 GLfloat pos = bp->pos;
149 if (pos < 0) pos = -pos;
151 pos = (asin (0.5 + pos/2) - 0.5) * 2;
153 for (i = 0; i < MI_COUNT(mi); i++)
156 glRotatef(bp->spikes[i*2], 0, 1, 0);
157 glRotatef(bp->spikes[i*2+1], 0, 0, 1);
158 glTranslatef(0.7, 0, 0);
159 glRotatef(-90, 0, 0, 1);
160 glScalef (diam, pos, diam);
161 glCallList (bp->spike_list);
164 mi->polygon_count += (SPIKE_FACES + 1);
170 move_spikes (ModeInfo *mi)
172 ball_configuration *bp = &bps[MI_SCREEN(mi)];
174 if (bp->pos >= 0) /* moving outward */
177 if (bp->pos >= 1) /* reverse gears at apex */
180 else /* moving inward */
183 if (bp->pos >= 0) /* stop at end */
184 randomize_spikes (mi);
190 ball_handle_event (ModeInfo *mi, XEvent *event)
192 ball_configuration *bp = &bps[MI_SCREEN(mi)];
194 if (event->xany.type == ButtonPress &&
195 event->xbutton.button == Button1)
197 bp->button_down_p = True;
198 gltrackball_start (bp->trackball,
199 event->xbutton.x, event->xbutton.y,
200 MI_WIDTH (mi), MI_HEIGHT (mi));
203 else if (event->xany.type == ButtonRelease &&
204 event->xbutton.button == Button1)
206 bp->button_down_p = False;
209 else if (event->xany.type == ButtonPress &&
210 (event->xbutton.button == Button4 ||
211 event->xbutton.button == Button5))
213 gltrackball_mousewheel (bp->trackball, event->xbutton.button, 10,
214 !!event->xbutton.state);
217 else if (event->xany.type == MotionNotify &&
220 gltrackball_track (bp->trackball,
221 event->xmotion.x, event->xmotion.y,
222 MI_WIDTH (mi), MI_HEIGHT (mi));
231 init_ball (ModeInfo *mi)
233 ball_configuration *bp;
234 int wire = MI_IS_WIREFRAME(mi);
237 bps = (ball_configuration *)
238 calloc (MI_NUM_SCREENS(mi), sizeof (ball_configuration));
240 fprintf(stderr, "%s: out of memory\n", progname);
244 bp = &bps[MI_SCREEN(mi)];
247 bp = &bps[MI_SCREEN(mi)];
249 bp->glx_context = init_GL(mi);
251 reshape_ball (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
255 GLfloat pos[4] = {1.0, 1.0, 1.0, 0.0};
256 GLfloat amb[4] = {0.0, 0.0, 0.0, 1.0};
257 GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
258 GLfloat spc[4] = {0.0, 1.0, 1.0, 1.0};
260 glEnable(GL_LIGHTING);
262 glEnable(GL_DEPTH_TEST);
263 glEnable(GL_CULL_FACE);
265 glLightfv(GL_LIGHT0, GL_POSITION, pos);
266 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
267 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
268 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
272 double spin_speed = 10.0;
273 double wander_speed = 0.12;
274 double spin_accel = 2.0;
276 bp->rot = make_rotator (do_spin ? spin_speed : 0,
277 do_spin ? spin_speed : 0,
278 do_spin ? spin_speed : 0,
280 do_wander ? wander_speed : 0,
282 bp->trackball = gltrackball_init ();
286 bp->colors = (XColor *) calloc(bp->ncolors, sizeof(XColor));
287 make_smooth_colormap (0, 0, 0,
288 bp->colors, &bp->ncolors,
291 bp->spikes = (int *) calloc(MI_COUNT(mi), sizeof(*bp->spikes) * 2);
293 bp->ball_list = glGenLists (1);
294 bp->spike_list = glGenLists (1);
296 glNewList (bp->ball_list, GL_COMPILE);
297 unit_sphere (SPHERE_STACKS, SPHERE_SLICES, wire);
300 glNewList (bp->spike_list, GL_COMPILE);
303 1, 0, SPIKE_FACES, SMOOTH_SPIKES, False, wire);
306 randomize_spikes (mi);
311 draw_ball (ModeInfo *mi)
313 ball_configuration *bp = &bps[MI_SCREEN(mi)];
314 Display *dpy = MI_DISPLAY(mi);
315 Window window = MI_WINDOW(mi);
318 static GLfloat bcolor[4] = {0.0, 0.0, 0.0, 1.0};
319 static GLfloat scolor[4] = {0.0, 0.0, 0.0, 1.0};
320 static GLfloat bspec[4] = {1.0, 1.0, 1.0, 1.0};
321 static GLfloat sspec[4] = {0.0, 0.0, 0.0, 1.0};
322 static GLfloat bshiny = 128.0;
323 static GLfloat sshiny = 0.0;
325 if (!bp->glx_context)
328 glShadeModel(GL_SMOOTH);
330 glEnable(GL_DEPTH_TEST);
331 glEnable(GL_NORMALIZE);
332 glEnable(GL_CULL_FACE);
334 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
338 glScalef(1.1, 1.1, 1.1);
342 get_position (bp->rot, &x, &y, &z, !bp->button_down_p);
343 glTranslatef((x - 0.5) * 8,
347 gltrackball_rotate (bp->trackball);
349 get_rotation (bp->rot, &x, &y, &z, !bp->button_down_p);
350 glRotatef (x * 360, 1.0, 0.0, 0.0);
351 glRotatef (y * 360, 0.0, 1.0, 0.0);
352 glRotatef (z * 360, 0.0, 0.0, 1.0);
355 bcolor[0] = bp->colors[bp->ccolor].red / 65536.0;
356 bcolor[1] = bp->colors[bp->ccolor].green / 65536.0;
357 bcolor[2] = bp->colors[bp->ccolor].blue / 65536.0;
359 c2 = (bp->ccolor + bp->color_shift) % bp->ncolors;
360 scolor[0] = bp->colors[c2].red / 65536.0;
361 scolor[1] = bp->colors[c2].green / 65536.0;
362 scolor[2] = bp->colors[c2].blue / 65536.0;
365 if (bp->ccolor >= bp->ncolors) bp->ccolor = 0;
367 mi->polygon_count = 0;
369 glScalef (2.0, 2.0, 2.0);
373 glMaterialfv (GL_FRONT, GL_SPECULAR, bspec);
374 glMateriali (GL_FRONT, GL_SHININESS, bshiny);
375 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, bcolor);
376 glCallList (bp->ball_list);
377 mi->polygon_count += (SPHERE_SLICES * SPHERE_STACKS);
379 glMaterialfv (GL_FRONT, GL_SPECULAR, sspec);
380 glMaterialf (GL_FRONT, GL_SHININESS, sshiny);
381 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, scolor);
385 if (mi->fps_p) do_fps (mi);
388 glXSwapBuffers(dpy, window);