http://svn.poeml.de/viewvc/ppc/src-unpacked/xscreensaver/xscreensaver-4.12.tar.bz2...
[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 static Bool LoadGLTextures(ModeInfo *mi)
131 {
132     Bool status;
133
134     status = True;
135     glGenTextures(1, &envTexture);
136     glBindTexture(GL_TEXTURE_2D, envTexture);
137     texti = xpm_to_ximage(MI_DISPLAY(mi), MI_VISUAL(mi), MI_COLORMAP(mi),
138                           blocktube_xpm);
139     if (!texti) {
140         status = False;
141     } else {
142         glPixelStorei(GL_UNPACK_ALIGNMENT,1);
143         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texti->width, texti->height, 0,
144             GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, texti->data);
145         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
146         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
147         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
148         glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
149     }
150     return status;
151 }
152
153 static void newTargetColor(void)
154 {
155     targetR = random() % 256;
156     targetG = random() % 256;
157     targetB = random() % 256;
158     deltaR = (targetR - currentR) / changetime;
159     deltaG = (targetG - currentG) / changetime;
160     deltaB = (targetB - currentB) / changetime;
161 }
162
163 static void randomize_entity (entity *ent)
164 {
165     ent->id = nextID++;
166     ent->tVal = 1 - ((float)random() / RAND_MAX / 1.5);
167     ent->age = 0;
168     ent->lifetime = 100;
169     ent->angle = random() % 360;
170     ent->angularVelocity = 0.5-((float)(random()) / RAND_MAX);
171     ent->position[0] = (float)(random()) / RAND_MAX + tunnelWidth;
172     ent->position[1] = (float)(random()) / RAND_MAX * 2;
173     ent->position[2] = -(float)(random()) / RAND_MAX * tunnelLength;
174 }
175
176 static void entityTick(entity *ent)
177 {
178     ent->angle += ent->angularVelocity;
179     ent->position[2] += 0.1;
180     if (ent->position[2] > zoom) {
181         ent->position[2] = -tunnelLength + ((float)(random()) / RAND_MAX) * 20;
182     }
183     ent->age += 0.1;
184 }
185
186 static void tick(void)
187 {
188     counter--;
189     if (!counter) {
190         if (!changing) {
191             newTargetColor();
192             counter = changetime;
193         } else {
194             counter = holdtime;
195         }
196         changing = (!changing);
197     } else {
198         if (changing) {
199             currentR += deltaR;
200             currentG += deltaG;
201             currentB += deltaB;
202         }
203     }
204 }
205
206 static void cube_vertices(float x, float y, float z, int wire);
207
208 void init_blocktube (ModeInfo *mi)
209 {
210     GLfloat fogColor[4] = {0,0,0,1};
211     blocktube_configuration *lp;
212     int wire = MI_IS_WIREFRAME(mi);
213
214     if (!lps) {
215       lps = (blocktube_configuration *)
216         calloc (MI_NUM_SCREENS(mi), sizeof (blocktube_configuration));
217       if (!lps) {
218         fprintf(stderr, "%s: out of memory\n", progname);
219         exit(1);
220       }
221       lp = &lps[MI_SCREEN(mi)];
222     }
223
224     lp = &lps[MI_SCREEN(mi)];
225     lp->glx_context = init_GL(mi);
226
227     if (wire) {
228       do_fog = False;
229       do_texture = False;
230       glLineWidth(2);
231     }
232
233     lp->block_dlist = glGenLists (1);
234     glNewList (lp->block_dlist, GL_COMPILE);
235     cube_vertices(0.15, 1.2, 5.25, wire);
236     glEndList ();
237
238 #if defined( I_HAVE_XPM )
239     if (do_texture) {
240       if (!LoadGLTextures(mi)) {
241         fprintf(stderr, "%s: can't load textures!\n", progname);
242         exit(1);
243       }
244       glEnable(GL_TEXTURE_2D);
245     }
246 #endif
247
248     /* kick on the fog machine */
249     if (do_fog) {
250       glEnable(GL_FOG);
251       glFogi(GL_FOG_MODE, GL_LINEAR);
252       glHint(GL_FOG_HINT, GL_NICEST);
253       glFogf(GL_FOG_START, 0);
254       glFogf(GL_FOG_END, tunnelLength/1.8);
255       glFogfv(GL_FOG_COLOR, fogColor);
256       glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
257     }
258     glShadeModel(GL_SMOOTH);
259     glEnable(GL_DEPTH_TEST);
260     glEnable(GL_CULL_FACE);
261     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
262     glClearDepth(1.0f);
263
264     if (!do_texture && !wire) {
265       /* If there is no texture, the boxes don't show up without a light.
266          Though I don't understand why all the blocks come out gray.
267        */
268       GLfloat pos[4] = {0.0, 1.0, 1.0, 0.0};
269       GLfloat amb[4] = {0.2, 0.2, 0.2, 1.0};
270       GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
271       GLfloat spc[4] = {1.0, 1.0, 1.0, 1.0};
272       glLightfv(GL_LIGHT0, GL_POSITION, pos);
273       glLightfv(GL_LIGHT0, GL_AMBIENT,  amb);
274       glLightfv(GL_LIGHT0, GL_DIFFUSE,  dif);
275       glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
276       glEnable(GL_LIGHTING);
277       glEnable(GL_LIGHT0);
278     }
279
280     counter = holdtime;
281     currentR = random() % 256;
282     currentG = random() % 256;
283     currentB = random() % 256;
284     newTargetColor();
285     for (loop = 0; loop < MAX_ENTITIES; loop++)
286     {
287         randomize_entity(&entities[loop]);
288     }
289     reshape_blocktube(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
290     glFlush();
291 }
292
293 void release_blocktube (ModeInfo *mi)
294 {
295 #if defined ( I_HAVE_XPM )
296     glDeleteTextures(1, &envTexture);
297     XDestroyImage(texti);
298 #endif
299 }
300
301 void reshape_blocktube (ModeInfo *mi, int width, int height)
302 {
303     GLfloat h = (GLfloat) height / (GLfloat) width;
304
305     glViewport(0, 0, (GLint) width, (GLint) height);
306     glMatrixMode(GL_PROJECTION);
307     glLoadIdentity();
308     gluPerspective(45.0, 1/h, 1.0, 100.0);
309     glMatrixMode(GL_MODELVIEW);
310 }
311
312 static void cube_vertices(float x, float y, float z, int wire)
313 {
314     float x2, y2, z2, nv = 0.7;
315     x2 = x/2;
316     y2 = y/2;
317     z2 = z/2;
318
319     glFrontFace(GL_CW);
320
321     glNormal3f(0, 0, nv);
322     glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
323     glTexCoord2f(0.0, 0.0); glVertex3f(-x2,  y2,  z2);
324     glTexCoord2f(1.0, 0.0); glVertex3f( x2,  y2,  z2);
325     glTexCoord2f(1.0, 1.0); glVertex3f( x2, -y2,  z2);
326     glTexCoord2f(0.0, 1.0); glVertex3f(-x2, -y2,  z2);
327     glEnd();
328
329     glNormal3f(0, 0, -nv);
330     glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
331     glTexCoord2f(1.0, 0.0); glVertex3f(-x2, -y2, -z2);
332     glTexCoord2f(1.0, 1.0); glVertex3f( x2, -y2, -z2);
333     glTexCoord2f(0.0, 1.0); glVertex3f( x2,  y2, -z2);
334     glTexCoord2f(0.0, 0.0); glVertex3f(-x2,  y2, -z2);
335     glEnd();
336
337     glNormal3f(0, nv, 0);
338     glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
339     glTexCoord2f(0.0, 1.0); glVertex3f(-x2,  y2, -z2);
340     glTexCoord2f(0.0, 0.0); glVertex3f( x2,  y2, -z2);
341     glTexCoord2f(1.0, 0.0); glVertex3f( x2,  y2,  z2);
342     glTexCoord2f(1.0, 1.0); glVertex3f(-x2,  y2,  z2);
343     glEnd();
344
345     glNormal3f(0, -nv, 0);
346     glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
347     glTexCoord2f(1.0, 1.0); glVertex3f(-x2, -y2, -z2);
348     glTexCoord2f(0.0, 1.0); glVertex3f(-x2, -y2,  z2);
349     glTexCoord2f(0.0, 0.0); glVertex3f( x2, -y2,  z2);
350     glTexCoord2f(1.0, 0.0); glVertex3f( x2, -y2, -z2);
351     glEnd();
352
353     if (wire) return;
354
355     glNormal3f(nv, 0, 0);
356     glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
357     glTexCoord2f(1.0, 0.0); glVertex3f( x2, -y2, -z2);
358     glTexCoord2f(1.0, 1.0); glVertex3f( x2, -y2,  z2);
359     glTexCoord2f(0.0, 1.0); glVertex3f( x2,  y2,  z2);
360     glTexCoord2f(0.0, 0.0); glVertex3f( x2,  y2, -z2);
361     glEnd();
362
363     glNormal3f(-nv, 0, 0);
364     glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
365     glTexCoord2f(0.0, 0.0); glVertex3f(-x2, -y2, -z2);
366     glTexCoord2f(1.0, 0.0); glVertex3f(-x2,  y2, -z2);
367     glTexCoord2f(1.0, 1.0); glVertex3f(-x2,  y2,  z2);
368     glTexCoord2f(0.0, 1.0); glVertex3f(-x2, -y2,  z2);
369     glEnd();
370 }
371
372 static void draw_block(ModeInfo *mi, entity *ent)
373 {
374     blocktube_configuration *lp = &lps[MI_SCREEN(mi)];
375     glCallList (lp->block_dlist);
376 }
377
378 void
379 draw_blocktube (ModeInfo *mi)
380 {
381     blocktube_configuration *lp = &lps[MI_SCREEN(mi)];
382     Display *dpy = MI_DISPLAY(mi);
383     Window window = MI_WINDOW(mi);
384     entity *cEnt = NULL;
385     int loop = 0;
386
387     if (!lp->glx_context)
388       return;
389
390     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
391
392     if (do_texture) {
393       glEnable(GL_TEXTURE_GEN_S);
394       glEnable(GL_TEXTURE_GEN_T);
395       glBindTexture(GL_TEXTURE_2D, envTexture);
396     }
397
398     for (loop = 0; loop < MAX_ENTITIES; loop++) {
399         cEnt = &entities[loop];
400
401         glLoadIdentity();
402         glTranslatef(0.0f, 0.0f, zoom);
403         glRotatef(tilt, 1.0f, 0.0f, 0.0f);
404         glRotatef(cEnt->angle, 0.0f, 0.0f, 1.0f);
405         glTranslatef(cEnt->position[0], cEnt->position[1], cEnt->position[2]);
406         glColor4ub((int)(currentR * cEnt->tVal),
407                    (int)(currentG * cEnt->tVal),
408                    (int)(currentB * cEnt->tVal), 255);
409         draw_block(mi, cEnt);
410         entityTick(cEnt);
411     }
412     tick();
413
414     if (mi->fps_p) do_fps (mi);
415     glFinish();
416     glXSwapBuffers(dpy, window);
417 }
418
419 #endif /* USE_GL */