http://www.jwz.org/xscreensaver/xscreensaver-5.13.tar.gz
[xscreensaver] / hacks / glx / blocktube.c
1 /* blocktube, Copyright (c) 2003 Lars Damerow <lars@oddment.org>
2  *
3  * Based on Jamie Zawinski's original dangerball code.
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and its
6  * documentation for any purpose is hereby granted without fee, provided that
7  * the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation.  No representations are made about the suitability of this
10  * software for any purpose.  It is provided "as is" without express or 
11  * implied warranty.
12  */
13
14 #define DEBUG 1
15
16 #define DEFAULTS        "*delay:        40000           \n" \
17                         "*wireframe:    False           \n" \
18                         "*showFPS:      False           \n" \
19
20 # define refresh_blocktube 0
21 # define blocktube_handle_event 0
22 #undef countof
23 #define countof(x) (sizeof((x))/sizeof((*x)))
24
25 #include "xlockmore.h"
26 #include "colors.h"
27 #include <math.h>
28 #include <ctype.h>
29
30 #ifdef USE_GL /* whole file */
31
32 #define DEF_HOLDTIME    "1000"
33 #define DEF_CHANGETIME  "200"
34 #define MAX_ENTITIES     1000
35 #define DEF_TEXTURE     "True"
36 #define DEF_FOG         "True"
37
38 #if defined(USE_XPM) || defined(USE_XPMINC) || defined(STANDALONE)
39 /* USE_XPM & USE_XPMINC in xlock mode ; HAVE_XPM in xscreensaver mode */
40 #include "xpm-ximage.h"
41 #define I_HAVE_XPM
42
43 #include "../images/blocktube.xpm"
44 #endif /* HAVE_XPM */
45
46 typedef struct {
47     int id, r, g, b;
48     GLfloat tVal;
49     int age;
50     int lifetime;
51     GLfloat position[3];
52     GLfloat angle;
53     GLfloat angularVelocity;
54 } entity;
55
56 typedef struct {
57   GLXContext *glx_context;
58   GLuint  block_dlist;
59   int nextID;
60
61   entity entities[MAX_ENTITIES];
62   float targetR, targetG, targetB,
63     currentR, currentG, currentB,
64     deltaR, deltaG, deltaB;
65   int counter;
66   int changing;
67   GLfloat zoom;
68   GLfloat tilt;
69   GLuint envTexture;
70   XImage *texti;
71
72   GLfloat tunnelLength;
73   GLfloat tunnelWidth;
74   int polys;
75
76 } blocktube_configuration;
77
78 static blocktube_configuration *lps = NULL;
79
80 static GLint holdtime;
81 static GLint changetime;
82 static int do_texture;
83 static int do_fog;
84
85 static XrmOptionDescRec opts[] = {
86     { "-holdtime",  ".holdtime",  XrmoptionSepArg, 0 },
87     { "-changetime",  ".changetime",  XrmoptionSepArg, 0 },
88     {"-texture",     ".texture",   XrmoptionNoArg, "True" },
89     {"+texture",     ".texture",   XrmoptionNoArg, "False" },
90     {"-fog",         ".fog",       XrmoptionNoArg, "True" },
91     {"+fog",         ".fog",       XrmoptionNoArg, "False" },
92 };
93
94 static argtype vars[] = {
95     {&holdtime, "holdtime",  "Hold Time",  DEF_HOLDTIME,  t_Int},
96     {&changetime, "changetime",  "Change Time",  DEF_CHANGETIME, \
97      t_Int},
98     {&do_texture, "texture",    "Texture", DEF_TEXTURE,   t_Bool},
99     {&do_fog,     "fog",        "Fog",     DEF_FOG,       t_Bool},
100 };
101
102 static OptionStruct desc[] = {
103     {"-holdtime", "how long to stay on the same color"},
104     {"-changetime", "how long it takes to fade to a new color"},
105 };
106
107 ENTRYPOINT ModeSpecOpt blocktube_opts = {countof(opts), opts, countof(vars), vars, desc};
108
109 #ifdef USE_MODULES
110 ModStruct blocktube_description =
111     {"blocktube", "init_blocktube", "draw_blocktube", "release_blocktube",
112      "draw_blocktube", "init_blocktube", (char *)NULL, &blocktube_opts,
113      40000, 30, 1, 1, 64, 1.0, "",
114      "A shifting tunnel of reflective blocks", 0, NULL};
115 #endif /* USE_MODULES */
116
117 #if defined( I_HAVE_XPM )
118 static Bool LoadGLTextures(ModeInfo *mi)
119 {
120     blocktube_configuration *lp = &lps[MI_SCREEN(mi)];
121     Bool status;
122
123     status = True;
124     glGenTextures(1, &lp->envTexture);
125     glBindTexture(GL_TEXTURE_2D, lp->envTexture);
126     lp->texti = xpm_to_ximage(MI_DISPLAY(mi), MI_VISUAL(mi), MI_COLORMAP(mi),
127                           blocktube_xpm);
128     if (!lp->texti) {
129         status = False;
130     } else {
131         glPixelStorei(GL_UNPACK_ALIGNMENT,1);
132         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, lp->texti->width, lp->texti->height, 0,
133             GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, lp->texti->data);
134         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
135         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
136         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
137         glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
138     }
139     return status;
140 }
141 #endif
142
143 static void newTargetColor(blocktube_configuration *lp)
144 {
145     int luminance = 0;
146
147     while (luminance <= 150) {
148         lp->targetR = random() % 256;
149         lp->targetG = random() % 256;
150         lp->targetB = random() % 256;
151         lp->deltaR = (lp->targetR - lp->currentR) / changetime;
152         lp->deltaG = (lp->targetG - lp->currentG) / changetime;
153         lp->deltaB = (lp->targetB - lp->currentB) / changetime;
154         luminance = 0.3 * lp->targetR + 0.59 * lp->targetG + 0.11 * lp->targetB;
155     }
156 }
157
158 static void randomize_entity (blocktube_configuration *lp, entity *ent)
159 {
160     ent->id = lp->nextID++;
161     ent->tVal = 1 - ((float)random() / RAND_MAX / 1.5);
162     ent->age = 0;
163     ent->lifetime = 100;
164     ent->angle = random() % 360;
165     ent->angularVelocity = 0.5-((float)(random()) / RAND_MAX);
166     ent->position[0] = (float)(random()) / RAND_MAX + lp->tunnelWidth;
167     ent->position[1] = (float)(random()) / RAND_MAX * 2;
168     ent->position[2] = -(float)(random()) / RAND_MAX * lp->tunnelLength;
169 }
170
171 static void entityTick(blocktube_configuration *lp, entity *ent)
172 {
173     ent->angle += ent->angularVelocity;
174     ent->position[2] += 0.1;
175     if (ent->position[2] > lp->zoom) {
176         ent->position[2] = -lp->tunnelLength + ((float)(random()) / RAND_MAX) * 20;
177     }
178     ent->age += 0.1;
179 }
180
181 static void tick(blocktube_configuration *lp)
182 {
183     lp->counter--;
184     if (!lp->counter) {
185         if (!lp->changing) {
186             newTargetColor(lp);
187             lp->counter = changetime;
188         } else {
189             lp->counter = holdtime;
190         }
191         lp->changing = (!lp->changing);
192     } else {
193         if (lp->changing) {
194             lp->currentR += lp->deltaR;
195             lp->currentG += lp->deltaG;
196             lp->currentB += lp->deltaB;
197         }
198     }
199 }
200
201 static int cube_vertices(float x, float y, float z, int wire);
202
203 ENTRYPOINT void reshape_blocktube (ModeInfo *mi, int width, int height);
204
205 ENTRYPOINT void init_blocktube (ModeInfo *mi)
206 {
207     int loop;
208     GLfloat fogColor[4] = {0,0,0,1};
209     blocktube_configuration *lp;
210     int wire = MI_IS_WIREFRAME(mi);
211
212     if (!lps) {
213       lps = (blocktube_configuration *)
214         calloc (MI_NUM_SCREENS(mi), sizeof (blocktube_configuration));
215       if (!lps) {
216         fprintf(stderr, "%s: out of memory\n", progname);
217         exit(1);
218       }
219     }
220
221     lp = &lps[MI_SCREEN(mi)];
222     lp->glx_context = init_GL(mi);
223
224     lp->zoom = 30;
225     lp->tilt = 4.5;
226     lp->tunnelLength = 200;
227     lp->tunnelWidth = 5;
228
229     if (wire) {
230       do_fog = False;
231       do_texture = False;
232       glLineWidth(2);
233     }
234
235     lp->block_dlist = glGenLists (1);
236     glNewList (lp->block_dlist, GL_COMPILE);
237     lp->polys = cube_vertices(0.15, 1.2, 5.25, wire);
238     glEndList ();
239
240 #if defined( I_HAVE_XPM )
241     if (do_texture) {
242       if (!LoadGLTextures(mi)) {
243         fprintf(stderr, "%s: can't load textures!\n", progname);
244         exit(1);
245       }
246       glEnable(GL_TEXTURE_2D);
247     }
248 #endif
249
250     /* kick on the fog machine */
251     if (do_fog) {
252       glEnable(GL_FOG);
253       glFogi(GL_FOG_MODE, GL_LINEAR);
254       glHint(GL_FOG_HINT, GL_NICEST);
255       glFogf(GL_FOG_START, 0);
256       glFogf(GL_FOG_END, lp->tunnelLength/1.8);
257       glFogfv(GL_FOG_COLOR, fogColor);
258       glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
259     }
260     glShadeModel(GL_SMOOTH);
261     glEnable(GL_DEPTH_TEST);
262     glEnable(GL_CULL_FACE);
263     glClearDepth(1.0f);
264
265     if (!do_texture && !wire) {
266       /* If there is no texture, the boxes don't show up without a light.
267          Though I don't understand why all the blocks come out gray.
268        */
269       GLfloat pos[4] = {0.0, 1.0, 1.0, 0.0};
270       GLfloat amb[4] = {0.2, 0.2, 0.2, 1.0};
271       GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
272       GLfloat spc[4] = {1.0, 1.0, 1.0, 1.0};
273       glLightfv(GL_LIGHT0, GL_POSITION, pos);
274       glLightfv(GL_LIGHT0, GL_AMBIENT,  amb);
275       glLightfv(GL_LIGHT0, GL_DIFFUSE,  dif);
276       glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
277       glEnable(GL_LIGHTING);
278       glEnable(GL_LIGHT0);
279     }
280
281     lp->counter = holdtime;
282     lp->currentR = random() % 256;
283     lp->currentG = random() % 256;
284     lp->currentB = random() % 256;
285     newTargetColor(lp);
286     for (loop = 0; loop < MAX_ENTITIES; loop++)
287     {
288         randomize_entity(lp, &lp->entities[loop]);
289     }
290     reshape_blocktube(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
291     glFlush();
292 }
293
294 ENTRYPOINT void release_blocktube (ModeInfo *mi)
295 {
296   if (lps) {
297     int screen;
298     for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
299       blocktube_configuration *lp = &lps[screen];
300 # if defined ( I_HAVE_XPM )
301       if (lp->envTexture)
302         glDeleteTextures(1, &lp->envTexture);
303       if (lp->texti)
304         XDestroyImage(lp->texti);
305 # endif
306     }
307     free (lps);
308     lps = 0;
309   }
310   FreeAllGL(mi);
311 }
312
313 ENTRYPOINT void reshape_blocktube (ModeInfo *mi, int width, int height)
314 {
315     GLfloat h = (GLfloat) height / (GLfloat) width;
316
317     glViewport(0, 0, (GLint) width, (GLint) height);
318     glMatrixMode(GL_PROJECTION);
319     glLoadIdentity();
320     gluPerspective(45.0, 1/h, 1.0, 100.0);
321     glMatrixMode(GL_MODELVIEW);
322 }
323
324 static int cube_vertices(float x, float y, float z, int wire)
325 {
326     int polygon_count = 0;
327     float x2, y2, z2, nv = 0.7;
328     x2 = x/2;
329     y2 = y/2;
330     z2 = z/2;
331
332     glFrontFace(GL_CW);
333
334     glNormal3f(0, 0, nv);
335     glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
336     glTexCoord2f(0.0, 0.0); glVertex3f(-x2,  y2,  z2);
337     glTexCoord2f(1.0, 0.0); glVertex3f( x2,  y2,  z2);
338     glTexCoord2f(1.0, 1.0); glVertex3f( x2, -y2,  z2);
339     glTexCoord2f(0.0, 1.0); glVertex3f(-x2, -y2,  z2);
340     polygon_count++;
341     glEnd();
342
343     glNormal3f(0, 0, -nv);
344     glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
345     glTexCoord2f(1.0, 0.0); glVertex3f(-x2, -y2, -z2);
346     glTexCoord2f(1.0, 1.0); glVertex3f( x2, -y2, -z2);
347     glTexCoord2f(0.0, 1.0); glVertex3f( x2,  y2, -z2);
348     glTexCoord2f(0.0, 0.0); glVertex3f(-x2,  y2, -z2);
349     polygon_count++;
350     glEnd();
351
352     glNormal3f(0, nv, 0);
353     glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
354     glTexCoord2f(0.0, 1.0); glVertex3f(-x2,  y2, -z2);
355     glTexCoord2f(0.0, 0.0); glVertex3f( x2,  y2, -z2);
356     glTexCoord2f(1.0, 0.0); glVertex3f( x2,  y2,  z2);
357     glTexCoord2f(1.0, 1.0); glVertex3f(-x2,  y2,  z2);
358     polygon_count++;
359     glEnd();
360
361     glNormal3f(0, -nv, 0);
362     glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
363     glTexCoord2f(1.0, 1.0); glVertex3f(-x2, -y2, -z2);
364     glTexCoord2f(0.0, 1.0); glVertex3f(-x2, -y2,  z2);
365     glTexCoord2f(0.0, 0.0); glVertex3f( x2, -y2,  z2);
366     glTexCoord2f(1.0, 0.0); glVertex3f( x2, -y2, -z2);
367     polygon_count++;
368     glEnd();
369
370     if (wire) return polygon_count;
371
372     glNormal3f(nv, 0, 0);
373     glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
374     glTexCoord2f(1.0, 0.0); glVertex3f( x2, -y2, -z2);
375     glTexCoord2f(1.0, 1.0); glVertex3f( x2, -y2,  z2);
376     glTexCoord2f(0.0, 1.0); glVertex3f( x2,  y2,  z2);
377     glTexCoord2f(0.0, 0.0); glVertex3f( x2,  y2, -z2);
378     polygon_count++;
379     glEnd();
380
381     glNormal3f(-nv, 0, 0);
382     glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
383     glTexCoord2f(0.0, 0.0); glVertex3f(-x2, -y2, -z2);
384     glTexCoord2f(1.0, 0.0); glVertex3f(-x2,  y2, -z2);
385     glTexCoord2f(1.0, 1.0); glVertex3f(-x2,  y2,  z2);
386     glTexCoord2f(0.0, 1.0); glVertex3f(-x2, -y2,  z2);
387     polygon_count++;
388     glEnd();
389
390     return polygon_count;
391 }
392
393 static void draw_block(ModeInfo *mi, entity *ent)
394 {
395     blocktube_configuration *lp = &lps[MI_SCREEN(mi)];
396     glCallList (lp->block_dlist);
397     mi->polygon_count += lp->polys;
398 }
399
400 ENTRYPOINT void
401 draw_blocktube (ModeInfo *mi)
402 {
403     blocktube_configuration *lp = &lps[MI_SCREEN(mi)];
404     Display *dpy = MI_DISPLAY(mi);
405     Window window = MI_WINDOW(mi);
406     entity *cEnt = NULL;
407     int loop = 0;
408
409     if (!lp->glx_context)
410       return;
411
412     mi->polygon_count = 0;
413
414     glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(lp->glx_context));
415
416     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
417
418     if (do_texture) {
419       glEnable(GL_TEXTURE_GEN_S);
420       glEnable(GL_TEXTURE_GEN_T);
421       glBindTexture(GL_TEXTURE_2D, lp->envTexture);
422     }
423
424     for (loop = 0; loop < MAX_ENTITIES; loop++) {
425         cEnt = &lp->entities[loop];
426
427         glLoadIdentity();
428         glTranslatef(0.0f, 0.0f, lp->zoom);
429         glRotatef(lp->tilt, 1.0f, 0.0f, 0.0f);
430         glRotatef(cEnt->angle, 0.0f, 0.0f, 1.0f);
431         glTranslatef(cEnt->position[0], cEnt->position[1], cEnt->position[2]);
432         glColor4ub((int)(lp->currentR * cEnt->tVal),
433                    (int)(lp->currentG * cEnt->tVal),
434                    (int)(lp->currentB * cEnt->tVal), 255);
435         draw_block(mi, cEnt);
436         entityTick(lp, cEnt);
437     }
438     tick(lp);
439
440     if (mi->fps_p) do_fps (mi);
441     glFinish();
442     glXSwapBuffers(dpy, window);
443 }
444
445 XSCREENSAVER_MODULE ("BlockTube", blocktube)
446
447 #endif /* USE_GL */