1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* rubik --- Shows an auto-solving Rubik's cube */
5 static const char sccsid[] = "@(#)rubik.c 5.01 2001/03/01 xlockmore";
9 * Permission to use, copy, modify, and distribute this software and its
10 * documentation for any purpose and without fee is hereby granted,
11 * provided that the above copyright notice appear in all copies and that
12 * both that copyright notice and this permission notice appear in
13 * supporting documentation.
15 * This file is provided AS IS with no warranties of any kind. The author
16 * shall have no liability with respect to the infringement of copyrights,
17 * trade secrets or any patents by this file or any part thereof. In no
18 * event will the author be liable for any lost revenue or profits or
19 * other special, indirect and consequential damages.
21 * This mode shows an auto-solving rubik's cube "puzzle". If somebody
22 * intends to make a game or something based on this code, please let me
23 * know first, my e-mail address is provided in this comment. Marcelo.
25 * Thanks goes also to Brian Paul for making it possible and inexpensive
26 * to use OpenGL at home.
28 * Since I'm not a native English speaker, my apologies for any grammatical
31 * My e-mail address is
32 * mfvianna@centroin.com.br
34 * Marcelo F. Vianna (Jul-31-1997)
37 * 05-Apr-2002: Removed all gllist uses (fix some bug with nvidia driver)
38 * 01-Mar-2001: Added FPS stuff - Eric Lassauge <lassauge@mail.dotcom.fr>
39 * 01-Nov-2000: Allocation checks
40 * 27-Apr-1999: LxMxN stuff added.
41 * 26-Sep-1998: Added some more movement (the cube does not stay in the screen
42 * center anymore. Also fixed the scale problem immediately after
43 * shuffling when the puzzle is solved.
44 * 08-Aug-1997: Now has some internals from xrubik by David Bagley
45 * This should make it easier to add features.
46 * 02-Aug-1997: Now behaves more like puzzle.c: first show the cube being
47 * shuffled and then being solved. A mode specific option was
49 * "+/-hideshuffling" to provide the original behavior (in which
50 * only the solution is shown).
51 * The color labels corners are now rounded.
52 * Optimized the cubit() routine using glLists.
53 * 01-Aug-1997: Shuffling now avoids movements that undoes the previous
54 * movement and three consecutive identical moves (which is
56 * improved the "cycles" option in replacement of David's hack,
57 * now rp->anglestep is a GLfloat, so this option selects the
58 * "exact" number of frames that a rotation (movement) takes to
60 * 30-Jul-1997: Initial release, there is no algorithm to solve the puzzle,
61 * instead, it randomly shuffle the cube and then make the
62 * movements in the reverse order.
63 * The mode was written in 1 day (I got sick and had the day off).
64 * There was not much to do since I could not leave home... :)
68 * Color labels mapping:
69 * =====================
77 * +-----------+-----------+-----------+
80 * |v LEFT(1) |v FRONT(2) |v RIGHT(3) |
83 * +-----------+-----------+-----------+
89 * +-----------+ +---+---+---+
90 * |0--> | | 0 | 1 | 2 |
92 * |v BACK(5) | | 3 | 4 | 5 |
95 * +-----------+ +---+---+---+
108 # define DEFAULTS "*delay: 20000 \n" \
110 "*showFPS: False \n" \
113 # define refresh_rubik 0
114 # include "xlockmore.h" /* from the xscreensaver distribution */
115 #else /* !STANDALONE */
116 # include "xlock.h" /* from the xlockmore distribution */
118 #endif /* !STANDALONE */
120 #include "gltrackball.h"
124 #define DEF_SIZEX "0"
125 #define DEF_SIZEY "0"
126 #define DEF_SIZEZ "0"
127 #define DEF_HIDESHUFFLING "False"
132 static Bool hideshuffling;
134 static XrmOptionDescRec opts[] =
136 {"-sizex", ".rubik.sizex", XrmoptionSepArg, 0},
137 {"-sizey", ".rubik.sizey", XrmoptionSepArg, 0},
138 {"-sizez", ".rubik.sizez", XrmoptionSepArg, 0},
139 {"-hideshuffling", ".rubik.hideshuffling", XrmoptionNoArg, "on"},
140 {"+hideshuffling", ".rubik.hideshuffling", XrmoptionNoArg, "off"}
143 static argtype vars[] =
145 {&sizex, "sizex", "SizeX", DEF_SIZEX, t_Int},
146 {&sizey, "sizey", "SizeY", DEF_SIZEY, t_Int},
147 {&sizez, "sizez", "SizeZ", DEF_SIZEZ, t_Int},
148 {&hideshuffling, "hideshuffling", "Hideshuffling", DEF_HIDESHUFFLING, t_Bool}
151 static OptionStruct desc[] =
153 {"-sizex num", "number of cubies along x axis (overrides size)"},
154 {"-sizey num", "number of cubies along y axis (overrides size)"},
155 {"-sizez num", "number of cubies along z axis (overrides size)"},
156 {"-/+hideshuffling", "turn on/off hidden shuffle phase"}
159 ENTRYPOINT ModeSpecOpt rubik_opts =
160 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
163 ModStruct rubik_description =
164 {"rubik", "init_rubik", "draw_rubik", "release_rubik",
165 "draw_rubik", "change_rubik", (char *) NULL, &rubik_opts,
166 10000, -30, 5, -6, 64, 1.0, "",
167 "Shows an auto-solving Rubik's Cube", 0, NULL};
171 #define VectMul(X1,Y1,Z1,X2,Y2,Z2) (Y1)*(Z2)-(Z1)*(Y2),(Z1)*(X2)-(X1)*(Z2),(X1)*(Y2)-(Y1)*(X2)
172 #define sqr(A) ((A)*(A))
179 #define ACTION_SOLVE 1
180 #define ACTION_SHUFFLE 0
182 #define DELAY_AFTER_SHUFFLING 5
183 #define DELAY_AFTER_SOLVING 20
185 /*************************************************************************/
188 #define MAXSIZEX (rp->sizex)
189 #define MAXSIZEY (rp->sizey)
190 #define MAXSIZEZ (rp->sizez)
191 #define AVSIZE ((rp->sizex+rp->sizey+rp->sizez)/3.0) /* Use of this should be minimized */
192 #define MAXMAXSIZE (MAX(MAXSIZEX,MAX(MAXSIZEY,MAXSIZEZ)))
193 #define MAXSIZEXY (MAXSIZEX*MAXSIZEY)
194 #define MAXSIZEYZ (MAXSIZEY*MAXSIZEZ)
195 #define MAXSIZEZX (MAXSIZEZ*MAXSIZEX)
196 #define LASTX (MAXSIZEX-1)
197 #define LASTY (MAXSIZEY-1)
198 #define LASTZ (MAXSIZEZ-1)
199 /* These are not likely to change but... */
204 #define Scale4Window (0.9/AVSIZE)
205 #define Scale4Iconic (2.1/AVSIZE)
207 #define MAXORIENT 4 /* Number of orientations of a square */
208 #define MAXFACES 6 /* Number of faces */
210 /* Directions relative to the face of a cubie */
215 #define CW (MAXORIENT+1)
216 #define HALF (MAXORIENT+2)
217 #define CCW (2*MAXORIENT-1)
223 #define BOTTOM_FACE 4
225 #define NO_FACE (MAXFACES)
226 #define NO_ROTATION (2*MAXORIENT)
227 #define NO_DEPTH MAXMAXSIZE
229 #define REVX(a) (MAXSIZEX - a - 1)
230 #define REVY(a) (MAXSIZEY - a - 1)
231 #define REVZ(a) (MAXSIZEZ - a - 1)
234 #define CUBEROUND (CUBELEN-0.05)
235 #define STICKERLONG (CUBEROUND-0.05)
236 #define STICKERSHORT (STICKERLONG-0.05)
237 #define STICKERDEPTH (CUBELEN+0.01)
241 typedef struct _RubikLoc {
243 int rotation; /* Not used yet */
246 typedef struct _RubikRowNext {
247 int face, direction, sideFace;
250 typedef struct _RubikMove {
255 typedef struct _RubikSlice {
261 * Pick a face and a direction on face the next face and orientation
264 static const RubikLoc slideNextRow[MAXFACES][MAXORIENT] =
299 * Examine cubie 0 on each face, its 4 movements (well only 2 since the
300 * other 2 will be opposites) and translate it into slice movements).
301 * CW = DEEP Depth CCW == SHALLOW Depth with reference to faces 0, 1, and 2
303 static const RubikLoc rotateSlice[MAXFACES][MAXORIENT / 2] =
332 * Rotate face clockwise by a number of orients, then the top of the
333 * face then points to this face
335 static const int rowToRotate[MAXFACES][MAXORIENT] =
346 * This translates a clockwise move to something more manageable
348 static const RubikRowNext rotateToRow[MAXFACES] = /*CW to min face */
365 int sizex, sizey, sizez;
366 float avsize, avsizeSq;
370 RubikLoc *cubeLoc[MAXFACES];
371 RubikLoc *rowLoc[MAXORIENT];
374 GLfloat PX, PY, VX, VY;
375 GLXContext *glx_context;
377 trackball_state *trackball;
380 static const float front_shininess[] = {60.0};
381 static const float front_specular[] = {0.7, 0.7, 0.7, 1.0};
382 static const float ambient[] = {0.0, 0.0, 0.0, 1.0};
383 static const float diffuse[] = {1.0, 1.0, 1.0, 1.0};
384 static const float position0[] = {1.0, 1.0, 1.0, 0.0};
385 static const float position1[] = {-1.0, -1.0, 1.0, 0.0};
386 static const float lmodel_ambient[] = {0.5, 0.5, 0.5, 1.0};
387 static const float lmodel_twoside[] = {GL_TRUE};
389 static const float MaterialRed[] = {0.5, 0.0, 0.0, 1.0};
390 static const float MaterialGreen[] = {0.0, 0.5, 0.0, 1.0};
391 static const float MaterialBlue[] = {0.0, 0.0, 0.5, 1.0};
392 static const float MaterialYellow[] = {0.7, 0.7, 0.0, 1.0};
393 static const float MaterialOrange[] = {0.9, 0.45, 0.36, 1.0};
396 static float MaterialMagenta[] = {0.7, 0.0, 0.7, 1.0};
397 static float MaterialCyan[] = {0.0, 0.7, 0.7, 1.0};
400 static const float MaterialWhite[] = {0.8, 0.8, 0.8, 1.0};
401 static const float MaterialGray[] = {0.2, 0.2, 0.2, 1.0};
402 static const float MaterialGray3[] = {0.3, 0.3, 0.3, 1.0};
403 static const float MaterialGray4[] = {0.4, 0.4, 0.4, 1.0};
404 static const float MaterialGray5[] = {0.5, 0.5, 0.5, 1.0};
405 static const float MaterialGray6[] = {0.6, 0.6, 0.6, 1.0};
406 static const float MaterialGray7[] = {0.7, 0.7, 0.7, 1.0};
408 static rubikstruct *rubik = (rubikstruct *) NULL;
412 pickcolor(int C, int mono)
417 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray3);
419 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialRed);
423 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray6);
425 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialYellow);
428 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
432 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray4);
434 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGreen);
438 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray7);
440 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialOrange);
444 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray5);
446 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialBlue);
449 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialCyan);
450 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialMagenta);
456 faceSizes(rubikstruct * rp, int face, int * sizeOfRow, int * sizeOfColumn)
461 *sizeOfRow = MAXSIZEX;
462 *sizeOfColumn = MAXSIZEZ;
466 *sizeOfRow = MAXSIZEZ;
467 *sizeOfColumn = MAXSIZEY;
471 *sizeOfRow = MAXSIZEX;
472 *sizeOfColumn = MAXSIZEY;
478 checkFaceSquare(rubikstruct * rp, int face)
480 int sizeOfRow, sizeOfColumn;
482 faceSizes(rp, face, &sizeOfRow, &sizeOfColumn);
483 return (sizeOfRow == sizeOfColumn);
484 /* Cubes can be made square with a 4x2 face where 90 degree turns
485 * should be permitted but that is kind of complicated for me.
486 * This can be done in 2 ways where the side of the cubies are
487 * the same size and one where one side (the side with half the
488 * number of cubies) is twice the size of the other. The first is
489 * complicated because faces of cubies can go under other faces.
490 * The second way is similar to "banded cubes" where scotch tape
491 * restricts the moves of some cubes. Here you have to keep track
492 * of the restrictions and show banded cubies graphically as one
498 sizeFace(rubikstruct * rp, int face)
500 int sizeOfRow, sizeOfColumn;
502 faceSizes(rp, face, &sizeOfRow, &sizeOfColumn);
503 return (sizeOfRow * sizeOfColumn);
507 sizeRow(rubikstruct * rp, int face)
509 int sizeOfRow, sizeOfColumn; /* sizeOfColumn not used */
511 faceSizes(rp, face, &sizeOfRow, &sizeOfColumn);
516 draw_stickerless_cubit(rubikstruct *rp, unsigned long *polysP)
519 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray);
520 /* Put sticker here */
521 glNormal3f(0.00, 0.00, 1.00);
522 glVertex3f(-CUBEROUND, -CUBEROUND, CUBELEN);
523 glVertex3f(CUBEROUND, -CUBEROUND, CUBELEN);
524 glVertex3f(CUBEROUND, CUBEROUND, CUBELEN);
525 glVertex3f(-CUBEROUND, CUBEROUND, CUBELEN);
527 glNormal3f(0.00, 0.00, -1.00);
528 glVertex3f(-CUBEROUND, CUBEROUND, -CUBELEN);
529 glVertex3f(CUBEROUND, CUBEROUND, -CUBELEN);
530 glVertex3f(CUBEROUND, -CUBEROUND, -CUBELEN);
531 glVertex3f(-CUBEROUND, -CUBEROUND, -CUBELEN);
533 glNormal3f(-1.00, 0.00, 0.00);
534 glVertex3f(-CUBELEN, -CUBEROUND, CUBEROUND);
535 glVertex3f(-CUBELEN, CUBEROUND, CUBEROUND);
536 glVertex3f(-CUBELEN, CUBEROUND, -CUBEROUND);
537 glVertex3f(-CUBELEN, -CUBEROUND, -CUBEROUND);
539 glNormal3f(1.00, 0.00, 0.00);
540 glVertex3f(CUBELEN, -CUBEROUND, -CUBEROUND);
541 glVertex3f(CUBELEN, CUBEROUND, -CUBEROUND);
542 glVertex3f(CUBELEN, CUBEROUND, CUBEROUND);
543 glVertex3f(CUBELEN, -CUBEROUND, CUBEROUND);
545 glNormal3f(0.00, -1.00, 0.00);
546 glVertex3f(CUBEROUND, -CUBELEN, -CUBEROUND);
547 glVertex3f(CUBEROUND, -CUBELEN, CUBEROUND);
548 glVertex3f(-CUBEROUND, -CUBELEN, CUBEROUND);
549 glVertex3f(-CUBEROUND, -CUBELEN, -CUBEROUND);
551 glNormal3f(0.00, 1.00, 0.00);
552 glVertex3f(-CUBEROUND, CUBELEN, -CUBEROUND);
553 glVertex3f(-CUBEROUND, CUBELEN, CUBEROUND);
554 glVertex3f(CUBEROUND, CUBELEN, CUBEROUND);
555 glVertex3f(CUBEROUND, CUBELEN, -CUBEROUND);
559 glNormal3f(-1.00, -1.00, 0.00);
560 glVertex3f(-CUBEROUND, -CUBELEN, -CUBEROUND);
561 glVertex3f(-CUBEROUND, -CUBELEN, CUBEROUND);
562 glVertex3f(-CUBELEN, -CUBEROUND, CUBEROUND);
563 glVertex3f(-CUBELEN, -CUBEROUND, -CUBEROUND);
565 glNormal3f(1.00, 1.00, 0.00);
566 glVertex3f(CUBEROUND, CUBELEN, -CUBEROUND);
567 glVertex3f(CUBEROUND, CUBELEN, CUBEROUND);
568 glVertex3f(CUBELEN, CUBEROUND, CUBEROUND);
569 glVertex3f(CUBELEN, CUBEROUND, -CUBEROUND);
571 glNormal3f(-1.00, 1.00, 0.00);
572 glVertex3f(-CUBELEN, CUBEROUND, -CUBEROUND);
573 glVertex3f(-CUBELEN, CUBEROUND, CUBEROUND);
574 glVertex3f(-CUBEROUND, CUBELEN, CUBEROUND);
575 glVertex3f(-CUBEROUND, CUBELEN, -CUBEROUND);
577 glNormal3f(1.00, -1.00, 0.00);
578 glVertex3f(CUBELEN, -CUBEROUND, -CUBEROUND);
579 glVertex3f(CUBELEN, -CUBEROUND, CUBEROUND);
580 glVertex3f(CUBEROUND, -CUBELEN, CUBEROUND);
581 glVertex3f(CUBEROUND, -CUBELEN, -CUBEROUND);
583 glNormal3f(0.00, -1.00, -1.00);
584 glVertex3f(-CUBEROUND, -CUBEROUND, -CUBELEN);
585 glVertex3f(CUBEROUND, -CUBEROUND, -CUBELEN);
586 glVertex3f(CUBEROUND, -CUBELEN, -CUBEROUND);
587 glVertex3f(-CUBEROUND, -CUBELEN, -CUBEROUND);
589 glNormal3f(0.00, 1.00, 1.00);
590 glVertex3f(-CUBEROUND, CUBEROUND, CUBELEN);
591 glVertex3f(CUBEROUND, CUBEROUND, CUBELEN);
592 glVertex3f(CUBEROUND, CUBELEN, CUBEROUND);
593 glVertex3f(-CUBEROUND, CUBELEN, CUBEROUND);
595 glNormal3f(0.00, -1.00, 1.00);
596 glVertex3f(-CUBEROUND, -CUBELEN, CUBEROUND);
597 glVertex3f(CUBEROUND, -CUBELEN, CUBEROUND);
598 glVertex3f(CUBEROUND, -CUBEROUND, CUBELEN);
599 glVertex3f(-CUBEROUND, -CUBEROUND, CUBELEN);
601 glNormal3f(0.00, 1.00, -1.00);
602 glVertex3f(-CUBEROUND, CUBELEN, -CUBEROUND);
603 glVertex3f(CUBEROUND, CUBELEN, -CUBEROUND);
604 glVertex3f(CUBEROUND, CUBEROUND, -CUBELEN);
605 glVertex3f(-CUBEROUND, CUBEROUND, -CUBELEN);
607 glNormal3f(-1.00, 0.00, -1.00);
608 glVertex3f(-CUBELEN, -CUBEROUND, -CUBEROUND);
609 glVertex3f(-CUBELEN, CUBEROUND, -CUBEROUND);
610 glVertex3f(-CUBEROUND, CUBEROUND, -CUBELEN);
611 glVertex3f(-CUBEROUND, -CUBEROUND, -CUBELEN);
613 glNormal3f(1.00, 0.00, 1.00);
614 glVertex3f(CUBELEN, -CUBEROUND, CUBEROUND);
615 glVertex3f(CUBELEN, CUBEROUND, CUBEROUND);
616 glVertex3f(CUBEROUND, CUBEROUND, CUBELEN);
617 glVertex3f(CUBEROUND, -CUBEROUND, CUBELEN);
619 glNormal3f(1.00, 0.00, -1.00);
620 glVertex3f(CUBEROUND, -CUBEROUND, -CUBELEN);
621 glVertex3f(CUBEROUND, CUBEROUND, -CUBELEN);
622 glVertex3f(CUBELEN, CUBEROUND, -CUBEROUND);
623 glVertex3f(CUBELEN, -CUBEROUND, -CUBEROUND);
625 glNormal3f(-1.00, 0.00, 1.00);
626 glVertex3f(-CUBEROUND, -CUBEROUND, CUBELEN);
627 glVertex3f(-CUBEROUND, CUBEROUND, CUBELEN);
628 glVertex3f(-CUBELEN, CUBEROUND, CUBEROUND);
629 glVertex3f(-CUBELEN, -CUBEROUND, CUBEROUND);
632 glBegin(GL_TRIANGLES);
633 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray);
634 /* Corners of cubit */
635 glNormal3f(1.00, 1.00, 1.00);
636 glVertex3f(CUBEROUND, CUBEROUND, CUBELEN);
637 glVertex3f(CUBELEN, CUBEROUND, CUBEROUND);
638 glVertex3f(CUBEROUND, CUBELEN, CUBEROUND);
640 glNormal3f(-1.00, -1.00, -1.00);
641 glVertex3f(-CUBEROUND, -CUBELEN, -CUBEROUND);
642 glVertex3f(-CUBELEN, -CUBEROUND, -CUBEROUND);
643 glVertex3f(-CUBEROUND, -CUBEROUND, -CUBELEN);
645 glNormal3f(-1.00, 1.00, 1.00);
646 glVertex3f(-CUBEROUND, CUBEROUND, CUBELEN);
647 glVertex3f(-CUBEROUND, CUBELEN, CUBEROUND);
649 glVertex3f(-CUBELEN, CUBEROUND, CUBEROUND);
650 glNormal3f(1.00, -1.00, -1.00);
651 glVertex3f(CUBELEN, -CUBEROUND, -CUBEROUND);
652 glVertex3f(CUBEROUND, -CUBELEN, -CUBEROUND);
653 glVertex3f(CUBEROUND, -CUBEROUND, -CUBELEN);
655 glNormal3f(1.00, -1.00, 1.00);
656 glVertex3f(CUBEROUND, -CUBEROUND, CUBELEN);
657 glVertex3f(CUBEROUND, -CUBELEN, CUBEROUND);
658 glVertex3f(CUBELEN, -CUBEROUND, CUBEROUND);
660 glNormal3f(-1.00, 1.00, -1.00);
661 glVertex3f(-CUBELEN, CUBEROUND, -CUBEROUND);
662 glVertex3f(-CUBEROUND, CUBELEN, -CUBEROUND);
663 glVertex3f(-CUBEROUND, CUBEROUND, -CUBELEN);
665 glNormal3f(-1.00, -1.00, 1.00);
666 glVertex3f(-CUBEROUND, -CUBEROUND, CUBELEN);
667 glVertex3f(-CUBELEN, -CUBEROUND, CUBEROUND);
668 glVertex3f(-CUBEROUND, -CUBELEN, CUBEROUND);
670 glNormal3f(1.00, 1.00, -1.00);
671 glVertex3f(CUBELEN, CUBEROUND, -CUBEROUND);
672 glVertex3f(CUBEROUND, CUBEROUND, -CUBELEN);
673 glVertex3f(CUBEROUND, CUBELEN, -CUBEROUND);
680 draw_cubit(ModeInfo * mi,
681 int back, int front, int left, int right, int bottom, int top,
682 unsigned long *polysP)
684 rubikstruct *rp = &rubik[MI_SCREEN(mi)];
685 int mono = MI_IS_MONO(mi);
687 if (!draw_stickerless_cubit(rp, polysP))
689 if (back != NO_FACE) {
691 pickcolor(back, mono);
692 glNormal3f(0.00, 0.00, -1.00);
693 glVertex3f(-STICKERSHORT, STICKERLONG, -STICKERDEPTH);
694 glVertex3f(STICKERSHORT, STICKERLONG, -STICKERDEPTH);
695 glVertex3f(STICKERLONG, STICKERSHORT, -STICKERDEPTH);
696 glVertex3f(STICKERLONG, -STICKERSHORT, -STICKERDEPTH);
697 glVertex3f(STICKERSHORT, -STICKERLONG, -STICKERDEPTH);
698 glVertex3f(-STICKERSHORT, -STICKERLONG, -STICKERDEPTH);
699 glVertex3f(-STICKERLONG, -STICKERSHORT, -STICKERDEPTH);
700 glVertex3f(-STICKERLONG, STICKERSHORT, -STICKERDEPTH);
704 if (front != NO_FACE) {
706 pickcolor(front, mono);
707 glNormal3f(0.00, 0.00, 1.00);
708 glVertex3f(-STICKERSHORT, -STICKERLONG, STICKERDEPTH);
709 glVertex3f(STICKERSHORT, -STICKERLONG, STICKERDEPTH);
710 glVertex3f(STICKERLONG, -STICKERSHORT, STICKERDEPTH);
711 glVertex3f(STICKERLONG, STICKERSHORT, STICKERDEPTH);
712 glVertex3f(STICKERSHORT, STICKERLONG, STICKERDEPTH);
713 glVertex3f(-STICKERSHORT, STICKERLONG, STICKERDEPTH);
714 glVertex3f(-STICKERLONG, STICKERSHORT, STICKERDEPTH);
715 glVertex3f(-STICKERLONG, -STICKERSHORT, STICKERDEPTH);
719 if (left != NO_FACE) {
721 pickcolor(left, mono);
722 glNormal3f(-1.00, 0.00, 0.00);
723 glVertex3f(-STICKERDEPTH, -STICKERSHORT, STICKERLONG);
724 glVertex3f(-STICKERDEPTH, STICKERSHORT, STICKERLONG);
725 glVertex3f(-STICKERDEPTH, STICKERLONG, STICKERSHORT);
726 glVertex3f(-STICKERDEPTH, STICKERLONG, -STICKERSHORT);
727 glVertex3f(-STICKERDEPTH, STICKERSHORT, -STICKERLONG);
728 glVertex3f(-STICKERDEPTH, -STICKERSHORT, -STICKERLONG);
729 glVertex3f(-STICKERDEPTH, -STICKERLONG, -STICKERSHORT);
730 glVertex3f(-STICKERDEPTH, -STICKERLONG, STICKERSHORT);
734 if (right != NO_FACE) {
736 pickcolor(right, mono);
737 glNormal3f(1.00, 0.00, 0.00);
738 glVertex3f(STICKERDEPTH, -STICKERSHORT, -STICKERLONG);
739 glVertex3f(STICKERDEPTH, STICKERSHORT, -STICKERLONG);
740 glVertex3f(STICKERDEPTH, STICKERLONG, -STICKERSHORT);
741 glVertex3f(STICKERDEPTH, STICKERLONG, STICKERSHORT);
742 glVertex3f(STICKERDEPTH, STICKERSHORT, STICKERLONG);
743 glVertex3f(STICKERDEPTH, -STICKERSHORT, STICKERLONG);
744 glVertex3f(STICKERDEPTH, -STICKERLONG, STICKERSHORT);
745 glVertex3f(STICKERDEPTH, -STICKERLONG, -STICKERSHORT);
749 if (bottom != NO_FACE) {
751 pickcolor(bottom, mono);
752 glNormal3f(0.00, -1.00, 0.00);
753 glVertex3f(STICKERLONG, -STICKERDEPTH, -STICKERSHORT);
754 glVertex3f(STICKERLONG, -STICKERDEPTH, STICKERSHORT);
755 glVertex3f(STICKERSHORT, -STICKERDEPTH, STICKERLONG);
756 glVertex3f(-STICKERSHORT, -STICKERDEPTH, STICKERLONG);
757 glVertex3f(-STICKERLONG, -STICKERDEPTH, STICKERSHORT);
758 glVertex3f(-STICKERLONG, -STICKERDEPTH, -STICKERSHORT);
759 glVertex3f(-STICKERSHORT, -STICKERDEPTH, -STICKERLONG);
760 glVertex3f(STICKERSHORT, -STICKERDEPTH, -STICKERLONG);
764 if (top != NO_FACE) {
766 pickcolor(top, mono);
767 glNormal3f(0.00, 1.00, 0.00);
768 glVertex3f(-STICKERLONG, STICKERDEPTH, -STICKERSHORT);
769 glVertex3f(-STICKERLONG, STICKERDEPTH, STICKERSHORT);
770 glVertex3f(-STICKERSHORT, STICKERDEPTH, STICKERLONG);
771 glVertex3f(STICKERSHORT, STICKERDEPTH, STICKERLONG);
772 glVertex3f(STICKERLONG, STICKERDEPTH, STICKERSHORT);
773 glVertex3f(STICKERLONG, STICKERDEPTH, -STICKERSHORT);
774 glVertex3f(STICKERSHORT, STICKERDEPTH, -STICKERLONG);
775 glVertex3f(-STICKERSHORT, STICKERDEPTH, -STICKERLONG);
782 /* Convert move to weird general notation */
784 convertMove(rubikstruct * rp, RubikMove move, RubikSlice * slice)
787 int sizeOfRow, sizeOfColumn;
789 plane = rotateSlice[(int) move.face][move.direction % 2];
790 (*slice).face = plane.face;
791 (*slice).rotation = plane.rotation;
793 faceSizes(rp, move.face, &sizeOfRow, &sizeOfColumn);
794 if (plane.face == 1 || /* VERTICAL */
795 (plane.face == 2 && (move.face == 1 || move.face == 3))) {
796 if ((*slice).rotation == CW)
797 (*slice).depth = sizeOfRow - 1 - move.position %
800 (*slice).depth = move.position % sizeOfRow;
801 } else { /* (plane.face == 0 || *//* HORIZONTAL *//*
802 (plane.face == 2 && (move.face == 0 || move.face == 4))) */
803 if ((*slice).rotation == CW)
804 (*slice).depth = sizeOfColumn - 1 - move.position /
807 (*slice).depth = move.position / sizeOfRow;
809 /* If (*slice).depth = 0 then face 0, face 1, or face 2 moves */
810 if (move.direction / 2)
811 (*slice).rotation = ((*slice).rotation == CW) ? CCW : CW;
814 /* Assume the size is at least 2, or its just not challenging... */
816 draw_cube(ModeInfo * mi)
819 #define SX ((GLint)S1*(MAXSIZEX-1))
820 #define SY ((GLint)S1*(MAXSIZEY-1))
821 #define SZ ((GLint)S1*(MAXSIZEZ-1))
822 #define HALFX (((GLfloat)MAXSIZEX-1.0)/2.0)
823 #define HALFY (((GLfloat)MAXSIZEY-1.0)/2.0)
824 #define HALFZ (((GLfloat)MAXSIZEZ-1.0)/2.0)
825 #define MIDX(a) (((GLfloat)(2*a-MAXSIZEX+1))/2.0)
826 #define MIDY(a) (((GLfloat)(2*a-MAXSIZEY+1))/2.0)
827 #define MIDZ(a) (((GLfloat)(2*a-MAXSIZEZ+1))/2.0)
828 #define DRAW_CUBIT(mi,b,f,l,r,bm,t) if (!draw_cubit(mi,b,f,l,r,bm,t,&mi->polygon_count)) return False
829 rubikstruct *rp = &rubik[MI_SCREEN(mi)];
834 if (rp->movement.face == NO_FACE) {
835 slice.face = NO_FACE;
836 slice.rotation = NO_ROTATION;
837 slice.depth = NO_DEPTH;
839 convertMove(rp, rp->movement, &slice);
841 rotatestep = (slice.rotation == CCW) ? rp->rotatestep : -rp->rotatestep;
845 * The glRotatef() routine transforms the coordinate system for every future
846 * vertex specification (this is not so simple, but by now comprehending this
847 * is sufficient). So if you want to rotate the inner slice, you can draw
848 * one slice, rotate the anglestep for the centerslice, draw the inner slice,
849 * rotate reversely and draw the other slice.
850 * There is a sequence for drawing cubies for each axis being moved...
852 switch (slice.face) {
854 case TOP_FACE: /* BOTTOM_FACE too */
856 if (slice.depth == MAXSIZEY - 1)
857 glRotatef(rotatestep, 0, HALFY, 0);
859 glTranslatef(-HALFX, -HALFY, -HALFZ);
861 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * FIRSTY].face, NO_FACE,
862 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face, NO_FACE,
863 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * LASTZ].face, NO_FACE);
864 for (k = 1; k < MAXSIZEZ - 1; k++) {
865 glTranslatef(0, 0, S1);
868 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * LASTY].face, NO_FACE,
869 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
871 glTranslatef(0, 0, S1);
873 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * LASTY].face,
874 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * LASTY].face, NO_FACE,
875 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
876 for (i = 1; i < MAXSIZEX - 1; i++) {
877 glTranslatef(S1, 0, -SZ);
879 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * FIRSTY].face, NO_FACE,
881 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * LASTZ].face, NO_FACE);
882 for (k = 1; k < MAXSIZEZ - 1; k++) {
883 glTranslatef(0, 0, S1);
887 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * REVZ(k)].face, NO_FACE);
889 glTranslatef(0, 0, S1);
891 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * LASTY].face,
893 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * FIRSTZ].face, NO_FACE);
895 glTranslatef(S1, 0, -SZ);
897 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * FIRSTY].face, NO_FACE,
898 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * LASTY].face,
899 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * LASTZ].face, NO_FACE);
900 for (k = 1; k < MAXSIZEZ - 1; k++) {
901 glTranslatef(0, 0, S1);
904 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * LASTY].face,
905 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
907 glTranslatef(0, 0, S1);
909 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * LASTY].face,
910 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face,
911 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
913 for (j = 1; j < MAXSIZEY - 1; j++) {
915 if (slice.depth == REVY(j))
916 glRotatef(rotatestep, 0, HALFY, 0);
917 glTranslatef(-HALFX, MIDY(j), -HALFZ);
919 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * j].face, NO_FACE,
920 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
922 for (k = 1; k < MAXSIZEZ - 1; k++) {
923 glTranslatef(0, 0, S1);
926 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * REVY(j)].face, NO_FACE,
929 glTranslatef(0, 0, S1);
931 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * REVY(j)].face,
932 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
934 for (i = 1; i < MAXSIZEX - 1; i++) {
935 glTranslatef(1, 0, -SZ);
937 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * j].face, NO_FACE,
941 glTranslatef(0, 0, SZ);
943 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * REVY(j)].face,
947 glTranslatef(S1, 0, -SZ);
949 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * j].face, NO_FACE,
950 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face,
952 for (k = 1; k < MAXSIZEZ - 1; k++) {
953 glTranslatef(0, 0, S1);
956 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * REVY(j)].face,
959 glTranslatef(0, 0, S1);
961 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * REVY(j)].face,
962 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face,
966 if (slice.depth == 0)
967 glRotatef(rotatestep, 0, HALFY, 0);
969 glTranslatef(-HALFX, HALFY, -HALFZ);
971 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * LASTY].face, NO_FACE,
972 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
973 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face);
974 for (k = 1; k < MAXSIZEZ - 1; k++) {
975 glTranslatef(0, 0, S1);
978 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * FIRSTY].face, NO_FACE,
979 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * k].face);
981 glTranslatef(0, 0, S1);
983 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * FIRSTY].face,
984 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
985 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * LASTZ].face);
986 for (i = 1; i < MAXSIZEX - 1; i++) {
987 glTranslatef(S1, 0, -SZ);
989 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * LASTY].face, NO_FACE,
991 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * FIRSTZ].face);
992 for (k = 1; k < MAXSIZEZ - 1; k++) {
993 glTranslatef(0, 0, S1);
997 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * k].face);
999 glTranslatef(0, 0, S1);
1001 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * FIRSTY].face,
1003 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * LASTZ].face);
1005 glTranslatef(S1, 0, -SZ);
1007 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * LASTY].face, NO_FACE,
1008 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face,
1009 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * FIRSTZ].face);
1010 for (k = 1; k < MAXSIZEZ - 1; k++) {
1011 glTranslatef(0, 0, S1);
1014 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * FIRSTY].face,
1015 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * k].face);
1017 glTranslatef(0, 0, S1);
1019 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * FIRSTY].face,
1020 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face,
1021 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * LASTZ].face);
1023 case LEFT_FACE: /* RIGHT_FACE too */
1024 /* rotatestep is negative because the RIGHT face is the default here */
1026 if (slice.depth == 0)
1027 glRotatef(-rotatestep, HALFX, 0, 0);
1029 glTranslatef(-HALFX, -HALFY, -HALFZ);
1031 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * FIRSTY].face, NO_FACE,
1032 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face, NO_FACE,
1033 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * LASTZ].face, NO_FACE);
1034 for (j = 1; j < MAXSIZEY - 1; j++) {
1035 glTranslatef(0, S1, 0);
1037 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * j].face, NO_FACE,
1038 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
1041 glTranslatef(0, S1, 0);
1043 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * LASTY].face, NO_FACE,
1044 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
1045 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face);
1046 for (k = 1; k < MAXSIZEZ - 1; k++) {
1047 glTranslatef(0, -SY, S1);
1050 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * LASTY].face, NO_FACE,
1051 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
1052 for (j = 1; j < MAXSIZEY - 1; j++) {
1053 glTranslatef(0, S1, 0);
1056 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * REVY(j)].face, NO_FACE,
1059 glTranslatef(0, S1, 0);
1062 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * FIRSTY].face, NO_FACE,
1063 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * k].face);
1065 glTranslatef(0, -SY, S1);
1067 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * LASTY].face,
1068 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * LASTY].face, NO_FACE,
1069 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
1070 for (j = 1; j < MAXSIZEY - 1; j++) {
1071 glTranslatef(0, S1, 0);
1073 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * REVY(j)].face,
1074 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
1077 glTranslatef(0, S1, 0);
1079 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * FIRSTY].face,
1080 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
1081 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * LASTZ].face);
1083 for (i = 1; i < MAXSIZEX - 1; i++) {
1085 if (slice.depth == i)
1086 glRotatef(-rotatestep, HALFX, 0, 0);
1087 glTranslatef(MIDX(i), -HALFY, -HALFZ);
1089 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * FIRSTY].face, NO_FACE,
1091 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * LASTZ].face, NO_FACE);
1092 for (j = 1; j < MAXSIZEY - 1; j++) {
1093 glTranslatef(0, S1, 0);
1095 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * j].face, NO_FACE,
1099 glTranslatef(0, S1, 0);
1101 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * LASTY].face, NO_FACE,
1103 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * FIRSTZ].face);
1104 for (k = 1; k < MAXSIZEZ - 1; k++) {
1105 glTranslatef(0, -SY, S1);
1109 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * REVZ(k)].face, NO_FACE);
1111 glTranslatef(0, SY, 0);
1115 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * k].face);
1117 glTranslatef(0, -SY, S1);
1119 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * LASTY].face,
1121 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * FIRSTZ].face, NO_FACE);
1122 for (j = 1; j < MAXSIZEY - 1; j++) {
1123 glTranslatef(0, S1, 0);
1125 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * REVY(j)].face,
1129 glTranslatef(0, S1, 0);
1131 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * FIRSTY].face,
1133 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * LASTZ].face);
1136 if (slice.depth == MAXSIZEX - 1)
1137 glRotatef(-rotatestep, HALFX, 0, 0);
1138 glTranslatef(HALFX, -HALFY, -HALFZ);
1140 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * FIRSTY].face, NO_FACE,
1141 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * LASTY].face,
1142 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * LASTZ].face, NO_FACE);
1143 for (j = 1; j < MAXSIZEY - 1; j++) {
1144 glTranslatef(0, S1, 0);
1146 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * j].face, NO_FACE,
1147 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face,
1150 glTranslatef(0, S1, 0);
1152 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * LASTY].face, NO_FACE,
1153 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face,
1154 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * FIRSTZ].face);
1155 for (k = 1; k < MAXSIZEZ - 1; k++) {
1156 glTranslatef(0, -SY, S1);
1159 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * LASTY].face,
1160 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
1161 for (j = 1; j < MAXSIZEY - 1; j++) {
1162 glTranslatef(0, S1, 0);
1165 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * REVY(j)].face,
1168 glTranslatef(0, S1, 0);
1171 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * FIRSTY].face,
1172 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * k].face);
1174 glTranslatef(0, -SY, S1);
1176 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * LASTY].face,
1177 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face,
1178 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
1179 for (j = 1; j < MAXSIZEY - 1; j++) {
1180 glTranslatef(0, S1, 0);
1182 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * REVY(j)].face,
1183 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face,
1186 glTranslatef(0, S1, 0);
1188 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * FIRSTY].face,
1189 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face,
1190 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * LASTZ].face);
1192 case FRONT_FACE: /* BACK_FACE too */
1194 if (slice.depth == MAXSIZEZ - 1)
1195 glRotatef(rotatestep, 0, 0, HALFZ);
1197 glTranslatef(-HALFX, -HALFY, -HALFZ);
1199 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * FIRSTY].face, NO_FACE,
1200 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face, NO_FACE,
1201 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * LASTZ].face, NO_FACE);
1202 for (i = 1; i < MAXSIZEX - 1; i++) {
1203 glTranslatef(S1, 0, 0);
1205 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * FIRSTY].face, NO_FACE,
1207 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * LASTZ].face, NO_FACE);
1209 glTranslatef(S1, 0, 0);
1211 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * FIRSTY].face, NO_FACE,
1212 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * LASTY].face,
1213 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * LASTZ].face, NO_FACE);
1214 for (j = 1; j < MAXSIZEY - 1; j++) {
1215 glTranslatef(-SX, S1, 0);
1217 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * j].face, NO_FACE,
1218 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
1220 for (i = 1; i < MAXSIZEX - 1; i++) {
1221 glTranslatef(S1, 0, 0);
1223 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * j].face, NO_FACE,
1227 glTranslatef(S1, 0, 0);
1229 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * j].face, NO_FACE,
1230 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face,
1233 glTranslatef(-SX, S1, 0);
1235 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * LASTY].face, NO_FACE,
1236 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
1237 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face);
1238 for (i = 1; i < MAXSIZEX - 1; i++) {
1239 glTranslatef(S1, 0, 0);
1241 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * LASTY].face, NO_FACE,
1243 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * FIRSTZ].face);
1245 glTranslatef(S1, 0, 0);
1247 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * LASTY].face, NO_FACE,
1248 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face,
1249 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * FIRSTZ].face);
1251 for (k = 1; k < MAXSIZEZ - 1; k++) {
1253 if (slice.depth == REVZ(k))
1254 glRotatef(rotatestep, 0, 0, HALFZ);
1255 glTranslatef(-HALFX, -HALFY, MIDZ(k));
1258 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * LASTY].face, NO_FACE,
1259 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
1260 for (i = 1; i < MAXSIZEX - 1; i++) {
1261 glTranslatef(S1, 0, 0);
1265 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * REVZ(k)].face, NO_FACE);
1267 glTranslatef(S1, 0, 0);
1270 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * LASTY].face,
1271 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
1272 for (j = 1; j < MAXSIZEY - 1; j++) {
1273 glTranslatef(-SX, S1, 0);
1276 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * REVY(j)].face, NO_FACE,
1279 glTranslatef(SX, 0, 0);
1282 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * REVY(j)].face,
1285 glTranslatef(-SX, S1, 0);
1288 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * FIRSTY].face, NO_FACE,
1289 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * k].face);
1290 for (i = 1; i < MAXSIZEX - 1; i++) {
1291 glTranslatef(S1, 0, 0);
1295 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * k].face);
1297 glTranslatef(S1, 0, 0);
1300 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * FIRSTY].face,
1301 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * k].face);
1304 if (slice.depth == 0)
1305 glRotatef(rotatestep, 0, 0, HALFZ);
1306 glTranslatef(-HALFX, -HALFY, HALFZ);
1308 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * LASTY].face,
1309 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * LASTY].face, NO_FACE,
1310 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
1311 for (i = 1; i < MAXSIZEX - 1; i++) {
1312 glTranslatef(S1, 0, 0);
1314 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * LASTY].face,
1316 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * FIRSTZ].face, NO_FACE);
1318 glTranslatef(S1, 0, 0);
1320 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * LASTY].face,
1321 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face,
1322 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
1323 for (j = 1; j < MAXSIZEY - 1; j++) {
1324 glTranslatef(-SX, S1, 0);
1326 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * REVY(j)].face,
1327 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
1329 for (i = 1; i < MAXSIZEX - 1; i++) {
1330 glTranslatef(S1, 0, 0);
1332 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * REVY(j)].face,
1336 glTranslatef(S1, 0, 0);
1338 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * REVY(j)].face,
1339 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face,
1342 glTranslatef(-SX, S1, 0);
1344 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * FIRSTY].face,
1345 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
1346 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * LASTZ].face);
1347 for (i = 1; i < MAXSIZEX - 1; i++) {
1348 glTranslatef(S1, 0, 0);
1350 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * FIRSTY].face,
1352 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * LASTZ].face);
1354 glTranslatef(S1, 0, 0);
1356 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * FIRSTY].face,
1357 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face,
1358 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * LASTZ].face);
1365 /* From David Bagley's xrubik. Used by permission. ;) */
1367 readRC(rubikstruct * rp, int face, int dir, int h, int orient, int size)
1371 sizeOfRow = sizeRow(rp, face);
1372 if (dir == TOP || dir == BOTTOM)
1373 for (g = 0; g < size; g++)
1374 rp->rowLoc[orient][g] =
1375 rp->cubeLoc[face][g * sizeOfRow + h];
1376 else /* dir == RIGHT || dir == LEFT */
1377 for (g = 0; g < size; g++)
1378 rp->rowLoc[orient][g] =
1379 rp->cubeLoc[face][h * sizeOfRow + g];
1383 rotateRC(rubikstruct * rp, int rotate, int orient, int size)
1387 for (g = 0; g < size; g++)
1388 rp->rowLoc[orient][g].rotation =
1389 (rp->rowLoc[orient][g].rotation + rotate) % MAXORIENT;
1393 reverseRC(rubikstruct * rp, int orient, int size)
1398 for (g = 0; g < size / 2; g++) {
1399 temp = rp->rowLoc[orient][size - 1 - g];
1400 rp->rowLoc[orient][size - 1 - g] = rp->rowLoc[orient][g];
1401 rp->rowLoc[orient][g] = temp;
1406 writeRC(rubikstruct * rp, int face, int dir, int h, int orient, int size)
1408 int g, position, sizeOfRow;
1410 sizeOfRow = sizeRow(rp, face);
1411 if (dir == TOP || dir == BOTTOM) {
1412 for (g = 0; g < size; g++) {
1413 position = g * sizeOfRow + h;
1414 rp->cubeLoc[face][position] = rp->rowLoc[orient][g];
1415 /* DrawSquare(face, position); */
1417 } else { /* dir == RIGHT || dir == LEFT */
1418 for (g = 0; g < size; g++) {
1419 position = h * sizeOfRow + g;
1420 rp->cubeLoc[face][position] = rp->rowLoc[orient][g];
1421 /* DrawSquare(face, position); */
1427 rotateFace(rubikstruct * rp, int face, int direction)
1429 int position, i, j, sizeOfRow, sizeOfColumn, sizeOnPlane;
1432 faceSizes(rp, face, &sizeOfRow, &sizeOfColumn);
1433 sizeOnPlane = sizeOfRow * sizeOfColumn;
1434 if ((faceLoc = (RubikLoc *) malloc(sizeOnPlane *
1435 sizeof (RubikLoc))) == NULL) {
1439 for (position = 0; position < sizeOnPlane; position++)
1440 faceLoc[position] = rp->cubeLoc[face][position];
1442 for (position = 0; position < sizeOnPlane; position++) {
1443 i = position % sizeOfRow;
1444 j = position / sizeOfRow;
1445 if (direction == CW)
1446 rp->cubeLoc[face][position] =
1447 faceLoc[(sizeOfRow - i - 1) * sizeOfRow + j];
1448 else if (direction == CCW)
1449 rp->cubeLoc[face][position] =
1450 faceLoc[i * sizeOfRow + sizeOfColumn - j - 1];
1451 else /* (direction == HALF) */
1452 rp->cubeLoc[face][position] =
1453 faceLoc[sizeOfRow - i - 1 + (sizeOfColumn - j - 1) * sizeOfRow];
1454 rp->cubeLoc[face][position].rotation =
1455 (rp->cubeLoc[face][position].rotation +
1456 direction - MAXORIENT) % MAXORIENT;
1457 /* DrawSquare(face, position); */
1459 if (faceLoc != NULL)
1460 (void) free((void *) faceLoc);
1464 /* Yeah this is big and ugly */
1466 slideRC(int face, int direction, int h, int sizeOnOppAxis,
1467 int *newFace, int *newDirection, int *newH,
1468 int *rotate, Bool *reverse)
1470 *newFace = slideNextRow[face][direction].face;
1471 *rotate = slideNextRow[face][direction].rotation;
1472 *newDirection = (*rotate + direction) % MAXORIENT;
1479 if (*newDirection == TOP || *newDirection == BOTTOM) {
1480 *newH = sizeOnOppAxis - 1 - h;
1482 } else { /* *newDirection == RIGHT || *newDirection == LEFT */
1488 *newH = sizeOnOppAxis - 1 - h;
1492 if (*newDirection == TOP || *newDirection == BOTTOM) {
1495 } else { /* *newDirection == RIGHT || *newDirection == LEFT */
1496 *newH = sizeOnOppAxis - 1 - h;
1501 (void) printf("slideRC: rotate %d\n", *rotate);
1508 moveRubik(rubikstruct * rp, int face, int direction, int position)
1510 int newFace, newDirection, rotate, reverse;
1512 int i, j, sizeOfRow, sizeOfColumn, sizeOnAxis, sizeOnOppAxis;
1514 faceSizes(rp, face, &sizeOfRow, &sizeOfColumn);
1515 if (direction == CW || direction == CCW) {
1516 direction = (direction == CCW) ?
1517 (rotateToRow[face].direction + 2) % MAXORIENT :
1518 rotateToRow[face].direction;
1519 if (rotateToRow[face].sideFace == RIGHT) {
1520 i = j = sizeOfColumn - 1;
1521 } else if (rotateToRow[face].sideFace == BOTTOM) {
1522 i = j = sizeOfRow - 1;
1526 face = rotateToRow[face].face;
1527 position = j * sizeOfRow + i;
1529 i = position % sizeOfRow;
1530 j = position / sizeOfRow;
1531 h = (direction == TOP || direction == BOTTOM) ? i : j;
1532 if (direction == TOP || direction == BOTTOM) {
1533 sizeOnAxis = sizeOfColumn;
1534 sizeOnOppAxis = sizeOfRow;
1536 sizeOnAxis = sizeOfRow;
1537 sizeOnOppAxis = sizeOfColumn;
1539 /* rotate sides CW or CCW or HALF) */
1541 if (h == sizeOnOppAxis - 1) {
1542 newDirection = (direction == TOP || direction == BOTTOM) ?
1544 if (rp->degreeTurn == 180) {
1545 if (!rotateFace(rp, rowToRotate[face][newDirection], HALF))
1547 } else if (direction == TOP || direction == RIGHT) {
1548 if (!rotateFace(rp, rowToRotate[face][newDirection], CW))
1550 } else { /* direction == BOTTOM || direction == LEFT */
1551 if (!rotateFace(rp, rowToRotate[face][newDirection], CCW))
1556 newDirection = (direction == TOP || direction == BOTTOM) ?
1558 if (rp->degreeTurn == 180) {
1559 if (!rotateFace(rp, rowToRotate[face][newDirection], HALF))
1561 } else if (direction == TOP || direction == RIGHT) {
1562 if (!rotateFace(rp, rowToRotate[face][newDirection], CCW))
1564 } else { /* direction == BOTTOM || direction == LEFT */
1565 if (!rotateFace(rp, rowToRotate[face][newDirection], CW))
1569 /* Slide rows or columns */
1570 readRC(rp, face, direction, h, 0, sizeOnAxis);
1571 if (rp->degreeTurn == 180) {
1572 int sizeOnDepthAxis;
1574 slideRC(face, direction, h, sizeOnOppAxis,
1575 &newFace, &newDirection, &newH, &rotate, &reverse);
1576 sizeOnDepthAxis = sizeFace(rp, newFace) / sizeOnOppAxis;
1577 readRC(rp, newFace, newDirection, newH, 1, sizeOnDepthAxis);
1578 rotateRC(rp, rotate, 0, sizeOnAxis);
1579 if (reverse == True)
1580 reverseRC(rp, 0, sizeOnAxis);
1582 direction = newDirection;
1584 for (k = 2; k <= MAXORIENT + 1; k++) {
1585 slideRC(face, direction, h, sizeOnOppAxis,
1586 &newFace, &newDirection, &newH, &rotate, &reverse);
1587 if (k != MAXORIENT && k != MAXORIENT + 1)
1588 readRC(rp, newFace, newDirection, newH, k,
1589 (k % 2) ? sizeOnDepthAxis : sizeOnAxis);
1590 rotateRC(rp, rotate, k - 2,
1591 (k % 2) ? sizeOnDepthAxis : sizeOnAxis);
1592 if (k != MAXORIENT + 1)
1593 rotateRC(rp, rotate, k - 1,
1594 (k % 2) ? sizeOnAxis : sizeOnDepthAxis);
1595 if (reverse == True) {
1596 reverseRC(rp, k - 2,
1597 (k % 2) ? sizeOnDepthAxis : sizeOnAxis);
1598 if (k != MAXORIENT + 1)
1599 reverseRC(rp, k - 1,
1600 (k % 2) ? sizeOnAxis : sizeOnDepthAxis);
1602 writeRC(rp, newFace, newDirection, newH, k - 2,
1603 (k % 2) ? sizeOnDepthAxis : sizeOnAxis);
1605 direction = newDirection;
1609 for (k = 1; k <= MAXORIENT; k++) {
1610 slideRC(face, direction, h, sizeOnOppAxis,
1611 &newFace, &newDirection, &newH, &rotate, &reverse);
1613 readRC(rp, newFace, newDirection, newH, k, sizeOnAxis);
1614 rotateRC(rp, rotate, k - 1, sizeOnAxis);
1615 if (reverse == True)
1616 reverseRC(rp, k - 1, sizeOnAxis);
1617 writeRC(rp, newFace, newDirection, newH, k - 1, sizeOnAxis);
1619 direction = newDirection;
1628 printCube(rubikstruct * rp)
1630 int face, position, sizeOfRow, sizeOfColumn;
1632 for (face = 0; face < MAXFACES; face++) {
1633 faceSizes(rp, face, &sizeOfRow, &sizeOfColumn);
1634 for (position = 0; position < sizeOfRow * sizeOfColumn; position++) {
1635 (void) printf("%d %d ", rp->cubeLoc[face][position].face,
1636 rp->cubeLoc[face][position].rotation);
1637 if (!((position + 1) % sizeOfRow))
1638 (void) printf("\n");
1640 (void) printf("\n");
1642 (void) printf("\n");
1648 evalmovement(ModeInfo * mi, RubikMove movement)
1650 rubikstruct *rp = &rubik[MI_SCREEN(mi)];
1655 if (movement.face < 0 || movement.face >= MAXFACES)
1657 if (!moveRubik(rp, movement.face, movement.direction, movement.position))
1663 compare_moves(rubikstruct * rp, RubikMove move1, RubikMove move2, Bool opp)
1665 RubikSlice slice1, slice2;
1667 convertMove(rp, move1, &slice1);
1668 convertMove(rp, move2, &slice2);
1669 if (slice1.face == slice2.face &&
1670 slice1.depth == slice2.depth) {
1671 if (slice1.rotation == slice2.rotation) { /* CW or CCW */
1683 shuffle(ModeInfo * mi)
1685 rubikstruct *rp = &rubik[MI_SCREEN(mi)];
1686 int i, face, position;
1694 i = NRAND(-i - MINSIZE + 1) + MINSIZE;
1695 else if (i < MINSIZE)
1698 if (LRAND() % 2 && !sizey && !sizez) { /* Make normal (NxNxN) cubes more likely */
1699 MAXSIZEX = MAXSIZEY = MAXSIZEZ = i;
1707 i = NRAND(-i - MINSIZE + 1) + MINSIZE;
1708 else if (i < MINSIZE)
1710 if (LRAND() % 2 && !sizez) { /* Make more MxNxN more likely than LxMxN */
1711 MAXSIZEY = MAXSIZEZ = i;
1719 i = NRAND(-i - MINSIZE + 1) + MINSIZE;
1720 else if (i < MINSIZE)
1726 for (face = 0; face < MAXFACES; face++) {
1727 if (rp->cubeLoc[face] != NULL)
1728 (void) free((void *) rp->cubeLoc[face]);
1729 if ((rp->cubeLoc[face] = (RubikLoc *) malloc(sizeFace(rp, face) *
1730 sizeof (RubikLoc))) == NULL) {
1733 for (position = 0; position < sizeFace(rp, face); position++) {
1734 rp->cubeLoc[face][position].face = face;
1735 rp->cubeLoc[face][position].rotation = TOP;
1738 for (i = 0; i < MAXORIENT; i++) {
1739 if (rp->rowLoc[i] != NULL)
1740 (void) free((void *) rp->rowLoc[i]);
1741 /* The following is reused so make it the biggest size */
1742 if ((rp->rowLoc[i] = (RubikLoc *) malloc(MAXMAXSIZE *
1743 sizeof (RubikLoc))) == NULL) {
1747 rp->storedmoves = MI_COUNT(mi);
1748 if (rp->storedmoves < 0) {
1749 if (rp->moves != NULL)
1750 (void) free((void *) rp->moves);
1751 rp->moves = (RubikMove *) NULL;
1752 rp->storedmoves = NRAND(-rp->storedmoves) + 1;
1754 if ((rp->storedmoves) && (rp->moves == NULL))
1755 if ((rp->moves = (RubikMove *) calloc(rp->storedmoves + 1,
1756 sizeof (RubikMove))) == NULL) {
1759 if (MI_CYCLES(mi) <= 1) {
1760 rp->anglestep = 90.0;
1762 rp->anglestep = 90.0 / (GLfloat) (MI_CYCLES(mi));
1765 for (i = 0; i < rp->storedmoves; i++) {
1769 move.face = NRAND(MAXFACES);
1770 move.direction = NRAND(MAXORIENT); /* Exclude CW and CCW, its ok */
1771 move.position = NRAND(sizeFace(rp, move.face));
1772 rp->degreeTurn = (checkFaceSquare(rp,
1773 rowToRotate[move.face][move.direction])) ? 90 : 180;
1775 if (i > 0) { /* avoid immediate undoing moves */
1776 if (compare_moves(rp, move, rp->moves[i - 1], True))
1778 if (rp->degreeTurn == 180 &&
1779 compare_moves(rp, move, rp->moves[i - 1], False))
1782 if (i > 1) /* avoid 3 consecutive identical moves */
1783 if (compare_moves(rp, move, rp->moves[i - 1], False) &&
1784 compare_moves(rp, move, rp->moves[i - 2], False))
1787 * Still some silly moves being made....
1789 } while (!condition);
1791 if (!evalmovement(mi, move))
1793 rp->moves[i] = move;
1796 if (NRAND(100) < 50)
1799 if (NRAND(100) < 50)
1801 rp->movement.face = NO_FACE;
1803 rp->action = hideshuffling ? ACTION_SOLVE : ACTION_SHUFFLE;
1804 rp->shufflingmoves = 0;
1810 reshape_rubik(ModeInfo * mi, int width, int height)
1812 rubikstruct *rp = &rubik[MI_SCREEN(mi)];
1814 glViewport(0, 0, rp->WindW = (GLint) width, rp->WindH = (GLint) height);
1815 glMatrixMode(GL_PROJECTION);
1817 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
1818 glMatrixMode(GL_MODELVIEW);
1823 rubik_handle_event (ModeInfo *mi, XEvent *event)
1825 rubikstruct *rp = &rubik[MI_SCREEN(mi)];
1827 if (event->xany.type == ButtonPress &&
1828 event->xbutton.button == Button1)
1830 rp->button_down_p = True;
1831 gltrackball_start (rp->trackball,
1832 event->xbutton.x, event->xbutton.y,
1833 MI_WIDTH (mi), MI_HEIGHT (mi));
1836 else if (event->xany.type == ButtonRelease &&
1837 event->xbutton.button == Button1)
1839 rp->button_down_p = False;
1842 else if (event->xany.type == ButtonPress &&
1843 (event->xbutton.button == Button4 ||
1844 event->xbutton.button == Button5 ||
1845 event->xbutton.button == Button6 ||
1846 event->xbutton.button == Button7))
1848 gltrackball_mousewheel (rp->trackball, event->xbutton.button, 10,
1849 !!event->xbutton.state);
1852 else if (event->xany.type == MotionNotify &&
1855 gltrackball_track (rp->trackball,
1856 event->xmotion.x, event->xmotion.y,
1857 MI_WIDTH (mi), MI_HEIGHT (mi));
1866 pinit(ModeInfo * mi)
1869 glColor3f(1.0, 1.0, 1.0);
1871 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
1872 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
1873 glLightfv(GL_LIGHT0, GL_POSITION, position0);
1874 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
1875 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
1876 glLightfv(GL_LIGHT1, GL_POSITION, position1);
1877 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
1878 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
1879 glEnable(GL_LIGHTING);
1880 glEnable(GL_LIGHT0);
1881 glEnable(GL_LIGHT1);
1882 glEnable(GL_DEPTH_TEST);
1883 glEnable(GL_NORMALIZE);
1884 glEnable(GL_CULL_FACE);
1886 glShadeModel(GL_FLAT);
1887 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
1888 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
1890 return (shuffle(mi));
1894 free_rubik(rubikstruct *rp)
1898 for (i = 0; i < MAXFACES; i++)
1899 if (rp->cubeLoc[i] != NULL) {
1900 (void) free((void *) rp->cubeLoc[i]);
1901 rp->cubeLoc[i] = (RubikLoc *) NULL;
1903 for (i = 0; i < MAXORIENT; i++)
1904 if (rp->rowLoc[i] != NULL) {
1905 (void) free((void *) rp->rowLoc[i]);
1906 rp->rowLoc[i] = (RubikLoc *) NULL;
1908 if (rp->moves != NULL) {
1909 (void) free((void *) rp->moves);
1910 rp->moves = (RubikMove *) NULL;
1915 release_rubik(ModeInfo * mi)
1917 if (rubik != NULL) {
1920 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
1921 rubikstruct *rp = &rubik[screen];
1925 (void) free((void *) rubik);
1926 rubik = (rubikstruct *) NULL;
1932 init_rubik(ModeInfo * mi)
1936 if (rubik == NULL) {
1937 if ((rubik = (rubikstruct *) calloc(MI_NUM_SCREENS(mi),
1938 sizeof (rubikstruct))) == NULL)
1941 rp = &rubik[MI_SCREEN(mi)];
1942 rp->step = NRAND(90);
1943 rp->PX = ((float) LRAND() / (float) MAXRAND) * 2.0 - 1.0;
1944 rp->PY = ((float) LRAND() / (float) MAXRAND) * 2.0 - 1.0;
1946 rp->trackball = gltrackball_init ();
1948 if ((rp->glx_context = init_GL(mi)) != NULL) {
1950 reshape_rubik(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
1951 glDrawBuffer(GL_BACK);
1954 if (MI_IS_VERBOSE(mi)) {
1955 (void) fprintf(stderr,
1956 "Could not allocate memory for rubik\n");
1966 draw_rubik(ModeInfo * mi)
1968 Bool bounced = False;
1969 Display *display = MI_DISPLAY(mi);
1970 Window window = MI_WINDOW(mi);
1975 rp = &rubik[MI_SCREEN(mi)];
1976 if (rp->cubeLoc[0] == NULL)
1979 MI_IS_DRAWN(mi) = True;
1980 if (!rp->glx_context)
1983 mi->polygon_count = 0;
1984 glXMakeCurrent(display, window, *(rp->glx_context));
1986 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1990 glTranslatef(0.0, 0.0, -10.0);
1996 rp->PY += (-1) - (rp->PY);
2001 rp->PY -= (rp->PY) - 1;
2006 rp->PX += (-1) - (rp->PX);
2011 rp->PX -= (rp->PX) - 1;
2016 rp->VX += ((float) LRAND() / (float) MAXRAND) * 0.002 - 0.001;
2017 rp->VY += ((float) LRAND() / (float) MAXRAND) * 0.002 - 0.001;
2022 if (rp->VX < -0.006)
2024 if (rp->VY < -0.006)
2027 if (!MI_IS_ICONIC(mi)) {
2028 glTranslatef(rp->PX, rp->PY, 0);
2029 glScalef(Scale4Window * rp->WindH / rp->WindW, Scale4Window, Scale4Window);
2031 glScalef(Scale4Iconic * rp->WindH / rp->WindW, Scale4Iconic, Scale4Iconic);
2034 /* Do it twice because we don't track the device's orientation. */
2035 glRotatef( current_device_rotation(), 0, 0, 1);
2036 gltrackball_rotate (rp->trackball);
2037 glRotatef(-current_device_rotation(), 0, 0, 1);
2039 glRotatef(rp->step * 100, 1, 0, 0);
2040 glRotatef(rp->step * 95, 0, 1, 0);
2041 glRotatef(rp->step * 90, 0, 0, 1);
2043 if (!draw_cube(mi)) {
2047 if (MI_IS_FPS(mi)) do_fps (mi);
2048 glXSwapBuffers(display, window);
2050 if (rp->action == ACTION_SHUFFLE) {
2052 if (++rp->rotatestep > DELAY_AFTER_SHUFFLING) {
2053 rp->movement.face = NO_FACE;
2055 rp->action = ACTION_SOLVE;
2059 if (rp->movement.face == NO_FACE) {
2060 if (rp->shufflingmoves < rp->storedmoves) {
2062 rp->movement = rp->moves[rp->shufflingmoves];
2068 if (rp->rotatestep == 0) {
2069 if (rp->movement.direction == CW || rp->movement.direction == CCW)
2070 rp->degreeTurn = (checkFaceSquare(rp, rp->movement.face)) ? 90 : 180;
2072 rp->degreeTurn = (checkFaceSquare(rp, rowToRotate[rp->movement.face][rp->movement.direction])) ? 90 : 180;
2074 rp->rotatestep += rp->anglestep;
2075 if (rp->rotatestep > rp->degreeTurn) {
2076 if (!evalmovement(mi, rp->movement)) {
2078 if (MI_IS_VERBOSE(mi)) {
2079 (void) fprintf(stderr,
2080 "Could not allocate memory for rubik\n");
2084 rp->shufflingmoves++;
2085 rp->movement.face = NO_FACE;
2091 if (++rp->rotatestep > DELAY_AFTER_SOLVING)
2094 if (MI_IS_VERBOSE(mi)) {
2095 (void) fprintf(stderr,
2096 "Could not allocate memory for rubik\n");
2101 if (rp->movement.face == NO_FACE) {
2102 if (rp->storedmoves > 0) {
2104 rp->movement = rp->moves[rp->storedmoves - 1];
2105 rp->movement.direction = (rp->movement.direction +
2106 (MAXORIENT / 2)) % MAXORIENT;
2112 if (rp->rotatestep == 0) {
2113 if (rp->movement.direction == CW || rp->movement.direction == CCW)
2114 rp->degreeTurn = (checkFaceSquare(rp, rp->movement.face)) ? 90 : 180;
2116 rp->degreeTurn = (checkFaceSquare(rp, rowToRotate[rp->movement.face][rp->movement.direction])) ? 90 : 180;
2118 rp->rotatestep += rp->anglestep;
2119 if (rp->rotatestep > rp->degreeTurn) {
2120 if (!evalmovement(mi, rp->movement)) {
2122 if (MI_IS_VERBOSE(mi)) {
2123 (void) fprintf(stderr,
2124 "Could not allocate memory for rubik\n");
2129 rp->movement.face = NO_FACE;
2144 change_rubik(ModeInfo * mi)
2150 rp = &rubik[MI_SCREEN(mi)];
2152 if (!rp->glx_context)
2156 if (MI_IS_VERBOSE(mi)) {
2157 (void) fprintf(stderr,
2158 "Could not allocate memory for rubik\n");
2163 #endif /* !STANDALONE */
2167 XSCREENSAVER_MODULE ("Rubik", rubik)