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