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" \
32 "*speed: " DEF_SPEED " \n" \
33 "*spin: " DEF_SPIN "\n" \
34 "*wander: " DEF_WANDER "\n" \
37 #define SPIKE_FACES 12 /* how densely to render spikes */
38 #define SMOOTH_SPIKES True
39 #define SPHERE_SLICES 32 /* how densely to render spheres */
40 #define SPHERE_STACKS 16
44 #define countof(x) (sizeof((x))/sizeof((*x)))
46 #include "xlockmore.h"
51 #include "gltrackball.h"
54 #ifdef USE_GL /* whole file */
59 GLXContext *glx_context;
61 trackball_state *trackball;
77 static ball_configuration *bps = NULL;
81 static Bool do_wander;
83 static XrmOptionDescRec opts[] = {
84 { "-spin", ".spin", XrmoptionNoArg, "True" },
85 { "+spin", ".spin", XrmoptionNoArg, "False" },
86 { "-speed", ".speed", XrmoptionSepArg, 0 },
87 { "-wander", ".wander", XrmoptionNoArg, "True" },
88 { "+wander", ".wander", XrmoptionNoArg, "False" }
91 static argtype vars[] = {
92 {&do_spin, "spin", "Spin", DEF_SPIN, t_Bool},
93 {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
94 {&speed, "speed", "Speed", DEF_SPEED, t_Float},
97 ModeSpecOpt sws_opts = {countof(opts), opts, countof(vars), vars, NULL};
100 /* Window management, etc
103 reshape_ball (ModeInfo *mi, int width, int height)
105 GLfloat h = (GLfloat) height / (GLfloat) width;
107 glViewport (0, 0, (GLint) width, (GLint) height);
109 glMatrixMode(GL_PROJECTION);
111 gluPerspective (30.0, 1/h, 1.0, 100.0);
113 glMatrixMode(GL_MODELVIEW);
115 gluLookAt( 0.0, 0.0, 30.0,
119 glClear(GL_COLOR_BUFFER_BIT);
124 randomize_spikes (ModeInfo *mi)
126 ball_configuration *bp = &bps[MI_SCREEN(mi)];
129 for (i = 0; i < MI_COUNT(mi); i++)
131 bp->spikes[i*2] = (random() % 360) - 180;
132 bp->spikes[i*2+1] = (random() % 180) - 90;
135 # define ROT_SCALE 22
136 for (i = 0; i < MI_COUNT(mi) * 2; i++)
137 bp->spikes[i] = (bp->spikes[i] / ROT_SCALE) * ROT_SCALE;
139 if ((random() % 3) == 0)
140 bp->color_shift = random() % (bp->ncolors / 2);
146 draw_spikes (ModeInfo *mi)
148 ball_configuration *bp = &bps[MI_SCREEN(mi)];
150 GLfloat pos = bp->pos;
153 if (pos < 0) pos = -pos;
155 pos = (asin (0.5 + pos/2) - 0.5) * 2;
157 for (i = 0; i < MI_COUNT(mi); i++)
160 glRotatef(bp->spikes[i*2], 0, 1, 0);
161 glRotatef(bp->spikes[i*2+1], 0, 0, 1);
162 glTranslatef(0.7, 0, 0);
163 glRotatef(-90, 0, 0, 1);
164 glScalef (diam, pos, diam);
165 glCallList (bp->spike_list);
168 mi->polygon_count += (SPIKE_FACES + 1);
174 move_spikes (ModeInfo *mi)
176 ball_configuration *bp = &bps[MI_SCREEN(mi)];
178 if (bp->pos >= 0) /* moving outward */
181 if (bp->pos >= 1) /* reverse gears at apex */
184 else /* moving inward */
187 if (bp->pos >= 0) /* stop at end */
188 randomize_spikes (mi);
194 ball_handle_event (ModeInfo *mi, XEvent *event)
196 ball_configuration *bp = &bps[MI_SCREEN(mi)];
198 if (event->xany.type == ButtonPress &&
199 event->xbutton.button & Button1)
201 bp->button_down_p = True;
202 gltrackball_start (bp->trackball,
203 event->xbutton.x, event->xbutton.y,
204 MI_WIDTH (mi), MI_HEIGHT (mi));
207 else if (event->xany.type == ButtonRelease &&
208 event->xbutton.button & Button1)
210 bp->button_down_p = False;
213 else if (event->xany.type == MotionNotify &&
216 gltrackball_track (bp->trackball,
217 event->xmotion.x, event->xmotion.y,
218 MI_WIDTH (mi), MI_HEIGHT (mi));
227 init_ball (ModeInfo *mi)
229 ball_configuration *bp;
230 int wire = MI_IS_WIREFRAME(mi);
233 bps = (ball_configuration *)
234 calloc (MI_NUM_SCREENS(mi), sizeof (ball_configuration));
236 fprintf(stderr, "%s: out of memory\n", progname);
240 bp = &bps[MI_SCREEN(mi)];
243 bp = &bps[MI_SCREEN(mi)];
245 bp->glx_context = init_GL(mi);
247 reshape_ball (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
251 GLfloat pos[4] = {1.0, 1.0, 1.0, 0.0};
252 GLfloat amb[4] = {0.0, 0.0, 0.0, 1.0};
253 GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
254 GLfloat spc[4] = {0.0, 1.0, 1.0, 1.0};
256 glEnable(GL_LIGHTING);
258 glEnable(GL_DEPTH_TEST);
259 glEnable(GL_CULL_FACE);
261 glLightfv(GL_LIGHT0, GL_POSITION, pos);
262 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
263 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
264 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
268 double spin_speed = 10.0;
269 double wander_speed = 0.12;
270 double spin_accel = 2.0;
272 bp->rot = make_rotator (do_spin ? spin_speed : 0,
273 do_spin ? spin_speed : 0,
274 do_spin ? spin_speed : 0,
276 do_wander ? wander_speed : 0,
278 bp->trackball = gltrackball_init ();
282 bp->colors = (XColor *) calloc(bp->ncolors, sizeof(XColor));
283 make_smooth_colormap (0, 0, 0,
284 bp->colors, &bp->ncolors,
287 bp->spikes = (int *) calloc(MI_COUNT(mi), sizeof(*bp->spikes) * 2);
289 bp->ball_list = glGenLists (1);
290 bp->spike_list = glGenLists (1);
292 glNewList (bp->ball_list, GL_COMPILE);
293 unit_sphere (SPHERE_STACKS, SPHERE_SLICES, wire);
296 glNewList (bp->spike_list, GL_COMPILE);
299 1, 0, SPIKE_FACES, SMOOTH_SPIKES, False, wire);
302 randomize_spikes (mi);
307 draw_ball (ModeInfo *mi)
309 ball_configuration *bp = &bps[MI_SCREEN(mi)];
310 Display *dpy = MI_DISPLAY(mi);
311 Window window = MI_WINDOW(mi);
314 static GLfloat bcolor[4] = {0.0, 0.0, 0.0, 1.0};
315 static GLfloat scolor[4] = {0.0, 0.0, 0.0, 1.0};
316 static GLfloat bspec[4] = {1.0, 1.0, 1.0, 1.0};
317 static GLfloat sspec[4] = {0.0, 0.0, 0.0, 1.0};
318 static GLfloat bshiny = 128.0;
319 static GLfloat sshiny = 0.0;
321 if (!bp->glx_context)
324 glShadeModel(GL_SMOOTH);
326 glEnable(GL_DEPTH_TEST);
327 glEnable(GL_NORMALIZE);
328 glEnable(GL_CULL_FACE);
330 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
334 glScalef(1.1, 1.1, 1.1);
338 get_position (bp->rot, &x, &y, &z, !bp->button_down_p);
339 glTranslatef((x - 0.5) * 8,
343 gltrackball_rotate (bp->trackball);
345 get_rotation (bp->rot, &x, &y, &z, !bp->button_down_p);
346 glRotatef (x * 360, 1.0, 0.0, 0.0);
347 glRotatef (y * 360, 0.0, 1.0, 0.0);
348 glRotatef (z * 360, 0.0, 0.0, 1.0);
351 bcolor[0] = bp->colors[bp->ccolor].red / 65536.0;
352 bcolor[1] = bp->colors[bp->ccolor].green / 65536.0;
353 bcolor[2] = bp->colors[bp->ccolor].blue / 65536.0;
355 c2 = (bp->ccolor + bp->color_shift) % bp->ncolors;
356 scolor[0] = bp->colors[c2].red / 65536.0;
357 scolor[1] = bp->colors[c2].green / 65536.0;
358 scolor[2] = bp->colors[c2].blue / 65536.0;
361 if (bp->ccolor >= bp->ncolors) bp->ccolor = 0;
363 mi->polygon_count = 0;
365 glScalef (2.0, 2.0, 2.0);
369 glMaterialfv (GL_FRONT, GL_SPECULAR, bspec);
370 glMateriali (GL_FRONT, GL_SHININESS, bshiny);
371 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, bcolor);
372 glCallList (bp->ball_list);
373 mi->polygon_count += (SPHERE_SLICES * SPHERE_STACKS);
375 glMaterialfv (GL_FRONT, GL_SPECULAR, sspec);
376 glMaterialf (GL_FRONT, GL_SHININESS, sshiny);
377 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, scolor);
381 if (mi->fps_p) do_fps (mi);
384 glXSwapBuffers(dpy, window);