1 /* cubestorm, Copyright (c) 2003, 2004 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 {&do_spin, "spin", "Spin", DEF_SPIN, t_Bool},
93 {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
94 {&speed, "speed", "Speed", DEF_SPEED, t_Float},
95 {&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 == ButtonPress &&
214 (event->xbutton.button == Button4 ||
215 event->xbutton.button == Button5))
217 gltrackball_mousewheel (bp->trackball, event->xbutton.button, 10,
218 !!event->xbutton.state);
221 else if (event->xany.type == MotionNotify &&
224 gltrackball_track (bp->trackball,
225 event->xmotion.x, event->xmotion.y,
226 MI_WIDTH (mi), MI_HEIGHT (mi));
229 else if (event->xany.type == KeyPress)
233 XLookupString (&event->xkey, &c, 1, &keysym, 0);
236 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
239 else if (c == '\r' || c == '\n')
241 new_cube_colors (mi);
250 init_cube (ModeInfo *mi)
252 cube_configuration *bp;
253 int wire = MI_IS_WIREFRAME(mi);
257 bps = (cube_configuration *)
258 calloc (MI_NUM_SCREENS(mi), sizeof (cube_configuration));
260 fprintf(stderr, "%s: out of memory\n", progname);
264 bp = &bps[MI_SCREEN(mi)];
267 bp = &bps[MI_SCREEN(mi)];
269 bp->glx_context = init_GL(mi);
271 if (MI_COUNT(mi) <= 0) MI_COUNT(mi) = 1;
273 bp->trackball = gltrackball_init ();
274 bp->subcubes = (subcube *) calloc (MI_COUNT(mi), sizeof(subcube));
275 for (i = 0; i < MI_COUNT(mi); i++)
277 double wander_speed, spin_speed, spin_accel;
281 wander_speed = 0.05 * speed;
282 spin_speed = 10.0 * speed;
283 spin_accel = 4.0 * speed;
288 spin_speed = 4.0 * speed;
289 spin_accel = 2.0 * speed;
292 bp->subcubes[i].rot = make_rotator (do_spin ? spin_speed : 0,
293 do_spin ? spin_speed : 0,
294 do_spin ? spin_speed : 0,
296 do_wander ? wander_speed : 0,
301 new_cube_colors (mi);
303 reshape_cube (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
307 GLfloat pos[4] = {1.0, 1.0, 1.0, 0.0};
308 GLfloat amb[4] = {0.0, 0.0, 0.0, 1.0};
309 GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
310 GLfloat spc[4] = {0.0, 1.0, 1.0, 1.0};
312 glEnable(GL_LIGHTING);
314 glEnable(GL_DEPTH_TEST);
315 glEnable(GL_CULL_FACE);
317 glLightfv(GL_LIGHT0, GL_POSITION, pos);
318 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
319 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
320 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
323 bp->cube_list = glGenLists (1);
324 glNewList (bp->cube_list, GL_COMPILE);
328 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
333 draw_cube (ModeInfo *mi)
335 cube_configuration *bp = &bps[MI_SCREEN(mi)];
336 Display *dpy = MI_DISPLAY(mi);
337 Window window = MI_WINDOW(mi);
338 int wire = MI_IS_WIREFRAME(mi);
342 if (!bp->glx_context)
345 glShadeModel(GL_SMOOTH);
347 glEnable(GL_DEPTH_TEST);
348 glEnable(GL_NORMALIZE);
349 glEnable(GL_CULL_FACE);
351 if (bp->clear_p) /* we're in "no vapor trails" mode */
353 glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
354 if (! (random() % (int) (25 / speed)))
359 if (! (random() % (int) (200 / speed)))
362 new_cube_colors (mi);
368 glScalef(1.1, 1.1, 1.1);
370 mi->polygon_count = 0;
372 get_position (bp->subcubes[0].rot, &x, &y, &z, !bp->button_down_p);
373 glTranslatef((x - 0.5) * 15,
376 gltrackball_rotate (bp->trackball);
378 glScalef (4.0, 4.0, 4.0);
380 for (i = 0; i < MI_COUNT(mi); i++)
382 static GLfloat bcolor[4] = {0.0, 0.0, 0.0, 1.0};
383 static GLfloat bspec[4] = {1.0, 1.0, 1.0, 1.0};
384 static GLfloat bshiny = 128.0;
388 if (i != 0) /* N+1 cubes rotate relative to cube 0 */
390 get_rotation (bp->subcubes[0].rot, &x, &y, &z, False);
391 glRotatef (x * 360, 1.0, 0.0, 0.0);
392 glRotatef (y * 360, 0.0, 1.0, 0.0);
393 glRotatef (z * 360, 0.0, 0.0, 1.0);
396 get_rotation (bp->subcubes[i].rot, &x, &y, &z, !bp->button_down_p);
397 glRotatef (x * 360, 1.0, 0.0, 0.0);
398 glRotatef (y * 360, 0.0, 1.0, 0.0);
399 glRotatef (z * 360, 0.0, 0.0, 1.0);
401 bcolor[0] = bp->colors[bp->subcubes[i].ccolor].red / 65536.0;
402 bcolor[1] = bp->colors[bp->subcubes[i].ccolor].green / 65536.0;
403 bcolor[2] = bp->colors[bp->subcubes[i].ccolor].blue / 65536.0;
404 bp->subcubes[i].ccolor++;
405 if (bp->subcubes[i].ccolor >= bp->ncolors)
406 bp->subcubes[i].ccolor = 0;
409 glColor3f (bcolor[0], bcolor[1], bcolor[2]);
412 glMaterialfv (GL_FRONT, GL_SPECULAR, bspec);
413 glMateriali (GL_FRONT, GL_SHININESS, bshiny);
414 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, bcolor);
417 glCallList (bp->cube_list);
418 mi->polygon_count += (4 * 2 * 6);
425 if (mi->fps_p) do_fps (mi);
428 glXSwapBuffers(dpy, window);