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