From http://www.jwz.org/xscreensaver/xscreensaver-5.30.tar.gz
[xscreensaver] / hacks / glx / cubestorm.c
1 /* cubestorm, Copyright (c) 2003-2014 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:      " DEF_COUNT   "\n" \
14                         "*showFPS:      False       \n" \
15                         "*fpsSolid:     True        \n" \
16                         "*wireframe:    False       \n" \
17
18
19 # define refresh_cube 0
20 # define release_cube 0
21 #undef countof
22 #define countof(x) (sizeof((x))/sizeof((*x)))
23
24 #include "xlockmore.h"
25 #include "colors.h"
26 #include "rotator.h"
27 #include "gltrackball.h"
28 #include <ctype.h>
29
30 #ifdef USE_GL /* whole file */
31
32 #define DEF_SPIN        "True"
33 #define DEF_WANDER      "True"
34 #define DEF_SPEED       "1.0"
35 #define DEF_THICKNESS   "0.06"
36 #define DEF_COUNT       "4"
37 #define DEF_DBUF        "False"
38
39 typedef struct {
40   rotator *rot;
41   int ccolor;
42 } subcube;
43
44 typedef struct {
45   GLXContext *glx_context;
46   trackball_state *trackball;
47   Bool button_down_p;
48   Bool clear_p;
49
50   GLuint cube_list;
51
52   int ncolors;
53   XColor *colors;
54
55   subcube *subcubes;
56
57 } cube_configuration;
58
59 static cube_configuration *bps = NULL;
60
61 static Bool do_spin;
62 static Bool do_wander;
63 static GLfloat speed;
64 static GLfloat thickness;
65 static Bool dbuf_p;
66
67 static XrmOptionDescRec opts[] = {
68   { "-spin",   ".spin",   XrmoptionNoArg, "True" },
69   { "+spin",   ".spin",   XrmoptionNoArg, "False" },
70   { "-wander", ".wander", XrmoptionNoArg, "True" },
71   { "+wander", ".wander", XrmoptionNoArg, "False" },
72   { "-speed",  ".speed",  XrmoptionSepArg, 0 },
73   { "-db",     ".doubleBuffer", XrmoptionNoArg, "True"},
74   { "+db",     ".doubleBuffer", XrmoptionNoArg, "False"},
75   { "-thickness", ".thickness", XrmoptionSepArg, 0 },
76 };
77
78 static argtype vars[] = {
79   {&do_spin,   "spin",   "Spin",   DEF_SPIN,   t_Bool},
80   {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
81   {&speed,     "speed",  "Speed",  DEF_SPEED,  t_Float},
82   {&thickness, "thickness", "Thickness",  DEF_THICKNESS,  t_Float},
83   {&dbuf_p, "doubleBuffer", "DoubleBuffer", DEF_DBUF, t_Bool},
84 };
85
86 ENTRYPOINT ModeSpecOpt cube_opts = {countof(opts), opts, countof(vars), vars, NULL};
87
88
89 static void
90 draw_face (ModeInfo *mi)
91 {
92   int wire = MI_IS_WIREFRAME(mi);
93
94   int i;
95   GLfloat t = thickness / 2;
96   GLfloat a = -0.5;
97   GLfloat b =  0.5;
98
99   if (t <= 0) t = 0.001;
100   else if (t > 0.5) t = 0.5;
101
102   glPushMatrix();
103   glFrontFace(GL_CW);
104
105   for (i = 0; i < 4; i++)
106     {
107       glNormal3f (0, 0, -1);
108       glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
109       glVertex3f (a,   a,   a);
110       glVertex3f (b,   a,   a);
111       glVertex3f (b-t, a+t, a);
112       glVertex3f (a+t, a+t, a);
113       glEnd();
114
115       glNormal3f (0, 1, 0);
116       glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
117       glVertex3f (b-t, a+t, a);
118       glVertex3f (b-t, a+t, a+t);
119       glVertex3f (a+t, a+t, a+t);
120       glVertex3f (a+t, a+t, a);
121       glEnd();
122
123       glRotatef(90, 0, 0, 1);
124     }
125   glPopMatrix();
126 }
127
128 static void
129 draw_faces (ModeInfo *mi)
130 {
131   glPushMatrix();
132   draw_face (mi);
133   glRotatef (90,  0, 1, 0); draw_face (mi);
134   glRotatef (90,  0, 1, 0); draw_face (mi);
135   glRotatef (90,  0, 1, 0); draw_face (mi);
136   glRotatef (90,  1, 0, 0); draw_face (mi);
137   glRotatef (180, 1, 0, 0); draw_face (mi);
138   glPopMatrix();
139 }
140
141
142 static void
143 new_cube_colors (ModeInfo *mi)
144 {
145   cube_configuration *bp = &bps[MI_SCREEN(mi)];
146   int i;
147   bp->ncolors = 128;
148   if (bp->colors) free (bp->colors);
149   bp->colors = (XColor *) calloc(bp->ncolors, sizeof(XColor));
150   make_smooth_colormap (0, 0, 0,
151                         bp->colors, &bp->ncolors,
152                         False, 0, False);
153   for (i = 0; i < MI_COUNT(mi); i++)
154     bp->subcubes[i].ccolor = random() % bp->ncolors;
155 }
156
157
158 /* Window management, etc
159  */
160 ENTRYPOINT void
161 reshape_cube (ModeInfo *mi, int width, int height)
162 {
163   GLfloat h = (GLfloat) height / (GLfloat) width;
164
165   glViewport (0, 0, (GLint) width, (GLint) height);
166
167   glMatrixMode(GL_PROJECTION);
168   glLoadIdentity();
169   gluPerspective (30.0, 1/h, 1.0, 100.0);
170
171   glMatrixMode(GL_MODELVIEW);
172   glLoadIdentity();
173   gluLookAt( 0.0, 0.0, 45.0,
174              0.0, 0.0, 0.0,
175              0.0, 1.0, 0.0);
176
177   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
178 }
179
180
181 ENTRYPOINT Bool
182 cube_handle_event (ModeInfo *mi, XEvent *event)
183 {
184   cube_configuration *bp = &bps[MI_SCREEN(mi)];
185
186   if (gltrackball_event_handler (event, bp->trackball,
187                                  MI_WIDTH (mi), MI_HEIGHT (mi),
188                                  &bp->button_down_p))
189     return True;
190   else if (event->xany.type == KeyPress)
191     {
192       KeySym keysym;
193       char c = 0;
194       XLookupString (&event->xkey, &c, 1, &keysym, 0);
195       if (c == ' ')
196         {
197           glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
198           return True;
199         }
200       else if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
201         goto DEF;
202     }
203   else if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
204     {
205     DEF:
206       new_cube_colors (mi);
207       return True;
208     }
209   return False;
210 }
211
212
213 ENTRYPOINT void 
214 init_cube (ModeInfo *mi)
215 {
216   cube_configuration *bp;
217   int wire = MI_IS_WIREFRAME(mi);
218   int i;
219
220   if (!bps) {
221     bps = (cube_configuration *)
222       calloc (MI_NUM_SCREENS(mi), sizeof (cube_configuration));
223     if (!bps) {
224       fprintf(stderr, "%s: out of memory\n", progname);
225       exit(1);
226     }
227   }
228
229 # ifdef HAVE_JWZGLES
230   dbuf_p = True;
231 # endif
232
233   bp = &bps[MI_SCREEN(mi)];
234
235   bp->glx_context = init_GL(mi);
236
237   if (MI_COUNT(mi) <= 0) MI_COUNT(mi) = 1;
238
239   bp->trackball = gltrackball_init (True);
240   bp->subcubes = (subcube *) calloc (MI_COUNT(mi), sizeof(subcube));
241   for (i = 0; i < MI_COUNT(mi); i++)
242     {
243       double wander_speed, spin_speed, spin_accel;
244
245       if (i == 0)
246         {
247           wander_speed = 0.05 * speed;
248           spin_speed   = 10.0 * speed;
249           spin_accel   = 4.0  * speed;
250         }
251       else
252         {
253           wander_speed = 0;
254           spin_speed   = 4.0 * speed;
255           spin_accel   = 2.0 * speed;
256         }
257
258       bp->subcubes[i].rot = make_rotator (do_spin ? spin_speed : 0,
259                                           do_spin ? spin_speed : 0,
260                                           do_spin ? spin_speed : 0,
261                                           spin_accel,
262                                           do_wander ? wander_speed : 0,
263                                           True);
264     }
265
266   bp->colors = 0;
267   new_cube_colors (mi);
268
269   reshape_cube (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
270
271   if (!wire)
272     {
273       GLfloat pos[4] = {1.0, 1.0, 1.0, 0.0};
274       GLfloat amb[4] = {0.0, 0.0, 0.0, 1.0};
275       GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
276       GLfloat spc[4] = {0.0, 1.0, 1.0, 1.0};
277
278       glEnable(GL_LIGHTING);
279       glEnable(GL_LIGHT0);
280       glEnable(GL_DEPTH_TEST);
281       glEnable(GL_CULL_FACE);
282
283       glLightfv(GL_LIGHT0, GL_POSITION, pos);
284       glLightfv(GL_LIGHT0, GL_AMBIENT,  amb);
285       glLightfv(GL_LIGHT0, GL_DIFFUSE,  dif);
286       glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
287     }
288
289   bp->cube_list = glGenLists (1);
290   glNewList (bp->cube_list, GL_COMPILE);
291   draw_faces (mi);
292   glEndList ();
293
294   glDrawBuffer(dbuf_p ? GL_BACK : GL_FRONT);
295   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
296 }
297
298
299 ENTRYPOINT void
300 draw_cube (ModeInfo *mi)
301 {
302   cube_configuration *bp = &bps[MI_SCREEN(mi)];
303   Display *dpy = MI_DISPLAY(mi);
304   Window window = MI_WINDOW(mi);
305   int wire = MI_IS_WIREFRAME(mi);
306   int i;
307   double x, y, z;
308
309   if (!bp->glx_context)
310     return;
311
312   glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
313
314   glShadeModel(GL_SMOOTH);
315
316   glEnable(GL_DEPTH_TEST);
317   glEnable(GL_NORMALIZE);
318   glEnable(GL_CULL_FACE);
319
320   if (bp->clear_p)   /* we're in "no vapor trails" mode */
321     {
322       glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
323       if (! (random() % (int) (25 / speed)))
324         bp->clear_p = False;
325     }
326   else
327     {
328       if (! (random() % (int) (200 / speed)))
329         {
330           bp->clear_p = True;
331           new_cube_colors (mi);
332         }
333     }
334
335   glPushMatrix ();
336
337   glScalef(1.1, 1.1, 1.1);
338
339   mi->polygon_count = 0;
340
341   get_position (bp->subcubes[0].rot, &x, &y, &z, !bp->button_down_p);
342   glTranslatef((x - 0.5) * 15,
343                (y - 0.5) * 15,
344                (z - 0.5) * 30);
345   gltrackball_rotate (bp->trackball);
346
347   glScalef (4.0, 4.0, 4.0);
348
349   for (i = 0; i < MI_COUNT(mi); i++)
350     {
351       GLfloat bcolor[4] = {0.0, 0.0, 0.0, 1.0};
352       GLfloat bspec[4]  = {1.0, 1.0, 1.0, 1.0};
353       GLfloat bshiny    = 128.0;
354
355       glPushMatrix();
356
357       if (i != 0)  /* N+1 cubes rotate relative to cube 0 */
358         {
359           get_rotation (bp->subcubes[0].rot, &x, &y, &z, False);
360           glRotatef (x * 360, 1.0, 0.0, 0.0);
361           glRotatef (y * 360, 0.0, 1.0, 0.0);
362           glRotatef (z * 360, 0.0, 0.0, 1.0);
363         }
364
365       get_rotation (bp->subcubes[i].rot, &x, &y, &z, !bp->button_down_p);
366       glRotatef (x * 360, 1.0, 0.0, 0.0);
367       glRotatef (y * 360, 0.0, 1.0, 0.0);
368       glRotatef (z * 360, 0.0, 0.0, 1.0);
369
370       bcolor[0] = bp->colors[bp->subcubes[i].ccolor].red   / 65536.0;
371       bcolor[1] = bp->colors[bp->subcubes[i].ccolor].green / 65536.0;
372       bcolor[2] = bp->colors[bp->subcubes[i].ccolor].blue  / 65536.0;
373       bp->subcubes[i].ccolor++;
374       if (bp->subcubes[i].ccolor >= bp->ncolors)
375         bp->subcubes[i].ccolor = 0;
376
377       if (wire)
378         glColor3f (bcolor[0], bcolor[1], bcolor[2]);
379       else
380         {
381           glMaterialfv (GL_FRONT, GL_SPECULAR,            bspec);
382           glMateriali  (GL_FRONT, GL_SHININESS,           bshiny);
383           glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, bcolor);
384         }
385
386       glCallList (bp->cube_list);
387       mi->polygon_count += (4 * 2 * 6);
388
389       glPopMatrix();
390     }
391
392   glPopMatrix ();
393
394   if (mi->fps_p) do_fps (mi);
395   glFinish();
396
397   if (dbuf_p)
398     glXSwapBuffers(dpy, window);
399 }
400
401 XSCREENSAVER_MODULE_2 ("CubeStorm", cubestorm, cube)
402
403 #endif /* USE_GL */