1 /* blocktube, Copyright (c) 2003 Lars Damerow <lars@oddment.org>
3 * Based on Jamie Zawinski's original dangerball code.
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
16 #define DEFAULTS "*delay: 40000 \n" \
17 "*wireframe: False \n" \
18 "*showFPS: False \n" \
20 # define refresh_blocktube 0
21 # define blocktube_handle_event 0
23 #define countof(x) (sizeof((x))/sizeof((*x)))
25 #include "xlockmore.h"
30 #ifdef USE_GL /* whole file */
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"
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"
43 #include "../images/blocktube.xpm"
53 GLfloat angularVelocity;
57 GLXContext *glx_context;
61 entity entities[MAX_ENTITIES];
62 float targetR, targetG, targetB,
63 currentR, currentG, currentB,
64 deltaR, deltaG, deltaB;
76 } blocktube_configuration;
78 static blocktube_configuration *lps = NULL;
80 static GLint holdtime;
81 static GLint changetime;
82 static int do_texture;
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" },
94 static argtype vars[] = {
95 {&holdtime, "holdtime", "Hold Time", DEF_HOLDTIME, t_Int},
96 {&changetime, "changetime", "Change Time", DEF_CHANGETIME, \
98 {&do_texture, "texture", "Texture", DEF_TEXTURE, t_Bool},
99 {&do_fog, "fog", "Fog", DEF_FOG, t_Bool},
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"},
107 ENTRYPOINT ModeSpecOpt blocktube_opts = {countof(opts), opts, countof(vars), vars, desc};
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 */
117 #if defined( I_HAVE_XPM )
118 static Bool LoadGLTextures(ModeInfo *mi)
120 blocktube_configuration *lp = &lps[MI_SCREEN(mi)];
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),
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 # ifndef HAVE_JWZGLES /* #### Sphere maps unimplemented */
137 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
138 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
145 static void newTargetColor(blocktube_configuration *lp)
149 while (luminance <= 150) {
150 lp->targetR = random() % 256;
151 lp->targetG = random() % 256;
152 lp->targetB = random() % 256;
153 lp->deltaR = (lp->targetR - lp->currentR) / changetime;
154 lp->deltaG = (lp->targetG - lp->currentG) / changetime;
155 lp->deltaB = (lp->targetB - lp->currentB) / changetime;
156 luminance = 0.3 * lp->targetR + 0.59 * lp->targetG + 0.11 * lp->targetB;
160 static void randomize_entity (blocktube_configuration *lp, entity *ent)
162 ent->id = lp->nextID++;
163 ent->tVal = 1 - ((float)random() / RAND_MAX / 1.5);
166 ent->angle = random() % 360;
167 ent->angularVelocity = 0.5-((float)(random()) / RAND_MAX);
168 ent->position[0] = (float)(random()) / RAND_MAX + lp->tunnelWidth;
169 ent->position[1] = (float)(random()) / RAND_MAX * 2;
170 ent->position[2] = -(float)(random()) / RAND_MAX * lp->tunnelLength;
173 static void entityTick(blocktube_configuration *lp, entity *ent)
175 ent->angle += ent->angularVelocity;
176 ent->position[2] += 0.1;
177 if (ent->position[2] > lp->zoom) {
178 ent->position[2] = -lp->tunnelLength + ((float)(random()) / RAND_MAX) * 20;
183 static void tick(blocktube_configuration *lp)
189 lp->counter = changetime;
191 lp->counter = holdtime;
193 lp->changing = (!lp->changing);
196 lp->currentR += lp->deltaR;
197 lp->currentG += lp->deltaG;
198 lp->currentB += lp->deltaB;
203 static int cube_vertices(float x, float y, float z, int wire);
205 ENTRYPOINT void reshape_blocktube (ModeInfo *mi, int width, int height);
207 ENTRYPOINT void init_blocktube (ModeInfo *mi)
210 GLfloat fogColor[4] = {0,0,0,1};
211 blocktube_configuration *lp;
212 int wire = MI_IS_WIREFRAME(mi);
215 lps = (blocktube_configuration *)
216 calloc (MI_NUM_SCREENS(mi), sizeof (blocktube_configuration));
218 fprintf(stderr, "%s: out of memory\n", progname);
223 lp = &lps[MI_SCREEN(mi)];
224 lp->glx_context = init_GL(mi);
228 lp->tunnelLength = 200;
237 lp->block_dlist = glGenLists (1);
238 glNewList (lp->block_dlist, GL_COMPILE);
239 lp->polys = cube_vertices(0.15, 1.2, 5.25, wire);
242 #if defined( I_HAVE_XPM )
244 if (!LoadGLTextures(mi)) {
245 fprintf(stderr, "%s: can't load textures!\n", progname);
248 glEnable(GL_TEXTURE_2D);
252 /* kick on the fog machine */
255 glFogi(GL_FOG_MODE, GL_LINEAR);
256 glHint(GL_FOG_HINT, GL_NICEST);
257 glFogf(GL_FOG_START, 0);
258 glFogf(GL_FOG_END, lp->tunnelLength/1.8);
259 glFogfv(GL_FOG_COLOR, fogColor);
260 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
262 glShadeModel(GL_SMOOTH);
263 glEnable(GL_DEPTH_TEST);
264 glEnable(GL_CULL_FACE);
267 if (!do_texture && !wire) {
268 /* If there is no texture, the boxes don't show up without a light.
269 Though I don't understand why all the blocks come out gray.
271 GLfloat pos[4] = {0.0, 1.0, 1.0, 0.0};
272 GLfloat amb[4] = {0.2, 0.2, 0.2, 1.0};
273 GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
274 GLfloat spc[4] = {1.0, 1.0, 1.0, 1.0};
275 glLightfv(GL_LIGHT0, GL_POSITION, pos);
276 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
277 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
278 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
279 glEnable(GL_LIGHTING);
283 lp->counter = holdtime;
284 lp->currentR = random() % 256;
285 lp->currentG = random() % 256;
286 lp->currentB = random() % 256;
288 for (loop = 0; loop < MAX_ENTITIES; loop++)
290 randomize_entity(lp, &lp->entities[loop]);
292 reshape_blocktube(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
296 ENTRYPOINT void release_blocktube (ModeInfo *mi)
300 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
301 blocktube_configuration *lp = &lps[screen];
302 # if defined ( I_HAVE_XPM )
304 glDeleteTextures(1, &lp->envTexture);
306 XDestroyImage(lp->texti);
315 ENTRYPOINT void reshape_blocktube (ModeInfo *mi, int width, int height)
317 GLfloat h = (GLfloat) height / (GLfloat) width;
319 glViewport(0, 0, (GLint) width, (GLint) height);
320 glMatrixMode(GL_PROJECTION);
322 gluPerspective(45.0, 1/h, 1.0, 100.0);
323 glMatrixMode(GL_MODELVIEW);
326 static int cube_vertices(float x, float y, float z, int wire)
328 int polygon_count = 0;
329 float x2, y2, z2, nv = 0.7;
336 glNormal3f(0, 0, nv);
337 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
338 glTexCoord2f(0.0, 0.0); glVertex3f(-x2, y2, z2);
339 glTexCoord2f(1.0, 0.0); glVertex3f( x2, y2, z2);
340 glTexCoord2f(1.0, 1.0); glVertex3f( x2, -y2, z2);
341 glTexCoord2f(0.0, 1.0); glVertex3f(-x2, -y2, z2);
345 glNormal3f(0, 0, -nv);
346 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
347 glTexCoord2f(1.0, 0.0); glVertex3f(-x2, -y2, -z2);
348 glTexCoord2f(1.0, 1.0); glVertex3f( x2, -y2, -z2);
349 glTexCoord2f(0.0, 1.0); glVertex3f( x2, y2, -z2);
350 glTexCoord2f(0.0, 0.0); glVertex3f(-x2, y2, -z2);
354 glNormal3f(0, nv, 0);
355 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
356 glTexCoord2f(0.0, 1.0); glVertex3f(-x2, y2, -z2);
357 glTexCoord2f(0.0, 0.0); glVertex3f( x2, y2, -z2);
358 glTexCoord2f(1.0, 0.0); glVertex3f( x2, y2, z2);
359 glTexCoord2f(1.0, 1.0); glVertex3f(-x2, y2, z2);
363 glNormal3f(0, -nv, 0);
364 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
365 glTexCoord2f(1.0, 1.0); glVertex3f(-x2, -y2, -z2);
366 glTexCoord2f(0.0, 1.0); glVertex3f(-x2, -y2, z2);
367 glTexCoord2f(0.0, 0.0); glVertex3f( x2, -y2, z2);
368 glTexCoord2f(1.0, 0.0); glVertex3f( x2, -y2, -z2);
372 if (wire) return polygon_count;
374 glNormal3f(nv, 0, 0);
375 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
376 glTexCoord2f(1.0, 0.0); glVertex3f( x2, -y2, -z2);
377 glTexCoord2f(1.0, 1.0); glVertex3f( x2, -y2, z2);
378 glTexCoord2f(0.0, 1.0); glVertex3f( x2, y2, z2);
379 glTexCoord2f(0.0, 0.0); glVertex3f( x2, y2, -z2);
383 glNormal3f(-nv, 0, 0);
384 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
385 glTexCoord2f(0.0, 0.0); glVertex3f(-x2, -y2, -z2);
386 glTexCoord2f(1.0, 0.0); glVertex3f(-x2, y2, -z2);
387 glTexCoord2f(1.0, 1.0); glVertex3f(-x2, y2, z2);
388 glTexCoord2f(0.0, 1.0); glVertex3f(-x2, -y2, z2);
392 return polygon_count;
395 static void draw_block(ModeInfo *mi, entity *ent)
397 blocktube_configuration *lp = &lps[MI_SCREEN(mi)];
398 glCallList (lp->block_dlist);
399 mi->polygon_count += lp->polys;
403 draw_blocktube (ModeInfo *mi)
405 blocktube_configuration *lp = &lps[MI_SCREEN(mi)];
406 Display *dpy = MI_DISPLAY(mi);
407 Window window = MI_WINDOW(mi);
411 if (!lp->glx_context)
414 mi->polygon_count = 0;
416 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(lp->glx_context));
418 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
421 glEnable(GL_TEXTURE_GEN_S);
422 glEnable(GL_TEXTURE_GEN_T);
423 glBindTexture(GL_TEXTURE_2D, lp->envTexture);
426 for (loop = 0; loop < MAX_ENTITIES; loop++) {
427 cEnt = &lp->entities[loop];
430 glTranslatef(0.0f, 0.0f, lp->zoom);
431 glRotatef(lp->tilt, 1.0f, 0.0f, 0.0f);
432 glRotatef(cEnt->angle, 0.0f, 0.0f, 1.0f);
433 glTranslatef(cEnt->position[0], cEnt->position[1], cEnt->position[2]);
434 glColor4ub((int)(lp->currentR * cEnt->tVal),
435 (int)(lp->currentG * cEnt->tVal),
436 (int)(lp->currentB * cEnt->tVal), 255);
437 draw_block(mi, cEnt);
438 entityTick(lp, cEnt);
442 if (mi->fps_p) do_fps (mi);
444 glXSwapBuffers(dpy, window);
447 XSCREENSAVER_MODULE ("BlockTube", blocktube)