1 /* cubenetic, Copyright (c) 2002-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 "Cubenetic"
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 ccs_opts xlockmore_opts
24 #define DEF_SPIN "XYZ"
25 #define DEF_WANDER "True"
26 #define DEF_TEXTURE "True"
28 #define DEF_WAVE_COUNT "3"
29 #define DEF_WAVE_SPEED "80"
30 #define DEF_WAVE_RADIUS "512"
32 #define DEFAULTS "*delay: 20000 \n" \
34 "*showFPS: False \n" \
35 "*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 */
74 GLXContext *glx_context;
76 trackball_state *trackball;
85 int texture_width, texture_height;
86 unsigned char *texture;
90 XColor *texture_colors;
94 static cube_configuration *ccs = NULL;
97 static Bool do_wander;
98 static Bool do_texture;
100 static int wave_count;
101 static int wave_speed;
102 static int wave_radius;
103 static int texture_size = 256;
105 static XrmOptionDescRec opts[] = {
106 { "-spin", ".spin", XrmoptionSepArg, 0 },
107 { "+spin", ".spin", XrmoptionNoArg, "" },
108 { "-wander", ".wander", XrmoptionNoArg, "True" },
109 { "+wander", ".wander", XrmoptionNoArg, "False" },
110 {"-texture", ".texture", XrmoptionNoArg, "true" },
111 {"+texture", ".texture", XrmoptionNoArg, "false" },
112 {"-waves", ".waves", XrmoptionSepArg, 0 },
113 {"-wave-speed", ".waveSpeed", XrmoptionSepArg, 0 },
114 {"-wave-radius", ".waveRadius", XrmoptionSepArg, 0 },
117 static argtype vars[] = {
118 {&do_spin, "spin", "Spin", DEF_SPIN, t_String},
119 {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
120 {&do_texture, "texture", "Texture", DEF_TEXTURE, t_Bool},
121 {&wave_count, "waves", "Waves", DEF_WAVE_COUNT, t_Int},
122 {&wave_speed, "waveSpeed", "WaveSpeed", DEF_WAVE_SPEED, t_Int},
123 {&wave_radius,"waveRadius","WaveRadius", DEF_WAVE_RADIUS,t_Int},
126 ModeSpecOpt ccs_opts = {countof(opts), opts, countof(vars), vars, NULL};
130 unit_cube (Bool wire)
132 glBegin (wire ? GL_LINE_LOOP : GL_QUADS); /* front */
133 glNormal3f (0, 0, 1);
134 glTexCoord2f(1, 0); glVertex3f ( 0.5, -0.5, 0.5);
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);
140 glBegin (wire ? GL_LINE_LOOP : GL_QUADS); /* back */
141 glNormal3f (0, 0, -1);
142 glTexCoord2f(0, 0); glVertex3f (-0.5, -0.5, -0.5);
143 glTexCoord2f(0, 1); glVertex3f (-0.5, 0.5, -0.5);
144 glTexCoord2f(1, 1); glVertex3f ( 0.5, 0.5, -0.5);
145 glTexCoord2f(1, 0); glVertex3f ( 0.5, -0.5, -0.5);
148 glBegin (wire ? GL_LINE_LOOP : GL_QUADS); /* left */
149 glNormal3f (-1, 0, 0);
150 glTexCoord2f(1, 1); glVertex3f (-0.5, 0.5, 0.5);
151 glTexCoord2f(1, 0); glVertex3f (-0.5, 0.5, -0.5);
152 glTexCoord2f(0, 0); glVertex3f (-0.5, -0.5, -0.5);
153 glTexCoord2f(0, 1); glVertex3f (-0.5, -0.5, 0.5);
156 glBegin (wire ? GL_LINE_LOOP : GL_QUADS); /* right */
157 glNormal3f (1, 0, 0);
158 glTexCoord2f(1, 1); glVertex3f ( 0.5, -0.5, -0.5);
159 glTexCoord2f(1, 0); glVertex3f ( 0.5, 0.5, -0.5);
160 glTexCoord2f(0, 0); glVertex3f ( 0.5, 0.5, 0.5);
161 glTexCoord2f(0, 1); glVertex3f ( 0.5, -0.5, 0.5);
166 glBegin (wire ? GL_LINE_LOOP : GL_QUADS); /* top */
167 glNormal3f (0, 1, 0);
168 glTexCoord2f(0, 0); glVertex3f ( 0.5, 0.5, 0.5);
169 glTexCoord2f(0, 1); glVertex3f ( 0.5, 0.5, -0.5);
170 glTexCoord2f(1, 1); glVertex3f (-0.5, 0.5, -0.5);
171 glTexCoord2f(1, 0); glVertex3f (-0.5, 0.5, 0.5);
174 glBegin (wire ? GL_LINE_LOOP : GL_QUADS); /* bottom */
175 glNormal3f (0, -1, 0);
176 glTexCoord2f(1, 0); glVertex3f (-0.5, -0.5, 0.5);
177 glTexCoord2f(0, 0); glVertex3f (-0.5, -0.5, -0.5);
178 glTexCoord2f(0, 1); glVertex3f ( 0.5, -0.5, -0.5);
179 glTexCoord2f(1, 1); glVertex3f ( 0.5, -0.5, 0.5);
185 /* Window management, etc
188 reshape_cube (ModeInfo *mi, int width, int height)
190 GLfloat h = (GLfloat) height / (GLfloat) width;
192 glViewport (0, 0, (GLint) width, (GLint) height);
194 glMatrixMode(GL_PROJECTION);
197 gluPerspective (30.0, 1/h, 1.0, 100.0);
198 glMatrixMode(GL_MODELVIEW);
200 gluLookAt( 0.0, 0.0, 30.0,
204 glClear(GL_COLOR_BUFFER_BIT);
210 Adapted from ../hacks/interference.c by Hannu Mallat.
214 init_wave (ModeInfo *mi)
216 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
219 cc->waves = ww = (waves *) calloc (sizeof(*cc->waves), 1);
220 ww->nwaves = wave_count;
221 ww->radius = wave_radius;
222 ww->speed = wave_speed;
223 ww->heights = (int *) calloc (sizeof(*ww->heights), ww->radius);
224 ww->srcs = (wave_src *) calloc (sizeof(*ww->srcs), ww->nwaves);
226 for (i = 0; i < ww->radius; i++)
228 float max = (cc->ncolors * (ww->radius - i) / (float) ww->radius);
229 ww->heights[i] = ((max + max * cos(i / 50.0)) / 2.0);
232 for (i = 0; i < ww->nwaves; i++)
234 ww->srcs[i].xth = frand(2.0) * M_PI;
235 ww->srcs[i].yth = frand(2.0) * M_PI;
240 interference (ModeInfo *mi)
242 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
243 waves *ww = cc->waves;
246 /* Move the wave origins around
248 for (i = 0; i < ww->nwaves; i++)
250 ww->srcs[i].xth += (ww->speed / 1000.0);
251 if (ww->srcs[i].xth > 2*M_PI)
252 ww->srcs[i].xth -= 2*M_PI;
253 ww->srcs[i].yth += (ww->speed / 1000.0);
254 if (ww->srcs[i].yth > 2*M_PI)
255 ww->srcs[i].yth -= 2*M_PI;
257 ww->srcs[i].x = (cc->texture_width/2 +
258 (cos (ww->srcs[i].xth) *
259 cc->texture_width / 2));
260 ww->srcs[i].y = (cc->texture_height/2 +
261 (cos (ww->srcs[i].yth) *
262 cc->texture_height / 2));
265 /* Compute the effect of the waves on each pixel,
266 and generate the output map.
268 for (y = 0; y < cc->texture_height; y++)
269 for (x = 0; x < cc->texture_width; x++)
273 for (i = 0; i < ww->nwaves; i++)
275 int dx = x - ww->srcs[i].x;
276 int dy = y - ww->srcs[i].y;
277 int dist = sqrt (dx*dx + dy*dy);
278 result += (dist > ww->radius ? 0 : ww->heights[dist]);
280 result %= cc->ncolors;
282 o = cc->texture + (((y * cc->texture_width) + x) << 2);
283 o[0] = (cc->texture_colors[result].red >> 8);
284 o[1] = (cc->texture_colors[result].green >> 8);
285 o[2] = (cc->texture_colors[result].blue >> 8);
295 init_texture (ModeInfo *mi)
297 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
300 glEnable(GL_TEXTURE_2D);
303 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
304 glGenTextures (1, &cc->texture_id);
305 glBindTexture (GL_TEXTURE_2D, cc->texture_id);
306 check_gl_error("texture binding");
308 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
309 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
310 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
311 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
312 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
313 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
314 glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
315 check_gl_error("texture initialization");
317 cc->texture_width = texture_size;
318 cc->texture_height = texture_size;
320 i = texture_size * texture_size * 4;
321 cc->texture = (unsigned char *) malloc (i);
322 memset (cc->texture, 0xFF, i);
327 shuffle_texture (ModeInfo *mi)
329 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
332 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA,
333 cc->texture_width, cc->texture_height, 0,
334 GL_RGBA, GL_UNSIGNED_BYTE,
336 check_gl_error("texture");
341 cube_handle_event (ModeInfo *mi, XEvent *event)
343 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
345 if (event->xany.type == ButtonPress &&
346 event->xbutton.button == Button1)
348 cc->button_down_p = True;
349 gltrackball_start (cc->trackball,
350 event->xbutton.x, event->xbutton.y,
351 MI_WIDTH (mi), MI_HEIGHT (mi));
354 else if (event->xany.type == ButtonRelease &&
355 event->xbutton.button == Button1)
357 cc->button_down_p = False;
360 else if (event->xany.type == ButtonPress &&
361 (event->xbutton.button == Button4 ||
362 event->xbutton.button == Button5))
364 gltrackball_mousewheel (cc->trackball, event->xbutton.button, 10,
365 !!event->xbutton.state);
368 else if (event->xany.type == MotionNotify &&
371 gltrackball_track (cc->trackball,
372 event->xmotion.x, event->xmotion.y,
373 MI_WIDTH (mi), MI_HEIGHT (mi));
382 init_cube (ModeInfo *mi)
385 cube_configuration *cc;
386 int wire = MI_IS_WIREFRAME(mi);
389 ccs = (cube_configuration *)
390 calloc (MI_NUM_SCREENS(mi), sizeof (cube_configuration));
392 fprintf(stderr, "%s: out of memory\n", progname);
397 cc = &ccs[MI_SCREEN(mi)];
399 if ((cc->glx_context = init_GL(mi)) != NULL) {
400 reshape_cube (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
405 static GLfloat pos[4] = {1.0, 0.5, 1.0, 0.0};
406 static GLfloat amb[4] = {0.2, 0.2, 0.2, 1.0};
407 static GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
409 glLightfv(GL_LIGHT0, GL_POSITION, pos);
410 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
411 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
413 glEnable(GL_LIGHTING);
415 glEnable(GL_DEPTH_TEST);
416 glEnable(GL_CULL_FACE);
421 Bool spinx=False, spiny=False, spinz=False;
422 double spin_speed = 1.0;
423 double wander_speed = 0.05;
428 if (*s == 'x' || *s == 'X') spinx = True;
429 else if (*s == 'y' || *s == 'Y') spiny = True;
430 else if (*s == 'z' || *s == 'Z') spinz = True;
434 "%s: spin must contain only the characters X, Y, or Z (not \"%s\")\n",
441 cc->rot = make_rotator (spinx ? spin_speed : 0,
442 spiny ? spin_speed : 0,
443 spinz ? spin_speed : 0,
445 do_wander ? wander_speed : 0,
446 (spinx && spiny && spinz));
447 cc->trackball = gltrackball_init ();
451 cc->texture_colors = (XColor *) calloc(cc->ncolors, sizeof(XColor));
452 cc->cube_colors = (XColor *) calloc(cc->ncolors, sizeof(XColor));
455 double H[3], S[3], V[3];
458 H[1] = ((H[0] + shift) < 360) ? (H[0]+shift) : (H[0] + shift - 360);
459 H[2] = ((H[1] + shift) < 360) ? (H[1]+shift) : (H[1] + shift - 360);
460 S[0] = S[1] = S[2] = 1.0;
461 V[0] = V[1] = V[2] = 1.0;
462 make_color_loop(0, 0,
466 cc->texture_colors, &cc->ncolors,
469 make_smooth_colormap (0, 0, 0,
470 cc->cube_colors, &cc->ncolors,
474 cc->ncubes = MI_COUNT (mi);
475 cc->cubes = (cube *) calloc (sizeof(cube), cc->ncubes);
476 for (i = 0; i < cc->ncubes; i++)
478 cube *cube = &cc->cubes[i];
479 cube->color = random() % cc->ncolors;
483 cube->dx = frand(0.1);
484 cube->dy = frand(0.1);
485 cube->dz = frand(0.1);
486 cube->dw = frand(0.1);
487 cube->dh = frand(0.1);
488 cube->dd = frand(0.1);
498 shuffle_texture (mi);
501 cc->cube_list = glGenLists (1);
502 glNewList (cc->cube_list, GL_COMPILE);
509 shuffle_cubes (ModeInfo *mi)
511 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
513 for (i = 0; i < cc->ncubes; i++)
515 # define SINOID(SCALE,FRAME,SIZE) \
516 ((((1 + sin((FRAME * (SCALE)) / 2 * M_PI)) / 2.0) * (SIZE)) - (SIZE)/2)
518 cube *cube = &cc->cubes[i];
519 cube->x = SINOID(cube->dx, cube->frame, 0.5);
520 cube->y = SINOID(cube->dy, cube->frame, 0.5);
521 cube->z = SINOID(cube->dz, cube->frame, 0.5);
522 cube->w = SINOID(cube->dw, cube->frame, 0.9) + 1.0;
523 cube->h = SINOID(cube->dh, cube->frame, 0.9) + 1.0;
524 cube->d = SINOID(cube->dd, cube->frame, 0.9) + 1.0;
532 draw_cube (ModeInfo *mi)
534 cube_configuration *cc = &ccs[MI_SCREEN(mi)];
535 Display *dpy = MI_DISPLAY(mi);
536 Window window = MI_WINDOW(mi);
539 if (!cc->glx_context)
542 glShadeModel(GL_FLAT);
544 glEnable(GL_DEPTH_TEST);
545 glEnable(GL_NORMALIZE);
546 glEnable(GL_CULL_FACE);
548 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
552 glScalef(1.1, 1.1, 1.1);
556 get_position (cc->rot, &x, &y, &z, !cc->button_down_p);
557 glTranslatef((x - 0.5) * 8,
561 gltrackball_rotate (cc->trackball);
563 get_rotation (cc->rot, &x, &y, &z, !cc->button_down_p);
564 glRotatef (x * 360, 1.0, 0.0, 0.0);
565 glRotatef (y * 360, 0.0, 1.0, 0.0);
566 glRotatef (z * 360, 0.0, 0.0, 1.0);
569 glScalef (2.5, 2.5, 2.5);
571 for (i = 0; i < cc->ncubes; i++)
573 cube *cube = &cc->cubes[i];
575 color[0] = cc->cube_colors[cube->color].red / 65536.0;
576 color[1] = cc->cube_colors[cube->color].green / 65536.0;
577 color[2] = cc->cube_colors[cube->color].blue / 65536.0;
580 if (cube->color >= cc->ncolors) cube->color = 0;
583 glTranslatef (cube->x, cube->y, cube->z);
584 glScalef (cube->w, cube->h, cube->d);
585 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
586 glCallList (cc->cube_list);
592 shuffle_texture (mi);
596 if (mi->fps_p) do_fps (mi);
599 glXSwapBuffers(dpy, window);