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.
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.
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.
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
23 * Vaclav (Vasek) Potocek
24 * vasek.potocek@post.cz
28 * Future: some simple "solve mode"
29 * Future: new light model, less ambient light
30 * Maybe: use rotator and trackball
31 * Maybe: display lists - seem impossible with colors
35 * Texture mapping is only available on RGBA contexts, Mono and color index
36 * visuals DO NOT support texture mapping in OpenGL.
38 * BUT Mesa do implements RGBA contexts in pseudo color visuals, so texture
39 * mapping should work on PseudoColor, DirectColor, TrueColor using Mesa. Mono
40 * is not officially supported for both OpenGL and Mesa, but seems to not crash
43 * In real OpenGL, PseudoColor DO NOT support texture map (as far as I know).
46 #define PROGCLASS "Cube21"
47 #define HACK_INIT init_cube21
48 #define HACK_DRAW draw_cube21
49 #define HACK_RESHAPE reshape_cube21
50 #define cube21_opts xlockmore_opts
52 #define DEF_XYSPEED "1.0"
53 #define DEF_TSPEED "3.0"
54 #define DEF_WSPEED "1.0"
55 #define DEF_TWAIT "40.0"
56 #define DEF_SIZE "0.7"
57 #define DEF_COLMODE "(none)"
59 #define DEFAULTS "*delay: 20000 \n" \
60 "*showFPS: False \n" \
61 "*wireframe: False \n"
63 #include "xlockmore.h"
68 #include "cube21-tex.h"
77 #define COS15 0.9659258263
78 #define SIN15 0.2588190451
79 #define COS30 0.8660254038
80 #define SIN30 0.5000000000
83 #define countof(x) (sizeof((x))/sizeof((*x)))
85 #define rnd01() (random()%2)
86 #define rndcolor() (frand(0.5)+0.3)
88 /*************************************************************************/
90 static Bool wander, spin, wire, tex, rndstart, cmat;
91 static float spinspeed, tspeed, wspeed, twait, size;
92 static char *colmode_s;
95 static argtype vars[] = {
96 { &spin, "spin", "Spin", "True", t_Bool},
97 { &wander, "wander", "Wander", "True", t_Bool},
98 { &rndstart, "randomize", "Randomize", "True", t_Bool},
99 { &tex, "texture", "Texture", "True", t_Bool},
100 { &spinspeed, "spinspeed", "SpinSpeed", DEF_XYSPEED, t_Float},
101 { &tspeed, "rotspeed", "RotSpeed", DEF_TSPEED, t_Float},
102 { &wspeed, "wanderspeed", "WanderSpeed", DEF_WSPEED, t_Float},
103 { &twait, "wait", "Wait", DEF_TWAIT, t_Float},
104 { &size, "cubesize", "CubeSize", DEF_SIZE, t_Float},
105 { &colmode_s, "colormode", "ColorMode", DEF_COLMODE, t_String}
108 static XrmOptionDescRec opts[] = {
109 { "-spin", ".spin", XrmoptionNoArg, "True" },
110 { "+spin", ".spin", XrmoptionNoArg, "False" },
111 { "-wander", ".wander", XrmoptionNoArg, "True" },
112 { "+wander", ".wander", XrmoptionNoArg, "False" },
113 { "-randomize", ".randomize", XrmoptionNoArg, "True" },
114 { "+randomize", ".randomize", XrmoptionNoArg, "False" },
115 { "-texture", ".texture", XrmoptionNoArg, "True" },
116 { "+texture", ".texture", XrmoptionNoArg, "False" },
117 { "-spinspeed", ".spinspeed", XrmoptionSepArg, 0 },
118 { "-wanderspeed", ".wanderspeed", XrmoptionSepArg, 0 },
119 { "-rotspeed", ".rotspeed", XrmoptionSepArg, 0 },
120 { "-wait", ".wait", XrmoptionSepArg, 0 },
121 { "-cubesize", ".cubesize", XrmoptionSepArg, 0 },
122 { "-colormode", ".colormode", XrmoptionSepArg, DEF_COLMODE }
125 ModeSpecOpt cube21_opts = {countof(opts), opts, countof(vars), vars, NULL};
128 ModStruct cube21_description =
129 { "cube21", "init_cube21", "draw_cube21", "release_cube21",
130 "draw_cube21", "change_cube21", NULL, &cube21_opts,
131 25000, 1, 1, 1, 1.0, 4, "",
132 "Shows randomly shuffling Cube 21", 0, NULL
138 CUBE21_PAUSE1 = CUBE21_STATE_BASIC,
140 CUBE21_ROT_TOP = CUBE21_ROT_BASE,
144 CUBE21_HALF1 = CUBE21_HALF_BASE,
153 CUBE21_COLOR_CLASSIC,
157 typedef int pieces_t[2][13];
158 typedef int cind_t[5][12];
159 typedef GLfloat col_t[6][3];
162 GLXContext *glx_context;
164 cube21_state state; /* type of "rotation" - shuffling */
165 GLfloat xrot, yrot; /* "spin" - object rotation around axis */
166 GLfloat posarg; /* position argument (for sine function) */
167 GLfloat t, tmax; /* rotation clock */
168 int hf[2], fr[2]; /* half flipped / face rotated flags */
169 int rface, ramount; /* selected face and amount of rotation in multiplies of 30deg */
170 int pieces[2][13]; /* locations of "narrow" and "wide" pieces */
171 int cind[5][12]; /* color indices */
172 GLfloat colors[6][3]; /* color map */
175 static cube21_conf *cube21 = NULL;
177 static GLfloat shininess = 20.0;
178 static GLfloat ambient[] = {0.0, 0.0, 0.0, 1.0};
179 static GLfloat diffuse[] = {1.0, 1.0, 1.0, 1.0};
180 static GLfloat position0[] = {1.0, 1.0, 1.0, 0.0};
181 static GLfloat position1[] = {-1.0, -1.0, 1.0, 0.0};
182 static GLfloat lmodel_ambient[] = {0.1, 0.1, 0.1, 1.0};
183 static GLfloat material_ambient[] = {0.7, 0.7, 0.7, 1.0};
184 static GLfloat material_diffuse[] = {0.7, 0.7, 0.7, 1.0};
185 static GLfloat material_specular[] = {0.2, 0.2, 0.2, 1.0};
186 static GLfloat color_inner[] = {1.0, 1.0, 1.0};
187 static GLfloat texc[8] = {0.0}, posc[6];
188 static GLfloat zpos = -18.0;
190 /*************************************************************************/
192 static void find_matches(pieces_t pieces, int matches[12], int s) {
194 for(i = 1; i<6; i++) {
195 if(pieces[s][i] && pieces[s][i+6]) {
200 for(i = 1; i<matches[0]; i++) {
201 matches[j++] = matches[i]-6;
207 static void rot_face(pieces_t pieces, cind_t colors, int s, int o) {
209 int tmp[12], tmpc[2][12];
210 int c0 = 2*s, c1 = c0+1;
211 for(i = 0; i<12; i++) {
212 tmp[i] = pieces[s][i];
213 tmpc[0][i] = colors[c0][i];
214 tmpc[1][i] = colors[c1][i];
217 for(i = 0; i<12; i++, o++) {
219 pieces[s][i] = tmp[o];
220 colors[c0][i] = tmpc[0][o];
221 colors[c1][i] = tmpc[1][o];
225 static void rot_halves(pieces_t pieces, cind_t colors, int hf[2], int s) {
226 int ss = 6*s, i, j, k, t;
227 for(i = 0; i<6; i++) {
228 j = ss+i; k = ss+6-i;
230 pieces[0][j] = pieces[1][k];
234 colors[0][j] = colors[2][k];
237 colors[1][j] = colors[3][k];
243 static void randomize(cube21_conf *cp) {
246 for(i = 0; i<SHUFFLE; i++) {
248 find_matches(cp->pieces, matches, s);
252 rot_face(cp->pieces, cp->cind, s, j);
254 rot_halves(cp->pieces, cp->cind, cp->hf, s);
258 static void finish(cube21_conf *cp) {
264 find_matches(cp->pieces, matches, s);
268 if(j==6 && rnd01()) j = -6;
269 cp->state = CUBE21_ROT_BASE+s;
270 cp->tmax = 30.0*abs(j);
271 cp->fr[0] = cp->fr[1] = 0;
276 case CUBE21_ROT_BOTTOM:
277 rot_face(cp->pieces, cp->cind, s = cp->rface, cp->ramount);
280 if(!cp->fr[s] && rnd01()) {
281 find_matches(cp->pieces, matches, s);
285 if(j==6 && rnd01()) j = -6;
286 cp->state = CUBE21_ROT_BASE+s;
287 cp->tmax = 30.0*abs(j);
292 cp->state = CUBE21_PAUSE2;
298 cp->ramount = -rnd01(); /* 0 or -1, only sign is significant in this case */
299 cp->state = CUBE21_HALF_BASE+s;
305 rot_halves(cp->pieces, cp->cind, cp->hf, cp->rface);
306 cp->state = CUBE21_PAUSE1;
313 static void draw_narrow_piece(GLfloat s, int c1, int c2, col_t colors) {
314 GLfloat s1 = posc[0]*s;
315 glBegin(GL_TRIANGLES);
316 glNormal3f(0.0, 0.0, s);
317 if(cmat) glColor3fv(colors[c1]);
318 glTexCoord2f(0.0, 0.25); glVertex3f(0.0, 0.0, s);
319 glTexCoord2f(0.5, texc[1]); glVertex3f(posc[1], 0.0, s);
320 glTexCoord2f(0.5, texc[2]); glVertex3f(posc[2], posc[3], s);
321 glNormal3f(0.0, 0.0, -s);
322 if(cmat) glColor3fv(color_inner);
323 glTexCoord2f(1.0, 1.0);
324 glVertex3f(0.0, 0.0, s1);
325 glVertex3f(posc[1], 0.0, s1);
326 glVertex3f(posc[2], posc[3], s1);
329 glNormal3f(0.0, -1.0, 0.0);
330 if(cmat) glColor3fv(color_inner);
331 glTexCoord2f(1.0, 1.0);
332 glVertex3f(0.0, 0.0, s);
333 glVertex3f(posc[1], 0.0, s);
334 glVertex3f(posc[1], 0.0, s1);
335 glVertex3f(0.0, 0.0, s1);
336 glNormal3f(COS15, SIN15, 0.0);
337 if(cmat) glColor3fv(colors[c2]);
338 glTexCoord2f(0.5, texc[1]); glVertex3f(posc[1], 0.0, s);
339 glTexCoord2f(0.5, texc[2]); glVertex3f(posc[2], posc[3], s);
340 glTexCoord2f(texc[4], texc[2]); glVertex3f(posc[2], posc[3], s1);
341 glTexCoord2f(texc[4], texc[1]); glVertex3f(posc[1], 0.0, s1);
342 glNormal3f(-SIN30, COS30, 0.0);
343 if(cmat) glColor3fv(color_inner);
344 glTexCoord2f(1.0, 1.0);
345 glVertex3f(0.0, 0.0, s);
346 glVertex3f(posc[2], posc[3], s);
347 glVertex3f(posc[2], posc[3], s1);
348 glVertex3f(0.0, 0.0, s1);
350 glRotatef(30.0, 0.0, 0.0, 1.0);
353 static void draw_wide_piece(GLfloat s, int c1, int c2, int c3, col_t colors) {
354 GLfloat s1 = posc[0]*s;
355 glBegin(GL_TRIANGLES);
356 glNormal3f(0.0, 0.0, s);
357 if(cmat) glColor3fv(colors[c1]);
358 glTexCoord2f(0.0, 0.25); glVertex3f(0.0, 0.0, s);
359 glTexCoord2f(0.5, texc[1]); glVertex3f(posc[1], 0.0, s);
360 glTexCoord2f(0.5, 0.0); glVertex3f(posc[4], posc[5], s);
361 glTexCoord2f(0.5, 0.0); glVertex3f(posc[4], posc[5], s);
362 glTexCoord2f(texc[3], 0.0); glVertex3f(posc[3], posc[2], s);
363 glTexCoord2f(0.0, 0.25); glVertex3f(0.0, 0.0, s);
364 glNormal3f(0.0, 0.0, -s);
365 if(cmat) glColor3fv(color_inner);
366 glTexCoord2f(1.0, 1.0);
367 glVertex3f(0.0, 0.0, s1);
368 glVertex3f(posc[1], 0.0, s1);
369 glVertex3f(posc[4], posc[5], s1);
370 glVertex3f(posc[4], posc[5], s1);
371 glVertex3f(posc[3], posc[2], s1);
372 glVertex3f(0.0, 0.0, s1);
375 glNormal3f(0.0, -1.0, 0);
376 if(cmat) glColor3fv(color_inner);
377 glTexCoord2f(1.0, 1.0);
378 glVertex3f(0.0, 0.0, s);
379 glVertex3f(posc[1], 0.0, s);
380 glVertex3f(posc[1], 0.0, s1);
381 glVertex3f(0.0, 0.0, s1);
382 glNormal3f(COS15, -SIN15, 0.0);
383 if(cmat) glColor3fv(colors[c2]);
384 glTexCoord2f(0.5, texc[1]); glVertex3f(posc[1], 0.0, s);
385 glTexCoord2f(0.5, 0.0); glVertex3f(posc[4], posc[5], s);
386 glTexCoord2f(texc[4], 0.0); glVertex3f(posc[4], posc[5], s1);
387 glTexCoord2f(texc[4], texc[1]); glVertex3f(posc[1], 0.0, s1);
388 glNormal3f(SIN15, COS15, 0.0);
389 if(cmat) glColor3fv(colors[c3]);
390 glTexCoord2f(0.5, texc[1]); glVertex3f(posc[4], posc[5], s);
391 glTexCoord2f(0.5, 0.0); glVertex3f(posc[3], posc[2], s);
392 glTexCoord2f(texc[4], 0.0); glVertex3f(posc[3], posc[2], s1);
393 glTexCoord2f(texc[4], texc[1]); glVertex3f(posc[4], posc[5], s1);
394 glNormal3f(-COS30, SIN30, 0.0);
395 if(cmat) glColor3fv(color_inner);
396 glTexCoord2f(1.0, 1.0);
397 glVertex3f(0.0, 0.0, s);
398 glVertex3f(posc[3], posc[2], s);
399 glVertex3f(posc[3], posc[2], s1);
400 glVertex3f(0.0, 0.0, s1);
402 glRotatef(60.0, 0.0, 0.0, 1.0);
405 static void draw_middle_piece(int s, cind_t cind, col_t colors) {
408 if(cmat) glColor3fv(color_inner);
409 glNormal3f(0.0, 0.0, 1.0);
410 glTexCoord2f(1.0, 1.0);
411 glVertex3f(posc[1], 0.0, posc[0]);
412 glVertex3f(posc[4], posc[5], posc[0]);
413 glVertex3f(-posc[5], posc[4], posc[0]);
414 glVertex3f(-posc[1], 0.0, posc[0]);
415 glNormal3f(0.0, 0.0, -1.0);
416 glTexCoord2f(1.0, 1.0);
417 glVertex3f(posc[1], 0.0, -posc[0]);
418 glVertex3f(posc[4], posc[5], -posc[0]);
419 glVertex3f(-posc[5], posc[4], -posc[0]);
420 glVertex3f(-posc[1], 0.0, -posc[0]);
421 glNormal3f(0.0, -1.0, 0.0);
422 glTexCoord2f(1.0, 1.0);
423 glVertex3f(-posc[1], 0.0, posc[0]);
424 glVertex3f(posc[1], 0.0, posc[0]);
425 glVertex3f(posc[1], 0.0, -posc[0]);
426 glVertex3f(-posc[1], 0.0, -posc[0]);
427 glNormal3f(COS15, -SIN15, 0.0);
428 if(cmat) glColor3fv(colors[cind[4][s]]);
429 glTexCoord2f(texc[5], 0.5); glVertex3f(posc[1], 0.0, posc[0]);
430 glTexCoord2f(1.0, 0.5); glVertex3f(posc[4], posc[5], posc[0]);
431 glTexCoord2f(1.0, texc[7]); glVertex3f(posc[4], posc[5], -posc[0]);
432 glTexCoord2f(texc[5], texc[7]); glVertex3f(posc[1], 0.0, -posc[0]);
433 glNormal3f(SIN15, COS15, 0.0);
434 if(cmat) glColor3fv(colors[cind[4][s+1]]);
435 glTexCoord2f(0.0, 0.5); glVertex3f(posc[4], posc[5], posc[0]);
436 glTexCoord2f(1.0, 0.5); glVertex3f(-posc[5], posc[4], posc[0]);
437 glTexCoord2f(1.0, texc[6]); glVertex3f(-posc[5], posc[4], -posc[0]);
438 glTexCoord2f(0.0, texc[6]); glVertex3f(posc[4], posc[5], -posc[0]);
439 glNormal3f(-COS15, SIN15, 0.0);
440 if(cmat) glColor3fv(colors[cind[4][s+4]]);
441 glTexCoord2f(0.0, 0.5); glVertex3f(-posc[5], posc[4], posc[0]);
442 glTexCoord2f(texc[5], 0.5); glVertex3f(-posc[1], 0.0, posc[0]);
443 glTexCoord2f(texc[5], texc[7]); glVertex3f(-posc[1], 0.0, -posc[0]);
444 glTexCoord2f(0.0, texc[7]); glVertex3f(-posc[5], posc[4], -posc[0]);
448 static void draw_middle(cube21_conf *cp) {
449 if(cp->hf[0]) glRotatef(180.0, 0.0, 1.0, 0.0);
450 draw_middle_piece(0, cp->cind, cp->colors);
451 if(cp->hf[0]) glRotatef(180.0, 0.0, 1.0, 0.0);
452 glRotatef(180.0, 0.0, 0.0, 1.0);
453 if(cp->hf[1]) glRotatef(180.0, 0.0, 1.0, 0.0);
454 draw_middle_piece(1, cp->cind, cp->colors);
455 if(cp->hf[1]) glRotatef(180.0, 0.0, 1.0, 0.0);
458 static void draw_half_face(cube21_conf *cp, int s, int o) {
459 int i, s1 = 1-s*2, s2 = s*2;
460 for(i = o; i<o+6; i++) {
461 if(cp->pieces[s][i+1])
462 draw_narrow_piece(s1, cp->cind[s2][i], cp->cind[s2+1][i], cp->colors);
464 draw_wide_piece(s1, cp->cind[s2][i], cp->cind[s2+1][i], cp->cind[s2+1][i+1], cp->colors);
470 static void draw_top_face(cube21_conf *cp) {
471 draw_half_face(cp, 0, 0);
472 draw_half_face(cp, 0, 6);
475 static void draw_bottom_face(cube21_conf *cp) {
476 draw_half_face(cp, 1, 0);
477 draw_half_face(cp, 1, 6);
480 static Bool draw_main(cube21_conf *cp) {
481 GLfloat theta = cp->ramount<0?cp->t:-cp->t;
482 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
485 glTranslatef(3.0*cp->ratio*sin(13.0*cp->posarg), 3.0*sin(17.0*cp->posarg), zpos);
487 glTranslatef(0, 0, zpos);
488 glScalef(size, size, size);
489 glRotatef(cp->xrot, 1.0, 0.0, 0.0);
490 glRotatef(cp->yrot, 0.0, 1.0, 0.0);
491 if(wire) glColor3f(0.7, 0.7, 0.7);
496 draw_bottom_face(cp);
500 glRotatef(theta, 0.0, 0.0, 1.0);
502 glRotatef(-theta, 0.0, 0.0, 1.0);
503 draw_bottom_face(cp);
506 case CUBE21_ROT_BOTTOM:
508 glRotatef(theta, 0.0, 0.0, 1.0);
509 draw_bottom_face(cp);
510 glRotatef(-theta, 0.0, 0.0, 1.0);
514 glRotatef(theta, 0.0, 1.0, 0.0);
516 draw_half_face(cp, 0, 0);
517 glRotatef(-180.0, 0.0, 0.0, 1.0);
518 draw_half_face(cp, 1, 0);
519 glRotatef(-180.0, 0.0, 0.0, 1.0);
520 if(cp->hf[0]) glRotatef(180.0, 0.0, 1.0, 0.0);
521 draw_middle_piece(0, cp->cind, cp->colors);
522 if(cp->hf[0]) glRotatef(180.0, 0.0, 1.0, 0.0);
523 if(cp->state==CUBE21_HALF1)
524 glRotatef(-theta, 0.0, 1.0, 0.0);
526 glRotatef(theta, 0.0, 1.0, 0.0);
527 glRotatef(180.0, 0.0, 0.0, 1.0);
528 draw_half_face(cp, 0, 6);
529 glRotatef(-180.0, 0.0, 0.0, 1.0);
530 draw_half_face(cp, 1, 6);
531 glRotatef(-180.0, 0.0, 0.0, 1.0);
532 if(cp->hf[1]) glRotatef(180.0, 0.0, 1.0, 0.0);
533 draw_middle_piece(1, cp->cind, cp->colors);
537 if((cp->xrot += spinspeed)>360.0) cp->xrot -= 360.0;
538 if((cp->yrot += spinspeed)>360.0) cp->yrot -= 360.0;
541 if((cp->posarg += wspeed/1000.0)>360.0) cp->posarg -= 360.0;
542 if((cp->t += tspeed)>cp->tmax) finish(cp);
546 void parse_colmode(void) {
548 colmode = CUBE21_COLOR_WHITE;
551 if(strstr(colmode_s, "se") || strstr(colmode_s, "sil")) colmode = CUBE21_COLOR_SILVER;
552 else if(strstr(colmode_s, "ce") || strstr(colmode_s, "cla")) colmode = CUBE21_COLOR_CLASSIC;
553 else if(strstr(colmode_s, "2") || strstr(colmode_s, "two")) colmode = CUBE21_COLOR_TWORND;
554 else if(strstr(colmode_s, "6") || strstr(colmode_s, "six")) colmode = CUBE21_COLOR_SIXRND;
555 else if(strstr(colmode_s, "1") || strstr(colmode_s, "ran") || strstr(colmode_s, "rnd")) colmode = CUBE21_COLOR_RANDOM;
556 else colmode = CUBE21_COLOR_WHITE;
559 static void init_texc(void) {
560 GLfloat p = tan(Pi/12);
562 /* Some significant non-trivial coordinates of the texture... */
563 texc[0] = p; /* 0.268 */
564 texc[1] = (1.0-p)/4; /* 0.183 */
565 texc[2] = (1.0+p)/4; /* 0.317 */
566 texc[3] = p/2; /* 0.134 */
567 texc[4] = 1.0-p/2; /* 0.866 = sqrt(3)/2 */
568 texc[5] = (1.0+p)/2; /* 0.634 */
569 texc[6] = (1.0-p)/2; /* 0.366 */
570 texc[7] = (1.0+p)/2; /* = texc[5], but has different purpose */
571 /* ...and of the object. We need them exactly at GLfloat precision
572 * for the edges to line up perfectly. */
573 posc[0] = p; /* 0.268 */
574 posc[1] = 1.0/cos(Pi/12); /* 1.035 */
575 posc[2] = cos(Pi/6)/cos(Pi/12); /* 0.897 */
576 posc[3] = sin(Pi/6)/cos(Pi/12); /* 0.518 */
577 posc[4] = sqrt(2)*cos(Pi/6); /* 1.225 */
578 posc[5] = sqrt(2)*sin(Pi/6); /* 0.707 = 1/sqrt(2) */
581 /* It doesn't look good */
584 static void init_gl(ModeInfo *mi) {
589 wire = MI_IS_WIREFRAME(mi);
590 cmat = !wire && (colmode != CUBE21_COLOR_WHITE);
596 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
599 if(!tex) color_inner[0] = color_inner[1] = color_inner[2] = 0.4;
601 glClearColor(0.0, 0.0, 0.0, 1.0);
602 glDrawBuffer(GL_BACK);
603 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
604 glShadeModel(GL_FLAT);
605 glDepthFunc(GL_LESS);
606 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
607 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
608 glLightfv(GL_LIGHT0, GL_POSITION, position0);
609 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
610 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
611 glLightfv(GL_LIGHT1, GL_POSITION, position1);
612 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
613 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
614 glEnable(GL_DEPTH_TEST);
617 glEnable(GL_LIGHTING);
618 glEnable(GL_NORMALIZE);
619 glEnable(GL_COLOR_MATERIAL);
620 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material_ambient);
621 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
622 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_specular);
623 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
625 glEnable(GL_TEXTURE_2D);
628 status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, cubetex.width, cubetex.height,
629 GL_RGB, GL_UNSIGNED_BYTE, cubetex.pixel_data);
631 const char *s = gluErrorString(status);
632 fprintf (stderr, "%s: error mipmapping texture: %s\n", progname, (s?s:"(unknown)"));
635 check_gl_error("mipmapping");
637 glTexImage2D(GL_TEXTURE_2D, 0, cubetex.bytes_per_pixel, cubetex.width, cubetex.height,
638 0, GL_RGB, GL_UNSIGNED_BYTE, cubetex.pixel_data);
640 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
641 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
642 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
643 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
645 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
647 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
651 static void init_cp(cube21_conf *cp) {
653 GLfloat ce_colors[6][3] = {
661 cp->state = CUBE21_STATE_BASIC;
662 cp->xrot = -65.0; cp->yrot = 185.0;
663 cp->posarg = (wspeed?random()%360:0.0);
664 cp->t = 0.0; cp->tmax = twait;
665 cp->hf[0] = cp->hf[1] = 0;
666 cp->fr[0] = cp->fr[1] = 0;
668 cp->pieces[0][i] = cp->pieces[1][i] = (i%3==1?0:1);
670 case CUBE21_COLOR_RANDOM:
671 case CUBE21_COLOR_TWORND:
672 case CUBE21_COLOR_SIXRND:
675 cp->colors[i][j] = rndcolor();
677 case CUBE21_COLOR_SILVER:
678 cp->colors[0][0] = 1.0;
679 cp->colors[0][1] = 1.0;
680 cp->colors[0][2] = 1.0;
681 cp->colors[1][0] = rndcolor();
682 cp->colors[1][1] = rndcolor();
683 cp->colors[1][2] = rndcolor();
685 case CUBE21_COLOR_CLASSIC:
688 cp->colors[i][j] = 0.2+0.7*ce_colors[i][j];
692 case CUBE21_COLOR_SILVER:
693 case CUBE21_COLOR_TWORND:
696 if(i==0) cp->cind[i][j] = 0;
697 else if(i==2) cp->cind[i][j] = 1;
698 else cp->cind[i][j] = ((j+5)%12)>=6?1:0;
700 case CUBE21_COLOR_CLASSIC:
701 case CUBE21_COLOR_SIXRND:
704 if(i==0) cp->cind[i][j] = 4;
705 else if(i==2) cp->cind[i][j] = 5;
706 else cp->cind[i][j] = ((j+5)%12)/3;
708 case CUBE21_COLOR_RANDOM:
714 if(rndstart) randomize(cp);
717 /*************************************************************************/
719 void reshape_cube21(ModeInfo *mi, int width, int height) {
720 cube21_conf *cp = &cube21[MI_SCREEN(mi)];
721 if(!height) height = 1;
722 cp->ratio = (GLfloat)width/(GLfloat)height;
723 glViewport(0, 0, (GLint) width, (GLint) height);
724 glMatrixMode(GL_PROJECTION);
726 gluPerspective(30.0, cp->ratio, 1.0, 100.0);
727 glMatrixMode(GL_MODELVIEW);
728 glClear(GL_COLOR_BUFFER_BIT);
731 void release_cube21(ModeInfo *mi) {
732 if (cube21 != NULL) {
734 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
735 cube21_conf *cp = &cube21[screen];
736 if (cp->glx_context) {
737 cp->glx_context = NULL;
740 free((void *)cube21);
746 void init_cube21(ModeInfo *mi) {
749 cube21 = (cube21_conf *)calloc(MI_NUM_SCREENS(mi), sizeof(cube21_conf));
753 cp = &cube21[MI_SCREEN(mi)];
754 if ((cp->glx_context = init_GL(mi)) != NULL) {
757 reshape_cube21(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
763 void draw_cube21(ModeInfo * mi) {
764 Display *display = MI_DISPLAY(mi);
765 Window window = MI_WINDOW(mi);
768 cp = &cube21[MI_SCREEN(mi)];
769 MI_IS_DRAWN(mi) = True;
770 if (!cp->glx_context) return;
771 glXMakeCurrent(display, window, *(cp->glx_context));
772 if (!draw_main(cp)) {
776 if (MI_IS_FPS(mi)) do_fps (mi);
778 glXSwapBuffers(display, window);
781 void change_cube21(ModeInfo * mi) {
782 cube21_conf *cp = &cube21[MI_SCREEN(mi)];
783 if (!cp->glx_context) return;
784 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cp->glx_context));