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