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