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