1 /* dangerball, Copyright (c) 2001-2017 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" \
16 "*suppressRotationAnimation: True\n" \
18 # define refresh_ball 0
19 # define release_ball 0
21 #define countof(x) (sizeof((x))/sizeof((*x)))
23 #include "xlockmore.h"
28 #include "gltrackball.h"
31 #ifdef USE_GL /* whole file */
34 #define DEF_SPIN "True"
35 #define DEF_WANDER "True"
36 #define DEF_SPEED "0.05"
38 #define SPIKE_FACES 12 /* how densely to render spikes */
39 #define SMOOTH_SPIKES True
40 #define SPHERE_SLICES 32 /* how densely to render spheres */
41 #define SPHERE_STACKS 16
45 GLXContext *glx_context;
47 trackball_state *trackball;
63 static ball_configuration *bps = NULL;
67 static Bool do_wander;
69 static XrmOptionDescRec opts[] = {
70 { "-spin", ".spin", XrmoptionNoArg, "True" },
71 { "+spin", ".spin", XrmoptionNoArg, "False" },
72 { "-speed", ".speed", XrmoptionSepArg, 0 },
73 { "-wander", ".wander", XrmoptionNoArg, "True" },
74 { "+wander", ".wander", XrmoptionNoArg, "False" }
77 static argtype vars[] = {
78 {&do_spin, "spin", "Spin", DEF_SPIN, t_Bool},
79 {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
80 {&speed, "speed", "Speed", DEF_SPEED, t_Float},
83 ENTRYPOINT ModeSpecOpt ball_opts = {countof(opts), opts, countof(vars), vars, NULL};
86 /* Window management, etc
89 reshape_ball (ModeInfo *mi, int width, int height)
91 GLfloat h = (GLfloat) height / (GLfloat) width;
93 glViewport (0, 0, (GLint) width, (GLint) height);
95 glMatrixMode(GL_PROJECTION);
97 gluPerspective (30.0, 1/h, 1.0, 100.0);
99 glMatrixMode(GL_MODELVIEW);
101 gluLookAt( 0.0, 0.0, 30.0,
105 # ifdef HAVE_MOBILE /* Keep it the same relative size when rotated. */
107 int o = (int) current_device_rotation();
108 if (o != 0 && o != 180 && o != -180)
109 glScalef (1/h, 1/h, 1/h);
113 glClear(GL_COLOR_BUFFER_BIT);
118 randomize_spikes (ModeInfo *mi)
120 ball_configuration *bp = &bps[MI_SCREEN(mi)];
123 for (i = 0; i < MI_COUNT(mi); i++)
125 bp->spikes[i*2] = (random() % 360) - 180;
126 bp->spikes[i*2+1] = (random() % 180) - 90;
129 # define ROT_SCALE 22
130 for (i = 0; i < MI_COUNT(mi) * 2; i++)
131 bp->spikes[i] = (bp->spikes[i] / ROT_SCALE) * ROT_SCALE;
133 if ((random() % 3) == 0)
134 bp->color_shift = random() % (bp->ncolors / 2);
140 draw_spikes (ModeInfo *mi)
142 ball_configuration *bp = &bps[MI_SCREEN(mi)];
144 GLfloat pos = bp->pos;
147 if (pos < 0) pos = -pos;
149 pos = (asin (0.5 + pos/2) - 0.5) * 2;
151 for (i = 0; i < MI_COUNT(mi); i++)
154 glRotatef(bp->spikes[i*2], 0, 1, 0);
155 glRotatef(bp->spikes[i*2+1], 0, 0, 1);
156 glTranslatef(0.7, 0, 0);
157 glRotatef(-90, 0, 0, 1);
158 glScalef (diam, pos, diam);
159 glCallList (bp->spike_list);
162 mi->polygon_count += (SPIKE_FACES + 1);
168 move_spikes (ModeInfo *mi)
170 ball_configuration *bp = &bps[MI_SCREEN(mi)];
172 if (bp->pos >= 0) /* moving outward */
175 if (bp->pos >= 1) /* reverse gears at apex */
178 else /* moving inward */
181 if (bp->pos >= 0) /* stop at end */
182 randomize_spikes (mi);
188 ball_handle_event (ModeInfo *mi, XEvent *event)
190 ball_configuration *bp = &bps[MI_SCREEN(mi)];
192 if (gltrackball_event_handler (event, bp->trackball,
193 MI_WIDTH (mi), MI_HEIGHT (mi),
202 init_ball (ModeInfo *mi)
204 ball_configuration *bp;
205 int wire = MI_IS_WIREFRAME(mi);
207 MI_INIT (mi, bps, 0);
208 bp = &bps[MI_SCREEN(mi)];
210 bp->glx_context = init_GL(mi);
212 reshape_ball (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
216 GLfloat pos[4] = {1.0, 1.0, 1.0, 0.0};
217 GLfloat amb[4] = {0.0, 0.0, 0.0, 1.0};
218 GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
219 GLfloat spc[4] = {0.0, 1.0, 1.0, 1.0};
221 glEnable(GL_LIGHTING);
223 glEnable(GL_DEPTH_TEST);
224 glEnable(GL_CULL_FACE);
226 glLightfv(GL_LIGHT0, GL_POSITION, pos);
227 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
228 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
229 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
233 double spin_speed = 10.0;
234 double wander_speed = 0.12;
235 double spin_accel = 2.0;
237 bp->rot = make_rotator (do_spin ? spin_speed : 0,
238 do_spin ? spin_speed : 0,
239 do_spin ? spin_speed : 0,
241 do_wander ? wander_speed : 0,
243 bp->trackball = gltrackball_init (True);
247 bp->colors = (XColor *) calloc(bp->ncolors, sizeof(XColor));
248 make_smooth_colormap (0, 0, 0,
249 bp->colors, &bp->ncolors,
252 bp->spikes = (int *) calloc(MI_COUNT(mi), sizeof(*bp->spikes) * 2);
254 bp->ball_list = glGenLists (1);
255 bp->spike_list = glGenLists (1);
257 glNewList (bp->ball_list, GL_COMPILE);
258 unit_sphere (SPHERE_STACKS, SPHERE_SLICES, wire);
261 glNewList (bp->spike_list, GL_COMPILE);
264 1, 0, SPIKE_FACES, SMOOTH_SPIKES, False, wire);
267 randomize_spikes (mi);
272 draw_ball (ModeInfo *mi)
274 ball_configuration *bp = &bps[MI_SCREEN(mi)];
275 Display *dpy = MI_DISPLAY(mi);
276 Window window = MI_WINDOW(mi);
279 static const GLfloat bspec[4] = {1.0, 1.0, 1.0, 1.0};
280 static const GLfloat sspec[4] = {0.0, 0.0, 0.0, 1.0};
281 static const GLfloat bshiny = 128.0;
282 static const GLfloat sshiny = 0.0;
284 GLfloat bcolor[4] = {0.0, 0.0, 0.0, 1.0};
285 GLfloat scolor[4] = {0.0, 0.0, 0.0, 1.0};
287 if (!bp->glx_context)
290 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
292 glShadeModel(GL_SMOOTH);
294 glEnable(GL_DEPTH_TEST);
295 glEnable(GL_NORMALIZE);
296 glEnable(GL_CULL_FACE);
298 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
302 glScalef(1.1, 1.1, 1.1);
306 get_position (bp->rot, &x, &y, &z, !bp->button_down_p);
307 glTranslatef((x - 0.5) * 8,
311 gltrackball_rotate (bp->trackball);
313 get_rotation (bp->rot, &x, &y, &z, !bp->button_down_p);
314 glRotatef (x * 360, 1.0, 0.0, 0.0);
315 glRotatef (y * 360, 0.0, 1.0, 0.0);
316 glRotatef (z * 360, 0.0, 0.0, 1.0);
319 bcolor[0] = bp->colors[bp->ccolor].red / 65536.0;
320 bcolor[1] = bp->colors[bp->ccolor].green / 65536.0;
321 bcolor[2] = bp->colors[bp->ccolor].blue / 65536.0;
323 c2 = (bp->ccolor + bp->color_shift) % bp->ncolors;
324 scolor[0] = bp->colors[c2].red / 65536.0;
325 scolor[1] = bp->colors[c2].green / 65536.0;
326 scolor[2] = bp->colors[c2].blue / 65536.0;
329 if (bp->ccolor >= bp->ncolors) bp->ccolor = 0;
331 mi->polygon_count = 0;
333 glScalef (2.0, 2.0, 2.0);
337 glMaterialfv (GL_FRONT, GL_SPECULAR, bspec);
338 glMateriali (GL_FRONT, GL_SHININESS, bshiny);
339 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, bcolor);
340 glCallList (bp->ball_list);
341 mi->polygon_count += (SPHERE_SLICES * SPHERE_STACKS);
343 glMaterialfv (GL_FRONT, GL_SPECULAR, sspec);
344 glMaterialf (GL_FRONT, GL_SHININESS, sshiny);
345 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, scolor);
349 if (mi->fps_p) do_fps (mi);
352 glXSwapBuffers(dpy, window);
355 XSCREENSAVER_MODULE_2 ("DangerBall", dangerball, ball)