From http://www.jwz.org/xscreensaver/xscreensaver-5.18.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   /* Do it twice because we don't track the device's orientation. */
519   glRotatef( current_device_rotation(), 0, 0, 1);
520   gltrackball_rotate (cp->trackball);
521   glRotatef(-current_device_rotation(), 0, 0, 1);
522
523   glRotatef(cp->xrot, 1.0, 0.0, 0.0);
524   glRotatef(cp->yrot, 0.0, 1.0, 0.0);
525   if(cp->wire) glColor3f(0.7, 0.7, 0.7);
526   switch(cp->state) {
527     case CUBE21_PAUSE1:
528     case CUBE21_PAUSE2:
529       draw_top_face(mi, cp);
530       draw_bottom_face(mi, cp);
531       draw_middle(cp);
532       break;
533     case CUBE21_ROT_TOP:
534       glRotatef(theta, 0.0, 0.0, 1.0);
535       draw_top_face(mi, cp);
536       glRotatef(-theta, 0.0, 0.0, 1.0);
537       draw_bottom_face(mi, cp);
538       draw_middle(cp);
539       break;
540     case CUBE21_ROT_BOTTOM:
541       draw_top_face(mi, cp);
542       glRotatef(theta, 0.0, 0.0, 1.0);
543       draw_bottom_face(mi, cp);
544       glRotatef(-theta, 0.0, 0.0, 1.0);
545       draw_middle(cp);
546       break;
547     case CUBE21_HALF1:
548       glRotatef(theta, 0.0, 1.0, 0.0);
549     case CUBE21_HALF2:
550       draw_half_face(mi, cp, 0, 0);
551       glRotatef(-180.0, 0.0, 0.0, 1.0);
552       draw_half_face(mi, cp, 1, 0);
553       glRotatef(-180.0, 0.0, 0.0, 1.0);
554       if(cp->hf[0]) glRotatef(180.0, 0.0, 1.0, 0.0);
555       draw_middle_piece(cp, 0, cp->cind, cp->colors);
556       if(cp->hf[0]) glRotatef(180.0, 0.0, 1.0, 0.0);
557       if(cp->state==CUBE21_HALF1)
558         glRotatef(-theta, 0.0, 1.0, 0.0);
559       else
560         glRotatef(theta, 0.0, 1.0, 0.0);
561       glRotatef(180.0, 0.0, 0.0, 1.0);
562       draw_half_face(mi, cp, 0, 6);
563       glRotatef(-180.0, 0.0, 0.0, 1.0);
564       draw_half_face(mi, cp, 1, 6);
565       glRotatef(-180.0, 0.0, 0.0, 1.0);
566       if(cp->hf[1]) glRotatef(180.0, 0.0, 1.0, 0.0);
567       draw_middle_piece(cp, 1, cp->cind, cp->colors);
568       break;
569   }
570   if(spin) {
571     if((cp->xrot += spinspeed)>360.0) cp->xrot -= 360.0;
572     if((cp->yrot += spinspeed)>360.0) cp->yrot -= 360.0;
573   }
574   if(wander)
575     if((cp->posarg += wspeed/1000.0)>360.0) cp->posarg -= 360.0;
576   if((cp->t += tspeed)>cp->tmax) finish(cp);
577   return True;
578 }
579
580 static void parse_colmode(void) 
581 {
582   if(!colmode_s) {
583     colmode = CUBE21_COLOR_WHITE;
584     return;
585   }
586   if(strstr(colmode_s, "se") || strstr(colmode_s, "sil")) colmode = CUBE21_COLOR_SILVER;
587   else if(strstr(colmode_s, "ce") || strstr(colmode_s, "cla")) colmode = CUBE21_COLOR_CLASSIC;
588   else if(strstr(colmode_s, "2") || strstr(colmode_s, "two")) colmode = CUBE21_COLOR_TWORND;
589   else if(strstr(colmode_s, "6") || strstr(colmode_s, "six")) colmode = CUBE21_COLOR_SIXRND;
590   else if(strstr(colmode_s, "1") || strstr(colmode_s, "ran") || strstr(colmode_s, "rnd")) colmode = CUBE21_COLOR_RANDOM;
591   else colmode = CUBE21_COLOR_WHITE;
592 }
593
594 static void init_posc(cube21_conf *cp) 
595 {
596   cp->texp = (1.0-tan(Pi/12.0))/2.0;
597   cp->texq = 1.0-cp->texp;
598   /* Some significant non-trivial coordinates
599    * of the object. We need them exactly at GLfloat precision
600    * for the edges to line up perfectly. */
601   cp->posc[0] = tan(Pi/12);            /* 0.268 */
602   cp->posc[1] = 1.0/cos(Pi/12);        /* 1.035 */
603   cp->posc[2] = cos(Pi/6)/cos(Pi/12);  /* 0.897 */
604   cp->posc[3] = sin(Pi/6)/cos(Pi/12);  /* 0.518 */
605   cp->posc[4] = sqrt(2)*cos(Pi/6);     /* 1.225 */
606   cp->posc[5] = sqrt(2)*sin(Pi/6);     /* 0.707 = 1/sqrt(2) */
607 }
608
609 static void draw_horz_line(cube21_conf *cp, int x1, int x2, int y) 
610 {
611   int x, y0 = y, w;
612   if(y<BORDER) y = -y;
613   else y = -BORDER;
614   for(; y<BORDER; y++) {
615     if(y0+y>=TEX_HEIGHT) break;
616     w = y*y*255/BORDER2;
617     for(x=x1; x<=x2; x++)
618       if(cp->texture[y0+y][x]>w) cp->texture[y0+y][x] = w;
619   }
620 }
621
622 static void draw_vert_line(cube21_conf *cp, int x, int y1, int y2) 
623 {
624   int x0 = x, y, w;
625   if(x<BORDER) x = -x;
626   else x = -BORDER;
627   for(; x<BORDER; x++) {
628     if(x0+x>=TEX_WIDTH) break;
629     w = x*x*255/BORDER2;
630     for(y=y1; y<=y2; y++)
631       if(cp->texture[y][x0+x]>w) cp->texture[y][x0+x] = w;
632   }
633 }
634
635 static void draw_slanted_horz(cube21_conf *cp, int x1, int y1, int x2, int y2) 
636 {
637   int x, y, dx = x2-x1, dy = y2-y1, y0, w;
638   for(x=x1; x<=x2; x++) {
639     y0 = y1+(y2-y1)*(x-x1)/(x2-x1);
640     for(y=-1-BORDER; y<2+BORDER; y++) {
641       w = dx*(y0+y-y1)-dy*(x-x1);
642       w = w*w/(dx*dx+dy*dy);
643       w = w*255/BORDER2;
644       if(cp->texture[y0+y][x]>w) cp->texture[y0+y][x] = w;
645     }
646   }
647 }
648
649 static void draw_slanted_vert(cube21_conf *cp, int x1, int y1, int x2, int y2) 
650 {
651   int x, y, dx = x2-x1, dy = y2-y1, x0, w;
652   for(y=y1; y<=y2; y++) {
653     x0 = x1+(x2-x1)*(y-y1)/(y2-y1);
654     for(x=-1-BORDER; x<2+BORDER; x++) {
655       w = dy*(x0+x-x1)-dx*(y-y1);
656       w = w*w/(dy*dy+dx*dx);
657       w = w*255/BORDER2;
658       if(cp->texture[y][x0+x]>w) cp->texture[y][x0+x] = w;
659     }
660   }
661 }
662
663 static void make_texture(cube21_conf *cp) 
664 {
665   int x, y, x0, y0;
666   float grayp[2] = {TEX_GRAY};
667   for(y=0; y<TEX_HEIGHT; y++)
668     for(x=0; x<TEX_WIDTH; x++)
669       cp->texture[y][x] = 255;
670   draw_horz_line(cp, 0, TEX_WIDTH-1, 0);
671   draw_horz_line(cp, cp->texq*TEX_WIDTH, TEX_WIDTH-1, cp->texp*TEX_HEIGHT);
672   draw_horz_line(cp, cp->texq*TEX_WIDTH, TEX_WIDTH-1, cp->texq*TEX_HEIGHT);
673   draw_horz_line(cp, 0, cp->texq*TEX_WIDTH, TEX_HEIGHT/2);
674   draw_horz_line(cp, 0, TEX_WIDTH-1, TEX_HEIGHT*3/4);
675   draw_horz_line(cp, 0, TEX_WIDTH-1, TEX_HEIGHT-1);
676   draw_vert_line(cp, 0, 0, TEX_HEIGHT-1);
677   draw_vert_line(cp, cp->texq*TEX_WIDTH, 0, TEX_HEIGHT*3/4);
678   draw_vert_line(cp, TEX_WIDTH-1, 0, TEX_HEIGHT-1);
679   draw_slanted_horz(cp, 0, cp->texp*TEX_HEIGHT, TEX_WIDTH/2, TEX_HEIGHT/2);
680   draw_slanted_vert(cp, cp->texp*TEX_WIDTH, 0, TEX_WIDTH/2, TEX_HEIGHT/2);
681   draw_slanted_vert(cp, cp->texq*TEX_WIDTH, 0, TEX_WIDTH/2, TEX_HEIGHT/2);
682   x0 = grayp[0]*TEX_WIDTH;
683   y0 = grayp[1]*TEX_HEIGHT;
684   for(y=-1; y<=1; y++)
685     for(x=-1; x<=1; x++)
686       cp->texture[y0+y][x0+x] = 100;   
687 }
688
689 /* It doesn't look good */
690 /*#define MIPMAP*/
691
692 static void init_gl(ModeInfo *mi) 
693 {
694   cube21_conf *cp = &cube21[MI_SCREEN(mi)];
695 #ifdef MIPMAP
696   int status;
697 #endif
698   parse_colmode();
699   cp->wire = MI_IS_WIREFRAME(mi);
700   cp->cmat = !cp->wire && (colmode != CUBE21_COLOR_WHITE);
701   if(MI_IS_MONO(mi)) {
702     tex = False;
703     cp->cmat = False;
704   }
705   if(cp->wire) {
706     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
707     return;
708   }
709   if(!tex)
710     cp->color_inner[0] = cp->color_inner[1] = cp->color_inner[2] = 0.4;
711   else
712     cp->color_inner[0] = cp->color_inner[1] = cp->color_inner[2] = 1.0;
713
714   glClearDepth(1.0);
715   glDrawBuffer(GL_BACK);
716   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
717   glShadeModel(GL_FLAT);
718   glDepthFunc(GL_LESS);
719   glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
720   glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
721   glLightfv(GL_LIGHT0, GL_POSITION, position0);
722   glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
723   glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
724   glLightfv(GL_LIGHT1, GL_POSITION, position1);
725   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
726   glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
727   glEnable(GL_DEPTH_TEST);
728   glEnable(GL_LIGHT0);
729   glEnable(GL_LIGHT1);
730   glEnable(GL_LIGHTING);
731   glEnable(GL_NORMALIZE);
732   glEnable(GL_COLOR_MATERIAL);
733   glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material_ambient);
734   glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
735   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_specular);
736   glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
737   if(!tex) return;
738   glEnable(GL_TEXTURE_2D);
739 #ifdef MIPMAP
740   clear_gl_error();
741   status = gluBuild2DMipmaps(GL_TEXTURE_2D, 1, TEX_WIDTH, TEX_HEIGHT,
742       GL_LUMINANCE, GL_UNSIGNED_BYTE, texture);
743   if (status) {
744     const char *s = gluErrorString(status);
745     fprintf (stderr, "%s: error mipmapping texture: %s\n", progname, (s?s:"(unknown)"));
746     exit (1);
747   }
748   check_gl_error("mipmapping");
749 #else    
750   glTexImage2D(GL_TEXTURE_2D, 0, 1, TEX_WIDTH, TEX_HEIGHT,
751       0, GL_LUMINANCE, GL_UNSIGNED_BYTE, cp->texture);
752 #endif  
753   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
754   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
755   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
756   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
757 #ifdef MIPMAP
758   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
759 #else
760   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
761 #endif
762 }
763
764 static void init_cp(cube21_conf *cp) 
765 {
766   int i, j;
767   GLfloat ce_colors[6][3] = {
768     {1.0, 1.0, 1.0},
769     {1.0, 0.5, 0.0},
770     {0.0, 0.9, 0.0},
771     {0.8, 0.0, 0.0},
772     {0.1, 0.1, 1.0},
773     {0.9, 0.9, 0.0}
774   };
775   cp->state = CUBE21_STATE_BASIC;
776   cp->xrot = -65.0; cp->yrot = 185.0;
777   cp->posarg = (wspeed?random()%360:0.0);
778   cp->t = 0.0; cp->tmax = twait;
779   cp->hf[0] = cp->hf[1] = 0;
780   cp->fr[0] = cp->fr[1] = 0;
781   for(i=0;i<13;i++)
782     cp->pieces[0][i] = cp->pieces[1][i] = (i%3==1?0:1);
783   switch(colmode) {
784     case CUBE21_COLOR_RANDOM:
785     case CUBE21_COLOR_TWORND:
786     case CUBE21_COLOR_SIXRND:
787       for(i=0; i<6; i++)
788         for(j=0; j<3; j++)
789           cp->colors[i][j] = rndcolor();
790       break;
791     case CUBE21_COLOR_SILVER:
792       cp->colors[0][0] = 1.0;
793       cp->colors[0][1] = 1.0;
794       cp->colors[0][2] = 1.0;
795       cp->colors[1][0] = rndcolor();
796       cp->colors[1][1] = rndcolor();
797       cp->colors[1][2] = rndcolor();
798       break;
799     case CUBE21_COLOR_CLASSIC:
800       for(i=0; i<6; i++)
801         for(j=0; j<3; j++)
802           cp->colors[i][j] = 0.2+0.7*ce_colors[i][j];
803       break;
804   }
805   switch(colmode) {
806     case CUBE21_COLOR_SILVER:
807     case CUBE21_COLOR_TWORND:
808       for(i=0; i<5; i++)
809         for(j=0; j<12; j++)
810           if(i==0) cp->cind[i][j] = 0;
811           else if(i==2) cp->cind[i][j] = 1;
812           else cp->cind[i][j] = ((j+5)%12)>=6?1:0;
813       break;
814     case CUBE21_COLOR_CLASSIC:
815     case CUBE21_COLOR_SIXRND:
816       for(i=0; i<5; i++)
817         for(j=0; j<12; j++)
818           if(i==0) cp->cind[i][j] = 4;
819           else if(i==2) cp->cind[i][j] = 5;
820           else cp->cind[i][j] = ((j+5)%12)/3;
821       break;
822     case CUBE21_COLOR_RANDOM:
823       for(i=0; i<5; i++)
824         for(j=0; j<12; j++)
825           cp->cind[i][j] = 0;
826       break;
827   }
828   if(rndstart) randomize(cp);
829 }
830
831 /*************************************************************************/
832
833 ENTRYPOINT void reshape_cube21(ModeInfo *mi, int width, int height) 
834 {
835   cube21_conf *cp = &cube21[MI_SCREEN(mi)];
836   if(!height) height = 1;
837   cp->ratio = (GLfloat)width/(GLfloat)height;
838   glViewport(0, 0, (GLint) width, (GLint) height);
839   glMatrixMode(GL_PROJECTION);
840   glLoadIdentity();
841   gluPerspective(30.0, cp->ratio, 1.0, 100.0);
842   glMatrixMode(GL_MODELVIEW);
843   glClear(GL_COLOR_BUFFER_BIT);
844 }
845
846 ENTRYPOINT Bool
847 cube21_handle_event (ModeInfo *mi, XEvent *event)
848 {
849   cube21_conf *cp = &cube21[MI_SCREEN(mi)];
850
851   if (event->xany.type == ButtonPress &&
852       event->xbutton.button == Button1)
853     {
854       cp->button_down_p = True;
855       gltrackball_start (cp->trackball,
856                          event->xbutton.x, event->xbutton.y,
857                          MI_WIDTH (mi), MI_HEIGHT (mi));
858       return True;
859     }
860   else if (event->xany.type == ButtonRelease &&
861            event->xbutton.button == Button1)
862     {
863       cp->button_down_p = False;
864       return True;
865     }
866   else if (event->xany.type == ButtonPress &&
867            (event->xbutton.button == Button4 ||
868             event->xbutton.button == Button5 ||
869             event->xbutton.button == Button6 ||
870             event->xbutton.button == Button7))
871     {
872       gltrackball_mousewheel (cp->trackball, event->xbutton.button, 10,
873                               !!event->xbutton.state);
874       return True;
875     }
876   else if (event->xany.type == MotionNotify &&
877            cp->button_down_p)
878     {
879       gltrackball_track (cp->trackball,
880                          event->xmotion.x, event->xmotion.y,
881                          MI_WIDTH (mi), MI_HEIGHT (mi));
882       return True;
883     }
884
885   return False;
886 }
887
888
889 ENTRYPOINT void release_cube21(ModeInfo *mi) 
890 {
891   if (cube21 != NULL) {
892     int screen;
893     for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
894       cube21_conf *cp = &cube21[screen];
895       if (cp->glx_context) {
896         cp->glx_context = NULL;
897       }
898     }
899     free((void *)cube21);
900     cube21 = NULL;
901   }
902   FreeAllGL(mi);
903 }
904
905 ENTRYPOINT void init_cube21(ModeInfo *mi) 
906 {
907   cube21_conf *cp;
908   if(!cube21) {
909     cube21 = (cube21_conf *)calloc(MI_NUM_SCREENS(mi), sizeof(cube21_conf));
910     if(!cube21) return;
911   }
912   cp = &cube21[MI_SCREEN(mi)];
913
914   cp->trackball = gltrackball_init ();
915
916   if(!cp->texp) {
917     init_posc(cp);
918     make_texture(cp);
919   }
920
921   if ((cp->glx_context = init_GL(mi)) != NULL) {
922     init_gl(mi);
923     init_cp(cp);
924     reshape_cube21(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
925   } else {
926     MI_CLEARWINDOW(mi);
927   }
928 }
929
930 ENTRYPOINT void draw_cube21(ModeInfo * mi) 
931 {
932   Display *display = MI_DISPLAY(mi);
933   Window window = MI_WINDOW(mi);
934   cube21_conf *cp;
935   if (!cube21) return;
936   cp = &cube21[MI_SCREEN(mi)];
937   MI_IS_DRAWN(mi) = True;
938   if (!cp->glx_context) return;
939   mi->polygon_count = 0;
940   glXMakeCurrent(display, window, *(cp->glx_context));
941   if (!draw_main(mi, cp)) {
942     release_cube21(mi);
943     return;
944   }
945   if (MI_IS_FPS(mi)) do_fps (mi);
946   glFlush();
947   glXSwapBuffers(display, window);
948 }
949
950 #ifndef STANDALONE
951 ENTRYPOINT void change_cube21(ModeInfo * mi) 
952 {
953   cube21_conf *cp = &cube21[MI_SCREEN(mi)];
954   if (!cp->glx_context) return;
955   glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cp->glx_context));
956   init_gl(mi);
957 }
958 #endif /* !STANDALONE */
959
960
961 XSCREENSAVER_MODULE ("Cube21", cube21)
962
963 #endif