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