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"
24 # define refresh_rubikblocks 0
25 #include "xlockmore.h"
27 #include "gltrackball.h"
31 #define DEF_SPIN "True"
32 #define DEF_WANDER "True"
33 #define DEF_TEXTURE "True"
34 #define DEF_RANDOMIZE "False"
35 #define DEF_SPINSPEED "0.1"
36 #define DEF_ROTSPEED "3.0"
37 #define DEF_WANDERSPEED "0.005"
38 #define DEF_WAIT "40.0"
39 #define DEF_CUBESIZE "1.0"
46 #define BORDER2 (BORDER*BORDER)
49 #define countof(x) (sizeof((x))/sizeof((*x)))
51 #define rnd01() ((int)(random()%2))
53 /*************************************************************************/
55 static Bool spin, wander, rndstart, tex;
56 static float spinspeed, tspeed, wspeed, twait, size;
58 static argtype vars[] = {
59 { &spin, "spin", "Spin", DEF_SPIN, t_Bool},
60 { &wander, "wander", "Wander", DEF_WANDER, t_Bool},
61 { &rndstart, "randomize", "Randomize", DEF_RANDOMIZE, t_Bool},
62 { &tex, "texture", "Texture", DEF_TEXTURE, t_Bool},
63 { &spinspeed, "spinspeed", "SpinSpeed", DEF_SPINSPEED, t_Float},
64 { &tspeed, "rotspeed", "RotSpeed", DEF_ROTSPEED, t_Float},
65 { &wspeed, "wanderspeed", "WanderSpeed", DEF_WANDERSPEED, t_Float},
66 { &twait, "wait", "Wait", DEF_WAIT, t_Float},
67 { &size, "cubesize", "CubeSize", DEF_CUBESIZE, t_Float},
70 static XrmOptionDescRec opts[] = {
71 { "-spin", ".spin", XrmoptionNoArg, "True" },
72 { "+spin", ".spin", XrmoptionNoArg, "False" },
73 { "-wander", ".wander", XrmoptionNoArg, "True" },
74 { "+wander", ".wander", XrmoptionNoArg, "False" },
75 { "-randomize", ".randomize", XrmoptionNoArg, "True" },
76 { "+randomize", ".randomize", XrmoptionNoArg, "False" },
77 { "-texture", ".texture", XrmoptionNoArg, "True" },
78 { "+texture", ".texture", XrmoptionNoArg, "False" },
79 { "-spinspeed", ".spinspeed", XrmoptionSepArg, 0 },
80 { "-wanderspeed", ".wanderspeed", XrmoptionSepArg, 0 },
81 { "-rotspeed", ".rotspeed", XrmoptionSepArg, 0 },
82 { "-wait", ".wait", XrmoptionSepArg, 0 },
83 { "-cubesize", ".cubesize", XrmoptionSepArg, 0 },
86 ENTRYPOINT ModeSpecOpt rubikblocks_opts = {countof(opts), opts, countof(vars), vars, NULL};
89 ModStruct rubikblocks_description =
90 { "rubikblocks", "init_rubikblocks", "draw_rubikblocks", "release_rubikblocks",
91 "draw_rubikblocks", "change_rubikblocks", NULL, &rubikblocks_opts,
92 25000, 1, 1, 1, 1.0, 4, "",
93 "Shows randomly shuffling Rubik's Mirror Blocks puzzle", 0, NULL
98 float pos[3]; /* _original_ position */
99 float qr[4]; /* quaternion of rotation */
100 Bool act; /* flag if it is undergoing the current rotation */
104 GLXContext *glx_context;
106 trackball_state *trackball;
110 Bool pause; /* pause between two rotations */
111 float qfram[4]; /* quaternion describing the rotation in one anim. frame */
112 GLfloat t, tmax; /* rotation clock */
113 piece_t pieces[27]; /* type and tilt of all the pieces */
115 unsigned char texture[TEX_HEIGHT][TEX_WIDTH];
120 static rubikblocks_conf *rubikblocks = NULL;
122 static const GLfloat shininess = 20.0;
123 static const GLfloat ambient[] = {0.0, 0.0, 0.0, 1.0};
124 static const GLfloat diffuse[] = {1.0, 1.0, 1.0, 1.0};
125 static const GLfloat position0[] = {1.0, 1.0, 1.0, 0.0};
126 static const GLfloat position1[] = {-1.0, -1.0, 1.0, 0.0};
127 static const GLfloat lmodel_ambient[] = {0.1, 0.1, 0.1, 1.0};
128 static const GLfloat material_ambient[] = {0.7, 0.7, 0.7, 1.0};
129 static const GLfloat material_diffuse[] = {0.7, 0.7, 0.7, 1.0};
130 static const GLfloat material_specular[] = {0.2, 0.2, 0.2, 1.0};
132 /*************************************************************************/
134 /* Multiplies two quaternions, src*dest, and stores the result in dest. */
136 mult_quat(float src[4], float dest[4])
139 r = src[0]*dest[0] - src[1]*dest[1] - src[2]*dest[2] - src[3]*dest[3];
140 i = src[0]*dest[1] + src[1]*dest[0] + src[2]*dest[3] - src[3]*dest[2];
141 j = src[0]*dest[2] + src[2]*dest[0] + src[3]*dest[1] - src[1]*dest[3];
142 k = src[0]*dest[3] + src[3]*dest[0] + src[1]*dest[2] - src[2]*dest[1];
149 /* Sets the 'act' flag for pieces which will undergo the rotation. */
151 flag_pieces(piece_t pieces[27], int axis, int side)
155 for(i = 0; i < 27; i++)
158 q[1] = pieces[i].pos[0];
159 q[2] = pieces[i].pos[1];
160 q[3] = pieces[i].pos[2];
161 mult_quat(pieces[i].qr, q);
162 for(j = 1; j < 4; j++)
164 mult_quat(pieces[i].qr, q);
165 for(j = 1; j < 4; j++)
167 if(fabs(q[axis] - side) < 0.1)
168 pieces[i].act = True;
170 pieces[i].act = False;
174 /* "Rounds" the value to the nearest from the set {0, +-1/2, +-1/sqrt(2), +-1}.
175 * It is guaranteed to be pretty close to one when this function is called. */
177 settle_value(float v)
179 if(v > 0.9) return 1;
180 else if(v < -0.9) return -1;
181 else if(v > 0.6) return M_SQRT1_2;
182 else if(v < -0.6) return -M_SQRT1_2;
183 else if(v > 0.4) return 0.5;
184 else if(v < -0.4) return -0.5;
189 randomize(rubikblocks_conf *cp)
193 for(i = 0; i < SHUFFLE; i++)
195 axis = (random()%3)+1;
197 flag_pieces(cp->pieces, axis, side);
198 for(j = 1; j < 4; j++)
200 cp->qfram[0] = M_SQRT1_2;
201 cp->qfram[axis] = M_SQRT1_2;
202 for(j = 0; j < 27; j++)
204 if(cp->pieces[j].act)
205 mult_quat(cp->qfram, cp->pieces[j].qr);
211 finish(rubikblocks_conf *cp)
231 flag_pieces(cp->pieces, axis, side);
233 cp->tmax = 90.0*angle;
234 for(i = 1; i < 4; i++)
236 cp->qfram[0] = cos(tspeed*M_PI/360);
237 cp->qfram[axis] = sin((rnd01()*2-1)*tspeed*M_PI/360);
241 for(i = 0; i < 27; i++)
243 for(j = 0; j < 4; j++)
245 cp->pieces[i].qr[j] = settle_value(cp->pieces[i].qr[j]);
255 draw_main(ModeInfo *mi, rubikblocks_conf *cp)
260 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
262 get_position(cp->rot, &x, &y, &z, !cp->button_down);
263 glTranslatef((x-0.5)*6, (y-0.5)*6, -20);
265 /* Do it twice because we don't track the device's orientation. */
266 glRotatef( current_device_rotation(), 0, 0, 1);
267 gltrackball_rotate(cp->trackball);
268 glRotatef(-current_device_rotation(), 0, 0, 1);
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 if(cp->wire) glColor3f(0.7, 0.7, 0.7);
278 for(i = 0; i < 27; i++)
279 if(cp->pieces[i].act)
280 mult_quat(cp->qfram, cp->pieces[i].qr);
281 for(i = 0; i < 27; i++)
284 if(fabs(cp->pieces[i].qr[0]) < 1)
285 glRotatef(360/M_PI*acos(cp->pieces[i].qr[0]),
286 cp->pieces[i].qr[1], cp->pieces[i].qr[2], cp->pieces[i].qr[3]);
287 glCallList(cp->list_base + i);
290 if((cp->t += tspeed) > cp->tmax) finish(cp);
295 draw_horz_line(rubikblocks_conf *cp, int x1, int x2, int y)
298 if(y < BORDER) y = -y;
300 for(; y < BORDER; y++) {
301 if(y0+y >= TEX_HEIGHT) break;
303 for(x = x1; x <= x2; x++)
304 if(cp->texture[y0+y][x]>w) cp->texture[y0+y][x] = w;
309 draw_vert_line(rubikblocks_conf *cp, int x, int y1, int y2)
314 for(; x < BORDER; x++) {
315 if(x0+x >= TEX_WIDTH) break;
317 for(y = y1; y <= y2; y++)
318 if(cp->texture[y][x0+x]>w) cp->texture[y][x0+x] = w;
323 make_texture(rubikblocks_conf *cp)
326 for(y = 0; y < TEX_HEIGHT; y++)
327 for(x = 0; x < TEX_WIDTH; x++)
328 cp->texture[y][x] = 255;
329 draw_horz_line(cp, 0, TEX_WIDTH-1, 0);
330 draw_horz_line(cp, 0, TEX_WIDTH-1, TEX_HEIGHT-1);
331 draw_vert_line(cp, 0, 0, TEX_HEIGHT-1);
332 draw_vert_line(cp, TEX_WIDTH-1, 0, TEX_HEIGHT-1);
335 /* These simple transforms make the actual shape of the pieces. The parameters
336 * A, B and C affect the excentricity of the pieces in each direction. */
341 if(x > 1.4) return 1.5 - A;
342 else if(x < -1.4) return -1.5 - A;
349 const float B = 0.25;
350 if(y > 1.4) return 1.5 - B;
351 else if(y < -1.4) return -1.5 - B;
359 if(z > 1.4) return 1.5 - C;
360 else if(z < -1.4) return -1.5 - C;
365 init_lists(rubikblocks_conf *cp)
370 base = cp->list_base = glGenLists(27);
371 for(i = 0; i < 27; i++)
373 x = cp->pieces[i].pos[0];
374 y = cp->pieces[i].pos[1];
375 z = cp->pieces[i].pos[2];
376 glNewList(base+i, GL_COMPILE);
377 glBegin(GL_QUAD_STRIP);
380 glVertex3f(fx(x+0.5), fy(y-0.5), fz(z-0.5));
382 glVertex3f(fx(x+0.5), fy(y+0.5), fz(z-0.5));
384 glVertex3f(fx(x+0.5), fy(y-0.5), fz(z+0.5));
386 glVertex3f(fx(x+0.5), fy(y+0.5), fz(z+0.5));
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));
392 glNormal3f(-1, 0, 0);
394 glVertex3f(fx(x-0.5), fy(y-0.5), fz(z-0.5));
396 glVertex3f(fx(x-0.5), fy(y+0.5), fz(z-0.5));
397 glNormal3f(0, 0, -1);
399 glVertex3f(fx(x+0.5), fy(y-0.5), fz(z-0.5));
401 glVertex3f(fx(x+0.5), fy(y+0.5), fz(z-0.5));
406 glVertex3f(fx(x+0.5), fy(y+0.5), fz(z+0.5));
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));
412 glVertex3f(fx(x-0.5), fy(y+0.5), fz(z+0.5));
413 glNormal3f(0, -1, 0);
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));
427 /* It looks terrible... FIXME: any other ideas, maybe some anisotropic filtering? */
431 init_gl(ModeInfo *mi)
433 rubikblocks_conf *cp = &rubikblocks[MI_SCREEN(mi)];
437 cp->wire = MI_IS_WIREFRAME(mi);
439 # ifdef HAVE_JWZGLES /* #### glPolygonMode other than GL_FILL unimplemented */
446 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
451 glDrawBuffer(GL_BACK);
452 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
453 glShadeModel(GL_FLAT);
454 glDepthFunc(GL_LESS);
455 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
456 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
457 glLightfv(GL_LIGHT0, GL_POSITION, position0);
458 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
459 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
460 glLightfv(GL_LIGHT1, GL_POSITION, position1);
461 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
462 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
463 glEnable(GL_DEPTH_TEST);
466 glEnable(GL_LIGHTING);
467 glEnable(GL_NORMALIZE);
468 glEnable(GL_COLOR_MATERIAL);
469 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material_ambient);
470 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
471 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_specular);
472 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
474 glEnable(GL_TEXTURE_2D);
477 status = gluBuild2DMipmaps(GL_TEXTURE_2D, 1, TEX_WIDTH, TEX_HEIGHT,
478 GL_LUMINANCE, GL_UNSIGNED_BYTE, cp->texture);
480 const char *s = (char *)gluErrorString(status);
481 fprintf (stderr, "%s: error mipmapping texture: %s\n", progname, (s?s:"(unknown)"));
484 check_gl_error("mipmapping");
486 glTexImage2D(GL_TEXTURE_2D, 0, 1, TEX_WIDTH, TEX_HEIGHT,
487 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, cp->texture);
489 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
490 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
491 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
492 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
494 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
496 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
501 init_cp(rubikblocks_conf *cp)
509 for(i = -1, m = 0; i <= 1; i++)
510 for(j = -1; j <= 1; j++)
511 for(k = -1; k <= 1; k++)
513 cp->pieces[m].pos[0] = k;
514 cp->pieces[m].pos[1] = j;
515 cp->pieces[m].pos[2] = i;
516 cp->pieces[m].qr[0] = 1;
517 cp->pieces[m].qr[1] = 0;
518 cp->pieces[m].qr[2] = 0;
519 cp->pieces[m].qr[3] = 0;
523 cp->rot = make_rotator(spin?spinspeed:0, spin?spinspeed:0, spin?spinspeed:0,
524 0.1, wander?wspeed:0, True);
525 cp->trackball = gltrackball_init();
527 if(rndstart) randomize(cp);
530 /*************************************************************************/
533 reshape_rubikblocks(ModeInfo *mi, int width, int height)
535 rubikblocks_conf *cp = &rubikblocks[MI_SCREEN(mi)];
536 if(!height) height = 1;
537 cp->ratio = (GLfloat)width/(GLfloat)height;
538 glViewport(0, 0, (GLint) width, (GLint) height);
539 glMatrixMode(GL_PROJECTION);
541 gluPerspective(30.0, cp->ratio, 1.0, 100.0);
542 glMatrixMode(GL_MODELVIEW);
543 glClear(GL_COLOR_BUFFER_BIT);
547 release_rubikblocks(ModeInfo *mi)
549 if (rubikblocks != NULL)
552 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
554 rubikblocks_conf *cp = &rubikblocks[screen];
555 if (cp->glx_context) {
556 cp->glx_context = NULL;
559 free((void *)rubikblocks);
566 init_rubikblocks(ModeInfo *mi)
568 rubikblocks_conf *cp;
571 rubikblocks = (rubikblocks_conf *)calloc(MI_NUM_SCREENS(mi), sizeof(rubikblocks_conf));
572 if(!rubikblocks) return;
574 cp = &rubikblocks[MI_SCREEN(mi)];
579 if ((cp->glx_context = init_GL(mi)) != NULL)
584 reshape_rubikblocks(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
593 draw_rubikblocks(ModeInfo * mi)
595 Display *display = MI_DISPLAY(mi);
596 Window window = MI_WINDOW(mi);
597 rubikblocks_conf *cp;
598 if (!rubikblocks) return;
599 cp = &rubikblocks[MI_SCREEN(mi)];
600 MI_IS_DRAWN(mi) = True;
601 if (!cp->glx_context) return;
602 mi->polygon_count = 0;
603 glXMakeCurrent(display, window, *(cp->glx_context));
604 if (!draw_main(mi, cp))
606 release_rubikblocks(mi);
609 if (MI_IS_FPS(mi)) do_fps (mi);
611 glXSwapBuffers(display, window);
616 change_rubikblocks(ModeInfo * mi)
618 rubikblocks_conf *cp = &rubikblocks[MI_SCREEN(mi)];
619 if (!cp->glx_context) return;
620 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cp->glx_context));
623 #endif /* !STANDALONE */
626 rubikblocks_handle_event (ModeInfo *mi, XEvent *event)
628 rubikblocks_conf *cp = &rubikblocks[MI_SCREEN(mi)];
629 if(event->xany.type == ButtonPress && event->xbutton.button == Button1)
631 cp->button_down = True;
632 gltrackball_start(cp->trackball, event->xbutton.x, event->xbutton.y,
633 MI_WIDTH(mi), MI_HEIGHT(mi));
636 else if(event->xany.type == ButtonRelease && event->xbutton.button == Button1)
638 cp->button_down = False;
641 else if(event->xany.type == ButtonPress &&
642 (event->xbutton.button == Button4 || event->xbutton.button == Button5 ||
643 event->xbutton.button == Button6 || event->xbutton.button == Button7))
645 gltrackball_mousewheel(cp->trackball,
646 event->xbutton.button, 5, !!event->xbutton.state);
649 else if(event->xany.type == MotionNotify && cp->button_down)
651 gltrackball_track(cp->trackball, event->xmotion.x, event->xmotion.y,
652 MI_WIDTH (mi), MI_HEIGHT (mi));
659 XSCREENSAVER_MODULE ("RubikBlocks", rubikblocks)