http://ftp.ksu.edu.tw/FTP/FreeBSD/distfiles/xscreensaver-4.20.tar.gz
[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 == ButtonPress &&
214            (event->xbutton.button == Button4 ||
215             event->xbutton.button == Button5))
216     {
217       gltrackball_mousewheel (bp->trackball, event->xbutton.button, 10,
218                               !!event->xbutton.state);
219       return True;
220     }
221   else if (event->xany.type == MotionNotify &&
222            bp->button_down_p)
223     {
224       gltrackball_track (bp->trackball,
225                          event->xmotion.x, event->xmotion.y,
226                          MI_WIDTH (mi), MI_HEIGHT (mi));
227       return True;
228     }
229   else if (event->xany.type == KeyPress)
230     {
231       KeySym keysym;
232       char c = 0;
233       XLookupString (&event->xkey, &c, 1, &keysym, 0);
234       if (c == ' ')
235         {
236           glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
237           return True;
238         }
239       else if (c == '\r' || c == '\n')
240         {
241           new_cube_colors (mi);
242           return True;
243         }
244     }
245   return False;
246 }
247
248
249 void 
250 init_cube (ModeInfo *mi)
251 {
252   cube_configuration *bp;
253   int wire = MI_IS_WIREFRAME(mi);
254   int i;
255
256   if (!bps) {
257     bps = (cube_configuration *)
258       calloc (MI_NUM_SCREENS(mi), sizeof (cube_configuration));
259     if (!bps) {
260       fprintf(stderr, "%s: out of memory\n", progname);
261       exit(1);
262     }
263
264     bp = &bps[MI_SCREEN(mi)];
265   }
266
267   bp = &bps[MI_SCREEN(mi)];
268
269   bp->glx_context = init_GL(mi);
270
271   if (MI_COUNT(mi) <= 0) MI_COUNT(mi) = 1;
272
273   bp->trackball = gltrackball_init ();
274   bp->subcubes = (subcube *) calloc (MI_COUNT(mi), sizeof(subcube));
275   for (i = 0; i < MI_COUNT(mi); i++)
276     {
277       double wander_speed, spin_speed, spin_accel;
278
279       if (i == 0)
280         {
281           wander_speed = 0.05 * speed;
282           spin_speed   = 10.0 * speed;
283           spin_accel   = 4.0  * speed;
284         }
285       else
286         {
287           wander_speed = 0;
288           spin_speed   = 4.0 * speed;
289           spin_accel   = 2.0 * speed;
290         }
291
292       bp->subcubes[i].rot = make_rotator (do_spin ? spin_speed : 0,
293                                           do_spin ? spin_speed : 0,
294                                           do_spin ? spin_speed : 0,
295                                           spin_accel,
296                                           do_wander ? wander_speed : 0,
297                                           True);
298     }
299
300   bp->colors = 0;
301   new_cube_colors (mi);
302
303   reshape_cube (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
304
305   if (!wire)
306     {
307       GLfloat pos[4] = {1.0, 1.0, 1.0, 0.0};
308       GLfloat amb[4] = {0.0, 0.0, 0.0, 1.0};
309       GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
310       GLfloat spc[4] = {0.0, 1.0, 1.0, 1.0};
311
312       glEnable(GL_LIGHTING);
313       glEnable(GL_LIGHT0);
314       glEnable(GL_DEPTH_TEST);
315       glEnable(GL_CULL_FACE);
316
317       glLightfv(GL_LIGHT0, GL_POSITION, pos);
318       glLightfv(GL_LIGHT0, GL_AMBIENT,  amb);
319       glLightfv(GL_LIGHT0, GL_DIFFUSE,  dif);
320       glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
321     }
322
323   bp->cube_list = glGenLists (1);
324   glNewList (bp->cube_list, GL_COMPILE);
325   draw_faces (mi);
326   glEndList ();
327
328   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
329 }
330
331
332 void
333 draw_cube (ModeInfo *mi)
334 {
335   cube_configuration *bp = &bps[MI_SCREEN(mi)];
336   Display *dpy = MI_DISPLAY(mi);
337   Window window = MI_WINDOW(mi);
338   int wire = MI_IS_WIREFRAME(mi);
339   int i;
340   double x, y, z;
341
342   if (!bp->glx_context)
343     return;
344
345   glShadeModel(GL_SMOOTH);
346
347   glEnable(GL_DEPTH_TEST);
348   glEnable(GL_NORMALIZE);
349   glEnable(GL_CULL_FACE);
350
351   if (bp->clear_p)   /* we're in "no vapor trails" mode */
352     {
353       glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
354       if (! (random() % (int) (25 / speed)))
355         bp->clear_p = False;
356     }
357   else
358     {
359       if (! (random() % (int) (200 / speed)))
360         {
361           bp->clear_p = True;
362           new_cube_colors (mi);
363         }
364     }
365
366   glPushMatrix ();
367
368   glScalef(1.1, 1.1, 1.1);
369
370   mi->polygon_count = 0;
371
372   get_position (bp->subcubes[0].rot, &x, &y, &z, !bp->button_down_p);
373   glTranslatef((x - 0.5) * 15,
374                (y - 0.5) * 15,
375                (z - 0.5) * 30);
376   gltrackball_rotate (bp->trackball);
377
378   glScalef (4.0, 4.0, 4.0);
379
380   for (i = 0; i < MI_COUNT(mi); i++)
381     {
382       static GLfloat bcolor[4] = {0.0, 0.0, 0.0, 1.0};
383       static GLfloat bspec[4]  = {1.0, 1.0, 1.0, 1.0};
384       static GLfloat bshiny    = 128.0;
385
386       glPushMatrix();
387
388       if (i != 0)  /* N+1 cubes rotate relative to cube 0 */
389         {
390           get_rotation (bp->subcubes[0].rot, &x, &y, &z, False);
391           glRotatef (x * 360, 1.0, 0.0, 0.0);
392           glRotatef (y * 360, 0.0, 1.0, 0.0);
393           glRotatef (z * 360, 0.0, 0.0, 1.0);
394         }
395
396       get_rotation (bp->subcubes[i].rot, &x, &y, &z, !bp->button_down_p);
397       glRotatef (x * 360, 1.0, 0.0, 0.0);
398       glRotatef (y * 360, 0.0, 1.0, 0.0);
399       glRotatef (z * 360, 0.0, 0.0, 1.0);
400
401       bcolor[0] = bp->colors[bp->subcubes[i].ccolor].red   / 65536.0;
402       bcolor[1] = bp->colors[bp->subcubes[i].ccolor].green / 65536.0;
403       bcolor[2] = bp->colors[bp->subcubes[i].ccolor].blue  / 65536.0;
404       bp->subcubes[i].ccolor++;
405       if (bp->subcubes[i].ccolor >= bp->ncolors)
406         bp->subcubes[i].ccolor = 0;
407
408       if (wire)
409         glColor3f (bcolor[0], bcolor[1], bcolor[2]);
410       else
411         {
412           glMaterialfv (GL_FRONT, GL_SPECULAR,            bspec);
413           glMateriali  (GL_FRONT, GL_SHININESS,           bshiny);
414           glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, bcolor);
415         }
416
417       glCallList (bp->cube_list);
418       mi->polygon_count += (4 * 2 * 6);
419
420       glPopMatrix();
421     }
422
423   glPopMatrix ();
424
425   if (mi->fps_p) do_fps (mi);
426   glFinish();
427
428   glXSwapBuffers(dpy, window);
429 }
430
431 #endif /* USE_GL */