1 /* cubestorm, Copyright (c) 2003 Jamie Zawinski <jwz@jwz.org>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
12 #include <X11/Intrinsic.h>
14 extern XtAppContext app;
16 #define PROGCLASS "CubeStorm"
17 #define HACK_INIT init_cube
18 #define HACK_DRAW draw_cube
19 #define HACK_RESHAPE reshape_cube
20 #define HACK_HANDLE_EVENT cube_handle_event
21 #define EVENT_MASK PointerMotionMask
22 #define sws_opts xlockmore_opts
24 #define DEF_SPIN "True"
25 #define DEF_WANDER "True"
26 #define DEF_SPEED "1.0"
27 #define DEF_THICKNESS "0.06"
30 #define DEFAULTS "*delay: 30000 \n" \
32 "*showFPS: False \n" \
33 "*fpsSolid: True \n" \
34 "*wireframe: False \n" \
35 "*speed: " DEF_SPEED " \n" \
36 "*spin: " DEF_SPIN "\n" \
37 "*wander: " DEF_WANDER "\n" \
38 "*thickness: " DEF_THICKNESS "\n" \
39 "*count: " DEF_COUNT "\n" \
43 #define countof(x) (sizeof((x))/sizeof((*x)))
45 #include "xlockmore.h"
48 #include "gltrackball.h"
51 #ifdef USE_GL /* whole file */
61 GLXContext *glx_context;
62 trackball_state *trackball;
75 static cube_configuration *bps = NULL;
78 static Bool do_wander;
80 static GLfloat thickness;
82 static XrmOptionDescRec opts[] = {
83 { "-spin", ".spin", XrmoptionNoArg, "True" },
84 { "+spin", ".spin", XrmoptionNoArg, "False" },
85 { "-wander", ".wander", XrmoptionNoArg, "True" },
86 { "+wander", ".wander", XrmoptionNoArg, "False" },
87 { "-speed", ".speed", XrmoptionSepArg, 0 },
88 { "-thickness", ".thickness", XrmoptionSepArg, 0 },
91 static argtype vars[] = {
92 {(caddr_t *) &do_spin, "spin", "Spin", DEF_SPIN, t_Bool},
93 {(caddr_t *) &do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
94 {(caddr_t *) &speed, "speed", "Speed", DEF_SPEED, t_Float},
95 {(caddr_t *) &thickness, "thickness", "Thickness", DEF_THICKNESS, t_Float},
98 ModeSpecOpt sws_opts = {countof(opts), opts, countof(vars), vars, NULL};
102 draw_face (ModeInfo *mi)
104 int wire = MI_IS_WIREFRAME(mi);
107 GLfloat t = thickness / 2;
111 if (t <= 0) t = 0.001;
112 else if (t > 0.5) t = 0.5;
117 for (i = 0; i < 4; i++)
119 glNormal3f (0, 0, -1);
120 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
121 glVertex3f (a, a, a);
122 glVertex3f (b, a, a);
123 glVertex3f (b-t, a+t, a);
124 glVertex3f (a+t, a+t, a);
127 glNormal3f (0, 1, 0);
128 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
129 glVertex3f (b-t, a+t, a);
130 glVertex3f (b-t, a+t, a+t);
131 glVertex3f (a+t, a+t, a+t);
132 glVertex3f (a+t, a+t, a);
135 glRotatef(90, 0, 0, 1);
141 draw_faces (ModeInfo *mi)
145 glRotatef (90, 0, 1, 0); draw_face (mi);
146 glRotatef (90, 0, 1, 0); draw_face (mi);
147 glRotatef (90, 0, 1, 0); draw_face (mi);
148 glRotatef (90, 1, 0, 0); draw_face (mi);
149 glRotatef (180, 1, 0, 0); draw_face (mi);
155 new_cube_colors (ModeInfo *mi)
157 cube_configuration *bp = &bps[MI_SCREEN(mi)];
160 if (bp->colors) free (bp->colors);
161 bp->colors = (XColor *) calloc(bp->ncolors, sizeof(XColor));
162 make_smooth_colormap (0, 0, 0,
163 bp->colors, &bp->ncolors,
165 for (i = 0; i < MI_COUNT(mi); i++)
166 bp->subcubes[i].ccolor = random() % bp->ncolors;
170 /* Window management, etc
173 reshape_cube (ModeInfo *mi, int width, int height)
175 GLfloat h = (GLfloat) height / (GLfloat) width;
177 glViewport (0, 0, (GLint) width, (GLint) height);
179 glMatrixMode(GL_PROJECTION);
181 gluPerspective (30.0, 1/h, 1.0, 100.0);
183 glMatrixMode(GL_MODELVIEW);
185 gluLookAt( 0.0, 0.0, 45.0,
189 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
194 cube_handle_event (ModeInfo *mi, XEvent *event)
196 cube_configuration *bp = &bps[MI_SCREEN(mi)];
198 if (event->xany.type == ButtonPress &&
199 event->xbutton.button & Button1)
201 bp->button_down_p = True;
202 gltrackball_start (bp->trackball,
203 event->xbutton.x, event->xbutton.y,
204 MI_WIDTH (mi), MI_HEIGHT (mi));
207 else if (event->xany.type == ButtonRelease &&
208 event->xbutton.button & Button1)
210 bp->button_down_p = False;
213 else if (event->xany.type == MotionNotify &&
216 gltrackball_track (bp->trackball,
217 event->xmotion.x, event->xmotion.y,
218 MI_WIDTH (mi), MI_HEIGHT (mi));
221 else if (event->xany.type == KeyPress)
225 XLookupString (&event->xkey, &c, 1, &keysym, 0);
228 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
231 else if (c == '\r' || c == '\n')
233 new_cube_colors (mi);
242 init_cube (ModeInfo *mi)
244 cube_configuration *bp;
245 int wire = MI_IS_WIREFRAME(mi);
249 bps = (cube_configuration *)
250 calloc (MI_NUM_SCREENS(mi), sizeof (cube_configuration));
252 fprintf(stderr, "%s: out of memory\n", progname);
256 bp = &bps[MI_SCREEN(mi)];
259 bp = &bps[MI_SCREEN(mi)];
261 bp->glx_context = init_GL(mi);
263 if (MI_COUNT(mi) <= 0) MI_COUNT(mi) = 1;
265 bp->trackball = gltrackball_init ();
266 bp->subcubes = (subcube *) calloc (MI_COUNT(mi), sizeof(subcube));
267 for (i = 0; i < MI_COUNT(mi); i++)
269 double wander_speed, spin_speed, spin_accel;
273 wander_speed = 0.05 * speed;
274 spin_speed = 10.0 * speed;
275 spin_accel = 4.0 * speed;
280 spin_speed = 4.0 * speed;
281 spin_accel = 2.0 * speed;
284 bp->subcubes[i].rot = make_rotator (do_spin ? spin_speed : 0,
285 do_spin ? spin_speed : 0,
286 do_spin ? spin_speed : 0,
288 do_wander ? wander_speed : 0,
293 new_cube_colors (mi);
295 reshape_cube (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
299 GLfloat pos[4] = {1.0, 1.0, 1.0, 0.0};
300 GLfloat amb[4] = {0.0, 0.0, 0.0, 1.0};
301 GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
302 GLfloat spc[4] = {0.0, 1.0, 1.0, 1.0};
304 glEnable(GL_LIGHTING);
306 glEnable(GL_DEPTH_TEST);
307 glEnable(GL_CULL_FACE);
309 glLightfv(GL_LIGHT0, GL_POSITION, pos);
310 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
311 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
312 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
315 bp->cube_list = glGenLists (1);
316 glNewList (bp->cube_list, GL_COMPILE);
320 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
325 draw_cube (ModeInfo *mi)
327 cube_configuration *bp = &bps[MI_SCREEN(mi)];
328 Display *dpy = MI_DISPLAY(mi);
329 Window window = MI_WINDOW(mi);
330 int wire = MI_IS_WIREFRAME(mi);
334 if (!bp->glx_context)
337 glShadeModel(GL_SMOOTH);
339 glEnable(GL_DEPTH_TEST);
340 glEnable(GL_NORMALIZE);
341 glEnable(GL_CULL_FACE);
343 if (bp->clear_p) /* we're in "no vapor trails" mode */
345 glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
346 if (! (random() % (int) (25 / speed)))
351 if (! (random() % (int) (200 / speed)))
354 new_cube_colors (mi);
360 glScalef(1.1, 1.1, 1.1);
362 mi->polygon_count = 0;
364 get_position (bp->subcubes[0].rot, &x, &y, &z, !bp->button_down_p);
365 glTranslatef((x - 0.5) * 15,
368 gltrackball_rotate (bp->trackball);
370 glScalef (4.0, 4.0, 4.0);
372 for (i = 0; i < MI_COUNT(mi); i++)
374 static GLfloat bcolor[4] = {0.0, 0.0, 0.0, 1.0};
375 static GLfloat bspec[4] = {1.0, 1.0, 1.0, 1.0};
376 static GLfloat bshiny = 128.0;
380 if (i != 0) /* N+1 cubes rotate relative to cube 0 */
382 get_rotation (bp->subcubes[0].rot, &x, &y, &z, False);
383 glRotatef (x * 360, 1.0, 0.0, 0.0);
384 glRotatef (y * 360, 0.0, 1.0, 0.0);
385 glRotatef (z * 360, 0.0, 0.0, 1.0);
388 get_rotation (bp->subcubes[i].rot, &x, &y, &z, !bp->button_down_p);
389 glRotatef (x * 360, 1.0, 0.0, 0.0);
390 glRotatef (y * 360, 0.0, 1.0, 0.0);
391 glRotatef (z * 360, 0.0, 0.0, 1.0);
393 bcolor[0] = bp->colors[bp->subcubes[i].ccolor].red / 65536.0;
394 bcolor[1] = bp->colors[bp->subcubes[i].ccolor].green / 65536.0;
395 bcolor[2] = bp->colors[bp->subcubes[i].ccolor].blue / 65536.0;
396 bp->subcubes[i].ccolor++;
397 if (bp->subcubes[i].ccolor >= bp->ncolors)
398 bp->subcubes[i].ccolor = 0;
401 glColor3f (bcolor[0], bcolor[1], bcolor[2]);
404 glMaterialfv (GL_FRONT, GL_SPECULAR, bspec);
405 glMateriali (GL_FRONT, GL_SHININESS, bshiny);
406 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, bcolor);
409 glCallList (bp->cube_list);
410 mi->polygon_count += (4 * 2 * 6);
417 if (mi->fps_p) do_fps (mi);
420 glXSwapBuffers(dpy, window);