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 refresh_rubikblocks 0
26 #include "xlockmore.h"
28 #include "gltrackball.h"
32 #define DEF_SPIN "True"
33 #define DEF_WANDER "True"
34 #define DEF_TEXTURE "True"
35 #define DEF_RANDOMIZE "False"
36 #define DEF_SPINSPEED "0.1"
37 #define DEF_ROTSPEED "3.0"
38 #define DEF_WANDERSPEED "0.005"
39 #define DEF_WAIT "40.0"
40 #define DEF_CUBESIZE "1.0"
47 #define BORDER2 (BORDER*BORDER)
50 #define countof(x) (sizeof((x))/sizeof((*x)))
52 #define rnd01() ((int)(random()%2))
54 /*************************************************************************/
56 static Bool spin, wander, rndstart, tex;
57 static float spinspeed, tspeed, wspeed, twait, size;
59 static argtype vars[] = {
60 { &spin, "spin", "Spin", DEF_SPIN, t_Bool},
61 { &wander, "wander", "Wander", DEF_WANDER, t_Bool},
62 { &rndstart, "randomize", "Randomize", DEF_RANDOMIZE, t_Bool},
63 { &tex, "texture", "Texture", DEF_TEXTURE, t_Bool},
64 { &spinspeed, "spinspeed", "SpinSpeed", DEF_SPINSPEED, t_Float},
65 { &tspeed, "rotspeed", "RotSpeed", DEF_ROTSPEED, t_Float},
66 { &wspeed, "wanderspeed", "WanderSpeed", DEF_WANDERSPEED, t_Float},
67 { &twait, "wait", "Wait", DEF_WAIT, t_Float},
68 { &size, "cubesize", "CubeSize", DEF_CUBESIZE, t_Float},
71 static XrmOptionDescRec opts[] = {
72 { "-spin", ".spin", XrmoptionNoArg, "True" },
73 { "+spin", ".spin", XrmoptionNoArg, "False" },
74 { "-wander", ".wander", XrmoptionNoArg, "True" },
75 { "+wander", ".wander", XrmoptionNoArg, "False" },
76 { "-randomize", ".randomize", XrmoptionNoArg, "True" },
77 { "+randomize", ".randomize", XrmoptionNoArg, "False" },
78 { "-texture", ".texture", XrmoptionNoArg, "True" },
79 { "+texture", ".texture", XrmoptionNoArg, "False" },
80 { "-spinspeed", ".spinspeed", XrmoptionSepArg, 0 },
81 { "-wanderspeed", ".wanderspeed", XrmoptionSepArg, 0 },
82 { "-rotspeed", ".rotspeed", XrmoptionSepArg, 0 },
83 { "-wait", ".wait", XrmoptionSepArg, 0 },
84 { "-cubesize", ".cubesize", XrmoptionSepArg, 0 },
87 ENTRYPOINT ModeSpecOpt rubikblocks_opts = {countof(opts), opts, countof(vars), vars, NULL};
90 ModStruct rubikblocks_description =
91 { "rubikblocks", "init_rubikblocks", "draw_rubikblocks", "release_rubikblocks",
92 "draw_rubikblocks", "change_rubikblocks", NULL, &rubikblocks_opts,
93 25000, 1, 1, 1, 1.0, 4, "",
94 "Shows randomly shuffling Rubik's Mirror Blocks puzzle", 0, NULL
99 float pos[3]; /* _original_ position */
100 float qr[4]; /* quaternion of rotation */
101 Bool act; /* flag if it is undergoing the current rotation */
105 GLXContext *glx_context;
107 trackball_state *trackball;
111 Bool pause; /* pause between two rotations */
112 float qfram[4]; /* quaternion describing the rotation in one anim. frame */
113 GLfloat t, tmax; /* rotation clock */
114 piece_t pieces[27]; /* type and tilt of all the pieces */
116 unsigned char texture[TEX_HEIGHT][TEX_WIDTH];
121 static rubikblocks_conf *rubikblocks = NULL;
123 static const GLfloat shininess = 20.0;
124 static const GLfloat ambient[] = {0.0, 0.0, 0.0, 1.0};
125 static const GLfloat diffuse[] = {1.0, 1.0, 1.0, 1.0};
126 static const GLfloat position0[] = {1.0, 1.0, 1.0, 0.0};
127 static const GLfloat position1[] = {-1.0, -1.0, 1.0, 0.0};
128 static const GLfloat lmodel_ambient[] = {0.1, 0.1, 0.1, 1.0};
129 static const GLfloat material_ambient[] = {0.7, 0.7, 0.7, 1.0};
130 static const GLfloat material_diffuse[] = {0.7, 0.7, 0.7, 1.0};
131 static const GLfloat material_specular[] = {0.2, 0.2, 0.2, 1.0};
133 /*************************************************************************/
135 /* Multiplies two quaternions, src*dest, and stores the result in dest. */
137 mult_quat(float src[4], float dest[4])
140 r = src[0]*dest[0] - src[1]*dest[1] - src[2]*dest[2] - src[3]*dest[3];
141 i = src[0]*dest[1] + src[1]*dest[0] + src[2]*dest[3] - src[3]*dest[2];
142 j = src[0]*dest[2] + src[2]*dest[0] + src[3]*dest[1] - src[1]*dest[3];
143 k = src[0]*dest[3] + src[3]*dest[0] + src[1]*dest[2] - src[2]*dest[1];
150 /* Sets the 'act' flag for pieces which will undergo the rotation. */
152 flag_pieces(piece_t pieces[27], int axis, int side)
156 for(i = 0; i < 27; i++)
159 q[1] = pieces[i].pos[0];
160 q[2] = pieces[i].pos[1];
161 q[3] = pieces[i].pos[2];
162 mult_quat(pieces[i].qr, q);
163 for(j = 1; j < 4; j++)
165 mult_quat(pieces[i].qr, q);
166 for(j = 1; j < 4; j++)
168 if(fabs(q[axis] - side) < 0.1)
169 pieces[i].act = True;
171 pieces[i].act = False;
175 /* "Rounds" the value to the nearest from the set {0, +-1/2, +-1/sqrt(2), +-1}.
176 * It is guaranteed to be pretty close to one when this function is called. */
178 settle_value(float v)
180 if(v > 0.9) return 1;
181 else if(v < -0.9) return -1;
182 else if(v > 0.6) return M_SQRT1_2;
183 else if(v < -0.6) return -M_SQRT1_2;
184 else if(v > 0.4) return 0.5;
185 else if(v < -0.4) return -0.5;
190 randomize(rubikblocks_conf *cp)
194 for(i = 0; i < SHUFFLE; i++)
196 axis = (random()%3)+1;
198 flag_pieces(cp->pieces, axis, side);
199 for(j = 1; j < 4; j++)
201 cp->qfram[0] = M_SQRT1_2;
202 cp->qfram[axis] = M_SQRT1_2;
203 for(j = 0; j < 27; j++)
205 if(cp->pieces[j].act)
206 mult_quat(cp->qfram, cp->pieces[j].qr);
212 finish(rubikblocks_conf *cp)
232 flag_pieces(cp->pieces, axis, side);
234 cp->tmax = 90.0*angle;
235 for(i = 1; i < 4; i++)
237 cp->qfram[0] = cos(tspeed*M_PI/360);
238 cp->qfram[axis] = sin((rnd01()*2-1)*tspeed*M_PI/360);
242 for(i = 0; i < 27; i++)
244 for(j = 0; j < 4; j++)
246 cp->pieces[i].qr[j] = settle_value(cp->pieces[i].qr[j]);
256 draw_main(ModeInfo *mi, rubikblocks_conf *cp)
261 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
264 get_position(cp->rot, &x, &y, &z, !cp->button_down);
265 glTranslatef((x-0.5)*6, (y-0.5)*6, -20);
267 gltrackball_rotate(cp->trackball);
269 get_rotation(cp->rot, &x, &y, &z, !cp->button_down);
270 glRotatef(x*360, 1, 0, 0);
271 glRotatef(y*360, 0, 1, 0);
272 glRotatef(z*360, 0, 0, 1);
273 glScalef(size, size, size);
275 # ifdef HAVE_MOBILE /* Keep it the same relative size when rotated. */
277 GLfloat h = MI_HEIGHT(mi) / (GLfloat) MI_WIDTH(mi);
278 int o = (int) current_device_rotation();
279 if (o != 0 && o != 180 && o != -180)
280 glScalef (1/h, 1/h, 1/h);
284 if(cp->wire) glColor3f(0.7, 0.7, 0.7);
286 for(i = 0; i < 27; i++)
287 if(cp->pieces[i].act)
288 mult_quat(cp->qfram, cp->pieces[i].qr);
289 for(i = 0; i < 27; i++)
292 if(fabs(cp->pieces[i].qr[0]) < 1)
293 glRotatef(360/M_PI*acos(cp->pieces[i].qr[0]),
294 cp->pieces[i].qr[1], cp->pieces[i].qr[2], cp->pieces[i].qr[3]);
295 glCallList(cp->list_base + i);
298 if((cp->t += tspeed) > cp->tmax) finish(cp);
303 draw_horz_line(rubikblocks_conf *cp, int x1, int x2, int y)
306 if(y < BORDER) y = -y;
308 for(; y < BORDER; y++) {
309 if(y0+y >= TEX_HEIGHT) break;
311 for(x = x1; x <= x2; x++)
312 if(cp->texture[y0+y][x]>w) cp->texture[y0+y][x] = w;
317 draw_vert_line(rubikblocks_conf *cp, int x, int y1, int y2)
322 for(; x < BORDER; x++) {
323 if(x0+x >= TEX_WIDTH) break;
325 for(y = y1; y <= y2; y++)
326 if(cp->texture[y][x0+x]>w) cp->texture[y][x0+x] = w;
331 make_texture(rubikblocks_conf *cp)
334 for(y = 0; y < TEX_HEIGHT; y++)
335 for(x = 0; x < TEX_WIDTH; x++)
336 cp->texture[y][x] = 255;
337 draw_horz_line(cp, 0, TEX_WIDTH-1, 0);
338 draw_horz_line(cp, 0, TEX_WIDTH-1, TEX_HEIGHT-1);
339 draw_vert_line(cp, 0, 0, TEX_HEIGHT-1);
340 draw_vert_line(cp, TEX_WIDTH-1, 0, TEX_HEIGHT-1);
343 /* These simple transforms make the actual shape of the pieces. The parameters
344 * A, B and C affect the excentricity of the pieces in each direction. */
349 if(x > 1.4) return 1.5 - A;
350 else if(x < -1.4) return -1.5 - A;
357 const float B = 0.25;
358 if(y > 1.4) return 1.5 - B;
359 else if(y < -1.4) return -1.5 - B;
367 if(z > 1.4) return 1.5 - C;
368 else if(z < -1.4) return -1.5 - C;
373 init_lists(rubikblocks_conf *cp)
378 base = cp->list_base = glGenLists(27);
379 for(i = 0; i < 27; i++)
381 x = cp->pieces[i].pos[0];
382 y = cp->pieces[i].pos[1];
383 z = cp->pieces[i].pos[2];
384 glNewList(base+i, GL_COMPILE);
385 glBegin(GL_QUAD_STRIP);
388 glVertex3f(fx(x+0.5), fy(y-0.5), fz(z-0.5));
390 glVertex3f(fx(x+0.5), fy(y+0.5), fz(z-0.5));
392 glVertex3f(fx(x+0.5), fy(y-0.5), fz(z+0.5));
394 glVertex3f(fx(x+0.5), fy(y+0.5), fz(z+0.5));
397 glVertex3f(fx(x-0.5), fy(y-0.5), fz(z+0.5));
399 glVertex3f(fx(x-0.5), fy(y+0.5), fz(z+0.5));
400 glNormal3f(-1, 0, 0);
402 glVertex3f(fx(x-0.5), fy(y-0.5), fz(z-0.5));
404 glVertex3f(fx(x-0.5), fy(y+0.5), fz(z-0.5));
405 glNormal3f(0, 0, -1);
407 glVertex3f(fx(x+0.5), fy(y-0.5), fz(z-0.5));
409 glVertex3f(fx(x+0.5), fy(y+0.5), fz(z-0.5));
414 glVertex3f(fx(x+0.5), fy(y+0.5), fz(z+0.5));
416 glVertex3f(fx(x+0.5), fy(y+0.5), fz(z-0.5));
418 glVertex3f(fx(x-0.5), fy(y+0.5), fz(z-0.5));
420 glVertex3f(fx(x-0.5), fy(y+0.5), fz(z+0.5));
421 glNormal3f(0, -1, 0);
423 glVertex3f(fx(x+0.5), fy(y-0.5), fz(z-0.5));
425 glVertex3f(fx(x+0.5), fy(y-0.5), fz(z+0.5));
427 glVertex3f(fx(x-0.5), fy(y-0.5), fz(z+0.5));
429 glVertex3f(fx(x-0.5), fy(y-0.5), fz(z-0.5));
435 /* It looks terrible... FIXME: any other ideas, maybe some anisotropic filtering? */
439 init_gl(ModeInfo *mi)
441 rubikblocks_conf *cp = &rubikblocks[MI_SCREEN(mi)];
445 cp->wire = MI_IS_WIREFRAME(mi);
447 # ifdef HAVE_JWZGLES /* #### glPolygonMode other than GL_FILL unimplemented */
454 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
459 glDrawBuffer(GL_BACK);
460 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
461 glShadeModel(GL_FLAT);
462 glDepthFunc(GL_LESS);
463 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
464 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
465 glLightfv(GL_LIGHT0, GL_POSITION, position0);
466 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
467 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
468 glLightfv(GL_LIGHT1, GL_POSITION, position1);
469 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
470 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
471 glEnable(GL_DEPTH_TEST);
474 glEnable(GL_LIGHTING);
475 glEnable(GL_NORMALIZE);
476 glEnable(GL_COLOR_MATERIAL);
477 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material_ambient);
478 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
479 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_specular);
480 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
482 glEnable(GL_TEXTURE_2D);
485 status = gluBuild2DMipmaps(GL_TEXTURE_2D, 1, TEX_WIDTH, TEX_HEIGHT,
486 GL_LUMINANCE, GL_UNSIGNED_BYTE, cp->texture);
488 const char *s = (char *)gluErrorString(status);
489 fprintf (stderr, "%s: error mipmapping texture: %s\n", progname, (s?s:"(unknown)"));
492 check_gl_error("mipmapping");
494 glTexImage2D(GL_TEXTURE_2D, 0, 1, TEX_WIDTH, TEX_HEIGHT,
495 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, cp->texture);
497 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
498 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
499 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
500 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
502 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
504 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
509 init_cp(rubikblocks_conf *cp)
517 for(i = -1, m = 0; i <= 1; i++)
518 for(j = -1; j <= 1; j++)
519 for(k = -1; k <= 1; k++)
521 cp->pieces[m].pos[0] = k;
522 cp->pieces[m].pos[1] = j;
523 cp->pieces[m].pos[2] = i;
524 cp->pieces[m].qr[0] = 1;
525 cp->pieces[m].qr[1] = 0;
526 cp->pieces[m].qr[2] = 0;
527 cp->pieces[m].qr[3] = 0;
531 cp->rot = make_rotator(spin?spinspeed:0, spin?spinspeed:0, spin?spinspeed:0,
532 0.1, wander?wspeed:0, True);
533 cp->trackball = gltrackball_init(True);
535 if(rndstart) randomize(cp);
538 /*************************************************************************/
541 reshape_rubikblocks(ModeInfo *mi, int width, int height)
543 rubikblocks_conf *cp = &rubikblocks[MI_SCREEN(mi)];
544 if(!height) height = 1;
545 cp->ratio = (GLfloat)width/(GLfloat)height;
546 glViewport(0, 0, (GLint) width, (GLint) height);
547 glMatrixMode(GL_PROJECTION);
549 gluPerspective(30.0, cp->ratio, 1.0, 100.0);
550 glMatrixMode(GL_MODELVIEW);
551 glClear(GL_COLOR_BUFFER_BIT);
555 release_rubikblocks(ModeInfo *mi)
557 if (rubikblocks != NULL)
560 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
562 rubikblocks_conf *cp = &rubikblocks[screen];
563 if (cp->glx_context) {
564 cp->glx_context = NULL;
567 free((void *)rubikblocks);
574 init_rubikblocks(ModeInfo *mi)
576 rubikblocks_conf *cp;
579 rubikblocks = (rubikblocks_conf *)calloc(MI_NUM_SCREENS(mi), sizeof(rubikblocks_conf));
580 if(!rubikblocks) return;
582 cp = &rubikblocks[MI_SCREEN(mi)];
587 if ((cp->glx_context = init_GL(mi)) != NULL)
592 reshape_rubikblocks(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
601 draw_rubikblocks(ModeInfo * mi)
603 Display *display = MI_DISPLAY(mi);
604 Window window = MI_WINDOW(mi);
605 rubikblocks_conf *cp;
606 if (!rubikblocks) return;
607 cp = &rubikblocks[MI_SCREEN(mi)];
608 MI_IS_DRAWN(mi) = True;
609 if (!cp->glx_context) return;
610 mi->polygon_count = 0;
611 glXMakeCurrent(display, window, *(cp->glx_context));
612 if (!draw_main(mi, cp))
614 release_rubikblocks(mi);
617 if (MI_IS_FPS(mi)) do_fps (mi);
619 glXSwapBuffers(display, window);
624 change_rubikblocks(ModeInfo * mi)
626 rubikblocks_conf *cp = &rubikblocks[MI_SCREEN(mi)];
627 if (!cp->glx_context) return;
628 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cp->glx_context));
631 #endif /* !STANDALONE */
634 rubikblocks_handle_event (ModeInfo *mi, XEvent *event)
636 rubikblocks_conf *cp = &rubikblocks[MI_SCREEN(mi)];
638 if (gltrackball_event_handler (event, cp->trackball,
639 MI_WIDTH (mi), MI_HEIGHT (mi),
647 XSCREENSAVER_MODULE ("RubikBlocks", rubikblocks)