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" \
19 "*suppressRotationAnimation: True\n" \
21 # define release_blocktube 0
22 # define blocktube_handle_event xlockmore_no_events
24 #define countof(x) (sizeof((x))/sizeof((*x)))
26 #include "xlockmore.h"
31 #ifdef USE_GL /* whole file */
33 #define DEF_HOLDTIME "1000"
34 #define DEF_CHANGETIME "200"
35 #define MAX_ENTITIES 1000
36 #define DEF_TEXTURE "True"
37 #define DEF_FOG "True"
39 #if defined(USE_XPM) || defined(USE_XPMINC) || defined(STANDALONE)
40 /* USE_XPM & USE_XPMINC in xlock mode ; HAVE_XPM in xscreensaver mode */
41 #include "ximage-loader.h"
44 #include "images/gen/blocktube_png.h"
54 GLfloat angularVelocity;
58 GLXContext *glx_context;
62 entity entities[MAX_ENTITIES];
63 float targetR, targetG, targetB,
64 currentR, currentG, currentB,
65 deltaR, deltaG, deltaB;
77 } blocktube_configuration;
79 static blocktube_configuration *lps = NULL;
81 static GLint holdtime;
82 static GLint changetime;
83 static int do_texture;
86 static XrmOptionDescRec opts[] = {
87 { "-holdtime", ".holdtime", XrmoptionSepArg, 0 },
88 { "-changetime", ".changetime", XrmoptionSepArg, 0 },
89 {"-texture", ".texture", XrmoptionNoArg, "True" },
90 {"+texture", ".texture", XrmoptionNoArg, "False" },
91 {"-fog", ".fog", XrmoptionNoArg, "True" },
92 {"+fog", ".fog", XrmoptionNoArg, "False" },
95 static argtype vars[] = {
96 {&holdtime, "holdtime", "Hold Time", DEF_HOLDTIME, t_Int},
97 {&changetime, "changetime", "Change Time", DEF_CHANGETIME, \
99 {&do_texture, "texture", "Texture", DEF_TEXTURE, t_Bool},
100 {&do_fog, "fog", "Fog", DEF_FOG, t_Bool},
103 static OptionStruct desc[] = {
104 {"-holdtime", "how long to stay on the same color"},
105 {"-changetime", "how long it takes to fade to a new color"},
108 ENTRYPOINT ModeSpecOpt blocktube_opts = {countof(opts), opts, countof(vars), vars, desc};
111 ModStruct blocktube_description =
112 {"blocktube", "init_blocktube", "draw_blocktube", (char *)NULL,
113 "draw_blocktube", "init_blocktube", "free_blocktube", &blocktube_opts,
114 40000, 30, 1, 1, 64, 1.0, "",
115 "A shifting tunnel of reflective blocks", 0, NULL};
116 #endif /* USE_MODULES */
118 #if defined( I_HAVE_XPM )
119 static Bool LoadGLTextures(ModeInfo *mi)
121 blocktube_configuration *lp = &lps[MI_SCREEN(mi)];
125 glGenTextures(1, &lp->envTexture);
126 glBindTexture(GL_TEXTURE_2D, lp->envTexture);
127 lp->texti = image_data_to_ximage(MI_DISPLAY(mi), MI_VISUAL(mi),
128 blocktube_png, sizeof(blocktube_png));
132 glPixelStorei(GL_UNPACK_ALIGNMENT,1);
133 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, lp->texti->width, lp->texti->height, 0,
134 GL_RGBA, GL_UNSIGNED_BYTE, lp->texti->data);
135 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
136 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
137 # ifndef HAVE_JWZGLES /* #### Sphere maps unimplemented */
138 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
139 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
146 static void newTargetColor(blocktube_configuration *lp)
150 while (luminance <= 150) {
151 lp->targetR = random() % 256;
152 lp->targetG = random() % 256;
153 lp->targetB = random() % 256;
154 lp->deltaR = (lp->targetR - lp->currentR) / changetime;
155 lp->deltaG = (lp->targetG - lp->currentG) / changetime;
156 lp->deltaB = (lp->targetB - lp->currentB) / changetime;
157 luminance = 0.3 * lp->targetR + 0.59 * lp->targetG + 0.11 * lp->targetB;
161 static void randomize_entity (blocktube_configuration *lp, entity *ent)
163 ent->id = lp->nextID++;
164 ent->tVal = 1 - ((float)random() / RAND_MAX / 1.5);
167 ent->angle = random() % 360;
168 ent->angularVelocity = 0.5-((float)(random()) / RAND_MAX);
169 ent->position[0] = (float)(random()) / RAND_MAX + lp->tunnelWidth;
170 ent->position[1] = (float)(random()) / RAND_MAX * 2;
171 ent->position[2] = -(float)(random()) / RAND_MAX * lp->tunnelLength;
174 static void entityTick(blocktube_configuration *lp, entity *ent)
176 ent->angle += ent->angularVelocity;
177 ent->position[2] += 0.1;
178 if (ent->position[2] > lp->zoom) {
179 ent->position[2] = -lp->tunnelLength + ((float)(random()) / RAND_MAX) * 20;
184 static void tick(blocktube_configuration *lp)
190 lp->counter = changetime;
192 lp->counter = holdtime;
194 lp->changing = (!lp->changing);
197 lp->currentR += lp->deltaR;
198 lp->currentG += lp->deltaG;
199 lp->currentB += lp->deltaB;
204 static int cube_vertices(float x, float y, float z, int wire);
206 ENTRYPOINT void reshape_blocktube (ModeInfo *mi, int width, int height);
208 ENTRYPOINT void init_blocktube (ModeInfo *mi)
211 GLfloat fogColor[4] = {0,0,0,1};
212 blocktube_configuration *lp;
213 int wire = MI_IS_WIREFRAME(mi);
217 lp = &lps[MI_SCREEN(mi)];
218 lp->glx_context = init_GL(mi);
222 lp->tunnelLength = 200;
231 lp->block_dlist = glGenLists (1);
232 glNewList (lp->block_dlist, GL_COMPILE);
233 lp->polys = cube_vertices(0.15, 1.2, 5.25, wire);
236 #if defined( I_HAVE_XPM )
238 if (!LoadGLTextures(mi)) {
239 fprintf(stderr, "%s: can't load textures!\n", progname);
242 glEnable(GL_TEXTURE_2D);
246 /* kick on the fog machine */
249 glFogi(GL_FOG_MODE, GL_LINEAR);
250 glHint(GL_FOG_HINT, GL_NICEST);
251 glFogf(GL_FOG_START, 0);
252 glFogf(GL_FOG_END, lp->tunnelLength/1.8);
253 glFogfv(GL_FOG_COLOR, fogColor);
254 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
256 glShadeModel(GL_SMOOTH);
257 glEnable(GL_DEPTH_TEST);
258 glEnable(GL_CULL_FACE);
261 if (!do_texture && !wire) {
262 /* If there is no texture, the boxes don't show up without a light.
263 Though I don't understand why all the blocks come out gray.
265 GLfloat pos[4] = {0.0, 1.0, 1.0, 0.0};
266 GLfloat amb[4] = {0.2, 0.2, 0.2, 1.0};
267 GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
268 GLfloat spc[4] = {1.0, 1.0, 1.0, 1.0};
269 glLightfv(GL_LIGHT0, GL_POSITION, pos);
270 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
271 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
272 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
273 glEnable(GL_LIGHTING);
277 lp->counter = holdtime;
278 lp->currentR = random() % 256;
279 lp->currentG = random() % 256;
280 lp->currentB = random() % 256;
282 for (loop = 0; loop < MAX_ENTITIES; loop++)
284 randomize_entity(lp, &lp->entities[loop]);
286 reshape_blocktube(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
290 ENTRYPOINT void free_blocktube (ModeInfo *mi)
292 blocktube_configuration *lp = &lps[MI_SCREEN(mi)];
293 # if defined ( I_HAVE_XPM )
294 if (lp->glx_context) {
295 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(lp->glx_context));
298 glDeleteTextures(1, &lp->envTexture);
300 XDestroyImage(lp->texti);
305 ENTRYPOINT void reshape_blocktube (ModeInfo *mi, int width, int height)
307 blocktube_configuration *lp = &lps[MI_SCREEN(mi)];
308 GLfloat h = (GLfloat) height / (GLfloat) width;
311 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(lp->glx_context));
313 if (width > height * 5) { /* tiny window: show middle */
316 h = height / (GLfloat) width;
319 glViewport(0, y, (GLint) width, (GLint) height);
320 glMatrixMode(GL_PROJECTION);
322 gluPerspective(45.0, 1/h, 1.0, 100.0);
323 glMatrixMode(GL_MODELVIEW);
325 # ifdef HAVE_MOBILE /* Keep it the same relative size when rotated. */
327 int o = (int) current_device_rotation();
328 if (o != 0 && o != 180 && o != -180)
329 glScalef (1/h, 1/h, 1/h);
334 static int cube_vertices(float x, float y, float z, int wire)
336 int polygon_count = 0;
337 float x2, y2, z2, nv = 0.7;
344 glNormal3f(0, 0, nv);
345 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
346 glTexCoord2f(0.0, 0.0); glVertex3f(-x2, y2, z2);
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);
353 glNormal3f(0, 0, -nv);
354 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
355 glTexCoord2f(1.0, 0.0); glVertex3f(-x2, -y2, -z2);
356 glTexCoord2f(1.0, 1.0); glVertex3f( x2, -y2, -z2);
357 glTexCoord2f(0.0, 1.0); glVertex3f( x2, y2, -z2);
358 glTexCoord2f(0.0, 0.0); glVertex3f(-x2, y2, -z2);
362 glNormal3f(0, nv, 0);
363 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
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 glTexCoord2f(1.0, 1.0); glVertex3f(-x2, y2, z2);
371 glNormal3f(0, -nv, 0);
372 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
373 glTexCoord2f(1.0, 1.0); glVertex3f(-x2, -y2, -z2);
374 glTexCoord2f(0.0, 1.0); glVertex3f(-x2, -y2, z2);
375 glTexCoord2f(0.0, 0.0); glVertex3f( x2, -y2, z2);
376 glTexCoord2f(1.0, 0.0); glVertex3f( x2, -y2, -z2);
380 if (wire) return polygon_count;
382 glNormal3f(nv, 0, 0);
383 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
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 glTexCoord2f(0.0, 0.0); glVertex3f( x2, y2, -z2);
391 glNormal3f(-nv, 0, 0);
392 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
393 glTexCoord2f(0.0, 0.0); glVertex3f(-x2, -y2, -z2);
394 glTexCoord2f(1.0, 0.0); glVertex3f(-x2, y2, -z2);
395 glTexCoord2f(1.0, 1.0); glVertex3f(-x2, y2, z2);
396 glTexCoord2f(0.0, 1.0); glVertex3f(-x2, -y2, z2);
400 return polygon_count;
403 static void draw_block(ModeInfo *mi, entity *ent)
405 blocktube_configuration *lp = &lps[MI_SCREEN(mi)];
406 glCallList (lp->block_dlist);
407 mi->polygon_count += lp->polys;
411 draw_blocktube (ModeInfo *mi)
413 blocktube_configuration *lp = &lps[MI_SCREEN(mi)];
414 Display *dpy = MI_DISPLAY(mi);
415 Window window = MI_WINDOW(mi);
419 if (!lp->glx_context)
422 mi->polygon_count = 0;
424 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(lp->glx_context));
426 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
429 glEnable(GL_TEXTURE_GEN_S);
430 glEnable(GL_TEXTURE_GEN_T);
431 glBindTexture(GL_TEXTURE_2D, lp->envTexture);
434 for (loop = 0; loop < MAX_ENTITIES; loop++) {
435 cEnt = &lp->entities[loop];
438 glTranslatef(0.0f, 0.0f, lp->zoom);
439 glRotatef(lp->tilt, 1.0f, 0.0f, 0.0f);
440 glRotatef(cEnt->angle, 0.0f, 0.0f, 1.0f);
441 glTranslatef(cEnt->position[0], cEnt->position[1], cEnt->position[2]);
442 glColor4ub((int)(lp->currentR * cEnt->tVal),
443 (int)(lp->currentG * cEnt->tVal),
444 (int)(lp->currentB * cEnt->tVal), 255);
445 draw_block(mi, cEnt);
446 entityTick(lp, cEnt);
450 if (mi->fps_p) do_fps (mi);
452 glXSwapBuffers(dpy, window);
455 XSCREENSAVER_MODULE ("BlockTube", blocktube)