http://ftp.ksu.edu.tw/FTP/FreeBSD/distfiles/xscreensaver-4.23.tar.gz
[xscreensaver] / hacks / glx / cube21.c
1 /*-
2  * Permission to use, copy, modify, and distribute this software and its
3  * documentation for any purpose and without fee is hereby granted,
4  * provided that the above copyright notice appear in all copies and that
5  * both that copyright notice and this permission notice appear in
6  * supporting documentation.
7  *
8  * This file is provided AS IS with no warranties of any kind.  The author
9  * shall have no liability with respect to the infringement of copyrights,
10  * trade secrets or any patents by this file or any part thereof.  In no
11  * event will the author be liable for any lost revenue or profits or
12  * other special, indirect and consequential damages.
13  *
14  * Cube 21 - a Rubik-like puzzle.  It changes its shape and has more than
15  * 200 configurations.  It is known better as Square-1, but it is called
16  * Cube 21 in the Czech republic, where it was invented in 1992.
17  * 
18  * This file is derived from cage.c,
19  * "cage --- the Impossible Cage, an Escher like scene",
20  * by Marcelo F. Vienna,
21  * parts from gltext.c by Jamie Zawinski
22  *
23  * Vaclav (Vasek) Potocek
24  * vasek.potocek@post.cz
25  */
26
27 /* TODO:
28  *  Future: some simple "solve mode"
29  *  Future: new light model, less ambient light
30  *  Maybe: use rotator and trackball
31  *  Maybe: display lists - seem impossible with colors
32  */
33
34 /*-
35  * Texture mapping is only available on RGBA contexts, Mono and color index
36  * visuals DO NOT support texture mapping in OpenGL.
37  *
38  * BUT Mesa do implements RGBA contexts in pseudo color visuals, so texture
39  * mapping should work on PseudoColor, DirectColor, TrueColor using Mesa. Mono
40  * is not officially supported for both OpenGL and Mesa, but seems to not crash
41  * Mesa.
42  *
43  * In real OpenGL, PseudoColor DO NOT support texture map (as far as I know).
44  */
45
46 #define PROGCLASS     "Cube21"
47 #define HACK_INIT     init_cube21
48 #define HACK_DRAW     draw_cube21
49 #define HACK_RESHAPE  reshape_cube21
50 #define cube21_opts   xlockmore_opts
51
52 #define DEF_XYSPEED   "1.0"
53 #define DEF_TSPEED    "3.0"
54 #define DEF_WSPEED    "1.0"
55 #define DEF_TWAIT     "40.0"
56 #define DEF_SIZE      "0.7"
57 #define DEF_COLMODE   "(none)"
58
59 #define DEFAULTS   "*delay:         20000         \n" \
60                    "*showFPS:       False         \n" \
61                    "*wireframe:     False         \n"
62
63 #include "xlockmore.h"
64
65 #ifdef USE_GL
66
67 #include <GL/glu.h>
68 #include "cube21-tex.h"
69
70 #ifdef Pi
71 #undef Pi
72 #endif
73 #define Pi      M_PI
74
75 #define SHUFFLE 100
76
77 #define COS15   0.9659258263
78 #define SIN15   0.2588190451
79 #define COS30   0.8660254038
80 #define SIN30   0.5000000000
81
82 #undef countof
83 #define countof(x) (sizeof((x))/sizeof((*x)))
84
85 #define rnd01() (random()%2)
86 #define rndcolor() (frand(0.5)+0.3)
87
88 /*************************************************************************/
89
90 static Bool wander, spin, wire, tex, rndstart, cmat;
91 static float spinspeed, tspeed, wspeed, twait, size;
92 static char *colmode_s;
93 static int colmode;
94
95 static argtype vars[] = {
96   { &spin,      "spin",        "Spin",        "True",      t_Bool},
97   { &wander,    "wander",      "Wander",      "True",      t_Bool},
98   { &rndstart,  "randomize",   "Randomize",   "True",      t_Bool},
99   { &tex,       "texture",     "Texture",     "True",      t_Bool},
100   { &spinspeed, "spinspeed",   "SpinSpeed",   DEF_XYSPEED, t_Float},
101   { &tspeed,    "rotspeed",    "RotSpeed",    DEF_TSPEED,  t_Float},
102   { &wspeed,    "wanderspeed", "WanderSpeed", DEF_WSPEED,  t_Float},
103   { &twait,     "wait",        "Wait",        DEF_TWAIT,   t_Float},
104   { &size,      "cubesize",    "CubeSize",    DEF_SIZE,    t_Float},
105   { &colmode_s, "colormode",   "ColorMode",   DEF_COLMODE, t_String}
106 };
107
108 static XrmOptionDescRec opts[] = {
109   { "-spin",        ".spin",        XrmoptionNoArg,  "True" },
110   { "+spin",        ".spin",        XrmoptionNoArg,  "False" },
111   { "-wander",      ".wander",      XrmoptionNoArg,  "True" },
112   { "+wander",      ".wander",      XrmoptionNoArg,  "False" },
113   { "-randomize",   ".randomize",   XrmoptionNoArg,  "True" },
114   { "+randomize",   ".randomize",   XrmoptionNoArg,  "False" },
115   { "-texture",     ".texture",     XrmoptionNoArg,  "True" },
116   { "+texture",     ".texture",     XrmoptionNoArg,  "False" },
117   { "-spinspeed",   ".spinspeed",   XrmoptionSepArg, 0 },
118   { "-wanderspeed", ".wanderspeed", XrmoptionSepArg, 0 },
119   { "-rotspeed",    ".rotspeed",    XrmoptionSepArg, 0 },
120   { "-wait",        ".wait",        XrmoptionSepArg, 0 },
121   { "-cubesize",    ".cubesize",    XrmoptionSepArg, 0 },
122   { "-colormode",   ".colormode",   XrmoptionSepArg, DEF_COLMODE }
123 };
124
125 ModeSpecOpt cube21_opts = {countof(opts), opts, countof(vars), vars, NULL};
126
127 #ifdef USE_MODULES
128 ModStruct   cube21_description =
129 { "cube21", "init_cube21", "draw_cube21", "release_cube21",
130   "draw_cube21", "change_cube21", NULL, &cube21_opts,
131   25000, 1, 1, 1, 1.0, 4, "",
132   "Shows randomly shuffling Cube 21", 0, NULL
133 };
134 #endif
135
136 typedef enum {
137   CUBE21_STATE_BASIC,
138   CUBE21_PAUSE1 = CUBE21_STATE_BASIC,
139   CUBE21_ROT_BASE,
140   CUBE21_ROT_TOP = CUBE21_ROT_BASE,
141   CUBE21_ROT_BOTTOM,
142   CUBE21_PAUSE2,
143   CUBE21_HALF_BASE,
144   CUBE21_HALF1 = CUBE21_HALF_BASE,
145   CUBE21_HALF2
146 } cube21_state;
147
148 typedef enum {
149   CUBE21_COLOR_WHITE,
150   CUBE21_COLOR_RANDOM,
151   CUBE21_COLOR_SILVER,
152   CUBE21_COLOR_TWORND,
153   CUBE21_COLOR_CLASSIC,
154   CUBE21_COLOR_SIXRND
155 } cube21_cmode;
156
157 typedef int pieces_t[2][13];
158 typedef int cind_t[5][12];
159 typedef GLfloat col_t[6][3];
160
161 typedef struct {
162   GLXContext    *glx_context;
163   GLfloat       ratio;
164   cube21_state  state;          /* type of "rotation" - shuffling */
165   GLfloat       xrot, yrot;     /* "spin" - object rotation around axis */
166   GLfloat       posarg;         /* position argument (for sine function) */
167   GLfloat       t, tmax;        /* rotation clock */
168   int           hf[2], fr[2];   /* half flipped / face rotated flags */
169   int           rface, ramount; /* selected face and amount of rotation in multiplies of 30deg */
170   int           pieces[2][13];  /* locations of "narrow" and "wide" pieces */
171   int           cind[5][12];    /* color indices */
172   GLfloat       colors[6][3];   /* color map */
173 } cube21_conf;
174
175 static cube21_conf *cube21 = NULL;
176
177 static GLfloat shininess = 20.0;
178 static GLfloat ambient[] = {0.0, 0.0, 0.0, 1.0};
179 static GLfloat diffuse[] = {1.0, 1.0, 1.0, 1.0};
180 static GLfloat position0[] = {1.0, 1.0, 1.0, 0.0};
181 static GLfloat position1[] = {-1.0, -1.0, 1.0, 0.0};
182 static GLfloat lmodel_ambient[] = {0.1, 0.1, 0.1, 1.0};
183 static GLfloat material_ambient[] = {0.7, 0.7, 0.7, 1.0};
184 static GLfloat material_diffuse[] = {0.7, 0.7, 0.7, 1.0};
185 static GLfloat material_specular[] = {0.2, 0.2, 0.2, 1.0};
186 static GLfloat color_inner[] = {1.0, 1.0, 1.0};
187 static GLfloat texc[8] = {0.0}, posc[6];
188 static GLfloat zpos = -18.0;
189
190 /*************************************************************************/
191
192 static void find_matches(pieces_t pieces, int matches[12], int s) {
193   int i, j = 1;
194   for(i = 1; i<6; i++) {
195     if(pieces[s][i] && pieces[s][i+6]) {
196       matches[j++] = i;
197     }
198   }
199   matches[0] = j;
200   for(i = 1; i<matches[0]; i++) {
201     matches[j++] = matches[i]-6;
202   }
203   matches[j++] = 6;
204   matches[0] = j;
205 }
206
207 static void rot_face(pieces_t pieces, cind_t colors, int s, int o) {
208   int i;
209   int tmp[12], tmpc[2][12];
210   int c0 = 2*s, c1 = c0+1;
211   for(i = 0; i<12; i++) {
212     tmp[i] = pieces[s][i];
213     tmpc[0][i] = colors[c0][i];
214     tmpc[1][i] = colors[c1][i];
215   }
216   if(o<0) o += 12;
217   for(i = 0; i<12; i++, o++) {
218     if(o==12) o = 0;
219     pieces[s][i] = tmp[o];
220     colors[c0][i] = tmpc[0][o];
221     colors[c1][i] = tmpc[1][o];
222   }
223 }
224
225 static void rot_halves(pieces_t pieces, cind_t colors, int hf[2], int s) {
226   int ss = 6*s, i, j, k, t;
227   for(i = 0; i<6; i++) {
228     j = ss+i; k = ss+6-i;
229     t = pieces[0][j];
230     pieces[0][j] = pieces[1][k];
231     pieces[1][k] = t;
232     k--;
233     t = colors[0][j];
234     colors[0][j] = colors[2][k];
235     colors[2][k] = t;
236     t = colors[1][j];
237     colors[1][j] = colors[3][k];
238     colors[3][k] = t;
239   }
240   hf[s] ^= 1;
241 }
242
243 static void randomize(cube21_conf *cp) {
244   int i, j, s;
245   int matches[12];
246   for(i = 0; i<SHUFFLE; i++) {
247     s = rnd01();
248     find_matches(cp->pieces, matches, s);
249     j = matches[0]-1;
250     j = random()%j;
251     j = matches[j+1];
252     rot_face(cp->pieces, cp->cind, s, j);
253     s = rnd01();
254     rot_halves(cp->pieces, cp->cind, cp->hf, s);
255   }
256 }
257
258 static void finish(cube21_conf *cp) {
259   int j, s;
260   int matches[12];
261   switch(cp->state) {
262     case CUBE21_PAUSE1:
263       s = rnd01();
264       find_matches(cp->pieces, matches, s);
265       j = matches[0]-1;
266       j = random()%j;
267       j = matches[j+1];
268       if(j==6 && rnd01()) j = -6;
269       cp->state = CUBE21_ROT_BASE+s;
270       cp->tmax = 30.0*abs(j);
271       cp->fr[0] = cp->fr[1] = 0;
272       cp->rface = s;
273       cp->ramount = j;
274       break;
275     case CUBE21_ROT_TOP:
276     case CUBE21_ROT_BOTTOM:
277       rot_face(cp->pieces, cp->cind, s = cp->rface, cp->ramount);
278       cp->fr[s] = 1;
279       s ^= 1;
280       if(!cp->fr[s] && rnd01()) {
281         find_matches(cp->pieces, matches, s);
282         j = matches[0]-1;
283         j = random()%j;
284         j = matches[j+1];
285         if(j==6 && rnd01()) j = -6;
286         cp->state = CUBE21_ROT_BASE+s;
287         cp->tmax = 30.0*abs(j);
288         cp->rface = s;
289         cp->ramount = j;        
290         break;
291       } else {
292         cp->state = CUBE21_PAUSE2;
293         cp->tmax = twait;
294         break;
295       }
296     case CUBE21_PAUSE2:
297       s = rnd01();
298       cp->ramount = -rnd01();       /* 0 or -1, only sign is significant in this case */
299       cp->state = CUBE21_HALF_BASE+s;
300       cp->tmax = 180.0;
301       cp->rface = s;
302       break;
303     case CUBE21_HALF1:
304     case CUBE21_HALF2:
305       rot_halves(cp->pieces, cp->cind, cp->hf, cp->rface);
306       cp->state = CUBE21_PAUSE1;
307       cp->tmax = twait;
308       break;
309   }
310   cp->t = 0;
311 }
312
313 static void draw_narrow_piece(GLfloat s, int c1, int c2, col_t colors) {
314   GLfloat s1 = posc[0]*s;
315   glBegin(GL_TRIANGLES);
316   glNormal3f(0.0, 0.0, s);
317   if(cmat) glColor3fv(colors[c1]);
318   glTexCoord2f(0.0, 0.25);     glVertex3f(0.0, 0.0, s);
319   glTexCoord2f(0.5, texc[1]);  glVertex3f(posc[1], 0.0, s);
320   glTexCoord2f(0.5, texc[2]);  glVertex3f(posc[2], posc[3], s);
321   glNormal3f(0.0, 0.0, -s);
322   if(cmat) glColor3fv(color_inner);
323   glTexCoord2f(1.0, 1.0);
324   glVertex3f(0.0, 0.0, s1);
325   glVertex3f(posc[1], 0.0, s1);
326   glVertex3f(posc[2], posc[3], s1);
327   glEnd();
328   glBegin(GL_QUADS);
329   glNormal3f(0.0, -1.0, 0.0);
330   if(cmat) glColor3fv(color_inner);
331   glTexCoord2f(1.0, 1.0);
332   glVertex3f(0.0, 0.0, s);
333   glVertex3f(posc[1], 0.0, s);
334   glVertex3f(posc[1], 0.0, s1);
335   glVertex3f(0.0, 0.0, s1);
336   glNormal3f(COS15, SIN15, 0.0);
337   if(cmat) glColor3fv(colors[c2]);
338   glTexCoord2f(0.5, texc[1]);      glVertex3f(posc[1], 0.0, s);
339   glTexCoord2f(0.5, texc[2]);      glVertex3f(posc[2], posc[3], s);
340   glTexCoord2f(texc[4], texc[2]);  glVertex3f(posc[2], posc[3], s1);
341   glTexCoord2f(texc[4], texc[1]);  glVertex3f(posc[1], 0.0, s1);
342   glNormal3f(-SIN30, COS30, 0.0);
343   if(cmat) glColor3fv(color_inner);
344   glTexCoord2f(1.0, 1.0);
345   glVertex3f(0.0, 0.0, s);
346   glVertex3f(posc[2], posc[3], s);
347   glVertex3f(posc[2], posc[3], s1);
348   glVertex3f(0.0, 0.0, s1);
349   glEnd();
350   glRotatef(30.0, 0.0, 0.0, 1.0);
351 }
352
353 static void draw_wide_piece(GLfloat s, int c1, int c2, int c3, col_t colors) {
354   GLfloat s1 = posc[0]*s;
355   glBegin(GL_TRIANGLES);
356   glNormal3f(0.0, 0.0, s);
357   if(cmat) glColor3fv(colors[c1]);
358   glTexCoord2f(0.0, 0.25);     glVertex3f(0.0, 0.0, s);
359   glTexCoord2f(0.5, texc[1]);  glVertex3f(posc[1], 0.0, s);
360   glTexCoord2f(0.5, 0.0);      glVertex3f(posc[4], posc[5], s);
361   glTexCoord2f(0.5, 0.0);      glVertex3f(posc[4], posc[5], s);
362   glTexCoord2f(texc[3], 0.0);  glVertex3f(posc[3], posc[2], s);
363   glTexCoord2f(0.0, 0.25);     glVertex3f(0.0, 0.0, s);
364   glNormal3f(0.0, 0.0, -s);
365   if(cmat) glColor3fv(color_inner);
366   glTexCoord2f(1.0, 1.0);
367   glVertex3f(0.0, 0.0, s1);
368   glVertex3f(posc[1], 0.0, s1);
369   glVertex3f(posc[4], posc[5], s1);
370   glVertex3f(posc[4], posc[5], s1);
371   glVertex3f(posc[3], posc[2], s1);
372   glVertex3f(0.0, 0.0, s1);
373   glEnd();
374   glBegin(GL_QUADS);
375   glNormal3f(0.0, -1.0, 0);
376   if(cmat) glColor3fv(color_inner);
377   glTexCoord2f(1.0, 1.0);
378   glVertex3f(0.0, 0.0, s);
379   glVertex3f(posc[1], 0.0, s);
380   glVertex3f(posc[1], 0.0, s1);
381   glVertex3f(0.0, 0.0, s1);
382   glNormal3f(COS15, -SIN15, 0.0);
383   if(cmat) glColor3fv(colors[c2]);
384   glTexCoord2f(0.5, texc[1]);      glVertex3f(posc[1], 0.0, s);
385   glTexCoord2f(0.5, 0.0);          glVertex3f(posc[4], posc[5], s);
386   glTexCoord2f(texc[4], 0.0);      glVertex3f(posc[4], posc[5], s1);
387   glTexCoord2f(texc[4], texc[1]);  glVertex3f(posc[1], 0.0, s1);
388   glNormal3f(SIN15, COS15, 0.0);
389   if(cmat) glColor3fv(colors[c3]);
390   glTexCoord2f(0.5, texc[1]);      glVertex3f(posc[4], posc[5], s);
391   glTexCoord2f(0.5, 0.0);          glVertex3f(posc[3], posc[2], s);
392   glTexCoord2f(texc[4], 0.0);      glVertex3f(posc[3], posc[2], s1);
393   glTexCoord2f(texc[4], texc[1]);  glVertex3f(posc[4], posc[5], s1);
394   glNormal3f(-COS30, SIN30, 0.0);
395   if(cmat) glColor3fv(color_inner);
396   glTexCoord2f(1.0, 1.0);
397   glVertex3f(0.0, 0.0, s);
398   glVertex3f(posc[3], posc[2], s);
399   glVertex3f(posc[3], posc[2], s1);
400   glVertex3f(0.0, 0.0, s1);
401   glEnd();
402   glRotatef(60.0, 0.0, 0.0, 1.0);
403 }
404
405 static void draw_middle_piece(int s, cind_t cind, col_t colors) {
406   s *= 6;
407   glBegin(GL_QUADS);
408   if(cmat) glColor3fv(color_inner);
409   glNormal3f(0.0, 0.0, 1.0);
410   glTexCoord2f(1.0, 1.0);
411   glVertex3f(posc[1], 0.0, posc[0]);
412   glVertex3f(posc[4], posc[5], posc[0]);
413   glVertex3f(-posc[5], posc[4], posc[0]);
414   glVertex3f(-posc[1], 0.0, posc[0]);
415   glNormal3f(0.0, 0.0, -1.0);
416   glTexCoord2f(1.0, 1.0);
417   glVertex3f(posc[1], 0.0, -posc[0]);
418   glVertex3f(posc[4], posc[5], -posc[0]);
419   glVertex3f(-posc[5], posc[4], -posc[0]);
420   glVertex3f(-posc[1], 0.0, -posc[0]);
421   glNormal3f(0.0, -1.0, 0.0);
422   glTexCoord2f(1.0, 1.0);
423   glVertex3f(-posc[1], 0.0, posc[0]);
424   glVertex3f(posc[1], 0.0, posc[0]);
425   glVertex3f(posc[1], 0.0, -posc[0]);
426   glVertex3f(-posc[1], 0.0, -posc[0]);
427   glNormal3f(COS15, -SIN15, 0.0);
428   if(cmat) glColor3fv(colors[cind[4][s]]);
429   glTexCoord2f(texc[5], 0.5);      glVertex3f(posc[1], 0.0, posc[0]);
430   glTexCoord2f(1.0, 0.5);          glVertex3f(posc[4], posc[5], posc[0]);
431   glTexCoord2f(1.0, texc[7]);      glVertex3f(posc[4], posc[5], -posc[0]);
432   glTexCoord2f(texc[5], texc[7]);  glVertex3f(posc[1], 0.0, -posc[0]);
433   glNormal3f(SIN15, COS15, 0.0);
434   if(cmat) glColor3fv(colors[cind[4][s+1]]);
435   glTexCoord2f(0.0, 0.5);      glVertex3f(posc[4], posc[5], posc[0]);
436   glTexCoord2f(1.0, 0.5);      glVertex3f(-posc[5], posc[4], posc[0]);
437   glTexCoord2f(1.0, texc[6]);  glVertex3f(-posc[5], posc[4], -posc[0]);
438   glTexCoord2f(0.0, texc[6]);  glVertex3f(posc[4], posc[5], -posc[0]);
439   glNormal3f(-COS15, SIN15, 0.0);
440   if(cmat) glColor3fv(colors[cind[4][s+4]]);
441   glTexCoord2f(0.0, 0.5);          glVertex3f(-posc[5], posc[4], posc[0]);
442   glTexCoord2f(texc[5], 0.5);      glVertex3f(-posc[1], 0.0, posc[0]);
443   glTexCoord2f(texc[5], texc[7]);  glVertex3f(-posc[1], 0.0, -posc[0]);
444   glTexCoord2f(0.0, texc[7]);      glVertex3f(-posc[5], posc[4], -posc[0]);
445   glEnd();
446 }
447
448 static void draw_middle(cube21_conf *cp) {
449   if(cp->hf[0]) glRotatef(180.0, 0.0, 1.0, 0.0);
450   draw_middle_piece(0, cp->cind, cp->colors);
451   if(cp->hf[0]) glRotatef(180.0, 0.0, 1.0, 0.0);
452   glRotatef(180.0, 0.0, 0.0, 1.0);
453   if(cp->hf[1]) glRotatef(180.0, 0.0, 1.0, 0.0);
454   draw_middle_piece(1, cp->cind, cp->colors);
455   if(cp->hf[1]) glRotatef(180.0, 0.0, 1.0, 0.0);
456 }
457
458 static void draw_half_face(cube21_conf *cp, int s, int o) {
459   int i, s1 = 1-s*2, s2 = s*2;
460   for(i = o; i<o+6; i++) {
461     if(cp->pieces[s][i+1])
462       draw_narrow_piece(s1, cp->cind[s2][i], cp->cind[s2+1][i], cp->colors);
463     else {
464       draw_wide_piece(s1, cp->cind[s2][i], cp->cind[s2+1][i], cp->cind[s2+1][i+1], cp->colors);
465       i++;
466     }
467   }
468 }
469
470 static void draw_top_face(cube21_conf *cp) {
471   draw_half_face(cp, 0, 0);
472   draw_half_face(cp, 0, 6);
473 }
474
475 static void draw_bottom_face(cube21_conf *cp) {
476   draw_half_face(cp, 1, 0);
477   draw_half_face(cp, 1, 6);
478 }
479
480 static Bool draw_main(cube21_conf *cp) {
481   GLfloat theta = cp->ramount<0?cp->t:-cp->t;
482   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
483   glLoadIdentity();
484   if(wander)
485     glTranslatef(3.0*cp->ratio*sin(13.0*cp->posarg), 3.0*sin(17.0*cp->posarg), zpos);
486   else
487     glTranslatef(0, 0, zpos);
488   glScalef(size, size, size);
489   glRotatef(cp->xrot, 1.0, 0.0, 0.0);
490   glRotatef(cp->yrot, 0.0, 1.0, 0.0);
491   if(wire) glColor3f(0.7, 0.7, 0.7);
492   switch(cp->state) {
493     case CUBE21_PAUSE1:
494     case CUBE21_PAUSE2:
495       draw_top_face(cp);
496       draw_bottom_face(cp);
497       draw_middle(cp);
498       break;
499     case CUBE21_ROT_TOP:
500       glRotatef(theta, 0.0, 0.0, 1.0);
501       draw_top_face(cp);
502       glRotatef(-theta, 0.0, 0.0, 1.0);
503       draw_bottom_face(cp);
504       draw_middle(cp);
505       break;
506     case CUBE21_ROT_BOTTOM:
507       draw_top_face(cp);
508       glRotatef(theta, 0.0, 0.0, 1.0);
509       draw_bottom_face(cp);
510       glRotatef(-theta, 0.0, 0.0, 1.0);
511       draw_middle(cp);
512       break;
513     case CUBE21_HALF1:
514       glRotatef(theta, 0.0, 1.0, 0.0);
515     case CUBE21_HALF2:
516       draw_half_face(cp, 0, 0);
517       glRotatef(-180.0, 0.0, 0.0, 1.0);
518       draw_half_face(cp, 1, 0);
519       glRotatef(-180.0, 0.0, 0.0, 1.0);
520       if(cp->hf[0]) glRotatef(180.0, 0.0, 1.0, 0.0);
521       draw_middle_piece(0, cp->cind, cp->colors);
522       if(cp->hf[0]) glRotatef(180.0, 0.0, 1.0, 0.0);
523       if(cp->state==CUBE21_HALF1)
524         glRotatef(-theta, 0.0, 1.0, 0.0);
525       else
526         glRotatef(theta, 0.0, 1.0, 0.0);
527       glRotatef(180.0, 0.0, 0.0, 1.0);
528       draw_half_face(cp, 0, 6);
529       glRotatef(-180.0, 0.0, 0.0, 1.0);
530       draw_half_face(cp, 1, 6);
531       glRotatef(-180.0, 0.0, 0.0, 1.0);
532       if(cp->hf[1]) glRotatef(180.0, 0.0, 1.0, 0.0);
533       draw_middle_piece(1, cp->cind, cp->colors);
534       break;
535   }
536   if(spin) {
537     if((cp->xrot += spinspeed)>360.0) cp->xrot -= 360.0;
538     if((cp->yrot += spinspeed)>360.0) cp->yrot -= 360.0;
539   }
540   if(wander)
541     if((cp->posarg += wspeed/1000.0)>360.0) cp->posarg -= 360.0;
542   if((cp->t += tspeed)>cp->tmax) finish(cp);
543   return True;
544 }
545
546 void parse_colmode(void) {
547   if(!colmode_s) {
548     colmode = CUBE21_COLOR_WHITE;
549     return;
550   }
551   if(strstr(colmode_s, "se") || strstr(colmode_s, "sil")) colmode = CUBE21_COLOR_SILVER;
552   else if(strstr(colmode_s, "ce") || strstr(colmode_s, "cla")) colmode = CUBE21_COLOR_CLASSIC;
553   else if(strstr(colmode_s, "2") || strstr(colmode_s, "two")) colmode = CUBE21_COLOR_TWORND;
554   else if(strstr(colmode_s, "6") || strstr(colmode_s, "six")) colmode = CUBE21_COLOR_SIXRND;
555   else if(strstr(colmode_s, "1") || strstr(colmode_s, "ran") || strstr(colmode_s, "rnd")) colmode = CUBE21_COLOR_RANDOM;
556   else colmode = CUBE21_COLOR_WHITE;
557 }
558
559 static void init_texc(void) {
560   GLfloat p = tan(Pi/12);
561   if(texc[0]) return;
562   /* Some significant non-trivial coordinates of the texture... */
563   texc[0] = p;                     /* 0.268 */
564   texc[1] = (1.0-p)/4;             /* 0.183 */
565   texc[2] = (1.0+p)/4;             /* 0.317 */
566   texc[3] = p/2;                   /* 0.134 */
567   texc[4] = 1.0-p/2;               /* 0.866 = sqrt(3)/2 */
568   texc[5] = (1.0+p)/2;             /* 0.634 */
569   texc[6] = (1.0-p)/2;             /* 0.366 */
570   texc[7] = (1.0+p)/2;             /* = texc[5], but has different purpose */
571   /* ...and of the object. We need them exactly at GLfloat precision
572    * for the edges to line up perfectly. */
573   posc[0] = p;                     /* 0.268 */
574   posc[1] = 1.0/cos(Pi/12);        /* 1.035 */
575   posc[2] = cos(Pi/6)/cos(Pi/12);  /* 0.897 */
576   posc[3] = sin(Pi/6)/cos(Pi/12);  /* 0.518 */
577   posc[4] = sqrt(2)*cos(Pi/6);     /* 1.225 */
578   posc[5] = sqrt(2)*sin(Pi/6);     /* 0.707 = 1/sqrt(2) */
579 }
580
581 /* It doesn't look good */
582 /*#define MIPMAP*/
583
584 static void init_gl(ModeInfo *mi) {
585 #ifdef MIPMAP
586   int status;
587 #endif
588   parse_colmode();
589   wire = MI_IS_WIREFRAME(mi);
590   cmat = !wire && (colmode != CUBE21_COLOR_WHITE);
591   if(MI_IS_MONO(mi)) {
592     tex = False;
593     cmat = False;
594   }
595   if(wire) {
596     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
597     return;
598   }
599   if(!tex) color_inner[0] = color_inner[1] = color_inner[2] = 0.4;
600   glClearDepth(1.0);
601   glClearColor(0.0, 0.0, 0.0, 1.0);
602   glDrawBuffer(GL_BACK);
603   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
604   glShadeModel(GL_FLAT);
605   glDepthFunc(GL_LESS);
606   glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
607   glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
608   glLightfv(GL_LIGHT0, GL_POSITION, position0);
609   glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
610   glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
611   glLightfv(GL_LIGHT1, GL_POSITION, position1);
612   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
613   glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
614   glEnable(GL_DEPTH_TEST);
615   glEnable(GL_LIGHT0);
616   glEnable(GL_LIGHT1);
617   glEnable(GL_LIGHTING);
618   glEnable(GL_NORMALIZE);
619   glEnable(GL_COLOR_MATERIAL);
620   glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material_ambient);
621   glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
622   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_specular);
623   glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
624   if(!tex) return;
625   glEnable(GL_TEXTURE_2D);
626 #ifdef MIPMAP
627   clear_gl_error();
628   status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, cubetex.width, cubetex.height,
629       GL_RGB, GL_UNSIGNED_BYTE, cubetex.pixel_data);
630   if (status) {
631     const char *s = gluErrorString(status);
632     fprintf (stderr, "%s: error mipmapping texture: %s\n", progname, (s?s:"(unknown)"));
633     exit (1);
634   }
635   check_gl_error("mipmapping");
636 #else    
637   glTexImage2D(GL_TEXTURE_2D, 0, cubetex.bytes_per_pixel, cubetex.width, cubetex.height,
638       0, GL_RGB, GL_UNSIGNED_BYTE, cubetex.pixel_data);
639 #endif  
640   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
641   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
642   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
643   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
644 #ifdef MIPMAP
645   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
646 #else
647   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
648 #endif
649 }
650
651 static void init_cp(cube21_conf *cp) {
652   int i, j;
653   GLfloat ce_colors[6][3] = {
654     {1.0, 1.0, 1.0},
655     {1.0, 0.5, 0.0},
656     {0.0, 0.9, 0.0},
657     {0.8, 0.0, 0.0},
658     {0.1, 0.1, 1.0},
659     {0.9, 0.9, 0.0}
660   };
661   cp->state = CUBE21_STATE_BASIC;
662   cp->xrot = -65.0; cp->yrot = 185.0;
663   cp->posarg = (wspeed?random()%360:0.0);
664   cp->t = 0.0; cp->tmax = twait;
665   cp->hf[0] = cp->hf[1] = 0;
666   cp->fr[0] = cp->fr[1] = 0;
667   for(i=0;i<13;i++)
668     cp->pieces[0][i] = cp->pieces[1][i] = (i%3==1?0:1);
669   switch(colmode) {
670     case CUBE21_COLOR_RANDOM:
671     case CUBE21_COLOR_TWORND:
672     case CUBE21_COLOR_SIXRND:
673       for(i=0; i<6; i++)
674         for(j=0; j<3; j++)
675           cp->colors[i][j] = rndcolor();
676       break;
677     case CUBE21_COLOR_SILVER:
678       cp->colors[0][0] = 1.0;
679       cp->colors[0][1] = 1.0;
680       cp->colors[0][2] = 1.0;
681       cp->colors[1][0] = rndcolor();
682       cp->colors[1][1] = rndcolor();
683       cp->colors[1][2] = rndcolor();
684       break;
685     case CUBE21_COLOR_CLASSIC:
686       for(i=0; i<6; i++)
687         for(j=0; j<3; j++)
688           cp->colors[i][j] = 0.2+0.7*ce_colors[i][j];
689       break;
690   }
691   switch(colmode) {
692     case CUBE21_COLOR_SILVER:
693     case CUBE21_COLOR_TWORND:
694       for(i=0; i<5; i++)
695         for(j=0; j<12; j++)
696           if(i==0) cp->cind[i][j] = 0;
697           else if(i==2) cp->cind[i][j] = 1;
698           else cp->cind[i][j] = ((j+5)%12)>=6?1:0;
699       break;
700     case CUBE21_COLOR_CLASSIC:
701     case CUBE21_COLOR_SIXRND:
702       for(i=0; i<5; i++)
703         for(j=0; j<12; j++)
704           if(i==0) cp->cind[i][j] = 4;
705           else if(i==2) cp->cind[i][j] = 5;
706           else cp->cind[i][j] = ((j+5)%12)/3;
707       break;
708     case CUBE21_COLOR_RANDOM:
709       for(i=0; i<5; i++)
710         for(j=0; j<12; j++)
711           cp->cind[i][j] = 0;
712       break;
713   }
714   if(rndstart) randomize(cp);
715 }
716
717 /*************************************************************************/
718
719 void reshape_cube21(ModeInfo *mi, int width, int height) {
720   cube21_conf *cp = &cube21[MI_SCREEN(mi)];
721   if(!height) height = 1;
722   cp->ratio = (GLfloat)width/(GLfloat)height;
723   glViewport(0, 0, (GLint) width, (GLint) height);
724   glMatrixMode(GL_PROJECTION);
725   glLoadIdentity();
726   gluPerspective(30.0, cp->ratio, 1.0, 100.0);
727   glMatrixMode(GL_MODELVIEW);
728   glClear(GL_COLOR_BUFFER_BIT);
729 }
730
731 void release_cube21(ModeInfo *mi) {
732   if (cube21 != NULL) {
733     int screen;
734     for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
735       cube21_conf *cp = &cube21[screen];
736       if (cp->glx_context) {
737         cp->glx_context = NULL;
738       }
739     }
740     free((void *)cube21);
741     cube21 = NULL;
742   }
743   FreeAllGL(mi);
744 }
745
746 void init_cube21(ModeInfo *mi) {
747   cube21_conf *cp;
748   if(!cube21) {
749     cube21 = (cube21_conf *)calloc(MI_NUM_SCREENS(mi), sizeof(cube21_conf));
750     if(!cube21) return;
751   }
752   init_texc();
753   cp = &cube21[MI_SCREEN(mi)];
754   if ((cp->glx_context = init_GL(mi)) != NULL) {
755     init_gl(mi);
756     init_cp(cp);
757     reshape_cube21(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
758   } else {
759     MI_CLEARWINDOW(mi);
760   }
761 }
762
763 void draw_cube21(ModeInfo * mi) {
764   Display *display = MI_DISPLAY(mi);
765   Window window = MI_WINDOW(mi);
766   cube21_conf *cp;
767   if (!cube21) return;
768   cp = &cube21[MI_SCREEN(mi)];
769   MI_IS_DRAWN(mi) = True;
770   if (!cp->glx_context) return;
771   glXMakeCurrent(display, window, *(cp->glx_context));
772   if (!draw_main(cp)) {
773     release_cube21(mi);
774     return;
775   }
776   if (MI_IS_FPS(mi)) do_fps (mi);
777   glFlush();
778   glXSwapBuffers(display, window);
779 }
780
781 void change_cube21(ModeInfo * mi) {
782   cube21_conf *cp = &cube21[MI_SCREEN(mi)];
783   if (!cp->glx_context) return;
784   glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cp->glx_context));
785   init_gl(mi);
786 }
787
788 #endif