1 /* cubenetic, Copyright (c) 2002-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: 20000 \n" \
14 "*showFPS: False \n" \
15 "*wireframe: False \n" \
16 "*suppressRotationAnimation: True\n" \
19 # define release_cube 0
21 #define countof(x) (sizeof((x))/sizeof((*x)))
23 #include "xlockmore.h"
26 #include "gltrackball.h"
29 #ifdef USE_GL /* whole file */
32 #define DEF_SPIN "XYZ"
33 #define DEF_WANDER "True"
34 #define DEF_TEXTURE "True"
37 #define DEF_WAVE_SPEED "80"
38 #define DEF_WAVE_RADIUS "512"
64 GLXContext *glx_context;
66 trackball_state *trackball;
76 int texture_width, texture_height;
77 unsigned char *texture;
81 XColor *texture_colors;
85 static cube_configuration *ccs = NULL;
88 static Bool do_wander;
89 static Bool do_texture;
91 static int wave_count;
92 static int wave_speed;
93 static int wave_radius;
94 static int texture_size = 256;
96 static XrmOptionDescRec opts[] = {
97 { "-spin", ".spin", XrmoptionSepArg, 0 },
98 { "+spin", ".spin", XrmoptionNoArg, "" },
99 { "-wander", ".wander", XrmoptionNoArg, "True" },
100 { "+wander", ".wander", XrmoptionNoArg, "False" },
101 {"-texture", ".texture", XrmoptionNoArg, "true" },
102 {"+texture", ".texture", XrmoptionNoArg, "false" },
103 {"-waves", ".waves", XrmoptionSepArg, 0 },
104 {"-wave-speed", ".waveSpeed", XrmoptionSepArg, 0 },
105 {"-wave-radius", ".waveRadius", XrmoptionSepArg, 0 },
108 static argtype vars[] = {
109 {&do_spin, "spin", "Spin", DEF_SPIN, t_String},
110 {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
111 {&do_texture, "texture", "Texture", DEF_TEXTURE, t_Bool},
112 {&wave_count, "waves", "Waves", DEF_WAVES, t_Int},
113 {&wave_speed, "waveSpeed", "WaveSpeed", DEF_WAVE_SPEED, t_Int},
114 {&wave_radius,"waveRadius","WaveRadius", DEF_WAVE_RADIUS,t_Int},
117 ENTRYPOINT ModeSpecOpt cube_opts = {countof(opts), opts, countof(vars), vars, NULL};
121 unit_cube (Bool wire)
124 glBegin (wire ? GL_LINE_LOOP : GL_QUADS); /* front */
125 glNormal3f (0, 0, 1);
126 glTexCoord2f(1, 0); glVertex3f ( 0.5, -0.5, 0.5);
127 glTexCoord2f(0, 0); glVertex3f ( 0.5, 0.5, 0.5);
128 glTexCoord2f(0, 1); glVertex3f (-0.5, 0.5, 0.5);
129 glTexCoord2f(1, 1); glVertex3f (-0.5, -0.5, 0.5);
133 glBegin (wire ? GL_LINE_LOOP : GL_QUADS); /* back */
134 glNormal3f (0, 0, -1);
135 glTexCoord2f(0, 0); glVertex3f (-0.5, -0.5, -0.5);
136 glTexCoord2f(0, 1); glVertex3f (-0.5, 0.5, -0.5);
137 glTexCoord2f(1, 1); glVertex3f ( 0.5, 0.5, -0.5);
138 glTexCoord2f(1, 0); glVertex3f ( 0.5, -0.5, -0.5);
142 glBegin (wire ? GL_LINE_LOOP : GL_QUADS); /* left */
143 glNormal3f (-1, 0, 0);
144 glTexCoord2f(1, 1); glVertex3f (-0.5, 0.5, 0.5);
145 glTexCoord2f(1, 0); glVertex3f (-0.5, 0.5, -0.5);
146 glTexCoord2f(0, 0); glVertex3f (-0.5, -0.5, -0.5);
147 glTexCoord2f(0, 1); glVertex3f (-0.5, -0.5, 0.5);
151 glBegin (wire ? GL_LINE_LOOP : GL_QUADS); /* right */
152 glNormal3f (1, 0, 0);
153 glTexCoord2f(1, 1); glVertex3f ( 0.5, -0.5, -0.5);
154 glTexCoord2f(1, 0); glVertex3f ( 0.5, 0.5, -0.5);
155 glTexCoord2f(0, 0); glVertex3f ( 0.5, 0.5, 0.5);
156 glTexCoord2f(0, 1); glVertex3f ( 0.5, -0.5, 0.5);
160 if (wire) return polys;
162 glBegin (wire ? GL_LINE_LOOP : GL_QUADS); /* top */
163 glNormal3f (0, 1, 0);
164 glTexCoord2f(0, 0); glVertex3f ( 0.5, 0.5, 0.5);
165 glTexCoord2f(0, 1); glVertex3f ( 0.5, 0.5, -0.5);
166 glTexCoord2f(1, 1); glVertex3f (-0.5, 0.5, -0.5);
167 glTexCoord2f(1, 0); glVertex3f (-0.5, 0.5, 0.5);
171 glBegin (wire ? GL_LINE_LOOP : GL_QUADS); /* bottom */
172 glNormal3f (0, -1, 0);
173 glTexCoord2f(1, 0); glVertex3f (-0.5, -0.5, 0.5);
174 glTexCoord2f(0, 0); glVertex3f (-0.5, -0.5, -0.5);
175 glTexCoord2f(0, 1); glVertex3f ( 0.5, -0.5, -0.5);
176 glTexCoord2f(1, 1); glVertex3f ( 0.5, -0.5, 0.5);
184 /* Window management, etc
187 reshape_cube (ModeInfo *mi, int width, int height)
189 GLfloat h = (GLfloat) height / (GLfloat) width;
192 if (width > height * 5) { /* tiny window: show middle */
195 h = height / (GLfloat) width;
198 glViewport (0, y, (GLint) width, (GLint) height);
200 glMatrixMode(GL_PROJECTION);
203 gluPerspective (30.0, 1/h, 1.0, 100.0);
204 glMatrixMode(GL_MODELVIEW);
206 gluLookAt( 0.0, 0.0, 30.0,
210 # ifdef HAVE_MOBILE /* Keep it the same relative size when rotated. */
212 int o = (int) current_device_rotation();
213 if (o != 0 && o != 180 && o != -180)
214 glScalef (1/h, 1/h, 1/h);
218 glClear(GL_COLOR_BUFFER_BIT);
224 Adapted from ../hacks/interference.c by Hannu Mallat.
228 init_wave (ModeInfo *mi)
230 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
233 cc->waves = ww = (waves *) calloc (sizeof(*cc->waves), 1);
234 ww->nwaves = wave_count;
235 ww->radius = wave_radius;
236 ww->speed = wave_speed;
237 ww->heights = (int *) calloc (sizeof(*ww->heights), ww->radius);
238 ww->srcs = (wave_src *) calloc (sizeof(*ww->srcs), ww->nwaves);
240 for (i = 0; i < ww->radius; i++)
242 float max = (cc->ncolors * (ww->radius - i) / (float) ww->radius);
243 ww->heights[i] = ((max + max * cos(i / 50.0)) / 2.0);
246 for (i = 0; i < ww->nwaves; i++)
248 ww->srcs[i].xth = frand(2.0) * M_PI;
249 ww->srcs[i].yth = frand(2.0) * M_PI;
254 interference (ModeInfo *mi)
256 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
257 waves *ww = cc->waves;
260 /* Move the wave origins around
262 for (i = 0; i < ww->nwaves; i++)
264 ww->srcs[i].xth += (ww->speed / 1000.0);
265 if (ww->srcs[i].xth > 2*M_PI)
266 ww->srcs[i].xth -= 2*M_PI;
267 ww->srcs[i].yth += (ww->speed / 1000.0);
268 if (ww->srcs[i].yth > 2*M_PI)
269 ww->srcs[i].yth -= 2*M_PI;
271 ww->srcs[i].x = (cc->texture_width/2 +
272 (cos (ww->srcs[i].xth) *
273 cc->texture_width / 2));
274 ww->srcs[i].y = (cc->texture_height/2 +
275 (cos (ww->srcs[i].yth) *
276 cc->texture_height / 2));
279 /* Compute the effect of the waves on each pixel,
280 and generate the output map.
282 for (y = 0; y < cc->texture_height; y++)
283 for (x = 0; x < cc->texture_width; x++)
287 for (i = 0; i < ww->nwaves; i++)
289 int dx = x - ww->srcs[i].x;
290 int dy = y - ww->srcs[i].y;
291 int dist = sqrt (dx*dx + dy*dy);
292 result += (dist > ww->radius ? 0 : ww->heights[dist]);
294 result %= cc->ncolors;
296 o = cc->texture + (((y * cc->texture_width) + x) << 2);
297 o[0] = (cc->texture_colors[result].red >> 8);
298 o[1] = (cc->texture_colors[result].green >> 8);
299 o[2] = (cc->texture_colors[result].blue >> 8);
309 init_texture (ModeInfo *mi)
311 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
314 glEnable(GL_TEXTURE_2D);
317 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
318 glGenTextures (1, &cc->texture_id);
319 glBindTexture (GL_TEXTURE_2D, cc->texture_id);
320 check_gl_error("texture binding");
322 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
323 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
324 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
325 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
326 glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
327 check_gl_error("texture initialization");
329 cc->texture_width = texture_size;
330 cc->texture_height = texture_size;
332 i = texture_size * texture_size * 4;
333 cc->texture = (unsigned char *) malloc (i);
334 memset (cc->texture, 0xFF, i);
339 shuffle_texture (ModeInfo *mi)
341 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
344 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA,
345 cc->texture_width, cc->texture_height, 0,
346 GL_RGBA, GL_UNSIGNED_BYTE,
348 check_gl_error("texture");
353 reset_colors (ModeInfo *mi)
355 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
356 double H[3], S[3], V[3];
359 H[1] = ((H[0] + shift) < 360) ? (H[0]+shift) : (H[0] + shift - 360);
360 H[2] = ((H[1] + shift) < 360) ? (H[1]+shift) : (H[1] + shift - 360);
361 S[0] = S[1] = S[2] = 1.0;
362 V[0] = V[1] = V[2] = 1.0;
363 make_color_loop(0, 0, 0,
367 cc->texture_colors, &cc->ncolors,
370 make_smooth_colormap (0, 0, 0,
371 cc->cube_colors, &cc->ncolors,
377 cube_handle_event (ModeInfo *mi, XEvent *event)
379 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
381 if (gltrackball_event_handler (event, cc->trackball,
382 MI_WIDTH (mi), MI_HEIGHT (mi),
385 else if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
396 init_cube (ModeInfo *mi)
399 cube_configuration *cc;
400 int wire = MI_IS_WIREFRAME(mi);
404 cc = &ccs[MI_SCREEN(mi)];
406 if ((cc->glx_context = init_GL(mi)) != NULL) {
407 reshape_cube (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
412 static const GLfloat pos[4] = {1.0, 0.5, 1.0, 0.0};
413 static const GLfloat amb[4] = {0.2, 0.2, 0.2, 1.0};
414 static const GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
416 glLightfv(GL_LIGHT0, GL_POSITION, pos);
417 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
418 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
420 glEnable(GL_LIGHTING);
422 glEnable(GL_DEPTH_TEST);
423 glEnable(GL_CULL_FACE);
428 Bool spinx=False, spiny=False, spinz=False;
429 double spin_speed = 1.0;
430 double wander_speed = 0.05;
435 if (*s == 'x' || *s == 'X') spinx = True;
436 else if (*s == 'y' || *s == 'Y') spiny = True;
437 else if (*s == 'z' || *s == 'Z') spinz = True;
438 else if (*s == '0') ;
442 "%s: spin must contain only the characters X, Y, or Z (not \"%s\")\n",
449 cc->rot = make_rotator (spinx ? spin_speed : 0,
450 spiny ? spin_speed : 0,
451 spinz ? spin_speed : 0,
453 do_wander ? wander_speed : 0,
454 (spinx && spiny && spinz));
455 cc->trackball = gltrackball_init (True);
459 cc->texture_colors = (XColor *) calloc(cc->ncolors, sizeof(XColor));
460 cc->cube_colors = (XColor *) calloc(cc->ncolors, sizeof(XColor));
464 cc->ncubes = MI_COUNT (mi);
465 cc->cubes = (cube *) calloc (sizeof(cube), cc->ncubes);
466 for (i = 0; i < cc->ncubes; i++)
468 cube *cube = &cc->cubes[i];
469 cube->color = random() % cc->ncolors;
473 cube->dx = frand(0.1);
474 cube->dy = frand(0.1);
475 cube->dz = frand(0.1);
476 cube->dw = frand(0.1);
477 cube->dh = frand(0.1);
478 cube->dd = frand(0.1);
488 shuffle_texture (mi);
491 cc->cube_list = glGenLists (1);
492 glNewList (cc->cube_list, GL_COMPILE);
493 cc->cube_polys = unit_cube (wire);
499 shuffle_cubes (ModeInfo *mi)
501 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
503 for (i = 0; i < cc->ncubes; i++)
505 # define SINOID(SCALE,FRAME,SIZE) \
506 ((((1 + sin((FRAME * (SCALE)) / 2 * M_PI)) / 2.0) * (SIZE)) - (SIZE)/2)
508 cube *cube = &cc->cubes[i];
509 cube->x = SINOID(cube->dx, cube->frame, 0.5);
510 cube->y = SINOID(cube->dy, cube->frame, 0.5);
511 cube->z = SINOID(cube->dz, cube->frame, 0.5);
512 cube->w = SINOID(cube->dw, cube->frame, 0.9) + 1.0;
513 cube->h = SINOID(cube->dh, cube->frame, 0.9) + 1.0;
514 cube->d = SINOID(cube->dd, cube->frame, 0.9) + 1.0;
522 draw_cube (ModeInfo *mi)
524 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
525 Display *dpy = MI_DISPLAY(mi);
526 Window window = MI_WINDOW(mi);
529 if (!cc->glx_context)
532 mi->polygon_count = 0;
533 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cc->glx_context));
535 glShadeModel(GL_FLAT);
537 glEnable(GL_DEPTH_TEST);
538 glEnable(GL_NORMALIZE);
539 glEnable(GL_CULL_FACE);
541 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
545 glScalef(1.1, 1.1, 1.1);
549 get_position (cc->rot, &x, &y, &z, !cc->button_down_p);
550 glTranslatef((x - 0.5) * 8,
554 gltrackball_rotate (cc->trackball);
556 get_rotation (cc->rot, &x, &y, &z, !cc->button_down_p);
557 glRotatef (x * 360, 1.0, 0.0, 0.0);
558 glRotatef (y * 360, 0.0, 1.0, 0.0);
559 glRotatef (z * 360, 0.0, 0.0, 1.0);
562 glScalef (2.5, 2.5, 2.5);
564 for (i = 0; i < cc->ncubes; i++)
566 cube *cube = &cc->cubes[i];
568 color[0] = cc->cube_colors[cube->color].red / 65536.0;
569 color[1] = cc->cube_colors[cube->color].green / 65536.0;
570 color[2] = cc->cube_colors[cube->color].blue / 65536.0;
573 if (cube->color >= cc->ncolors) cube->color = 0;
576 glTranslatef (cube->x, cube->y, cube->z);
577 glScalef (cube->w, cube->h, cube->d);
578 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
579 glCallList (cc->cube_list);
580 mi->polygon_count += cc->cube_polys;
586 shuffle_texture (mi);
590 if (mi->fps_p) do_fps (mi);
593 glXSwapBuffers(dpy, window);
596 XSCREENSAVER_MODULE_2 ("Cubenetic", cubenetic, cube)