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