1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* rubik --- Shows a auto-solving Rubik's cube */
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)rubik.c 4.04 97/07/28 xlockmore";
13 * Permission to use, copy, modify, and distribute this software and its
14 * documentation for any purpose and without fee is hereby granted,
15 * provided that the above copyright notice appear in all copies and that
16 * both that copyright notice and this permission notice appear in
17 * supporting documentation.
19 * This file is provided AS IS with no warranties of any kind. The author
20 * shall have no liability with respect to the infringement of copyrights,
21 * trade secrets or any patents by this file or any part thereof. In no
22 * event will the author be liable for any lost revenue or profits or
23 * other special, indirect and consequential damages.
25 * This mode shows a auto-solving rubik's cube "puzzle". If somebody
26 * intends to make a game or something based on this code, please let me
27 * know first, my e-mail address is provided in this comment. Marcelo.
29 * Thanks goes also to Brian Paul for making it possible and inexpensive
30 * to use OpenGL at home.
32 * Since I'm not a native English speaker, my apologies for any grammatical
35 * My e-mail addresses are
40 * Marcelo F. Vianna (Jul-31-1997)
43 * 08-Aug-97: Now has some internals from xrubik by David Bagley
44 * This should make it easier to add features.
45 * 02-Aug-97: Now behaves more like puzzle.c: first show the cube being
46 * shuffled and then being solved. A mode specific option was added:
47 * "+/-hideshuffling" to provide the original behavior (in which
48 * only the solution is shown).
49 * The color labels corners are now rounded.
50 * Optimized the cubit() routine using glLists.
51 * 01-Aug-97: Shuffling now avoids movements that undoes the previous movement
52 * and three consecutive identical moves (which is pretty stupid).
53 * improved the "cycles" option in replacement of David's hack,
54 * now rp->anglestep is a GLfloat, so this option selects the
55 * "exact" number of frames that a rotation (movement) takes to
57 * 30-Jul-97: Initial release, there is no algorithm to solve the puzzle,
58 * instead, it randomly shuffle the cube and then make the
59 * movements in the reverse order.
60 * The mode was written in 1 day (I got sick and had the day off).
61 * There was not much to do since I could not leave home... :)
66 * Color labels mapping:
67 * =====================
77 * +-----------+------------+-----------+
81 * | LEFT(1) | FRONT(2) | RIGHT(3) |
85 * +-----------+------------+-----------+
89 * | BOTTOM(4) | rp->faces[N][X+AVSIZE*Y]=
90 * | | rp->cubeLoc[N][X+AVSIZE*Y]=
93 * +------------+ | | | |
94 * |0--> | | 0 | 1 | 2 |
97 * | BACK(5) | | 3 | 4 | 5 |
101 * +------------+ +---+---+---+
113 * PURIFY 3.0a on SunOS4 reports an unitialized memory read on each of
114 * the glCallList() functions below when using MesaGL 2.1. This has
115 * been fixed in MesaGL 2.2 and later releases.
119 * due to a Bug/feature in VMS X11/Intrinsic.h has to be placed before xlock.
120 * otherwise caddr_t is not defined correctly
122 #include <X11/Intrinsic.h>
125 # define PROGCLASS "Rubik"
126 # define HACK_INIT init_rubik
127 # define HACK_DRAW draw_rubik
128 # define rubik_opts xlockmore_opts
129 # define DEFAULTS "*delay: 40000 \n" \
133 # include "xlockmore.h" /* from the xscreensaver distribution */
134 #else /* !STANDALONE */
135 # include "xlock.h" /* from the xlockmore distribution */
136 #endif /* !STANDALONE */
140 #define DEF_HIDESHUFFLING "False"
142 static Bool hideshuffling;
144 static XrmOptionDescRec opts[] =
146 {"-hideshuffling", ".rubik.hideshuffling", XrmoptionNoArg, (caddr_t) "on"},
147 {"+hideshuffling", ".rubik.hideshuffling", XrmoptionNoArg, (caddr_t) "off"}
150 static argtype vars[] =
152 {(caddr_t *) & hideshuffling, "hideshuffling", "Hideshuffling", DEF_HIDESHUFFLING, t_Bool}
155 static OptionStruct desc[] =
157 {"-/+hideshuffling", "turn on/off hidden shuffle phase"}
160 ModeSpecOpt rubik_opts =
161 {2, opts, 1, vars, desc};
163 #define VectMul(X1,Y1,Z1,X2,Y2,Z2) (Y1)*(Z2)-(Z1)*(Y2),(Z1)*(X2)-(X1)*(Z2),(X1)*(Y2)-(Y1)*(X2)
164 #define sqr(A) ((A)*(A))
171 #define ACTION_SOLVE 1
172 #define ACTION_SHUFFLE 0
174 #define DELAY_AFTER_SHUFFLING 5
175 #define DELAY_AFTER_SOLVING 20
177 /*************************************************************************/
180 #ifdef LMN /* LxMxN not completed yet... */
181 #define MAXSIZEX (rp->sizex)
182 #define MAXSIZEY (rp->sizey)
183 #define MAXSIZEZ (rp->sizez)
184 #define AVSIZE (rp->avsize)
185 #define MAXSIZE (rp->maxsize)
186 #define AVSIZESQ (rp->avsizeSq)
187 #define MAXSIZESQ (rp->maxsizeSq)
189 #define MAXSIZEX (rp->size)
190 #define MAXSIZEY (rp->size)
191 #define MAXSIZEZ (rp->size)
192 #define AVSIZE (rp->size)
193 #define MAXSIZE (rp->size)
194 #define AVSIZESQ (rp->sizeSq)
195 #define MAXSIZESQ (rp->sizeSq)
197 #define MAXSIZEXY (MAXSIZEX*MAXSIZEY)
198 #define MAXSIZEZY (MAXSIZEZ*MAXSIZEY)
199 #define MAXSIZEXZ (MAXSIZEX*MAXSIZEZ)
200 #define LASTX (MAXSIZEX-1)
201 #define LASTY (MAXSIZEY-1)
202 #define LASTZ (MAXSIZEZ-1)
203 /* These are not likely to change but... */
208 #define Scale4Window (0.9/AVSIZE)
209 #define Scale4Iconic (2.1/AVSIZE)
211 #define MAXORIENT 4 /* Number of orientations of a square */
212 #define MAXFACES 6 /* Number of faces */
214 /* Directions relative to the face of a cubie */
219 #define CW (MAXORIENT+1)
220 #define CCW (2*MAXORIENT-1)
226 #define BOTTOM_FACE 4
228 #define NO_FACE (MAXFACES)
229 #define NO_ROTATION (2*MAXORIENT)
230 #define NO_DEPTH MAXSIZE
232 #define REVX(a) (MAXSIZEX - a - 1)
233 #define REVY(a) (MAXSIZEY - a - 1)
234 #define REVZ(a) (MAXSIZEZ - a - 1)
236 typedef struct _RubikLoc {
238 int rotation; /* Not used yet */
241 typedef struct _RubikRowNext {
242 int face, direction, sideFace;
245 typedef struct _RubikMove {
250 typedef struct _RubikSlice {
256 * Pick a face and a direction on face the next face and orientation
259 static RubikLoc slideNextRow[MAXFACES][MAXORIENT] =
294 * Examine cubie 0 on each face, its 4 movements (well only 2 since the
295 * other 2 will be opposites) and translate it into slice movements).
296 * Beware.. using this for NxNxN makes some assumptions that referenced
297 * cubes are along the diagonal top-left to bottom-right.
298 * CW = DEEP Depth CCW == SHALLOW Depth with reference to faces 0, 1, and 2
300 static RubikLoc rotateSlice[MAXFACES][MAXORIENT / 2] =
329 * Rotate face clockwise by a number of orients, then the top of the
330 * face then points to this face
332 static int rowToRotate[MAXFACES][MAXORIENT] =
343 * This translates a clockwise move to something more manageable
345 static RubikRowNext rotateToRow[MAXFACES] = /*CW to min face */
361 #ifdef LMN /* Under construction */
362 int sizex, sizey, sizez;
364 int avsizeSq, maxsizeSq;
371 RubikLoc *cubeLoc[MAXFACES];
372 RubikLoc *rowLoc[MAXORIENT];
375 GLXContext glx_context;
376 int AreObjectsDefined[1];
379 static float front_shininess[] =
381 static float front_specular[] =
382 {0.7, 0.7, 0.7, 1.0};
383 static float ambient[] =
384 {0.0, 0.0, 0.0, 1.0};
385 static float diffuse[] =
386 {1.0, 1.0, 1.0, 1.0};
387 static float position0[] =
388 {1.0, 1.0, 1.0, 0.0};
389 static float position1[] =
390 {-1.0, -1.0, 1.0, 0.0};
391 static float lmodel_ambient[] =
392 {0.5, 0.5, 0.5, 1.0};
393 static float lmodel_twoside[] =
396 static float MaterialRed[] =
397 {0.5, 0.0, 0.0, 1.0};
398 static float MaterialGreen[] =
399 {0.0, 0.5, 0.0, 1.0};
400 static float MaterialBlue[] =
401 {0.0, 0.0, 0.5, 1.0};
402 static float MaterialYellow[] =
403 {0.7, 0.7, 0.0, 1.0};
404 static float MaterialOrange[] =
405 {0.9, 0.45, 0.36, 1.0};
408 static float MaterialMagenta[] =
409 {0.7, 0.0, 0.7, 1.0};
410 static float MaterialCyan[] =
411 {0.0, 0.7, 0.7, 1.0};
414 static float MaterialWhite[] =
415 {0.8, 0.8, 0.8, 1.0};
416 static float MaterialGray[] =
417 {0.2, 0.2, 0.2, 1.0};
418 static float MaterialGray3[] =
419 {0.3, 0.3, 0.3, 1.0};
420 static float MaterialGray4[] =
421 {0.4, 0.4, 0.4, 1.0};
422 static float MaterialGray5[] =
423 {0.5, 0.5, 0.5, 1.0};
424 static float MaterialGray6[] =
425 {0.6, 0.6, 0.6, 1.0};
426 static float MaterialGray7[] =
427 {0.7, 0.7, 0.7, 1.0};
429 static rubikstruct *rubik = NULL;
430 static GLuint objects;
435 pickcolor(int C, int mono)
440 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray3);
442 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialRed);
446 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray6);
448 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialYellow);
451 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
455 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray4);
457 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGreen);
461 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray7);
463 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialOrange);
467 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray5);
469 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialBlue);
472 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialCyan);
473 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialMagenta);
480 draw_cubit(ModeInfo * mi,
481 int back, int front, int left, int right, int bottom, int top)
483 rubikstruct *rp = &rubik[MI_SCREEN(mi)];
484 int mono = MI_WIN_IS_MONO(mi);
486 if (!rp->AreObjectsDefined[ObjCubit]) {
487 glNewList(objects + ObjCubit, GL_COMPILE_AND_EXECUTE);
489 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray);
490 glNormal3f(0.00, 0.00, 1.00);
491 glVertex3f(-0.45, -0.45, 0.50);
492 glVertex3f(0.45, -0.45, 0.50);
493 glVertex3f(0.45, 0.45, 0.50);
494 glVertex3f(-0.45, 0.45, 0.50);
495 glNormal3f(0.00, 0.00, -1.00);
496 glVertex3f(-0.45, 0.45, -0.50);
497 glVertex3f(0.45, 0.45, -0.50);
498 glVertex3f(0.45, -0.45, -0.50);
499 glVertex3f(-0.45, -0.45, -0.50);
500 glNormal3f(-1.00, 0.00, 0.00);
501 glVertex3f(-0.50, -0.45, 0.45);
502 glVertex3f(-0.50, 0.45, 0.45);
503 glVertex3f(-0.50, 0.45, -0.45);
504 glVertex3f(-0.50, -0.45, -0.45);
505 glNormal3f(1.00, 0.00, 0.00);
506 glVertex3f(0.50, -0.45, -0.45);
507 glVertex3f(0.50, 0.45, -0.45);
508 glVertex3f(0.50, 0.45, 0.45);
509 glVertex3f(0.50, -0.45, 0.45);
510 glNormal3f(0.00, -1.00, 0.00);
511 glVertex3f(0.45, -0.50, -0.45);
512 glVertex3f(0.45, -0.50, 0.45);
513 glVertex3f(-0.45, -0.50, 0.45);
514 glVertex3f(-0.45, -0.50, -0.45);
515 glNormal3f(0.00, 1.00, 0.00);
516 glVertex3f(-0.45, 0.50, -0.45);
517 glVertex3f(-0.45, 0.50, 0.45);
518 glVertex3f(0.45, 0.50, 0.45);
519 glVertex3f(0.45, 0.50, -0.45);
520 glNormal3f(-1.00, -1.00, 0.00);
521 glVertex3f(-0.45, -0.50, -0.45);
522 glVertex3f(-0.45, -0.50, 0.45);
523 glVertex3f(-0.50, -0.45, 0.45);
524 glVertex3f(-0.50, -0.45, -0.45);
525 glNormal3f(1.00, 1.00, 0.00);
526 glVertex3f(0.45, 0.50, -0.45);
527 glVertex3f(0.45, 0.50, 0.45);
528 glVertex3f(0.50, 0.45, 0.45);
529 glVertex3f(0.50, 0.45, -0.45);
530 glNormal3f(-1.00, 1.00, 0.00);
531 glVertex3f(-0.50, 0.45, -0.45);
532 glVertex3f(-0.50, 0.45, 0.45);
533 glVertex3f(-0.45, 0.50, 0.45);
534 glVertex3f(-0.45, 0.50, -0.45);
535 glNormal3f(1.00, -1.00, 0.00);
536 glVertex3f(0.50, -0.45, -0.45);
537 glVertex3f(0.50, -0.45, 0.45);
538 glVertex3f(0.45, -0.50, 0.45);
539 glVertex3f(0.45, -0.50, -0.45);
540 glNormal3f(0.00, -1.00, -1.00);
541 glVertex3f(-0.45, -0.45, -0.50);
542 glVertex3f(0.45, -0.45, -0.50);
543 glVertex3f(0.45, -0.50, -0.45);
544 glVertex3f(-0.45, -0.50, -0.45);
545 glNormal3f(0.00, 1.00, 1.00);
546 glVertex3f(-0.45, 0.45, 0.50);
547 glVertex3f(0.45, 0.45, 0.50);
548 glVertex3f(0.45, 0.50, 0.45);
549 glVertex3f(-0.45, 0.50, 0.45);
550 glNormal3f(0.00, -1.00, 1.00);
551 glVertex3f(-0.45, -0.50, 0.45);
552 glVertex3f(0.45, -0.50, 0.45);
553 glVertex3f(0.45, -0.45, 0.50);
554 glVertex3f(-0.45, -0.45, 0.50);
555 glNormal3f(0.00, 1.00, -1.00);
556 glVertex3f(-0.45, 0.50, -0.45);
557 glVertex3f(0.45, 0.50, -0.45);
558 glVertex3f(0.45, 0.45, -0.50);
559 glVertex3f(-0.45, 0.45, -0.50);
560 glNormal3f(-1.00, 0.00, -1.00);
561 glVertex3f(-0.50, -0.45, -0.45);
562 glVertex3f(-0.50, 0.45, -0.45);
563 glVertex3f(-0.45, 0.45, -0.50);
564 glVertex3f(-0.45, -0.45, -0.50);
565 glNormal3f(1.00, 0.00, 1.00);
566 glVertex3f(0.50, -0.45, 0.45);
567 glVertex3f(0.50, 0.45, 0.45);
568 glVertex3f(0.45, 0.45, 0.50);
569 glVertex3f(0.45, -0.45, 0.50);
570 glNormal3f(1.00, 0.00, -1.00);
571 glVertex3f(0.45, -0.45, -0.50);
572 glVertex3f(0.45, 0.45, -0.50);
573 glVertex3f(0.50, 0.45, -0.45);
574 glVertex3f(0.50, -0.45, -0.45);
575 glNormal3f(-1.00, 0.00, 1.00);
576 glVertex3f(-0.45, -0.45, 0.50);
577 glVertex3f(-0.45, 0.45, 0.50);
578 glVertex3f(-0.50, 0.45, 0.45);
579 glVertex3f(-0.50, -0.45, 0.45);
581 glBegin(GL_TRIANGLES);
582 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray);
583 glNormal3f(1.00, 1.00, 1.00);
584 glVertex3f(0.45, 0.45, 0.50);
585 glVertex3f(0.50, 0.45, 0.45);
586 glVertex3f(0.45, 0.50, 0.45);
587 glNormal3f(-1.00, -1.00, -1.00);
588 glVertex3f(-0.45, -0.50, -0.45);
589 glVertex3f(-0.50, -0.45, -0.45);
590 glVertex3f(-0.45, -0.45, -0.50);
591 glNormal3f(-1.00, 1.00, 1.00);
592 glVertex3f(-0.45, 0.45, 0.50);
593 glVertex3f(-0.45, 0.50, 0.45);
594 glVertex3f(-0.50, 0.45, 0.45);
595 glNormal3f(1.00, -1.00, -1.00);
596 glVertex3f(0.50, -0.45, -0.45);
597 glVertex3f(0.45, -0.50, -0.45);
598 glVertex3f(0.45, -0.45, -0.50);
599 glNormal3f(1.00, -1.00, 1.00);
600 glVertex3f(0.45, -0.45, 0.50);
601 glVertex3f(0.45, -0.50, 0.45);
602 glVertex3f(0.50, -0.45, 0.45);
603 glNormal3f(-1.00, 1.00, -1.00);
604 glVertex3f(-0.50, 0.45, -0.45);
605 glVertex3f(-0.45, 0.50, -0.45);
606 glVertex3f(-0.45, 0.45, -0.50);
607 glNormal3f(-1.00, -1.00, 1.00);
608 glVertex3f(-0.45, -0.45, 0.50);
609 glVertex3f(-0.50, -0.45, 0.45);
610 glVertex3f(-0.45, -0.50, 0.45);
611 glNormal3f(1.00, 1.00, -1.00);
612 glVertex3f(0.50, 0.45, -0.45);
613 glVertex3f(0.45, 0.45, -0.50);
614 glVertex3f(0.45, 0.50, -0.45);
617 rp->AreObjectsDefined[ObjCubit] = 1;
619 (void) printf("Cubit drawn SLOWLY\n");
622 glCallList(objects + ObjCubit);
624 (void) printf("Cubit drawn quickly\n");
628 if (back != NO_FACE) {
630 pickcolor(back, mono);
631 glNormal3f(0.00, 0.00, -1.00);
632 glVertex3f(-0.35, 0.40, -0.51);
633 glVertex3f(0.35, 0.40, -0.51);
634 glVertex3f(0.40, 0.35, -0.51);
635 glVertex3f(0.40, -0.35, -0.51);
636 glVertex3f(0.35, -0.40, -0.51);
637 glVertex3f(-0.35, -0.40, -0.51);
638 glVertex3f(-0.40, -0.35, -0.51);
639 glVertex3f(-0.40, 0.35, -0.51);
642 if (front != NO_FACE) {
644 pickcolor(front, mono);
645 glNormal3f(0.00, 0.00, 1.00);
646 glVertex3f(-0.35, -0.40, 0.51);
647 glVertex3f(0.35, -0.40, 0.51);
648 glVertex3f(0.40, -0.35, 0.51);
649 glVertex3f(0.40, 0.35, 0.51);
650 glVertex3f(0.35, 0.40, 0.51);
651 glVertex3f(-0.35, 0.40, 0.51);
652 glVertex3f(-0.40, 0.35, 0.51);
653 glVertex3f(-0.40, -0.35, 0.51);
656 if (left != NO_FACE) {
658 pickcolor(left, mono);
659 glNormal3f(-1.00, 0.00, 0.00);
660 glVertex3f(-0.51, -0.35, 0.40);
661 glVertex3f(-0.51, 0.35, 0.40);
662 glVertex3f(-0.51, 0.40, 0.35);
663 glVertex3f(-0.51, 0.40, -0.35);
664 glVertex3f(-0.51, 0.35, -0.40);
665 glVertex3f(-0.51, -0.35, -0.40);
666 glVertex3f(-0.51, -0.40, -0.35);
667 glVertex3f(-0.51, -0.40, 0.35);
670 if (right != NO_FACE) {
672 pickcolor(right, mono);
673 glNormal3f(1.00, 0.00, 0.00);
674 glVertex3f(0.51, -0.35, -0.40);
675 glVertex3f(0.51, 0.35, -0.40);
676 glVertex3f(0.51, 0.40, -0.35);
677 glVertex3f(0.51, 0.40, 0.35);
678 glVertex3f(0.51, 0.35, 0.40);
679 glVertex3f(0.51, -0.35, 0.40);
680 glVertex3f(0.51, -0.40, 0.35);
681 glVertex3f(0.51, -0.40, -0.35);
684 if (bottom != NO_FACE) {
686 pickcolor(bottom, mono);
687 glNormal3f(0.00, -1.00, 0.00);
688 glVertex3f(0.40, -0.51, -0.35);
689 glVertex3f(0.40, -0.51, 0.35);
690 glVertex3f(0.35, -0.51, 0.40);
691 glVertex3f(-0.35, -0.51, 0.40);
692 glVertex3f(-0.40, -0.51, 0.35);
693 glVertex3f(-0.40, -0.51, -0.35);
694 glVertex3f(-0.35, -0.51, -0.40);
695 glVertex3f(0.35, -0.51, -0.40);
698 if (top != NO_FACE) {
700 pickcolor(top, mono);
701 glNormal3f(0.00, 1.00, 0.00);
702 glVertex3f(-0.40, 0.51, -0.35);
703 glVertex3f(-0.40, 0.51, 0.35);
704 glVertex3f(-0.35, 0.51, 0.40);
705 glVertex3f(0.35, 0.51, 0.40);
706 glVertex3f(0.40, 0.51, 0.35);
707 glVertex3f(0.40, 0.51, -0.35);
708 glVertex3f(0.35, 0.51, -0.40);
709 glVertex3f(-0.35, 0.51, -0.40);
717 convertMove(rubikstruct * rp, RubikMove move)
722 plane = rotateSlice[(int) move.face][move.direction % 2];
723 slice.face = plane.face;
724 slice.rotation = plane.rotation;
725 if (slice.rotation == CW) /* I just know this to be true... */
726 slice.depth = AVSIZESQ - 1 - move.position;
728 slice.depth = move.position;
729 slice.depth = slice.depth / AVSIZE;
730 /* If slice.depth = 0 then face 0, face 1, or face 2 moves */
731 if (move.direction / 2)
732 slice.rotation = (plane.rotation == CW) ? CCW : CW;
736 /* Assume for the moment that the size is at least 2 */
738 draw_cube(ModeInfo * mi)
741 #define SX ((GLint)S1*(MAXSIZEX-1))
742 #define SY ((GLint)S1*(MAXSIZEY-1))
743 #define SZ ((GLint)S1*(MAXSIZEZ-1))
744 #define HALFX (((GLfloat)MAXSIZEX-1.0)/2.0)
745 #define HALFY (((GLfloat)MAXSIZEY-1.0)/2.0)
746 #define HALFZ (((GLfloat)MAXSIZEZ-1.0)/2.0)
747 #define MIDX(a) (((GLfloat)(2*a-MAXSIZEX+1))/2.0)
748 #define MIDY(a) (((GLfloat)(2*a-MAXSIZEY+1))/2.0)
749 #define MIDZ(a) (((GLfloat)(2*a-MAXSIZEZ+1))/2.0)
750 rubikstruct *rp = &rubik[MI_SCREEN(mi)];
755 if (rp->movement.face == NO_FACE) {
756 slice.face = NO_FACE;
757 slice.rotation = NO_ROTATION;
758 slice.depth = NO_DEPTH;
760 slice = convertMove(rp, rp->movement);
762 rotatestep = (slice.rotation == CCW) ? rp->rotatestep : -rp->rotatestep;
765 * The glRotatef() routine transforms the coordinate system for every future
766 * vertex specification (this is not so simple, but by now comprehending this
767 * is sufficient). So if you want to rotate the inner slice, you can draw
768 * one slice, rotate the anglestep for the centerslice, draw the inner slice,
769 * rotate reversely and draw the other slice.
770 * There is a sequence for drawing cubies for each axis being moved...
772 switch (slice.face) {
774 case TOP_FACE: /* BOTTOM_FACE too */
776 if (slice.depth == MAXSIZEY - 1)
777 glRotatef(rotatestep, 0, HALFY, 0);
779 glTranslatef(-HALFX, -HALFY, -HALFZ);
781 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * FIRSTY].face, NO_FACE,
782 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face, NO_FACE,
783 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * LASTZ].face, NO_FACE);
784 for (k = 1; k < MAXSIZEZ - 1; k++) {
785 glTranslatef(0, 0, S1);
788 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * LASTY].face, NO_FACE,
789 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
791 glTranslatef(0, 0, S1);
793 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * LASTY].face,
794 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * LASTY].face, NO_FACE,
795 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
796 for (i = 1; i < MAXSIZEX - 1; i++) {
797 glTranslatef(S1, 0, -SZ);
799 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * FIRSTY].face, NO_FACE,
801 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * LASTZ].face, NO_FACE);
802 for (k = 1; k < MAXSIZEZ - 1; k++) {
803 glTranslatef(0, 0, S1);
807 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * REVZ(k)].face, NO_FACE);
809 glTranslatef(0, 0, S1);
811 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * LASTY].face,
813 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * FIRSTZ].face, NO_FACE);
815 glTranslatef(1, 0, -SZ);
817 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * FIRSTY].face, NO_FACE,
818 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * LASTY].face,
819 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * LASTZ].face, NO_FACE);
820 for (k = 1; k < MAXSIZEZ - 1; k++) {
821 glTranslatef(0, 0, S1);
824 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * LASTY].face,
825 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
827 glTranslatef(0, 0, S1);
829 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * LASTY].face,
830 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face,
831 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
833 for (j = 1; j < MAXSIZEY - 1; j++) {
835 if (slice.depth == REVY(j))
836 glRotatef(rotatestep, 0, HALFY, 0);
837 glTranslatef(-HALFX, MIDY(j), -HALFZ);
839 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * j].face, NO_FACE,
840 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
842 for (k = 1; k < MAXSIZEZ - 1; k++) {
843 glTranslatef(0, 0, S1);
846 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * REVY(j)].face, NO_FACE,
849 glTranslatef(0, 0, S1);
851 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * REVY(j)].face,
852 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
854 for (i = 1; i < MAXSIZEX - 1; i++) {
855 glTranslatef(1, 0, -SZ);
857 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * j].face, NO_FACE,
861 glTranslatef(0, 0, SZ);
863 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * REVY(j)].face,
867 glTranslatef(S1, 0, -SZ);
869 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * j].face, NO_FACE,
870 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face,
872 for (k = 1; k < MAXSIZEZ - 1; k++) {
873 glTranslatef(0, 0, S1);
876 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * REVY(j)].face,
879 glTranslatef(0, 0, S1);
881 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * REVY(j)].face,
882 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face,
886 if (slice.depth == 0)
887 glRotatef(rotatestep, 0, HALFY, 0);
889 glTranslatef(-HALFX, HALFY, -HALFZ);
891 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * LASTY].face, NO_FACE,
892 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
893 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face);
894 for (k = 1; k < MAXSIZEZ - 1; k++) {
895 glTranslatef(0, 0, S1);
898 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * FIRSTY].face, NO_FACE,
899 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * k].face);
901 glTranslatef(0, 0, S1);
903 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * FIRSTY].face,
904 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
905 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * LASTZ].face);
906 for (i = 1; i < MAXSIZEX - 1; i++) {
907 glTranslatef(S1, 0, -SZ);
909 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * LASTY].face, NO_FACE,
911 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * FIRSTZ].face);
912 for (k = 1; k < MAXSIZEZ - 1; k++) {
913 glTranslatef(0, 0, S1);
917 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * k].face);
919 glTranslatef(0, 0, S1);
921 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * FIRSTY].face,
923 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * LASTZ].face);
925 glTranslatef(S1, 0, -SZ);
927 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * LASTY].face, NO_FACE,
928 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face,
929 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * FIRSTZ].face);
930 for (k = 1; k < MAXSIZEZ - 1; k++) {
931 glTranslatef(0, 0, S1);
934 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * FIRSTY].face,
935 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * k].face);
937 glTranslatef(0, 0, S1);
939 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * FIRSTY].face,
940 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face,
941 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * LASTZ].face);
943 case LEFT_FACE: /* RIGHT_FACE too */
944 /* rotatestep is negative because the RIGHT face is the default here */
946 if (slice.depth == 0)
947 glRotatef(-rotatestep, HALFX, 0, 0);
949 glTranslatef(-HALFX, -HALFY, -HALFZ);
951 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * FIRSTY].face, NO_FACE,
952 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face, NO_FACE,
953 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * LASTZ].face, NO_FACE);
954 for (j = 1; j < MAXSIZEY - 1; j++) {
955 glTranslatef(0, S1, 0);
957 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * j].face, NO_FACE,
958 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
961 glTranslatef(0, S1, 0);
963 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * LASTY].face, NO_FACE,
964 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
965 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face);
966 for (k = 1; k < MAXSIZEZ - 1; k++) {
967 glTranslatef(0, -SY, S1);
970 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * LASTY].face, NO_FACE,
971 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
972 for (j = 1; j < MAXSIZEY - 1; j++) {
973 glTranslatef(0, S1, 0);
976 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * REVY(j)].face, NO_FACE,
979 glTranslatef(0, S1, 0);
982 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * FIRSTY].face, NO_FACE,
983 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * k].face);
985 glTranslatef(0, -SY, S1);
987 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * LASTY].face,
988 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * LASTY].face, NO_FACE,
989 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
990 for (j = 1; j < MAXSIZEY - 1; j++) {
991 glTranslatef(0, S1, 0);
993 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * REVY(j)].face,
994 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
997 glTranslatef(0, S1, 0);
999 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * FIRSTY].face,
1000 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
1001 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * LASTZ].face);
1003 for (i = 1; i < MAXSIZEX - 1; i++) {
1005 if (slice.depth == i)
1006 glRotatef(-rotatestep, HALFX, 0, 0);
1007 glTranslatef(MIDX(i), -HALFY, -HALFZ);
1009 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * FIRSTY].face, NO_FACE,
1011 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * LASTZ].face, NO_FACE);
1012 for (j = 1; j < MAXSIZEY - 1; j++) {
1013 glTranslatef(0, S1, 0);
1015 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * j].face, NO_FACE,
1019 glTranslatef(0, S1, 0);
1021 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * LASTY].face, NO_FACE,
1023 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * FIRSTZ].face);
1024 for (k = 1; k < MAXSIZEZ - 1; k++) {
1025 glTranslatef(0, -SY, S1);
1029 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * REVZ(k)].face, NO_FACE);
1031 glTranslatef(0, SY, 0);
1035 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * k].face);
1037 glTranslatef(0, -SY, S1);
1039 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * LASTY].face,
1041 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * FIRSTZ].face, NO_FACE);
1042 for (j = 1; j < MAXSIZEY - 1; j++) {
1043 glTranslatef(0, S1, 0);
1045 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * REVY(j)].face,
1049 glTranslatef(0, S1, 0);
1051 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * FIRSTY].face,
1053 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * LASTZ].face);
1056 if (slice.depth == MAXSIZEX - 1)
1057 glRotatef(-rotatestep, HALFX, 0, 0);
1058 glTranslatef(HALFX, -HALFY, -HALFZ);
1060 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * FIRSTY].face, NO_FACE,
1061 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * LASTY].face,
1062 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * LASTZ].face, NO_FACE);
1063 for (j = 1; j < MAXSIZEY - 1; j++) {
1064 glTranslatef(0, S1, 0);
1066 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * j].face, NO_FACE,
1067 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face,
1070 glTranslatef(0, S1, 0);
1072 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * LASTY].face, NO_FACE,
1073 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face,
1074 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * FIRSTZ].face);
1075 for (k = 1; k < MAXSIZEZ - 1; k++) {
1076 glTranslatef(0, -SY, S1);
1079 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * LASTY].face,
1080 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
1081 for (j = 1; j < MAXSIZEY - 1; j++) {
1082 glTranslatef(0, S1, 0);
1085 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * REVY(j)].face,
1088 glTranslatef(0, S1, 0);
1091 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * FIRSTY].face,
1092 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * k].face);
1094 glTranslatef(0, -SY, S1);
1096 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * LASTY].face,
1097 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face,
1098 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
1099 for (j = 1; j < MAXSIZEY - 1; j++) {
1100 glTranslatef(0, S1, 0);
1102 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * REVY(j)].face,
1103 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face,
1106 glTranslatef(0, S1, 0);
1108 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * FIRSTY].face,
1109 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face,
1110 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * LASTZ].face);
1112 case FRONT_FACE: /* BACK_FACE too */
1114 if (slice.depth == MAXSIZEZ - 1)
1115 glRotatef(rotatestep, 0, 0, HALFZ);
1117 glTranslatef(-HALFX, -HALFY, -HALFZ);
1119 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * FIRSTY].face, NO_FACE,
1120 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face, NO_FACE,
1121 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * LASTZ].face, NO_FACE);
1122 for (i = 1; i < MAXSIZEX - 1; i++) {
1123 glTranslatef(S1, 0, 0);
1125 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * FIRSTY].face, NO_FACE,
1127 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * LASTZ].face, NO_FACE);
1129 glTranslatef(S1, 0, 0);
1131 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * FIRSTY].face, NO_FACE,
1132 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * LASTY].face,
1133 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * LASTZ].face, NO_FACE);
1134 for (j = 1; j < MAXSIZEY - 1; j++) {
1135 glTranslatef(-SX, S1, 0);
1137 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * j].face, NO_FACE,
1138 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
1140 for (i = 1; i < MAXSIZEX - 1; i++) {
1141 glTranslatef(S1, 0, 0);
1143 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * j].face, NO_FACE,
1147 glTranslatef(S1, 0, 0);
1149 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * j].face, NO_FACE,
1150 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face,
1153 glTranslatef(-SX, S1, 0);
1155 rp->cubeLoc[BACK_FACE][FIRSTX + MAXSIZEX * LASTY].face, NO_FACE,
1156 rp->cubeLoc[LEFT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
1157 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face);
1158 for (i = 1; i < MAXSIZEX - 1; i++) {
1159 glTranslatef(S1, 0, 0);
1161 rp->cubeLoc[BACK_FACE][i + MAXSIZEX * LASTY].face, NO_FACE,
1163 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * FIRSTZ].face);
1165 glTranslatef(S1, 0, 0);
1167 rp->cubeLoc[BACK_FACE][LASTX + MAXSIZEX * LASTY].face, NO_FACE,
1168 NO_FACE, rp->cubeLoc[RIGHT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face,
1169 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * FIRSTZ].face);
1171 for (k = 1; k < MAXSIZEZ - 1; k++) {
1173 if (slice.depth == REVZ(k))
1174 glRotatef(rotatestep, 0, 0, HALFZ);
1175 glTranslatef(-HALFX, -HALFY, MIDZ(k));
1178 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * LASTY].face, NO_FACE,
1179 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
1180 for (i = 1; i < MAXSIZEX - 1; i++) {
1181 glTranslatef(S1, 0, 0);
1185 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * REVZ(k)].face, NO_FACE);
1187 glTranslatef(S1, 0, 0);
1190 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * LASTY].face,
1191 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * REVZ(k)].face, NO_FACE);
1192 for (j = 1; j < MAXSIZEY - 1; j++) {
1193 glTranslatef(-SX, S1, 0);
1196 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * REVY(j)].face, NO_FACE,
1199 glTranslatef(SX, 0, 0);
1202 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * REVY(j)].face,
1205 glTranslatef(-SX, S1, 0);
1208 rp->cubeLoc[LEFT_FACE][k + MAXSIZEZ * FIRSTY].face, NO_FACE,
1209 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * k].face);
1210 for (i = 1; i < MAXSIZEX - 1; i++) {
1211 glTranslatef(S1, 0, 0);
1215 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * k].face);
1217 glTranslatef(S1, 0, 0);
1220 NO_FACE, rp->cubeLoc[RIGHT_FACE][REVZ(k) + MAXSIZEZ * FIRSTY].face,
1221 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * k].face);
1224 if (slice.depth == 0)
1225 glRotatef(rotatestep, 0, 0, HALFZ);
1226 glTranslatef(-HALFX, -HALFY, HALFZ);
1228 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * LASTY].face,
1229 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * LASTY].face, NO_FACE,
1230 rp->cubeLoc[BOTTOM_FACE][FIRSTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
1231 for (i = 1; i < MAXSIZEX - 1; i++) {
1232 glTranslatef(S1, 0, 0);
1234 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * LASTY].face,
1236 rp->cubeLoc[BOTTOM_FACE][i + MAXSIZEX * FIRSTZ].face, NO_FACE);
1238 glTranslatef(S1, 0, 0);
1240 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * LASTY].face,
1241 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * LASTY].face,
1242 rp->cubeLoc[BOTTOM_FACE][LASTX + MAXSIZEX * FIRSTZ].face, NO_FACE);
1243 for (j = 1; j < MAXSIZEY - 1; j++) {
1244 glTranslatef(-SX, S1, 0);
1246 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * REVY(j)].face,
1247 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * REVY(j)].face, NO_FACE,
1249 for (i = 1; i < MAXSIZEX - 1; i++) {
1250 glTranslatef(S1, 0, 0);
1252 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * REVY(j)].face,
1256 glTranslatef(S1, 0, 0);
1258 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * REVY(j)].face,
1259 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * REVY(j)].face,
1262 glTranslatef(-SX, S1, 0);
1264 NO_FACE, rp->cubeLoc[FRONT_FACE][FIRSTX + MAXSIZEX * FIRSTY].face,
1265 rp->cubeLoc[LEFT_FACE][LASTZ + MAXSIZEZ * FIRSTY].face, NO_FACE,
1266 NO_FACE, rp->cubeLoc[TOP_FACE][FIRSTX + MAXSIZEX * LASTZ].face);
1267 for (i = 1; i < MAXSIZEX - 1; i++) {
1268 glTranslatef(S1, 0, 0);
1270 NO_FACE, rp->cubeLoc[FRONT_FACE][i + MAXSIZEX * FIRSTY].face,
1272 NO_FACE, rp->cubeLoc[TOP_FACE][i + MAXSIZEX * LASTZ].face);
1274 glTranslatef(S1, 0, 0);
1276 NO_FACE, rp->cubeLoc[FRONT_FACE][LASTX + MAXSIZEX * FIRSTY].face,
1277 NO_FACE, rp->cubeLoc[RIGHT_FACE][FIRSTZ + MAXSIZEZ * FIRSTY].face,
1278 NO_FACE, rp->cubeLoc[TOP_FACE][LASTX + MAXSIZEX * LASTZ].face);
1284 /* From David Bagley's xrubik. Used by permission. ;) */
1286 readRC(rubikstruct * rp, int face, int dir, int h, int orient, int size)
1290 if (dir == TOP || dir == BOTTOM)
1291 for (g = 0; g < size; g++)
1292 rp->rowLoc[orient][g] =
1293 rp->cubeLoc[face][g * size + h];
1294 else /* dir == RIGHT || dir == LEFT */
1295 for (g = 0; g < size; g++)
1296 rp->rowLoc[orient][g] =
1297 rp->cubeLoc[face][h * size + g];
1301 rotateRC(rubikstruct * rp, int rotate, int orient, int size)
1305 for (g = 0; g < size; g++)
1306 rp->rowLoc[orient][g].rotation =
1307 (rp->rowLoc[orient][g].rotation + rotate) % MAXORIENT;
1311 reverseRC(rubikstruct * rp, int orient, int size)
1316 for (g = 0; g < size / 2; g++) {
1317 temp = rp->rowLoc[orient][size - 1 - g];
1318 rp->rowLoc[orient][size - 1 - g] = rp->rowLoc[orient][g];
1319 rp->rowLoc[orient][g] = temp;
1324 writeRC(rubikstruct * rp, int face, int dir, int h, int orient, int size)
1328 if (dir == TOP || dir == BOTTOM) {
1329 for (g = 0; g < size; g++) {
1330 position = g * size + h;
1331 rp->cubeLoc[face][position] = rp->rowLoc[orient][g];
1332 /* DrawSquare(face, position); */
1334 } else { /* dir == RIGHT || dir == LEFT */
1335 for (g = 0; g < size; g++) {
1336 position = h * size + g;
1337 rp->cubeLoc[face][position] = rp->rowLoc[orient][g];
1338 /* DrawSquare(face, position); */
1344 rotateFace(rubikstruct * rp, int face, int direction)
1347 RubikLoc *faceLoc = NULL;
1349 if ((faceLoc = (RubikLoc *) malloc(AVSIZESQ * sizeof (RubikLoc))) == NULL)
1350 (void) fprintf(stderr,
1351 "Could not allocate memory for rubik face position info\n");
1353 for (position = 0; position < AVSIZESQ; position++)
1354 faceLoc[position] = rp->cubeLoc[face][position];
1356 for (position = 0; position < AVSIZESQ; position++) {
1357 i = position % AVSIZE;
1358 j = position / AVSIZE;
1359 rp->cubeLoc[face][position] = (direction == CW) ?
1360 faceLoc[(AVSIZE - i - 1) * AVSIZE + j] :
1361 faceLoc[i * AVSIZE + AVSIZE - j - 1];
1362 rp->cubeLoc[face][position].rotation =
1363 (rp->cubeLoc[face][position].rotation + direction - MAXORIENT) %
1365 /* DrawSquare(face, position); */
1367 if (faceLoc != NULL)
1368 (void) free((void *) faceLoc);
1372 moveRubik(rubikstruct * rp, int face, int direction, int position)
1374 int newFace, newDirection, rotate, reverse = False;
1378 if (direction == CW || direction == CCW) {
1379 direction = (direction == CCW) ?
1380 (rotateToRow[face].direction + 2) % MAXORIENT :
1381 rotateToRow[face].direction;
1382 i = j = (rotateToRow[face].sideFace == RIGHT ||
1383 rotateToRow[face].sideFace == BOTTOM) ? AVSIZE - 1 : 0;
1384 face = rotateToRow[face].face;
1385 position = j * AVSIZE + i;
1387 i = position % AVSIZE;
1388 j = position / AVSIZE;
1389 h = (direction == TOP || direction == BOTTOM) ? i : j;
1390 /* rotate sides CW or CCW */
1391 if (h == AVSIZE - 1) {
1392 newDirection = (direction == TOP || direction == BOTTOM) ?
1394 if (direction == TOP || direction == RIGHT)
1395 rotateFace(rp, rowToRotate[face][newDirection], CW);
1396 else /* direction == BOTTOM || direction == LEFT */
1397 rotateFace(rp, rowToRotate[face][newDirection], CCW);
1400 newDirection = (direction == TOP || direction == BOTTOM) ?
1402 if (direction == TOP || direction == RIGHT)
1403 rotateFace(rp, rowToRotate[face][newDirection], CCW);
1404 else /* direction == BOTTOM || direction == LEFT */
1405 rotateFace(rp, rowToRotate[face][newDirection], CW);
1408 readRC(rp, face, direction, h, 0, AVSIZE);
1409 for (k = 1; k <= MAXORIENT; k++) {
1410 newFace = slideNextRow[face][direction].face;
1411 rotate = slideNextRow[face][direction].rotation;
1412 newDirection = (rotate + direction) % MAXORIENT;
1419 if (newDirection == TOP || newDirection == BOTTOM) {
1420 newH = AVSIZE - 1 - h;
1422 } else { /* newDirection == RIGHT || newDirection == LEFT */
1428 newH = AVSIZE - 1 - h;
1432 if (newDirection == TOP || newDirection == BOTTOM) {
1435 } else { /* newDirection == RIGHT || newDirection == LEFT */
1436 newH = AVSIZE - 1 - h;
1441 (void) printf("moveRubik: rotate %d\n", rotate);
1444 readRC(rp, newFace, newDirection, newH, k, AVSIZE);
1445 rotateRC(rp, rotate, k - 1, AVSIZE);
1446 if (reverse == True)
1447 reverseRC(rp, k - 1, AVSIZE);
1448 writeRC(rp, newFace, newDirection, newH, k - 1, AVSIZE);
1450 direction = newDirection;
1457 printCube(rubikstruct * rp)
1461 for (face = 0; face < MAXFACES; face++) {
1462 for (position = 0; position < AVSIZESQ; position++) {
1463 (void) printf("%d %d ", rp->cubeLoc[face][position].face,
1464 rp->cubeLoc[face][position].rotation);
1465 if (!((position + 1) % AVSIZE))
1466 (void) printf("\n");
1468 (void) printf("\n");
1470 (void) printf("\n");
1476 evalmovement(ModeInfo * mi, RubikMove movement)
1478 rubikstruct *rp = &rubik[MI_SCREEN(mi)];
1483 if (movement.face < 0 || movement.face >= MAXFACES)
1486 moveRubik(rp, movement.face, movement.direction, movement.position);
1491 compare_moves(rubikstruct * rp, RubikMove move1, RubikMove move2, Bool opp)
1493 RubikSlice slice1, slice2;
1495 slice1 = convertMove(rp, move1);
1496 slice2 = convertMove(rp, move2);
1497 if (slice1.face == slice2.face &&
1498 slice1.depth == slice2.depth) {
1499 if (slice1.rotation == slice2.rotation) { /* CW or CCW */
1511 shuffle(ModeInfo * mi)
1513 rubikstruct *rp = &rubik[MI_SCREEN(mi)];
1514 int i, face, position;
1517 AVSIZE = MI_SIZE(mi);
1518 if (AVSIZE < -MINSIZE)
1519 AVSIZE = NRAND(-AVSIZE - MINSIZE + 1) + MINSIZE;
1520 else if (AVSIZE < MINSIZE)
1522 /* Let me waste a little space for the moment */
1523 /* Future cube to be LxMxN and not just NxNxN, but not done yet */
1524 AVSIZESQ = AVSIZE * AVSIZE;
1530 MAXSIZESQ = AVSIZESQ;
1533 for (face = 0; face < MAXFACES; face++) {
1534 if (rp->cubeLoc[face] != NULL)
1535 (void) free((void *) rp->cubeLoc[face]);
1536 if ((rp->cubeLoc[face] =
1537 (RubikLoc *) malloc(AVSIZESQ * sizeof (RubikLoc))) == NULL)
1538 (void) fprintf(stderr,
1539 "Could not allocate memory for rubik cube position info\n");
1540 for (position = 0; position < AVSIZESQ; position++) {
1541 rp->cubeLoc[face][position].face = face;
1542 rp->cubeLoc[face][position].rotation = TOP;
1545 for (i = 0; i < MAXORIENT; i++) {
1546 if (rp->rowLoc[i] != NULL)
1547 (void) free((void *) rp->rowLoc[i]);
1548 if ((rp->rowLoc[i] =
1549 (RubikLoc *) malloc(AVSIZE * sizeof (RubikLoc))) == NULL)
1550 (void) fprintf(stderr,
1551 "Could not allocate memory for rubik row position info\n");
1553 rp->storedmoves = MI_BATCHCOUNT(mi);
1554 if (rp->storedmoves < 0) {
1555 if (rp->moves != NULL)
1556 (void) free((void *) rp->moves);
1558 rp->storedmoves = NRAND(-rp->storedmoves) + 1;
1560 if ((rp->storedmoves) && (rp->moves == NULL))
1562 (RubikMove *) calloc(rp->storedmoves + 1, sizeof (RubikMove))) == NULL)
1563 (void) fprintf(stderr,
1564 "Could not allocate memory for rubik move buffer\n");
1566 if (MI_CYCLES(mi) <= 1) {
1567 rp->anglestep = 90.0;
1569 rp->anglestep = 90.0 / (GLfloat) (MI_CYCLES(mi));
1572 for (i = 0; i < rp->storedmoves; i++) {
1576 move.face = NRAND(6);
1577 move.direction = NRAND(4); /* Exclude CW and CCW, its ok */
1579 * Randomize position along diagonal, each plane gets an equal chance.
1580 * This trick will only work for NxNxN cubes
1581 * draw_cube DEPENDS on that they are chosen this way.
1583 move.position = NRAND(AVSIZE) * (AVSIZE + 1);
1588 if (i > 0) /* avoid immediate undoing moves */
1589 if (compare_moves(rp, move, rp->moves[i - 1], True))
1591 if (i > 1) /* avoid 3 consecutive identical moves */
1592 if (compare_moves(rp, move, rp->moves[i - 1], False) &&
1593 compare_moves(rp, move, rp->moves[i - 2], False))
1596 * Still some silly moves being made....
1598 } while (!condition);
1600 evalmovement(mi, move);
1601 rp->moves[i] = move;
1603 rp->movement.face = NO_FACE;
1605 rp->action = hideshuffling ? ACTION_SOLVE : ACTION_SHUFFLE;
1606 rp->shufflingmoves = 0;
1611 reshape(ModeInfo * mi, int width, int height)
1613 rubikstruct *rp = &rubik[MI_SCREEN(mi)];
1615 glViewport(0, 0, rp->WindW = (GLint) width, rp->WindH = (GLint) height);
1616 glMatrixMode(GL_PROJECTION);
1618 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
1619 glMatrixMode(GL_MODELVIEW);
1621 rp->AreObjectsDefined[ObjCubit] = 0;
1625 pinit(ModeInfo * mi)
1628 glClearColor(0.0, 0.0, 0.0, 1.0);
1629 glColor3f(1.0, 1.0, 1.0);
1631 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
1632 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
1633 glLightfv(GL_LIGHT0, GL_POSITION, position0);
1634 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
1635 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
1636 glLightfv(GL_LIGHT1, GL_POSITION, position1);
1637 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
1638 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
1639 glEnable(GL_LIGHTING);
1640 glEnable(GL_LIGHT0);
1641 glEnable(GL_LIGHT1);
1642 glEnable(GL_DEPTH_TEST);
1643 glEnable(GL_NORMALIZE);
1644 glEnable(GL_CULL_FACE);
1646 glShadeModel(GL_FLAT);
1647 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
1648 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
1654 init_rubik(ModeInfo * mi)
1656 int screen = MI_SCREEN(mi);
1659 if (rubik == NULL) {
1660 if ((rubik = (rubikstruct *) calloc(MI_NUM_SCREENS(mi),
1661 sizeof (rubikstruct))) == NULL)
1664 rp = &rubik[screen];
1665 rp->step = NRAND(90);
1667 if ((rp->glx_context = init_GL(mi)) != NULL) {
1669 reshape(mi, MI_WIN_WIDTH(mi), MI_WIN_HEIGHT(mi));
1670 objects = glGenLists(1);
1676 draw_rubik(ModeInfo * mi)
1678 rubikstruct *rp = &rubik[MI_SCREEN(mi)];
1679 Display *display = MI_DISPLAY(mi);
1680 Window window = MI_WINDOW(mi);
1682 if (!rp->glx_context)
1685 glDrawBuffer(GL_BACK);
1686 glXMakeCurrent(display, window, rp->glx_context);
1688 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1692 glTranslatef(0.0, 0.0, -10.0);
1694 if (!MI_WIN_IS_ICONIC(mi)) {
1695 glScalef(Scale4Window * rp->WindH / rp->WindW, Scale4Window, Scale4Window);
1697 glScalef(Scale4Iconic * rp->WindH / rp->WindW, Scale4Iconic, Scale4Iconic);
1700 glRotatef(rp->step * 100, 1, 0, 0);
1701 glRotatef(rp->step * 95, 0, 1, 0);
1702 glRotatef(rp->step * 90, 0, 0, 1);
1704 if (rp->action == ACTION_SHUFFLE) {
1706 if (++rp->rotatestep > DELAY_AFTER_SHUFFLING) {
1707 rp->movement.face = NO_FACE;
1709 rp->action = ACTION_SOLVE;
1713 if (rp->movement.face == NO_FACE) {
1714 if (rp->shufflingmoves < rp->storedmoves) {
1716 rp->movement = rp->moves[rp->shufflingmoves];
1722 rp->rotatestep += rp->anglestep;
1723 if (rp->rotatestep > 90) {
1724 evalmovement(mi, rp->movement);
1725 rp->shufflingmoves++;
1726 rp->movement.face = NO_FACE;
1732 if (++rp->rotatestep > DELAY_AFTER_SOLVING)
1735 if (rp->movement.face == NO_FACE) {
1736 if (rp->storedmoves > 0) {
1738 rp->movement = rp->moves[rp->storedmoves - 1];
1739 rp->movement.direction = (rp->movement.direction + (MAXORIENT / 2)) %
1746 rp->rotatestep += rp->anglestep;
1747 if (rp->rotatestep > 90) {
1748 evalmovement(mi, rp->movement);
1750 rp->movement.face = NO_FACE;
1762 glXSwapBuffers(display, window);
1768 change_rubik(ModeInfo * mi)
1770 rubikstruct *rp = &rubik[MI_SCREEN(mi)];
1772 if (!rp->glx_context)
1778 release_rubik(ModeInfo * mi)
1780 if (rubik != NULL) {
1783 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
1784 rubikstruct *rp = &rubik[screen];
1787 for (i = 0; i < MAXFACES; i++)
1788 if (rp->cubeLoc[i] != NULL)
1789 (void) free((void *) rp->cubeLoc[i]);
1790 for (i = 0; i < MAXORIENT; i++)
1791 if (rp->rowLoc[i] != NULL)
1792 (void) free((void *) rp->rowLoc[i]);
1793 if (rp->moves != NULL)
1794 (void) free((void *) rp->moves);
1796 (void) free((void *) rubik);
1799 FreeAllGL(MI_DISPLAY(mi));