1 /* cubestorm, Copyright (c) 2003-2014 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 #define DEFAULTS "*delay: 30000 \n" \
13 "*count: " DEF_COUNT "\n" \
14 "*showFPS: False \n" \
15 "*fpsSolid: True \n" \
16 "*wireframe: False \n" \
17 "*suppressRotationAnimation: True\n" \
21 # define release_cube 0
23 #define countof(x) (sizeof((x))/sizeof((*x)))
25 #include "xlockmore.h"
28 #include "gltrackball.h"
31 #ifdef USE_GL /* whole file */
33 #define DEF_SPIN "True"
34 #define DEF_WANDER "True"
35 #define DEF_SPEED "1.0"
36 #define DEF_THICKNESS "0.06"
38 #define DEF_DBUF "False"
46 GLXContext *glx_context;
47 trackball_state *trackball;
60 static cube_configuration *bps = NULL;
63 static Bool do_wander;
65 static GLfloat thickness;
68 static XrmOptionDescRec opts[] = {
69 { "-spin", ".spin", XrmoptionNoArg, "True" },
70 { "+spin", ".spin", XrmoptionNoArg, "False" },
71 { "-wander", ".wander", XrmoptionNoArg, "True" },
72 { "+wander", ".wander", XrmoptionNoArg, "False" },
73 { "-speed", ".speed", XrmoptionSepArg, 0 },
74 { "-db", ".doubleBuffer", XrmoptionNoArg, "True"},
75 { "+db", ".doubleBuffer", XrmoptionNoArg, "False"},
76 { "-thickness", ".thickness", XrmoptionSepArg, 0 },
79 static argtype vars[] = {
80 {&do_spin, "spin", "Spin", DEF_SPIN, t_Bool},
81 {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
82 {&speed, "speed", "Speed", DEF_SPEED, t_Float},
83 {&thickness, "thickness", "Thickness", DEF_THICKNESS, t_Float},
84 {&dbuf_p, "doubleBuffer", "DoubleBuffer", DEF_DBUF, t_Bool},
87 ENTRYPOINT ModeSpecOpt cube_opts = {countof(opts), opts, countof(vars), vars, NULL};
91 draw_face (ModeInfo *mi)
93 int wire = MI_IS_WIREFRAME(mi);
96 GLfloat t = thickness / 2;
100 if (t <= 0) t = 0.001;
101 else if (t > 0.5) t = 0.5;
106 for (i = 0; i < 4; i++)
108 glNormal3f (0, 0, -1);
109 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
110 glVertex3f (a, a, a);
111 glVertex3f (b, a, a);
112 glVertex3f (b-t, a+t, a);
113 glVertex3f (a+t, a+t, a);
116 glNormal3f (0, 1, 0);
117 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
118 glVertex3f (b-t, a+t, a);
119 glVertex3f (b-t, a+t, a+t);
120 glVertex3f (a+t, a+t, a+t);
121 glVertex3f (a+t, a+t, a);
124 glRotatef(90, 0, 0, 1);
130 draw_faces (ModeInfo *mi)
134 glRotatef (90, 0, 1, 0); draw_face (mi);
135 glRotatef (90, 0, 1, 0); draw_face (mi);
136 glRotatef (90, 0, 1, 0); draw_face (mi);
137 glRotatef (90, 1, 0, 0); draw_face (mi);
138 glRotatef (180, 1, 0, 0); draw_face (mi);
144 new_cube_colors (ModeInfo *mi)
146 cube_configuration *bp = &bps[MI_SCREEN(mi)];
149 if (bp->colors) free (bp->colors);
150 bp->colors = (XColor *) calloc(bp->ncolors, sizeof(XColor));
151 make_smooth_colormap (0, 0, 0,
152 bp->colors, &bp->ncolors,
154 for (i = 0; i < MI_COUNT(mi); i++)
155 bp->subcubes[i].ccolor = random() % bp->ncolors;
159 /* Window management, etc
162 reshape_cube (ModeInfo *mi, int width, int height)
164 GLfloat h = (GLfloat) height / (GLfloat) width;
167 if (width > height * 5) { /* tiny window: show middle */
168 height = width * 9/16;
170 h = height / (GLfloat) width;
173 glViewport (0, y, (GLint) width, (GLint) height);
175 glMatrixMode(GL_PROJECTION);
177 gluPerspective (30.0, 1/h, 1.0, 100.0);
179 glMatrixMode(GL_MODELVIEW);
181 gluLookAt( 0.0, 0.0, 45.0,
185 # ifdef HAVE_MOBILE /* Keep it the same relative size when rotated. */
187 int o = (int) current_device_rotation();
188 if (o != 0 && o != 180 && o != -180)
189 glScalef (1/h, 1/h, 1/h);
193 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
198 cube_handle_event (ModeInfo *mi, XEvent *event)
200 cube_configuration *bp = &bps[MI_SCREEN(mi)];
202 if (gltrackball_event_handler (event, bp->trackball,
203 MI_WIDTH (mi), MI_HEIGHT (mi),
206 else if (event->xany.type == KeyPress)
210 XLookupString (&event->xkey, &c, 1, &keysym, 0);
213 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
216 else if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
219 else if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
222 new_cube_colors (mi);
230 init_cube (ModeInfo *mi)
232 cube_configuration *bp;
233 int wire = MI_IS_WIREFRAME(mi);
242 bp = &bps[MI_SCREEN(mi)];
244 bp->glx_context = init_GL(mi);
246 if (MI_COUNT(mi) <= 0) MI_COUNT(mi) = 1;
248 bp->trackball = gltrackball_init (True);
249 bp->subcubes = (subcube *) calloc (MI_COUNT(mi), sizeof(subcube));
250 for (i = 0; i < MI_COUNT(mi); i++)
252 double wander_speed, spin_speed, spin_accel;
256 wander_speed = 0.05 * speed;
257 spin_speed = 10.0 * speed;
258 spin_accel = 4.0 * speed;
263 spin_speed = 4.0 * speed;
264 spin_accel = 2.0 * speed;
267 bp->subcubes[i].rot = make_rotator (do_spin ? spin_speed : 0,
268 do_spin ? spin_speed : 0,
269 do_spin ? spin_speed : 0,
271 do_wander ? wander_speed : 0,
276 new_cube_colors (mi);
278 reshape_cube (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
282 GLfloat pos[4] = {1.0, 1.0, 1.0, 0.0};
283 GLfloat amb[4] = {0.0, 0.0, 0.0, 1.0};
284 GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
285 GLfloat spc[4] = {0.0, 1.0, 1.0, 1.0};
287 glEnable(GL_LIGHTING);
289 glEnable(GL_DEPTH_TEST);
290 glEnable(GL_CULL_FACE);
292 glLightfv(GL_LIGHT0, GL_POSITION, pos);
293 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
294 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
295 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
298 bp->cube_list = glGenLists (1);
299 glNewList (bp->cube_list, GL_COMPILE);
303 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
308 draw_cube (ModeInfo *mi)
310 cube_configuration *bp = &bps[MI_SCREEN(mi)];
311 Display *dpy = MI_DISPLAY(mi);
312 Window window = MI_WINDOW(mi);
313 int wire = MI_IS_WIREFRAME(mi);
317 if (!bp->glx_context)
320 glDrawBuffer(dbuf_p ? GL_BACK : GL_FRONT);
322 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
324 glShadeModel(GL_SMOOTH);
326 glEnable(GL_DEPTH_TEST);
327 glEnable(GL_NORMALIZE);
328 glEnable(GL_CULL_FACE);
330 if (bp->clear_p) /* we're in "no vapor trails" mode */
332 glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
333 if (! (random() % (int) (25 / speed)))
338 if (! (random() % (int) (200 / speed)))
341 new_cube_colors (mi);
347 glScalef(1.1, 1.1, 1.1);
349 mi->polygon_count = 0;
351 get_position (bp->subcubes[0].rot, &x, &y, &z, !bp->button_down_p);
352 glTranslatef((x - 0.5) * 15,
355 gltrackball_rotate (bp->trackball);
357 glScalef (4.0, 4.0, 4.0);
359 for (i = 0; i < MI_COUNT(mi); i++)
361 GLfloat bcolor[4] = {0.0, 0.0, 0.0, 1.0};
362 GLfloat bspec[4] = {1.0, 1.0, 1.0, 1.0};
363 GLfloat bshiny = 128.0;
367 if (i != 0) /* N+1 cubes rotate relative to cube 0 */
369 get_rotation (bp->subcubes[0].rot, &x, &y, &z, False);
370 glRotatef (x * 360, 1.0, 0.0, 0.0);
371 glRotatef (y * 360, 0.0, 1.0, 0.0);
372 glRotatef (z * 360, 0.0, 0.0, 1.0);
375 get_rotation (bp->subcubes[i].rot, &x, &y, &z, !bp->button_down_p);
376 glRotatef (x * 360, 1.0, 0.0, 0.0);
377 glRotatef (y * 360, 0.0, 1.0, 0.0);
378 glRotatef (z * 360, 0.0, 0.0, 1.0);
380 bcolor[0] = bp->colors[bp->subcubes[i].ccolor].red / 65536.0;
381 bcolor[1] = bp->colors[bp->subcubes[i].ccolor].green / 65536.0;
382 bcolor[2] = bp->colors[bp->subcubes[i].ccolor].blue / 65536.0;
383 bp->subcubes[i].ccolor++;
384 if (bp->subcubes[i].ccolor >= bp->ncolors)
385 bp->subcubes[i].ccolor = 0;
388 glColor3f (bcolor[0], bcolor[1], bcolor[2]);
391 glMaterialfv (GL_FRONT, GL_SPECULAR, bspec);
392 glMateriali (GL_FRONT, GL_SHININESS, bshiny);
393 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, bcolor);
396 glCallList (bp->cube_list);
397 mi->polygon_count += (4 * 2 * 6);
404 if (mi->fps_p) do_fps (mi);
408 glXSwapBuffers(dpy, window);
411 XSCREENSAVER_MODULE_2 ("CubeStorm", cubestorm, cube)