1 /* dangerball, Copyright (c) 2001-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
12 #define DEFAULTS "*delay: 30000 \n" \
14 "*showFPS: False \n" \
15 "*wireframe: False \n" \
17 # define refresh_ball 0
18 # define release_ball 0
20 #define countof(x) (sizeof((x))/sizeof((*x)))
22 #include "xlockmore.h"
27 #include "gltrackball.h"
30 #ifdef USE_GL /* whole file */
33 #define DEF_SPIN "True"
34 #define DEF_WANDER "True"
35 #define DEF_SPEED "0.05"
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
43 GLXContext *glx_context;
45 trackball_state *trackball;
61 static ball_configuration *bps = NULL;
65 static Bool do_wander;
67 static XrmOptionDescRec opts[] = {
68 { "-spin", ".spin", XrmoptionNoArg, "True" },
69 { "+spin", ".spin", XrmoptionNoArg, "False" },
70 { "-speed", ".speed", XrmoptionSepArg, 0 },
71 { "-wander", ".wander", XrmoptionNoArg, "True" },
72 { "+wander", ".wander", XrmoptionNoArg, "False" }
75 static argtype vars[] = {
76 {&do_spin, "spin", "Spin", DEF_SPIN, t_Bool},
77 {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
78 {&speed, "speed", "Speed", DEF_SPEED, t_Float},
81 ENTRYPOINT ModeSpecOpt ball_opts = {countof(opts), opts, countof(vars), vars, NULL};
84 /* Window management, etc
87 reshape_ball (ModeInfo *mi, int width, int height)
89 GLfloat h = (GLfloat) height / (GLfloat) width;
91 glViewport (0, 0, (GLint) width, (GLint) height);
93 glMatrixMode(GL_PROJECTION);
95 gluPerspective (30.0, 1/h, 1.0, 100.0);
97 glMatrixMode(GL_MODELVIEW);
99 gluLookAt( 0.0, 0.0, 30.0,
103 glClear(GL_COLOR_BUFFER_BIT);
108 randomize_spikes (ModeInfo *mi)
110 ball_configuration *bp = &bps[MI_SCREEN(mi)];
113 for (i = 0; i < MI_COUNT(mi); i++)
115 bp->spikes[i*2] = (random() % 360) - 180;
116 bp->spikes[i*2+1] = (random() % 180) - 90;
119 # define ROT_SCALE 22
120 for (i = 0; i < MI_COUNT(mi) * 2; i++)
121 bp->spikes[i] = (bp->spikes[i] / ROT_SCALE) * ROT_SCALE;
123 if ((random() % 3) == 0)
124 bp->color_shift = random() % (bp->ncolors / 2);
130 draw_spikes (ModeInfo *mi)
132 ball_configuration *bp = &bps[MI_SCREEN(mi)];
134 GLfloat pos = bp->pos;
137 if (pos < 0) pos = -pos;
139 pos = (asin (0.5 + pos/2) - 0.5) * 2;
141 for (i = 0; i < MI_COUNT(mi); i++)
144 glRotatef(bp->spikes[i*2], 0, 1, 0);
145 glRotatef(bp->spikes[i*2+1], 0, 0, 1);
146 glTranslatef(0.7, 0, 0);
147 glRotatef(-90, 0, 0, 1);
148 glScalef (diam, pos, diam);
149 glCallList (bp->spike_list);
152 mi->polygon_count += (SPIKE_FACES + 1);
158 move_spikes (ModeInfo *mi)
160 ball_configuration *bp = &bps[MI_SCREEN(mi)];
162 if (bp->pos >= 0) /* moving outward */
165 if (bp->pos >= 1) /* reverse gears at apex */
168 else /* moving inward */
171 if (bp->pos >= 0) /* stop at end */
172 randomize_spikes (mi);
178 ball_handle_event (ModeInfo *mi, XEvent *event)
180 ball_configuration *bp = &bps[MI_SCREEN(mi)];
182 if (event->xany.type == ButtonPress &&
183 event->xbutton.button == Button1)
185 bp->button_down_p = True;
186 gltrackball_start (bp->trackball,
187 event->xbutton.x, event->xbutton.y,
188 MI_WIDTH (mi), MI_HEIGHT (mi));
191 else if (event->xany.type == ButtonRelease &&
192 event->xbutton.button == Button1)
194 bp->button_down_p = False;
197 else if (event->xany.type == ButtonPress &&
198 (event->xbutton.button == Button4 ||
199 event->xbutton.button == Button5 ||
200 event->xbutton.button == Button6 ||
201 event->xbutton.button == Button7))
203 gltrackball_mousewheel (bp->trackball, event->xbutton.button, 10,
204 !!event->xbutton.state);
207 else if (event->xany.type == MotionNotify &&
210 gltrackball_track (bp->trackball,
211 event->xmotion.x, event->xmotion.y,
212 MI_WIDTH (mi), MI_HEIGHT (mi));
221 init_ball (ModeInfo *mi)
223 ball_configuration *bp;
224 int wire = MI_IS_WIREFRAME(mi);
227 bps = (ball_configuration *)
228 calloc (MI_NUM_SCREENS(mi), sizeof (ball_configuration));
230 fprintf(stderr, "%s: out of memory\n", progname);
234 bp = &bps[MI_SCREEN(mi)];
237 bp = &bps[MI_SCREEN(mi)];
239 bp->glx_context = init_GL(mi);
241 reshape_ball (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
245 GLfloat pos[4] = {1.0, 1.0, 1.0, 0.0};
246 GLfloat amb[4] = {0.0, 0.0, 0.0, 1.0};
247 GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
248 GLfloat spc[4] = {0.0, 1.0, 1.0, 1.0};
250 glEnable(GL_LIGHTING);
252 glEnable(GL_DEPTH_TEST);
253 glEnable(GL_CULL_FACE);
255 glLightfv(GL_LIGHT0, GL_POSITION, pos);
256 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
257 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
258 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
262 double spin_speed = 10.0;
263 double wander_speed = 0.12;
264 double spin_accel = 2.0;
266 bp->rot = make_rotator (do_spin ? spin_speed : 0,
267 do_spin ? spin_speed : 0,
268 do_spin ? spin_speed : 0,
270 do_wander ? wander_speed : 0,
272 bp->trackball = gltrackball_init ();
276 bp->colors = (XColor *) calloc(bp->ncolors, sizeof(XColor));
277 make_smooth_colormap (0, 0, 0,
278 bp->colors, &bp->ncolors,
281 bp->spikes = (int *) calloc(MI_COUNT(mi), sizeof(*bp->spikes) * 2);
283 bp->ball_list = glGenLists (1);
284 bp->spike_list = glGenLists (1);
286 glNewList (bp->ball_list, GL_COMPILE);
287 unit_sphere (SPHERE_STACKS, SPHERE_SLICES, wire);
290 glNewList (bp->spike_list, GL_COMPILE);
293 1, 0, SPIKE_FACES, SMOOTH_SPIKES, False, wire);
296 randomize_spikes (mi);
301 draw_ball (ModeInfo *mi)
303 ball_configuration *bp = &bps[MI_SCREEN(mi)];
304 Display *dpy = MI_DISPLAY(mi);
305 Window window = MI_WINDOW(mi);
308 static const GLfloat bspec[4] = {1.0, 1.0, 1.0, 1.0};
309 static const GLfloat sspec[4] = {0.0, 0.0, 0.0, 1.0};
310 static const GLfloat bshiny = 128.0;
311 static const GLfloat sshiny = 0.0;
313 GLfloat bcolor[4] = {0.0, 0.0, 0.0, 1.0};
314 GLfloat scolor[4] = {0.0, 0.0, 0.0, 1.0};
316 if (!bp->glx_context)
319 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
321 glShadeModel(GL_SMOOTH);
323 glEnable(GL_DEPTH_TEST);
324 glEnable(GL_NORMALIZE);
325 glEnable(GL_CULL_FACE);
327 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
331 glScalef(1.1, 1.1, 1.1);
335 get_position (bp->rot, &x, &y, &z, !bp->button_down_p);
336 glTranslatef((x - 0.5) * 8,
340 gltrackball_rotate (bp->trackball);
342 get_rotation (bp->rot, &x, &y, &z, !bp->button_down_p);
343 glRotatef (x * 360, 1.0, 0.0, 0.0);
344 glRotatef (y * 360, 0.0, 1.0, 0.0);
345 glRotatef (z * 360, 0.0, 0.0, 1.0);
348 bcolor[0] = bp->colors[bp->ccolor].red / 65536.0;
349 bcolor[1] = bp->colors[bp->ccolor].green / 65536.0;
350 bcolor[2] = bp->colors[bp->ccolor].blue / 65536.0;
352 c2 = (bp->ccolor + bp->color_shift) % bp->ncolors;
353 scolor[0] = bp->colors[c2].red / 65536.0;
354 scolor[1] = bp->colors[c2].green / 65536.0;
355 scolor[2] = bp->colors[c2].blue / 65536.0;
358 if (bp->ccolor >= bp->ncolors) bp->ccolor = 0;
360 mi->polygon_count = 0;
362 glScalef (2.0, 2.0, 2.0);
366 glMaterialfv (GL_FRONT, GL_SPECULAR, bspec);
367 glMateriali (GL_FRONT, GL_SHININESS, bshiny);
368 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, bcolor);
369 glCallList (bp->ball_list);
370 mi->polygon_count += (SPHERE_SLICES * SPHERE_STACKS);
372 glMaterialfv (GL_FRONT, GL_SPECULAR, sspec);
373 glMaterialf (GL_FRONT, GL_SHININESS, sshiny);
374 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, scolor);
378 if (mi->fps_p) do_fps (mi);
381 glXSwapBuffers(dpy, window);
384 XSCREENSAVER_MODULE_2 ("DangerBall", dangerball, ball)