1 /* rubikblocks, Copyright (c) 2009 Vasek Potocek <vasek.potocek@post.cz>
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 /* RubikBlocks - a Rubik's Mirror Blocks puzzle introduced in 2008.
13 * No mirrors in this version, though, hence the altered name.
17 * add reflection to the faces
20 #define DEFAULTS "*delay: 20000 \n" \
21 "*showFPS: False \n" \
22 "*wireframe: False \n" \
23 "*suppressRotationAnimation: True\n" \
25 # define free_rubikblocks 0
26 # define release_rubikblocks 0
27 #include "xlockmore.h"
29 #include "gltrackball.h"
33 #define DEF_SPIN "True"
34 #define DEF_WANDER "True"
35 #define DEF_TEXTURE "True"
36 #define DEF_RANDOMIZE "False"
37 #define DEF_SPINSPEED "0.1"
38 #define DEF_ROTSPEED "3.0"
39 #define DEF_WANDERSPEED "0.005"
40 #define DEF_WAIT "40.0"
41 #define DEF_CUBESIZE "1.0"
48 #define BORDER2 (BORDER*BORDER)
51 #define countof(x) (sizeof((x))/sizeof((*x)))
53 #define rnd01() ((int)(random()%2))
55 /*************************************************************************/
57 static Bool spin, wander, rndstart, tex;
58 static float spinspeed, tspeed, wspeed, twait, size;
60 static argtype vars[] = {
61 { &spin, "spin", "Spin", DEF_SPIN, t_Bool},
62 { &wander, "wander", "Wander", DEF_WANDER, t_Bool},
63 { &rndstart, "randomize", "Randomize", DEF_RANDOMIZE, t_Bool},
64 { &tex, "texture", "Texture", DEF_TEXTURE, t_Bool},
65 { &spinspeed, "spinspeed", "SpinSpeed", DEF_SPINSPEED, t_Float},
66 { &tspeed, "rotspeed", "RotSpeed", DEF_ROTSPEED, t_Float},
67 { &wspeed, "wanderspeed", "WanderSpeed", DEF_WANDERSPEED, t_Float},
68 { &twait, "wait", "Wait", DEF_WAIT, t_Float},
69 { &size, "cubesize", "CubeSize", DEF_CUBESIZE, t_Float},
72 static XrmOptionDescRec opts[] = {
73 { "-spin", ".spin", XrmoptionNoArg, "True" },
74 { "+spin", ".spin", XrmoptionNoArg, "False" },
75 { "-wander", ".wander", XrmoptionNoArg, "True" },
76 { "+wander", ".wander", XrmoptionNoArg, "False" },
77 { "-randomize", ".randomize", XrmoptionNoArg, "True" },
78 { "+randomize", ".randomize", XrmoptionNoArg, "False" },
79 { "-texture", ".texture", XrmoptionNoArg, "True" },
80 { "+texture", ".texture", XrmoptionNoArg, "False" },
81 { "-spinspeed", ".spinspeed", XrmoptionSepArg, 0 },
82 { "-wanderspeed", ".wanderspeed", XrmoptionSepArg, 0 },
83 { "-rotspeed", ".rotspeed", XrmoptionSepArg, 0 },
84 { "-wait", ".wait", XrmoptionSepArg, 0 },
85 { "-cubesize", ".cubesize", XrmoptionSepArg, 0 },
88 ENTRYPOINT ModeSpecOpt rubikblocks_opts = {countof(opts), opts, countof(vars), vars, NULL};
91 ModStruct rubikblocks_description =
92 { "rubikblocks", "init_rubikblocks", "draw_rubikblocks", NULL,
93 "draw_rubikblocks", "change_rubikblocks", NULL, &rubikblocks_opts,
94 25000, 1, 1, 1, 1.0, 4, "",
95 "Shows randomly shuffling Rubik's Mirror Blocks puzzle", 0, NULL
100 float pos[3]; /* _original_ position */
101 float qr[4]; /* quaternion of rotation */
102 Bool act; /* flag if it is undergoing the current rotation */
106 GLXContext *glx_context;
108 trackball_state *trackball;
112 Bool pause; /* pause between two rotations */
113 float qfram[4]; /* quaternion describing the rotation in one anim. frame */
114 GLfloat t, tmax; /* rotation clock */
115 piece_t pieces[27]; /* type and tilt of all the pieces */
117 unsigned char texture[TEX_HEIGHT][TEX_WIDTH];
122 static rubikblocks_conf *rubikblocks = NULL;
124 static const GLfloat shininess = 20.0;
125 static const GLfloat ambient[] = {0.0, 0.0, 0.0, 1.0};
126 static const GLfloat diffuse[] = {1.0, 1.0, 1.0, 1.0};
127 static const GLfloat position0[] = {1.0, 1.0, 1.0, 0.0};
128 static const GLfloat position1[] = {-1.0, -1.0, 1.0, 0.0};
129 static const GLfloat lmodel_ambient[] = {0.1, 0.1, 0.1, 1.0};
130 static const GLfloat material_ambient[] = {0.7, 0.7, 0.7, 1.0};
131 static const GLfloat material_diffuse[] = {0.7, 0.7, 0.7, 1.0};
132 static const GLfloat material_specular[] = {0.2, 0.2, 0.2, 1.0};
134 /*************************************************************************/
136 /* Multiplies two quaternions, src*dest, and stores the result in dest. */
138 mult_quat(float src[4], float dest[4])
141 r = src[0]*dest[0] - src[1]*dest[1] - src[2]*dest[2] - src[3]*dest[3];
142 i = src[0]*dest[1] + src[1]*dest[0] + src[2]*dest[3] - src[3]*dest[2];
143 j = src[0]*dest[2] + src[2]*dest[0] + src[3]*dest[1] - src[1]*dest[3];
144 k = src[0]*dest[3] + src[3]*dest[0] + src[1]*dest[2] - src[2]*dest[1];
151 /* Sets the 'act' flag for pieces which will undergo the rotation. */
153 flag_pieces(piece_t pieces[27], int axis, int side)
157 for(i = 0; i < 27; i++)
160 q[1] = pieces[i].pos[0];
161 q[2] = pieces[i].pos[1];
162 q[3] = pieces[i].pos[2];
163 mult_quat(pieces[i].qr, q);
164 for(j = 1; j < 4; j++)
166 mult_quat(pieces[i].qr, q);
167 for(j = 1; j < 4; j++)
169 if(fabs(q[axis] - side) < 0.1)
170 pieces[i].act = True;
172 pieces[i].act = False;
176 /* "Rounds" the value to the nearest from the set {0, +-1/2, +-1/sqrt(2), +-1}.
177 * It is guaranteed to be pretty close to one when this function is called. */
179 settle_value(float v)
181 if(v > 0.9) return 1;
182 else if(v < -0.9) return -1;
183 else if(v > 0.6) return M_SQRT1_2;
184 else if(v < -0.6) return -M_SQRT1_2;
185 else if(v > 0.4) return 0.5;
186 else if(v < -0.4) return -0.5;
191 randomize(rubikblocks_conf *cp)
195 for(i = 0; i < SHUFFLE; i++)
197 axis = (random()%3)+1;
199 flag_pieces(cp->pieces, axis, side);
200 for(j = 1; j < 4; j++)
202 cp->qfram[0] = M_SQRT1_2;
203 cp->qfram[axis] = M_SQRT1_2;
204 for(j = 0; j < 27; j++)
206 if(cp->pieces[j].act)
207 mult_quat(cp->qfram, cp->pieces[j].qr);
213 finish(rubikblocks_conf *cp)
233 flag_pieces(cp->pieces, axis, side);
235 cp->tmax = 90.0*angle;
236 for(i = 1; i < 4; i++)
238 cp->qfram[0] = cos(tspeed*M_PI/360);
239 cp->qfram[axis] = sin((rnd01()*2-1)*tspeed*M_PI/360);
243 for(i = 0; i < 27; i++)
245 for(j = 0; j < 4; j++)
247 cp->pieces[i].qr[j] = settle_value(cp->pieces[i].qr[j]);
257 draw_main(ModeInfo *mi, rubikblocks_conf *cp)
262 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
265 get_position(cp->rot, &x, &y, &z, !cp->button_down);
266 glTranslatef((x-0.5)*6, (y-0.5)*6, -20);
268 gltrackball_rotate(cp->trackball);
270 get_rotation(cp->rot, &x, &y, &z, !cp->button_down);
271 glRotatef(x*360, 1, 0, 0);
272 glRotatef(y*360, 0, 1, 0);
273 glRotatef(z*360, 0, 0, 1);
274 glScalef(size, size, size);
276 # ifdef HAVE_MOBILE /* Keep it the same relative size when rotated. */
278 GLfloat h = MI_HEIGHT(mi) / (GLfloat) MI_WIDTH(mi);
279 int o = (int) current_device_rotation();
280 if (o != 0 && o != 180 && o != -180)
281 glScalef (1/h, 1/h, 1/h);
285 if(cp->wire) glColor3f(0.7, 0.7, 0.7);
287 for(i = 0; i < 27; i++)
288 if(cp->pieces[i].act)
289 mult_quat(cp->qfram, cp->pieces[i].qr);
290 for(i = 0; i < 27; i++)
293 if(fabs(cp->pieces[i].qr[0]) < 1)
294 glRotatef(360/M_PI*acos(cp->pieces[i].qr[0]),
295 cp->pieces[i].qr[1], cp->pieces[i].qr[2], cp->pieces[i].qr[3]);
296 glCallList(cp->list_base + i);
299 if((cp->t += tspeed) > cp->tmax) finish(cp);
304 draw_horz_line(rubikblocks_conf *cp, int x1, int x2, int y)
307 if(y < BORDER) y = -y;
309 for(; y < BORDER; y++) {
310 if(y0+y >= TEX_HEIGHT) break;
312 for(x = x1; x <= x2; x++)
313 if(cp->texture[y0+y][x]>w) cp->texture[y0+y][x] = w;
318 draw_vert_line(rubikblocks_conf *cp, int x, int y1, int y2)
323 for(; x < BORDER; x++) {
324 if(x0+x >= TEX_WIDTH) break;
326 for(y = y1; y <= y2; y++)
327 if(cp->texture[y][x0+x]>w) cp->texture[y][x0+x] = w;
332 make_texture(rubikblocks_conf *cp)
335 for(y = 0; y < TEX_HEIGHT; y++)
336 for(x = 0; x < TEX_WIDTH; x++)
337 cp->texture[y][x] = 255;
338 draw_horz_line(cp, 0, TEX_WIDTH-1, 0);
339 draw_horz_line(cp, 0, TEX_WIDTH-1, TEX_HEIGHT-1);
340 draw_vert_line(cp, 0, 0, TEX_HEIGHT-1);
341 draw_vert_line(cp, TEX_WIDTH-1, 0, TEX_HEIGHT-1);
344 /* These simple transforms make the actual shape of the pieces. The parameters
345 * A, B and C affect the excentricity of the pieces in each direction. */
350 if(x > 1.4) return 1.5 - A;
351 else if(x < -1.4) return -1.5 - A;
358 const float B = 0.25;
359 if(y > 1.4) return 1.5 - B;
360 else if(y < -1.4) return -1.5 - B;
368 if(z > 1.4) return 1.5 - C;
369 else if(z < -1.4) return -1.5 - C;
374 init_lists(rubikblocks_conf *cp)
379 base = cp->list_base = glGenLists(27);
380 for(i = 0; i < 27; i++)
382 x = cp->pieces[i].pos[0];
383 y = cp->pieces[i].pos[1];
384 z = cp->pieces[i].pos[2];
385 glNewList(base+i, GL_COMPILE);
386 glBegin(GL_QUAD_STRIP);
389 glVertex3f(fx(x+0.5), fy(y-0.5), fz(z-0.5));
391 glVertex3f(fx(x+0.5), fy(y+0.5), fz(z-0.5));
393 glVertex3f(fx(x+0.5), fy(y-0.5), fz(z+0.5));
395 glVertex3f(fx(x+0.5), fy(y+0.5), fz(z+0.5));
398 glVertex3f(fx(x-0.5), fy(y-0.5), fz(z+0.5));
400 glVertex3f(fx(x-0.5), fy(y+0.5), fz(z+0.5));
401 glNormal3f(-1, 0, 0);
403 glVertex3f(fx(x-0.5), fy(y-0.5), fz(z-0.5));
405 glVertex3f(fx(x-0.5), fy(y+0.5), fz(z-0.5));
406 glNormal3f(0, 0, -1);
408 glVertex3f(fx(x+0.5), fy(y-0.5), fz(z-0.5));
410 glVertex3f(fx(x+0.5), fy(y+0.5), fz(z-0.5));
415 glVertex3f(fx(x+0.5), fy(y+0.5), fz(z+0.5));
417 glVertex3f(fx(x+0.5), fy(y+0.5), fz(z-0.5));
419 glVertex3f(fx(x-0.5), fy(y+0.5), fz(z-0.5));
421 glVertex3f(fx(x-0.5), fy(y+0.5), fz(z+0.5));
422 glNormal3f(0, -1, 0);
424 glVertex3f(fx(x+0.5), fy(y-0.5), fz(z-0.5));
426 glVertex3f(fx(x+0.5), fy(y-0.5), fz(z+0.5));
428 glVertex3f(fx(x-0.5), fy(y-0.5), fz(z+0.5));
430 glVertex3f(fx(x-0.5), fy(y-0.5), fz(z-0.5));
436 /* It looks terrible... FIXME: any other ideas, maybe some anisotropic filtering? */
440 init_gl(ModeInfo *mi)
442 rubikblocks_conf *cp = &rubikblocks[MI_SCREEN(mi)];
446 cp->wire = MI_IS_WIREFRAME(mi);
448 # ifdef HAVE_JWZGLES /* #### glPolygonMode other than GL_FILL unimplemented */
455 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
460 glDrawBuffer(GL_BACK);
461 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
462 glShadeModel(GL_FLAT);
463 glDepthFunc(GL_LESS);
464 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
465 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
466 glLightfv(GL_LIGHT0, GL_POSITION, position0);
467 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
468 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
469 glLightfv(GL_LIGHT1, GL_POSITION, position1);
470 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
471 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
472 glEnable(GL_DEPTH_TEST);
475 glEnable(GL_LIGHTING);
476 glEnable(GL_NORMALIZE);
477 glEnable(GL_COLOR_MATERIAL);
478 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material_ambient);
479 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
480 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_specular);
481 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
483 glEnable(GL_TEXTURE_2D);
486 status = gluBuild2DMipmaps(GL_TEXTURE_2D, 1, TEX_WIDTH, TEX_HEIGHT,
487 GL_LUMINANCE, GL_UNSIGNED_BYTE, cp->texture);
489 const char *s = (char *)gluErrorString(status);
490 fprintf (stderr, "%s: error mipmapping texture: %s\n", progname, (s?s:"(unknown)"));
493 check_gl_error("mipmapping");
495 glTexImage2D(GL_TEXTURE_2D, 0, 1, TEX_WIDTH, TEX_HEIGHT,
496 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, cp->texture);
498 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
499 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
500 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
501 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
503 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
505 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
510 init_cp(rubikblocks_conf *cp)
518 for(i = -1, m = 0; i <= 1; i++)
519 for(j = -1; j <= 1; j++)
520 for(k = -1; k <= 1; k++)
522 cp->pieces[m].pos[0] = k;
523 cp->pieces[m].pos[1] = j;
524 cp->pieces[m].pos[2] = i;
525 cp->pieces[m].qr[0] = 1;
526 cp->pieces[m].qr[1] = 0;
527 cp->pieces[m].qr[2] = 0;
528 cp->pieces[m].qr[3] = 0;
532 cp->rot = make_rotator(spin?spinspeed:0, spin?spinspeed:0, spin?spinspeed:0,
533 0.1, wander?wspeed:0, True);
534 cp->trackball = gltrackball_init(True);
536 if(rndstart) randomize(cp);
539 /*************************************************************************/
542 reshape_rubikblocks(ModeInfo *mi, int width, int height)
544 rubikblocks_conf *cp = &rubikblocks[MI_SCREEN(mi)];
547 if (width > height * 5) { /* tiny window: show middle */
551 if(!height) height = 1;
552 cp->ratio = (GLfloat)width/(GLfloat)height;
553 glViewport(0, y, (GLint) width, (GLint) height);
554 glMatrixMode(GL_PROJECTION);
556 gluPerspective(30.0, cp->ratio, 1.0, 100.0);
557 glMatrixMode(GL_MODELVIEW);
558 glClear(GL_COLOR_BUFFER_BIT);
562 init_rubikblocks(ModeInfo *mi)
564 rubikblocks_conf *cp;
565 MI_INIT(mi, rubikblocks);
566 cp = &rubikblocks[MI_SCREEN(mi)];
571 if ((cp->glx_context = init_GL(mi)) != NULL)
576 reshape_rubikblocks(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
585 draw_rubikblocks(ModeInfo * mi)
587 Display *display = MI_DISPLAY(mi);
588 Window window = MI_WINDOW(mi);
589 rubikblocks_conf *cp;
590 if (!rubikblocks) return;
591 cp = &rubikblocks[MI_SCREEN(mi)];
592 MI_IS_DRAWN(mi) = True;
593 if (!cp->glx_context) return;
594 mi->polygon_count = 0;
595 glXMakeCurrent(display, window, *(cp->glx_context));
596 if (!draw_main(mi, cp))
601 if (MI_IS_FPS(mi)) do_fps (mi);
603 glXSwapBuffers(display, window);
608 change_rubikblocks(ModeInfo * mi)
610 rubikblocks_conf *cp = &rubikblocks[MI_SCREEN(mi)];
611 if (!cp->glx_context) return;
612 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cp->glx_context));
615 #endif /* !STANDALONE */
618 rubikblocks_handle_event (ModeInfo *mi, XEvent *event)
620 rubikblocks_conf *cp = &rubikblocks[MI_SCREEN(mi)];
622 if (gltrackball_event_handler (event, cp->trackball,
623 MI_WIDTH (mi), MI_HEIGHT (mi),
631 XSCREENSAVER_MODULE ("RubikBlocks", rubikblocks)