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
14 #include <X11/Intrinsic.h>
18 extern XtAppContext app;
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
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"
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" \
42 #define countof(x) (sizeof((x))/sizeof((*x)))
44 #include "xlockmore.h"
49 #ifdef USE_GL /* whole file */
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"
58 #include "../images/blocktube.xpm"
62 GLXContext *glx_context;
64 } blocktube_configuration;
66 static int nextID = 1;
67 static blocktube_configuration *lps = NULL;
76 GLfloat angularVelocity;
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;
90 static GLuint envTexture;
92 static int do_texture;
95 GLfloat tunnelLength=200;
96 GLfloat tunnelWidth=5;
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" },
107 static argtype vars[] = {
108 {(caddr_t *) &holdtime, "holdtime", "Hold Time", DEF_HOLDTIME, t_Int},
109 {(caddr_t *) &changetime, "changetime", "Change Time", DEF_CHANGETIME, \
111 {(caddr_t *) &do_texture, "texture", "Texture", DEF_TEXTURE, t_Bool},
112 {(caddr_t *) &do_fog, "fog", "Fog", DEF_FOG, t_Bool},
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"},
120 ModeSpecOpt blocktube_opts = {countof(opts), opts, countof(vars), vars, desc};
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 */
130 static Bool LoadGLTextures(ModeInfo *mi)
135 glGenTextures(1, &envTexture);
136 glBindTexture(GL_TEXTURE_2D, envTexture);
137 texti = xpm_to_ximage(MI_DISPLAY(mi), MI_VISUAL(mi), MI_COLORMAP(mi),
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);
153 static void newTargetColor(void)
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;
163 static void randomize_entity (entity *ent)
166 ent->tVal = 1 - ((float)random() / RAND_MAX / 1.5);
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;
176 static void entityTick(entity *ent)
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;
186 static void tick(void)
192 counter = changetime;
196 changing = (!changing);
206 static void cube_vertices(float x, float y, float z, int wire);
208 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);
221 lp = &lps[MI_SCREEN(mi)];
224 lp = &lps[MI_SCREEN(mi)];
225 lp->glx_context = init_GL(mi);
233 lp->block_dlist = glGenLists (1);
234 glNewList (lp->block_dlist, GL_COMPILE);
235 cube_vertices(0.15, 1.2, 5.25, wire);
238 #if defined( I_HAVE_XPM )
240 if (!LoadGLTextures(mi)) {
241 fprintf(stderr, "%s: can't load textures!\n", progname);
244 glEnable(GL_TEXTURE_2D);
248 /* kick on the fog machine */
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);
258 glShadeModel(GL_SMOOTH);
259 glEnable(GL_DEPTH_TEST);
260 glEnable(GL_CULL_FACE);
261 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
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.
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);
281 currentR = random() % 256;
282 currentG = random() % 256;
283 currentB = random() % 256;
285 for (loop = 0; loop < MAX_ENTITIES; loop++)
287 randomize_entity(&entities[loop]);
289 reshape_blocktube(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
293 void release_blocktube (ModeInfo *mi)
295 #if defined ( I_HAVE_XPM )
296 glDeleteTextures(1, &envTexture);
297 XDestroyImage(texti);
301 void reshape_blocktube (ModeInfo *mi, int width, int height)
303 GLfloat h = (GLfloat) height / (GLfloat) width;
305 glViewport(0, 0, (GLint) width, (GLint) height);
306 glMatrixMode(GL_PROJECTION);
308 gluPerspective(45.0, 1/h, 1.0, 100.0);
309 glMatrixMode(GL_MODELVIEW);
312 static void cube_vertices(float x, float y, float z, int wire)
314 float x2, y2, z2, nv = 0.7;
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);
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);
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);
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);
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);
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);
372 static void draw_block(ModeInfo *mi, entity *ent)
374 blocktube_configuration *lp = &lps[MI_SCREEN(mi)];
375 glCallList (lp->block_dlist);
379 draw_blocktube (ModeInfo *mi)
381 blocktube_configuration *lp = &lps[MI_SCREEN(mi)];
382 Display *dpy = MI_DISPLAY(mi);
383 Window window = MI_WINDOW(mi);
387 if (!lp->glx_context)
390 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
393 glEnable(GL_TEXTURE_GEN_S);
394 glEnable(GL_TEXTURE_GEN_T);
395 glBindTexture(GL_TEXTURE_2D, envTexture);
398 for (loop = 0; loop < MAX_ENTITIES; loop++) {
399 cEnt = &entities[loop];
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);
414 if (mi->fps_p) do_fps (mi);
416 glXSwapBuffers(dpy, window);