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