began for maemo
[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 #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
75 } blocktube_configuration;
76
77 static blocktube_configuration *lps = NULL;
78
79 static GLint holdtime;
80 static GLint changetime;
81 static int do_texture;
82 static int do_fog;
83
84 static XrmOptionDescRec opts[] = {
85     { "-holdtime",  ".holdtime",  XrmoptionSepArg, 0 },
86     { "-changetime",  ".changetime",  XrmoptionSepArg, 0 },
87     {"-texture",     ".texture",   XrmoptionNoArg, "True" },
88     {"+texture",     ".texture",   XrmoptionNoArg, "False" },
89     {"-fog",         ".fog",       XrmoptionNoArg, "True" },
90     {"+fog",         ".fog",       XrmoptionNoArg, "False" },
91 };
92
93 static argtype vars[] = {
94     {&holdtime, "holdtime",  "Hold Time",  DEF_HOLDTIME,  t_Int},
95     {&changetime, "changetime",  "Change Time",  DEF_CHANGETIME, \
96      t_Int},
97     {&do_texture, "texture",    "Texture", DEF_TEXTURE,   t_Bool},
98     {&do_fog,     "fog",        "Fog",     DEF_FOG,       t_Bool},
99 };
100
101 static OptionStruct desc[] = {
102     {"-holdtime", "how long to stay on the same color"},
103     {"-changetime", "how long it takes to fade to a new color"},
104 };
105
106 ENTRYPOINT ModeSpecOpt blocktube_opts = {countof(opts), opts, countof(vars), vars, desc};
107
108 #ifdef USE_MODULES
109 ModStruct blocktube_description =
110     {"blocktube", "init_blocktube", "draw_blocktube", "release_blocktube",
111      "draw_blocktube", "init_blocktube", (char *)NULL, &blocktube_opts,
112      40000, 30, 1, 1, 64, 1.0, "",
113      "A shifting tunnel of reflective blocks", 0, NULL};
114 #endif /* USE_MODULES */
115
116 #if defined( I_HAVE_XPM )
117 static Bool LoadGLTextures(ModeInfo *mi)
118 {
119     blocktube_configuration *lp = &lps[MI_SCREEN(mi)];
120     Bool status;
121
122     status = True;
123     glGenTextures(1, &lp->envTexture);
124     glBindTexture(GL_TEXTURE_2D, lp->envTexture);
125     lp->texti = xpm_to_ximage(MI_DISPLAY(mi), MI_VISUAL(mi), MI_COLORMAP(mi),
126                           blocktube_xpm);
127     if (!lp->texti) {
128         status = False;
129     } else {
130         glPixelStorei(GL_UNPACK_ALIGNMENT,1);
131         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, lp->texti->width, lp->texti->height, 0,
132             GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, lp->texti->data);
133         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
134         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
135         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
136         glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
137     }
138     return status;
139 }
140 #endif
141
142 static void newTargetColor(blocktube_configuration *lp)
143 {
144     int luminance = 0;
145
146     while (luminance <= 150) {
147         lp->targetR = random() % 256;
148         lp->targetG = random() % 256;
149         lp->targetB = random() % 256;
150         lp->deltaR = (lp->targetR - lp->currentR) / changetime;
151         lp->deltaG = (lp->targetG - lp->currentG) / changetime;
152         lp->deltaB = (lp->targetB - lp->currentB) / changetime;
153         luminance = 0.3 * lp->targetR + 0.59 * lp->targetG + 0.11 * lp->targetB;
154     }
155 }
156
157 static void randomize_entity (blocktube_configuration *lp, entity *ent)
158 {
159     ent->id = lp->nextID++;
160     ent->tVal = 1 - ((float)random() / RAND_MAX / 1.5);
161     ent->age = 0;
162     ent->lifetime = 100;
163     ent->angle = random() % 360;
164     ent->angularVelocity = 0.5-((float)(random()) / RAND_MAX);
165     ent->position[0] = (float)(random()) / RAND_MAX + lp->tunnelWidth;
166     ent->position[1] = (float)(random()) / RAND_MAX * 2;
167     ent->position[2] = -(float)(random()) / RAND_MAX * lp->tunnelLength;
168 }
169
170 static void entityTick(blocktube_configuration *lp, entity *ent)
171 {
172     ent->angle += ent->angularVelocity;
173     ent->position[2] += 0.1;
174     if (ent->position[2] > lp->zoom) {
175         ent->position[2] = -lp->tunnelLength + ((float)(random()) / RAND_MAX) * 20;
176     }
177     ent->age += 0.1;
178 }
179
180 static void tick(blocktube_configuration *lp)
181 {
182     lp->counter--;
183     if (!lp->counter) {
184         if (!lp->changing) {
185             newTargetColor(lp);
186             lp->counter = changetime;
187         } else {
188             lp->counter = holdtime;
189         }
190         lp->changing = (!lp->changing);
191     } else {
192         if (lp->changing) {
193             lp->currentR += lp->deltaR;
194             lp->currentG += lp->deltaG;
195             lp->currentB += lp->deltaB;
196         }
197     }
198 }
199
200 static void cube_vertices(float x, float y, float z, int wire);
201
202 ENTRYPOINT void reshape_blocktube (ModeInfo *mi, int width, int height);
203
204 ENTRYPOINT void init_blocktube (ModeInfo *mi)
205 {
206     int loop;
207     GLfloat fogColor[4] = {0,0,0,1};
208     blocktube_configuration *lp;
209     int wire = MI_IS_WIREFRAME(mi);
210
211     if (!lps) {
212       lps = (blocktube_configuration *)
213         calloc (MI_NUM_SCREENS(mi), sizeof (blocktube_configuration));
214       if (!lps) {
215         fprintf(stderr, "%s: out of memory\n", progname);
216         exit(1);
217       }
218       lp = &lps[MI_SCREEN(mi)];
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     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     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     lp->counter = holdtime;
283     lp->currentR = random() % 256;
284     lp->currentG = random() % 256;
285     lp->currentB = random() % 256;
286     newTargetColor(lp);
287     for (loop = 0; loop < MAX_ENTITIES; loop++)
288     {
289         randomize_entity(lp, &lp->entities[loop]);
290     }
291     reshape_blocktube(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
292     glFlush();
293 }
294
295 ENTRYPOINT void release_blocktube (ModeInfo *mi)
296 {
297   if (lps) {
298     int screen;
299     for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
300       blocktube_configuration *lp = &lps[screen];
301 # if defined ( I_HAVE_XPM )
302       if (lp->envTexture)
303         glDeleteTextures(1, &lp->envTexture);
304       if (lp->texti)
305         XDestroyImage(lp->texti);
306 # endif
307     }
308     free (lps);
309     lps = 0;
310   }
311   FreeAllGL(mi);
312 }
313
314 ENTRYPOINT void reshape_blocktube (ModeInfo *mi, int width, int height)
315 {
316     GLfloat h = (GLfloat) height / (GLfloat) width;
317
318     glViewport(0, 0, (GLint) width, (GLint) height);
319     glMatrixMode(GL_PROJECTION);
320     glLoadIdentity();
321     gluPerspective(45.0, 1/h, 1.0, 100.0);
322     glMatrixMode(GL_MODELVIEW);
323 }
324
325 static void cube_vertices(float x, float y, float z, int wire)
326 {
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     glEnd();
341
342     glNormal3f(0, 0, -nv);
343     glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
344     glTexCoord2f(1.0, 0.0); glVertex3f(-x2, -y2, -z2);
345     glTexCoord2f(1.0, 1.0); glVertex3f( x2, -y2, -z2);
346     glTexCoord2f(0.0, 1.0); glVertex3f( x2,  y2, -z2);
347     glTexCoord2f(0.0, 0.0); glVertex3f(-x2,  y2, -z2);
348     glEnd();
349
350     glNormal3f(0, nv, 0);
351     glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
352     glTexCoord2f(0.0, 1.0); glVertex3f(-x2,  y2, -z2);
353     glTexCoord2f(0.0, 0.0); glVertex3f( x2,  y2, -z2);
354     glTexCoord2f(1.0, 0.0); glVertex3f( x2,  y2,  z2);
355     glTexCoord2f(1.0, 1.0); glVertex3f(-x2,  y2,  z2);
356     glEnd();
357
358     glNormal3f(0, -nv, 0);
359     glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
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     glTexCoord2f(1.0, 0.0); glVertex3f( x2, -y2, -z2);
364     glEnd();
365
366     if (wire) return;
367
368     glNormal3f(nv, 0, 0);
369     glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
370     glTexCoord2f(1.0, 0.0); glVertex3f( x2, -y2, -z2);
371     glTexCoord2f(1.0, 1.0); glVertex3f( x2, -y2,  z2);
372     glTexCoord2f(0.0, 1.0); glVertex3f( x2,  y2,  z2);
373     glTexCoord2f(0.0, 0.0); glVertex3f( x2,  y2, -z2);
374     glEnd();
375
376     glNormal3f(-nv, 0, 0);
377     glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
378     glTexCoord2f(0.0, 0.0); glVertex3f(-x2, -y2, -z2);
379     glTexCoord2f(1.0, 0.0); glVertex3f(-x2,  y2, -z2);
380     glTexCoord2f(1.0, 1.0); glVertex3f(-x2,  y2,  z2);
381     glTexCoord2f(0.0, 1.0); glVertex3f(-x2, -y2,  z2);
382     glEnd();
383 }
384
385 static void draw_block(ModeInfo *mi, entity *ent)
386 {
387     blocktube_configuration *lp = &lps[MI_SCREEN(mi)];
388     glCallList (lp->block_dlist);
389 }
390
391 ENTRYPOINT void
392 draw_blocktube (ModeInfo *mi)
393 {
394     blocktube_configuration *lp = &lps[MI_SCREEN(mi)];
395     Display *dpy = MI_DISPLAY(mi);
396     Window window = MI_WINDOW(mi);
397     entity *cEnt = NULL;
398     int loop = 0;
399
400     if (!lp->glx_context)
401       return;
402
403     glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(lp->glx_context));
404
405     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
406
407     if (do_texture) {
408       glEnable(GL_TEXTURE_GEN_S);
409       glEnable(GL_TEXTURE_GEN_T);
410       glBindTexture(GL_TEXTURE_2D, lp->envTexture);
411     }
412
413     for (loop = 0; loop < MAX_ENTITIES; loop++) {
414         cEnt = &lp->entities[loop];
415
416         glLoadIdentity();
417         glTranslatef(0.0f, 0.0f, lp->zoom);
418         glRotatef(lp->tilt, 1.0f, 0.0f, 0.0f);
419         glRotatef(cEnt->angle, 0.0f, 0.0f, 1.0f);
420         glTranslatef(cEnt->position[0], cEnt->position[1], cEnt->position[2]);
421         glColor4ub((int)(lp->currentR * cEnt->tVal),
422                    (int)(lp->currentG * cEnt->tVal),
423                    (int)(lp->currentB * cEnt->tVal), 255);
424         draw_block(mi, cEnt);
425         entityTick(lp, cEnt);
426     }
427     tick(lp);
428
429     if (mi->fps_p) do_fps (mi);
430     glFinish();
431     glXSwapBuffers(dpy, window);
432 }
433
434 XSCREENSAVER_MODULE ("BlockTube", blocktube)
435
436 #endif /* USE_GL */