a882500a15bf54b6959af936f551e19e92412b23
[xscreensaver] / hacks / glx / dangerball.c
1 /* dangerball, Copyright (c) 2001-2004 Jamie Zawinski <jwz@jwz.org>
2  *
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 
9  * implied warranty.
10  */
11
12 #define DEFAULTS        "*delay:        30000       \n" \
13                         "*count:        30          \n" \
14                         "*showFPS:      False       \n" \
15                         "*wireframe:    False       \n" \
16
17 # define refresh_ball 0
18 # define release_ball 0
19 #undef countof
20 #define countof(x) (sizeof((x))/sizeof((*x)))
21
22 #include "xlockmore.h"
23 #include "colors.h"
24 #include "sphere.h"
25 #include "tube.h"
26 #include "rotator.h"
27 #include "gltrackball.h"
28 #include <ctype.h>
29
30 #ifdef USE_GL /* whole file */
31
32
33 #define DEF_SPIN        "True"
34 #define DEF_WANDER      "True"
35 #define DEF_SPEED       "0.05"
36
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
41
42 typedef struct {
43   GLXContext *glx_context;
44   rotator *rot;
45   trackball_state *trackball;
46   Bool button_down_p;
47
48   GLuint ball_list;
49   GLuint spike_list;
50
51   GLfloat pos;
52   int *spikes;
53
54   int ncolors;
55   XColor *colors;
56   int ccolor;
57   int color_shift;
58
59 } ball_configuration;
60
61 static ball_configuration *bps = NULL;
62
63 static Bool do_spin;
64 static GLfloat speed;
65 static Bool do_wander;
66
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" }
73 };
74
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},
79 };
80
81 ENTRYPOINT ModeSpecOpt ball_opts = {countof(opts), opts, countof(vars), vars, NULL};
82
83
84 /* Window management, etc
85  */
86 ENTRYPOINT void
87 reshape_ball (ModeInfo *mi, int width, int height)
88 {
89   GLfloat h = (GLfloat) height / (GLfloat) width;
90
91   glViewport (0, 0, (GLint) width, (GLint) height);
92
93   glMatrixMode(GL_PROJECTION);
94   glLoadIdentity();
95   gluPerspective (30.0, 1/h, 1.0, 100.0);
96
97   glMatrixMode(GL_MODELVIEW);
98   glLoadIdentity();
99   gluLookAt( 0.0, 0.0, 30.0,
100              0.0, 0.0, 0.0,
101              0.0, 1.0, 0.0);
102
103   glClear(GL_COLOR_BUFFER_BIT);
104 }
105
106
107 static void
108 randomize_spikes (ModeInfo *mi)
109 {
110   ball_configuration *bp = &bps[MI_SCREEN(mi)];
111   int i;
112   bp->pos = 0;
113   for (i = 0; i < MI_COUNT(mi); i++)
114     {
115       bp->spikes[i*2]   = (random() % 360) - 180;
116       bp->spikes[i*2+1] = (random() % 180) - 90;
117     }
118
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;
122
123   if ((random() % 3) == 0)
124     bp->color_shift = random() % (bp->ncolors / 2);
125   else
126     bp->color_shift = 0;
127 }
128
129 static void
130 draw_spikes (ModeInfo *mi)
131 {
132   ball_configuration *bp = &bps[MI_SCREEN(mi)];
133   GLfloat diam = 0.2;
134   GLfloat pos = bp->pos;
135   int i;
136
137   if (pos < 0) pos = -pos;
138
139   pos = (asin (0.5 + pos/2) - 0.5) * 2;
140
141   for (i = 0; i < MI_COUNT(mi); i++)
142     {
143       glPushMatrix();
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);
150       glPopMatrix();
151
152       mi->polygon_count += (SPIKE_FACES + 1);
153     }
154 }
155
156
157 static void
158 move_spikes (ModeInfo *mi)
159 {
160   ball_configuration *bp = &bps[MI_SCREEN(mi)];
161
162   if (bp->pos >= 0)             /* moving outward */
163     {
164       bp->pos += speed;
165       if (bp->pos >= 1)         /*  reverse gears at apex */
166         bp->pos = -1;
167     }
168   else                          /* moving inward */
169     {
170       bp->pos += speed;
171       if (bp->pos >= 0)         /*  stop at end */
172         randomize_spikes (mi);
173     }
174 }
175
176
177 ENTRYPOINT Bool
178 ball_handle_event (ModeInfo *mi, XEvent *event)
179 {
180   ball_configuration *bp = &bps[MI_SCREEN(mi)];
181
182   if (event->xany.type == ButtonPress &&
183       event->xbutton.button == Button1)
184     {
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));
189       return True;
190     }
191   else if (event->xany.type == ButtonRelease &&
192            event->xbutton.button == Button1)
193     {
194       bp->button_down_p = False;
195       return True;
196     }
197   else if (event->xany.type == ButtonPress &&
198            (event->xbutton.button == Button4 ||
199             event->xbutton.button == Button5))
200     {
201       gltrackball_mousewheel (bp->trackball, event->xbutton.button, 10,
202                               !!event->xbutton.state);
203       return True;
204     }
205   else if (event->xany.type == MotionNotify &&
206            bp->button_down_p)
207     {
208       gltrackball_track (bp->trackball,
209                          event->xmotion.x, event->xmotion.y,
210                          MI_WIDTH (mi), MI_HEIGHT (mi));
211       return True;
212     }
213
214   return False;
215 }
216
217
218 ENTRYPOINT void 
219 init_ball (ModeInfo *mi)
220 {
221   ball_configuration *bp;
222   int wire = MI_IS_WIREFRAME(mi);
223
224   if (!bps) {
225     bps = (ball_configuration *)
226       calloc (MI_NUM_SCREENS(mi), sizeof (ball_configuration));
227     if (!bps) {
228       fprintf(stderr, "%s: out of memory\n", progname);
229       exit(1);
230     }
231
232     bp = &bps[MI_SCREEN(mi)];
233   }
234
235   bp = &bps[MI_SCREEN(mi)];
236
237   bp->glx_context = init_GL(mi);
238
239   reshape_ball (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
240
241   if (!wire)
242     {
243       GLfloat pos[4] = {1.0, 1.0, 1.0, 0.0};
244       GLfloat amb[4] = {0.0, 0.0, 0.0, 1.0};
245       GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
246       GLfloat spc[4] = {0.0, 1.0, 1.0, 1.0};
247
248       glEnable(GL_LIGHTING);
249       glEnable(GL_LIGHT0);
250       glEnable(GL_DEPTH_TEST);
251       glEnable(GL_CULL_FACE);
252
253       glLightfv(GL_LIGHT0, GL_POSITION, pos);
254       glLightfv(GL_LIGHT0, GL_AMBIENT,  amb);
255       glLightfv(GL_LIGHT0, GL_DIFFUSE,  dif);
256       glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
257     }
258
259   {
260     double spin_speed   = 10.0;
261     double wander_speed = 0.12;
262     double spin_accel   = 2.0;
263
264     bp->rot = make_rotator (do_spin ? spin_speed : 0,
265                             do_spin ? spin_speed : 0,
266                             do_spin ? spin_speed : 0,
267                             spin_accel,
268                             do_wander ? wander_speed : 0,
269                             True);
270     bp->trackball = gltrackball_init ();
271   }
272
273   bp->ncolors = 128;
274   bp->colors = (XColor *) calloc(bp->ncolors, sizeof(XColor));
275   make_smooth_colormap (0, 0, 0,
276                         bp->colors, &bp->ncolors,
277                         False, 0, False);
278
279   bp->spikes = (int *) calloc(MI_COUNT(mi), sizeof(*bp->spikes) * 2);
280
281   bp->ball_list = glGenLists (1);
282   bp->spike_list = glGenLists (1);
283
284   glNewList (bp->ball_list, GL_COMPILE);
285   unit_sphere (SPHERE_STACKS, SPHERE_SLICES, wire);
286   glEndList ();
287
288   glNewList (bp->spike_list, GL_COMPILE);
289   cone (0, 0, 0,
290         0, 1, 0,
291         1, 0, SPIKE_FACES, SMOOTH_SPIKES, False, wire);
292   glEndList ();
293
294   randomize_spikes (mi);
295 }
296
297
298 ENTRYPOINT void
299 draw_ball (ModeInfo *mi)
300 {
301   ball_configuration *bp = &bps[MI_SCREEN(mi)];
302   Display *dpy = MI_DISPLAY(mi);
303   Window window = MI_WINDOW(mi);
304   int c2;
305
306   static const GLfloat bspec[4]  = {1.0, 1.0, 1.0, 1.0};
307   static const GLfloat sspec[4]  = {0.0, 0.0, 0.0, 1.0};
308   static const GLfloat bshiny    = 128.0;
309   static const GLfloat sshiny    = 0.0;
310
311   GLfloat bcolor[4] = {0.0, 0.0, 0.0, 1.0};
312   GLfloat scolor[4] = {0.0, 0.0, 0.0, 1.0};
313
314   if (!bp->glx_context)
315     return;
316
317   glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
318
319   glShadeModel(GL_SMOOTH);
320
321   glEnable(GL_DEPTH_TEST);
322   glEnable(GL_NORMALIZE);
323   glEnable(GL_CULL_FACE);
324
325   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
326
327   glPushMatrix ();
328
329   glScalef(1.1, 1.1, 1.1);
330
331   {
332     double x, y, z;
333     get_position (bp->rot, &x, &y, &z, !bp->button_down_p);
334     glTranslatef((x - 0.5) * 8,
335                  (y - 0.5) * 8,
336                  (z - 0.5) * 15);
337
338     gltrackball_rotate (bp->trackball);
339
340     get_rotation (bp->rot, &x, &y, &z, !bp->button_down_p);
341     glRotatef (x * 360, 1.0, 0.0, 0.0);
342     glRotatef (y * 360, 0.0, 1.0, 0.0);
343     glRotatef (z * 360, 0.0, 0.0, 1.0);
344   }
345
346   bcolor[0] = bp->colors[bp->ccolor].red   / 65536.0;
347   bcolor[1] = bp->colors[bp->ccolor].green / 65536.0;
348   bcolor[2] = bp->colors[bp->ccolor].blue  / 65536.0;
349
350   c2 = (bp->ccolor + bp->color_shift) % bp->ncolors;
351   scolor[0] = bp->colors[c2].red   / 65536.0;
352   scolor[1] = bp->colors[c2].green / 65536.0;
353   scolor[2] = bp->colors[c2].blue  / 65536.0;
354
355   bp->ccolor++;
356   if (bp->ccolor >= bp->ncolors) bp->ccolor = 0;
357
358   mi->polygon_count = 0;
359
360   glScalef (2.0, 2.0, 2.0);
361
362   move_spikes (mi);
363
364   glMaterialfv (GL_FRONT, GL_SPECULAR,            bspec);
365   glMateriali  (GL_FRONT, GL_SHININESS,           bshiny);
366   glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, bcolor);
367   glCallList (bp->ball_list);
368   mi->polygon_count += (SPHERE_SLICES * SPHERE_STACKS);
369
370   glMaterialfv (GL_FRONT, GL_SPECULAR,            sspec);
371   glMaterialf  (GL_FRONT, GL_SHININESS,           sshiny);
372   glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, scolor);
373   draw_spikes (mi);
374   glPopMatrix ();
375
376   if (mi->fps_p) do_fps (mi);
377   glFinish();
378
379   glXSwapBuffers(dpy, window);
380 }
381
382 XSCREENSAVER_MODULE_2 ("DangerBall", dangerball, ball)
383
384 #endif /* USE_GL */