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" \
31 "*count: " DEF_COUNT "\n" \
32 "*showFPS: False \n" \
33 "*fpsSolid: True \n" \
34 "*wireframe: False \n" \
38 #define countof(x) (sizeof((x))/sizeof((*x)))
40 #include "xlockmore.h"
43 #include "gltrackball.h"
46 #ifdef USE_GL /* whole file */
56 GLXContext *glx_context;
57 trackball_state *trackball;
70 static cube_configuration *bps = NULL;
73 static Bool do_wander;
75 static GLfloat thickness;
77 static XrmOptionDescRec opts[] = {
78 { "-spin", ".spin", XrmoptionNoArg, "True" },
79 { "+spin", ".spin", XrmoptionNoArg, "False" },
80 { "-wander", ".wander", XrmoptionNoArg, "True" },
81 { "+wander", ".wander", XrmoptionNoArg, "False" },
82 { "-speed", ".speed", XrmoptionSepArg, 0 },
83 { "-thickness", ".thickness", XrmoptionSepArg, 0 },
86 static argtype vars[] = {
87 {&do_spin, "spin", "Spin", DEF_SPIN, t_Bool},
88 {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
89 {&speed, "speed", "Speed", DEF_SPEED, t_Float},
90 {&thickness, "thickness", "Thickness", DEF_THICKNESS, t_Float},
93 ModeSpecOpt sws_opts = {countof(opts), opts, countof(vars), vars, NULL};
97 draw_face (ModeInfo *mi)
99 int wire = MI_IS_WIREFRAME(mi);
102 GLfloat t = thickness / 2;
106 if (t <= 0) t = 0.001;
107 else if (t > 0.5) t = 0.5;
112 for (i = 0; i < 4; i++)
114 glNormal3f (0, 0, -1);
115 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
116 glVertex3f (a, a, a);
117 glVertex3f (b, a, a);
118 glVertex3f (b-t, a+t, a);
119 glVertex3f (a+t, a+t, a);
122 glNormal3f (0, 1, 0);
123 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
124 glVertex3f (b-t, a+t, a);
125 glVertex3f (b-t, a+t, a+t);
126 glVertex3f (a+t, a+t, a+t);
127 glVertex3f (a+t, a+t, a);
130 glRotatef(90, 0, 0, 1);
136 draw_faces (ModeInfo *mi)
140 glRotatef (90, 0, 1, 0); draw_face (mi);
141 glRotatef (90, 0, 1, 0); draw_face (mi);
142 glRotatef (90, 0, 1, 0); draw_face (mi);
143 glRotatef (90, 1, 0, 0); draw_face (mi);
144 glRotatef (180, 1, 0, 0); draw_face (mi);
150 new_cube_colors (ModeInfo *mi)
152 cube_configuration *bp = &bps[MI_SCREEN(mi)];
155 if (bp->colors) free (bp->colors);
156 bp->colors = (XColor *) calloc(bp->ncolors, sizeof(XColor));
157 make_smooth_colormap (0, 0, 0,
158 bp->colors, &bp->ncolors,
160 for (i = 0; i < MI_COUNT(mi); i++)
161 bp->subcubes[i].ccolor = random() % bp->ncolors;
165 /* Window management, etc
168 reshape_cube (ModeInfo *mi, int width, int height)
170 GLfloat h = (GLfloat) height / (GLfloat) width;
172 glViewport (0, 0, (GLint) width, (GLint) height);
174 glMatrixMode(GL_PROJECTION);
176 gluPerspective (30.0, 1/h, 1.0, 100.0);
178 glMatrixMode(GL_MODELVIEW);
180 gluLookAt( 0.0, 0.0, 45.0,
184 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
189 cube_handle_event (ModeInfo *mi, XEvent *event)
191 cube_configuration *bp = &bps[MI_SCREEN(mi)];
193 if (event->xany.type == ButtonPress &&
194 event->xbutton.button == Button1)
196 bp->button_down_p = True;
197 gltrackball_start (bp->trackball,
198 event->xbutton.x, event->xbutton.y,
199 MI_WIDTH (mi), MI_HEIGHT (mi));
202 else if (event->xany.type == ButtonRelease &&
203 event->xbutton.button == Button1)
205 bp->button_down_p = False;
208 else if (event->xany.type == ButtonPress &&
209 (event->xbutton.button == Button4 ||
210 event->xbutton.button == Button5))
212 gltrackball_mousewheel (bp->trackball, event->xbutton.button, 10,
213 !!event->xbutton.state);
216 else if (event->xany.type == MotionNotify &&
219 gltrackball_track (bp->trackball,
220 event->xmotion.x, event->xmotion.y,
221 MI_WIDTH (mi), MI_HEIGHT (mi));
224 else if (event->xany.type == KeyPress)
228 XLookupString (&event->xkey, &c, 1, &keysym, 0);
231 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
234 else if (c == '\r' || c == '\n')
236 new_cube_colors (mi);
245 init_cube (ModeInfo *mi)
247 cube_configuration *bp;
248 int wire = MI_IS_WIREFRAME(mi);
252 bps = (cube_configuration *)
253 calloc (MI_NUM_SCREENS(mi), sizeof (cube_configuration));
255 fprintf(stderr, "%s: out of memory\n", progname);
259 bp = &bps[MI_SCREEN(mi)];
262 bp = &bps[MI_SCREEN(mi)];
264 bp->glx_context = init_GL(mi);
266 if (MI_COUNT(mi) <= 0) MI_COUNT(mi) = 1;
268 bp->trackball = gltrackball_init ();
269 bp->subcubes = (subcube *) calloc (MI_COUNT(mi), sizeof(subcube));
270 for (i = 0; i < MI_COUNT(mi); i++)
272 double wander_speed, spin_speed, spin_accel;
276 wander_speed = 0.05 * speed;
277 spin_speed = 10.0 * speed;
278 spin_accel = 4.0 * speed;
283 spin_speed = 4.0 * speed;
284 spin_accel = 2.0 * speed;
287 bp->subcubes[i].rot = make_rotator (do_spin ? spin_speed : 0,
288 do_spin ? spin_speed : 0,
289 do_spin ? spin_speed : 0,
291 do_wander ? wander_speed : 0,
296 new_cube_colors (mi);
298 reshape_cube (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
302 GLfloat pos[4] = {1.0, 1.0, 1.0, 0.0};
303 GLfloat amb[4] = {0.0, 0.0, 0.0, 1.0};
304 GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
305 GLfloat spc[4] = {0.0, 1.0, 1.0, 1.0};
307 glEnable(GL_LIGHTING);
309 glEnable(GL_DEPTH_TEST);
310 glEnable(GL_CULL_FACE);
312 glLightfv(GL_LIGHT0, GL_POSITION, pos);
313 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
314 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
315 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
318 bp->cube_list = glGenLists (1);
319 glNewList (bp->cube_list, GL_COMPILE);
323 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
328 draw_cube (ModeInfo *mi)
330 cube_configuration *bp = &bps[MI_SCREEN(mi)];
331 Display *dpy = MI_DISPLAY(mi);
332 Window window = MI_WINDOW(mi);
333 int wire = MI_IS_WIREFRAME(mi);
337 if (!bp->glx_context)
340 glShadeModel(GL_SMOOTH);
342 glEnable(GL_DEPTH_TEST);
343 glEnable(GL_NORMALIZE);
344 glEnable(GL_CULL_FACE);
346 if (bp->clear_p) /* we're in "no vapor trails" mode */
348 glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
349 if (! (random() % (int) (25 / speed)))
354 if (! (random() % (int) (200 / speed)))
357 new_cube_colors (mi);
363 glScalef(1.1, 1.1, 1.1);
365 mi->polygon_count = 0;
367 get_position (bp->subcubes[0].rot, &x, &y, &z, !bp->button_down_p);
368 glTranslatef((x - 0.5) * 15,
371 gltrackball_rotate (bp->trackball);
373 glScalef (4.0, 4.0, 4.0);
375 for (i = 0; i < MI_COUNT(mi); i++)
377 static GLfloat bcolor[4] = {0.0, 0.0, 0.0, 1.0};
378 static GLfloat bspec[4] = {1.0, 1.0, 1.0, 1.0};
379 static GLfloat bshiny = 128.0;
383 if (i != 0) /* N+1 cubes rotate relative to cube 0 */
385 get_rotation (bp->subcubes[0].rot, &x, &y, &z, False);
386 glRotatef (x * 360, 1.0, 0.0, 0.0);
387 glRotatef (y * 360, 0.0, 1.0, 0.0);
388 glRotatef (z * 360, 0.0, 0.0, 1.0);
391 get_rotation (bp->subcubes[i].rot, &x, &y, &z, !bp->button_down_p);
392 glRotatef (x * 360, 1.0, 0.0, 0.0);
393 glRotatef (y * 360, 0.0, 1.0, 0.0);
394 glRotatef (z * 360, 0.0, 0.0, 1.0);
396 bcolor[0] = bp->colors[bp->subcubes[i].ccolor].red / 65536.0;
397 bcolor[1] = bp->colors[bp->subcubes[i].ccolor].green / 65536.0;
398 bcolor[2] = bp->colors[bp->subcubes[i].ccolor].blue / 65536.0;
399 bp->subcubes[i].ccolor++;
400 if (bp->subcubes[i].ccolor >= bp->ncolors)
401 bp->subcubes[i].ccolor = 0;
404 glColor3f (bcolor[0], bcolor[1], bcolor[2]);
407 glMaterialfv (GL_FRONT, GL_SPECULAR, bspec);
408 glMateriali (GL_FRONT, GL_SHININESS, bshiny);
409 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, bcolor);
412 glCallList (bp->cube_list);
413 mi->polygon_count += (4 * 2 * 6);
420 if (mi->fps_p) do_fps (mi);
423 glXSwapBuffers(dpy, window);