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