f31bf026a9e59bfa23d708c701c801e3be77d033
[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 #include <X11/Intrinsic.h>
13
14 extern XtAppContext app;
15
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
23
24 #define DEF_SPIN        "True"
25 #define DEF_WANDER      "True"
26 #define DEF_SPEED       "0.05"
27
28 #define DEFAULTS        "*delay:        30000       \n" \
29                         "*count:        30          \n" \
30                         "*showFPS:      False       \n" \
31                         "*wireframe:    False       \n" \
32                         "*speed:      " DEF_SPEED " \n" \
33                         "*spin:       " DEF_SPIN   "\n" \
34                         "*wander:     " DEF_WANDER "\n" \
35
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
43 #undef countof
44 #define countof(x) (sizeof((x))/sizeof((*x)))
45
46 #include "xlockmore.h"
47 #include "colors.h"
48 #include "sphere.h"
49 #include "tube.h"
50 #include "rotator.h"
51 #include "gltrackball.h"
52 #include <ctype.h>
53
54 #ifdef USE_GL /* whole file */
55
56 #include <GL/glu.h>
57
58 typedef struct {
59   GLXContext *glx_context;
60   rotator *rot;
61   trackball_state *trackball;
62   Bool button_down_p;
63
64   GLuint ball_list;
65   GLuint spike_list;
66
67   GLfloat pos;
68   int *spikes;
69
70   int ncolors;
71   XColor *colors;
72   int ccolor;
73   int color_shift;
74
75 } ball_configuration;
76
77 static ball_configuration *bps = NULL;
78
79 static Bool do_spin;
80 static GLfloat speed;
81 static Bool do_wander;
82
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" }
89 };
90
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},
95 };
96
97 ModeSpecOpt sws_opts = {countof(opts), opts, countof(vars), vars, NULL};
98
99
100 /* Window management, etc
101  */
102 void
103 reshape_ball (ModeInfo *mi, int width, int height)
104 {
105   GLfloat h = (GLfloat) height / (GLfloat) width;
106
107   glViewport (0, 0, (GLint) width, (GLint) height);
108
109   glMatrixMode(GL_PROJECTION);
110   glLoadIdentity();
111   gluPerspective (30.0, 1/h, 1.0, 100.0);
112
113   glMatrixMode(GL_MODELVIEW);
114   glLoadIdentity();
115   gluLookAt( 0.0, 0.0, 30.0,
116              0.0, 0.0, 0.0,
117              0.0, 1.0, 0.0);
118
119   glClear(GL_COLOR_BUFFER_BIT);
120 }
121
122
123 static void
124 randomize_spikes (ModeInfo *mi)
125 {
126   ball_configuration *bp = &bps[MI_SCREEN(mi)];
127   int i;
128   bp->pos = 0;
129   for (i = 0; i < MI_COUNT(mi); i++)
130     {
131       bp->spikes[i*2]   = (random() % 360) - 180;
132       bp->spikes[i*2+1] = (random() % 180) - 90;
133     }
134
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;
138
139   if ((random() % 3) == 0)
140     bp->color_shift = random() % (bp->ncolors / 2);
141   else
142     bp->color_shift = 0;
143 }
144
145 static void
146 draw_spikes (ModeInfo *mi)
147 {
148   ball_configuration *bp = &bps[MI_SCREEN(mi)];
149   GLfloat diam = 0.2;
150   GLfloat pos = bp->pos;
151   int i;
152
153   if (pos < 0) pos = -pos;
154
155   pos = (asin (0.5 + pos/2) - 0.5) * 2;
156
157   for (i = 0; i < MI_COUNT(mi); i++)
158     {
159       glPushMatrix();
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);
166       glPopMatrix();
167
168       mi->polygon_count += (SPIKE_FACES + 1);
169     }
170 }
171
172
173 static void
174 move_spikes (ModeInfo *mi)
175 {
176   ball_configuration *bp = &bps[MI_SCREEN(mi)];
177
178   if (bp->pos >= 0)             /* moving outward */
179     {
180       bp->pos += speed;
181       if (bp->pos >= 1)         /*  reverse gears at apex */
182         bp->pos = -1;
183     }
184   else                          /* moving inward */
185     {
186       bp->pos += speed;
187       if (bp->pos >= 0)         /*  stop at end */
188         randomize_spikes (mi);
189     }
190 }
191
192
193 Bool
194 ball_handle_event (ModeInfo *mi, XEvent *event)
195 {
196   ball_configuration *bp = &bps[MI_SCREEN(mi)];
197
198   if (event->xany.type == ButtonPress &&
199       event->xbutton.button & Button1)
200     {
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));
205       return True;
206     }
207   else if (event->xany.type == ButtonRelease &&
208            event->xbutton.button & Button1)
209     {
210       bp->button_down_p = False;
211       return True;
212     }
213   else if (event->xany.type == MotionNotify &&
214            bp->button_down_p)
215     {
216       gltrackball_track (bp->trackball,
217                          event->xmotion.x, event->xmotion.y,
218                          MI_WIDTH (mi), MI_HEIGHT (mi));
219       return True;
220     }
221
222   return False;
223 }
224
225
226 void 
227 init_ball (ModeInfo *mi)
228 {
229   ball_configuration *bp;
230   int wire = MI_IS_WIREFRAME(mi);
231
232   if (!bps) {
233     bps = (ball_configuration *)
234       calloc (MI_NUM_SCREENS(mi), sizeof (ball_configuration));
235     if (!bps) {
236       fprintf(stderr, "%s: out of memory\n", progname);
237       exit(1);
238     }
239
240     bp = &bps[MI_SCREEN(mi)];
241   }
242
243   bp = &bps[MI_SCREEN(mi)];
244
245   bp->glx_context = init_GL(mi);
246
247   reshape_ball (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
248
249   if (!wire)
250     {
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};
255
256       glEnable(GL_LIGHTING);
257       glEnable(GL_LIGHT0);
258       glEnable(GL_DEPTH_TEST);
259       glEnable(GL_CULL_FACE);
260
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);
265     }
266
267   {
268     double spin_speed   = 10.0;
269     double wander_speed = 0.15;
270     double spin_accel   = 2.0;
271
272     bp->rot = make_rotator (do_spin ? spin_speed : 0,
273                             do_spin ? spin_speed : 0,
274                             do_spin ? spin_speed : 0,
275                             spin_accel,
276                             do_wander ? wander_speed : 0,
277                             True);
278     bp->trackball = gltrackball_init ();
279   }
280
281   bp->ncolors = 128;
282   bp->colors = (XColor *) calloc(bp->ncolors, sizeof(XColor));
283   make_smooth_colormap (0, 0, 0,
284                         bp->colors, &bp->ncolors,
285                         False, 0, False);
286
287   bp->spikes = (int *) calloc(MI_COUNT(mi), sizeof(*bp->spikes) * 2);
288
289   bp->ball_list = glGenLists (1);
290   bp->spike_list = glGenLists (1);
291
292   glNewList (bp->ball_list, GL_COMPILE);
293   unit_sphere (SPHERE_STACKS, SPHERE_SLICES, wire);
294   glEndList ();
295
296   glNewList (bp->spike_list, GL_COMPILE);
297   cone (0, 0, 0,
298         0, 1, 0,
299         1, 0, SPIKE_FACES, SMOOTH_SPIKES, False, wire);
300   glEndList ();
301
302   randomize_spikes (mi);
303 }
304
305
306 void
307 draw_ball (ModeInfo *mi)
308 {
309   ball_configuration *bp = &bps[MI_SCREEN(mi)];
310   Display *dpy = MI_DISPLAY(mi);
311   Window window = MI_WINDOW(mi);
312   int c2;
313
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;
320
321   if (!bp->glx_context)
322     return;
323
324   glShadeModel(GL_SMOOTH);
325
326   glEnable(GL_DEPTH_TEST);
327   glEnable(GL_NORMALIZE);
328   glEnable(GL_CULL_FACE);
329
330   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
331
332   glPushMatrix ();
333
334   glScalef(1.1, 1.1, 1.1);
335
336   {
337     double x, y, z;
338     get_position (bp->rot, &x, &y, &z, !bp->button_down_p);
339     glTranslatef((x - 0.5) * 8,
340                  (y - 0.5) * 8,
341                  (z - 0.5) * 15);
342
343     gltrackball_rotate (bp->trackball);
344
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);
349   }
350
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;
354
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;
359
360   bp->ccolor++;
361   if (bp->ccolor >= bp->ncolors) bp->ccolor = 0;
362
363   mi->polygon_count = 0;
364
365   glScalef (2.0, 2.0, 2.0);
366
367   move_spikes (mi);
368
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);
374
375   glMaterialfv (GL_FRONT, GL_SPECULAR,            sspec);
376   glMaterialf  (GL_FRONT, GL_SHININESS,           sshiny);
377   glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, scolor);
378   draw_spikes (mi);
379   glPopMatrix ();
380
381   if (mi->fps_p) do_fps (mi);
382   glFinish();
383
384   glXSwapBuffers(dpy, window);
385 }
386
387 #endif /* USE_GL */