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