ftp://ftp.swin.edu.au/slackware/slackware-9.1/source/xap/xscreensaver/xscreensaver...
[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 #include <X11/Intrinsic.h>
15
16 #define DEBUG 1
17
18 extern XtAppContext app;
19
20 #define PROGCLASS       "Blocktube"
21 #define HACK_INIT       init_blocktube
22 #define HACK_DRAW       draw_blocktube
23 #define HACK_RESHAPE    reshape_blocktube
24 #define EVENT_MASK      PointerMotionMask
25 #define blocktube_opts  xlockmore_opts
26
27 #define DEF_HOLDTIME    "1000"
28 #define DEF_CHANGETIME  "200"
29 #define MAX_ENTITIES     1000
30 #define DEF_TEXTURE     "True"
31 #define DEF_FOG         "True"
32
33 #define DEFAULTS        "*delay:        10000           \n" \
34                         "*holdtime:   " DEF_HOLDTIME   "\n" \
35                         "*changetime: " DEF_CHANGETIME "\n" \
36                         "*wireframe:    False           \n" \
37                         "*texture:    " DEF_TEXTURE    "\n" \
38                         "*fog:        " DEF_FOG        "\n" \
39                         "*showFPS:      False           \n" \
40
41 #undef countof
42 #define countof(x) (sizeof((x))/sizeof((*x)))
43
44 #include "xlockmore.h"
45 #include "colors.h"
46 #include <math.h>
47 #include <ctype.h>
48
49 #ifdef USE_GL /* whole file */
50
51 #include <GL/glu.h>
52
53 #if defined( USE_XPM ) || defined( USE_XPMINC ) || defined( HAVE_XPM )
54 /* USE_XPM & USE_XPMINC in xlock mode ; HAVE_XPM in xscreensaver mode */
55 #include "xpm-ximage.h"
56 #define I_HAVE_XPM
57
58 #include "../images/blocktube.xpm"
59 #endif /* HAVE_XPM */
60
61 typedef struct {
62     GLXContext *glx_context;
63     GLuint  block_dlist;
64 } blocktube_configuration;
65
66 static int nextID = 1;
67 static blocktube_configuration *lps = NULL;
68
69 typedef struct {
70     int id, r, g, b;
71     GLfloat tVal;
72     int age;
73     int lifetime;
74     GLfloat position[3];
75     GLfloat angle;
76     GLfloat angularVelocity;
77 } entity;
78
79 static entity entities[MAX_ENTITIES];
80 static float targetR, targetG, targetB,
81              currentR, currentG, currentB,
82              deltaR, deltaG, deltaB;
83 static GLint holdtime;
84 static GLint changetime;
85 static int counter = 0;
86 static int changing = 0;
87 static GLfloat zoom = 30.0f;
88 static GLfloat tilt = 4.5f;
89 static GLuint loop;
90 static GLuint envTexture;
91 static XImage *texti;
92 static int do_texture;
93 static int do_fog;
94
95 GLfloat tunnelLength=200;
96 GLfloat tunnelWidth=5;
97
98 static XrmOptionDescRec opts[] = {
99     { "-holdtime",  ".holdtime",  XrmoptionSepArg, 0 },
100     { "-changetime",  ".changetime",  XrmoptionSepArg, 0 },
101     {"-texture",     ".texture",   XrmoptionNoArg, (caddr_t) "True" },
102     {"+texture",     ".texture",   XrmoptionNoArg, (caddr_t) "False" },
103     {"-fog",         ".fog",       XrmoptionNoArg, (caddr_t) "True" },
104     {"+fog",         ".fog",       XrmoptionNoArg, (caddr_t) "False" },
105 };
106
107 static argtype vars[] = {
108     {(caddr_t *) &holdtime, "holdtime",  "Hold Time",  DEF_HOLDTIME,  t_Int},
109     {(caddr_t *) &changetime, "changetime",  "Change Time",  DEF_CHANGETIME, \
110      t_Int},
111     {(caddr_t *) &do_texture, "texture",    "Texture", DEF_TEXTURE,   t_Bool},
112     {(caddr_t *) &do_fog,     "fog",        "Fog",     DEF_FOG,       t_Bool},
113 };
114
115 static OptionStruct desc[] = {
116     {"-holdtime", "how long to stay on the same color"},
117     {"-changetime", "how long it takes to fade to a new color"},
118 };
119
120 ModeSpecOpt blocktube_opts = {countof(opts), opts, countof(vars), vars, desc};
121
122 #ifdef USE_MODULES
123 ModStruct blocktube_description =
124     {"blocktube", "init_blocktube", "draw_blocktube", "release_blocktube",
125      "draw_blocktube", "init_blocktube", (char *)NULL, &blocktube_opts,
126      10000, 30, 1, 1, 64, 1.0, "",
127      "A shifting tunnel of reflective blocks", 0, NULL};
128 #endif /* USE_MODULES */
129
130 #if defined( I_HAVE_XPM )
131 static Bool LoadGLTextures(ModeInfo *mi)
132 {
133     Bool status;
134
135     status = True;
136     glGenTextures(1, &envTexture);
137     glBindTexture(GL_TEXTURE_2D, envTexture);
138     texti = xpm_to_ximage(MI_DISPLAY(mi), MI_VISUAL(mi), MI_COLORMAP(mi),
139                           blocktube_xpm);
140     if (!texti) {
141         status = False;
142     } else {
143         glPixelStorei(GL_UNPACK_ALIGNMENT,1);
144         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texti->width, texti->height, 0,
145             GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, texti->data);
146         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
147         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
148         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
149         glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
150     }
151     return status;
152 }
153 #endif
154
155 static void newTargetColor(void)
156 {
157     targetR = random() % 256;
158     targetG = random() % 256;
159     targetB = random() % 256;
160     deltaR = (targetR - currentR) / changetime;
161     deltaG = (targetG - currentG) / changetime;
162     deltaB = (targetB - currentB) / changetime;
163 }
164
165 static void randomize_entity (entity *ent)
166 {
167     ent->id = nextID++;
168     ent->tVal = 1 - ((float)random() / RAND_MAX / 1.5);
169     ent->age = 0;
170     ent->lifetime = 100;
171     ent->angle = random() % 360;
172     ent->angularVelocity = 0.5-((float)(random()) / RAND_MAX);
173     ent->position[0] = (float)(random()) / RAND_MAX + tunnelWidth;
174     ent->position[1] = (float)(random()) / RAND_MAX * 2;
175     ent->position[2] = -(float)(random()) / RAND_MAX * tunnelLength;
176 }
177
178 static void entityTick(entity *ent)
179 {
180     ent->angle += ent->angularVelocity;
181     ent->position[2] += 0.1;
182     if (ent->position[2] > zoom) {
183         ent->position[2] = -tunnelLength + ((float)(random()) / RAND_MAX) * 20;
184     }
185     ent->age += 0.1;
186 }
187
188 static void tick(void)
189 {
190     counter--;
191     if (!counter) {
192         if (!changing) {
193             newTargetColor();
194             counter = changetime;
195         } else {
196             counter = holdtime;
197         }
198         changing = (!changing);
199     } else {
200         if (changing) {
201             currentR += deltaR;
202             currentG += deltaG;
203             currentB += deltaB;
204         }
205     }
206 }
207
208 static void cube_vertices(float x, float y, float z, int wire);
209
210 void init_blocktube (ModeInfo *mi)
211 {
212     GLfloat fogColor[4] = {0,0,0,1};
213     blocktube_configuration *lp;
214     int wire = MI_IS_WIREFRAME(mi);
215
216     if (!lps) {
217       lps = (blocktube_configuration *)
218         calloc (MI_NUM_SCREENS(mi), sizeof (blocktube_configuration));
219       if (!lps) {
220         fprintf(stderr, "%s: out of memory\n", progname);
221         exit(1);
222       }
223       lp = &lps[MI_SCREEN(mi)];
224     }
225
226     lp = &lps[MI_SCREEN(mi)];
227     lp->glx_context = init_GL(mi);
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     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, 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     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
264     glClearDepth(1.0f);
265
266     if (!do_texture && !wire) {
267       /* If there is no texture, the boxes don't show up without a light.
268          Though I don't understand why all the blocks come out gray.
269        */
270       GLfloat pos[4] = {0.0, 1.0, 1.0, 0.0};
271       GLfloat amb[4] = {0.2, 0.2, 0.2, 1.0};
272       GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
273       GLfloat spc[4] = {1.0, 1.0, 1.0, 1.0};
274       glLightfv(GL_LIGHT0, GL_POSITION, pos);
275       glLightfv(GL_LIGHT0, GL_AMBIENT,  amb);
276       glLightfv(GL_LIGHT0, GL_DIFFUSE,  dif);
277       glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
278       glEnable(GL_LIGHTING);
279       glEnable(GL_LIGHT0);
280     }
281
282     counter = holdtime;
283     currentR = random() % 256;
284     currentG = random() % 256;
285     currentB = random() % 256;
286     newTargetColor();
287     for (loop = 0; loop < MAX_ENTITIES; loop++)
288     {
289         randomize_entity(&entities[loop]);
290     }
291     reshape_blocktube(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
292     glFlush();
293 }
294
295 void release_blocktube (ModeInfo *mi)
296 {
297 #if defined ( I_HAVE_XPM )
298     glDeleteTextures(1, &envTexture);
299     XDestroyImage(texti);
300 #endif
301 }
302
303 void reshape_blocktube (ModeInfo *mi, int width, int height)
304 {
305     GLfloat h = (GLfloat) height / (GLfloat) width;
306
307     glViewport(0, 0, (GLint) width, (GLint) height);
308     glMatrixMode(GL_PROJECTION);
309     glLoadIdentity();
310     gluPerspective(45.0, 1/h, 1.0, 100.0);
311     glMatrixMode(GL_MODELVIEW);
312 }
313
314 static void cube_vertices(float x, float y, float z, int wire)
315 {
316     float x2, y2, z2, nv = 0.7;
317     x2 = x/2;
318     y2 = y/2;
319     z2 = z/2;
320
321     glFrontFace(GL_CW);
322
323     glNormal3f(0, 0, nv);
324     glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
325     glTexCoord2f(0.0, 0.0); glVertex3f(-x2,  y2,  z2);
326     glTexCoord2f(1.0, 0.0); glVertex3f( x2,  y2,  z2);
327     glTexCoord2f(1.0, 1.0); glVertex3f( x2, -y2,  z2);
328     glTexCoord2f(0.0, 1.0); glVertex3f(-x2, -y2,  z2);
329     glEnd();
330
331     glNormal3f(0, 0, -nv);
332     glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
333     glTexCoord2f(1.0, 0.0); glVertex3f(-x2, -y2, -z2);
334     glTexCoord2f(1.0, 1.0); glVertex3f( x2, -y2, -z2);
335     glTexCoord2f(0.0, 1.0); glVertex3f( x2,  y2, -z2);
336     glTexCoord2f(0.0, 0.0); glVertex3f(-x2,  y2, -z2);
337     glEnd();
338
339     glNormal3f(0, nv, 0);
340     glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
341     glTexCoord2f(0.0, 1.0); glVertex3f(-x2,  y2, -z2);
342     glTexCoord2f(0.0, 0.0); glVertex3f( x2,  y2, -z2);
343     glTexCoord2f(1.0, 0.0); glVertex3f( x2,  y2,  z2);
344     glTexCoord2f(1.0, 1.0); glVertex3f(-x2,  y2,  z2);
345     glEnd();
346
347     glNormal3f(0, -nv, 0);
348     glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
349     glTexCoord2f(1.0, 1.0); glVertex3f(-x2, -y2, -z2);
350     glTexCoord2f(0.0, 1.0); glVertex3f(-x2, -y2,  z2);
351     glTexCoord2f(0.0, 0.0); glVertex3f( x2, -y2,  z2);
352     glTexCoord2f(1.0, 0.0); glVertex3f( x2, -y2, -z2);
353     glEnd();
354
355     if (wire) return;
356
357     glNormal3f(nv, 0, 0);
358     glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
359     glTexCoord2f(1.0, 0.0); glVertex3f( x2, -y2, -z2);
360     glTexCoord2f(1.0, 1.0); glVertex3f( x2, -y2,  z2);
361     glTexCoord2f(0.0, 1.0); glVertex3f( x2,  y2,  z2);
362     glTexCoord2f(0.0, 0.0); glVertex3f( x2,  y2, -z2);
363     glEnd();
364
365     glNormal3f(-nv, 0, 0);
366     glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
367     glTexCoord2f(0.0, 0.0); glVertex3f(-x2, -y2, -z2);
368     glTexCoord2f(1.0, 0.0); glVertex3f(-x2,  y2, -z2);
369     glTexCoord2f(1.0, 1.0); glVertex3f(-x2,  y2,  z2);
370     glTexCoord2f(0.0, 1.0); glVertex3f(-x2, -y2,  z2);
371     glEnd();
372 }
373
374 static void draw_block(ModeInfo *mi, entity *ent)
375 {
376     blocktube_configuration *lp = &lps[MI_SCREEN(mi)];
377     glCallList (lp->block_dlist);
378 }
379
380 void
381 draw_blocktube (ModeInfo *mi)
382 {
383     blocktube_configuration *lp = &lps[MI_SCREEN(mi)];
384     Display *dpy = MI_DISPLAY(mi);
385     Window window = MI_WINDOW(mi);
386     entity *cEnt = NULL;
387     int loop = 0;
388
389     if (!lp->glx_context)
390       return;
391
392     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
393
394     if (do_texture) {
395       glEnable(GL_TEXTURE_GEN_S);
396       glEnable(GL_TEXTURE_GEN_T);
397       glBindTexture(GL_TEXTURE_2D, envTexture);
398     }
399
400     for (loop = 0; loop < MAX_ENTITIES; loop++) {
401         cEnt = &entities[loop];
402
403         glLoadIdentity();
404         glTranslatef(0.0f, 0.0f, zoom);
405         glRotatef(tilt, 1.0f, 0.0f, 0.0f);
406         glRotatef(cEnt->angle, 0.0f, 0.0f, 1.0f);
407         glTranslatef(cEnt->position[0], cEnt->position[1], cEnt->position[2]);
408         glColor4ub((int)(currentR * cEnt->tVal),
409                    (int)(currentG * cEnt->tVal),
410                    (int)(currentB * cEnt->tVal), 255);
411         draw_block(mi, cEnt);
412         entityTick(cEnt);
413     }
414     tick();
415
416     if (mi->fps_p) do_fps (mi);
417     glFinish();
418     glXSwapBuffers(dpy, window);
419 }
420
421 #endif /* USE_GL */