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 * some simple "solve mode"
29 * use rotator and trackball
33 * Texture mapping is only available on RGBA contexts, Mono and color index
34 * visuals DO NOT support texture mapping in OpenGL.
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
41 * In real OpenGL, PseudoColor DO NOT support texture map (as far as I know).
44 #define PROGCLASS "Cube21"
45 #define HACK_INIT init_cube21
46 #define HACK_DRAW draw_cube21
47 #define HACK_RESHAPE reshape_cube21
48 #define cube21_opts xlockmore_opts
50 #define DEF_XYSPEED "1.0"
51 #define DEF_TSPEED "3.0"
52 #define DEF_WSPEED "1.0"
53 #define DEF_TWAIT "40.0"
54 #define DEF_SIZE "0.7"
55 #define DEF_COLMODE "(none)"
57 #define DEFAULTS "*delay: 20000 \n" \
58 "*showFPS: False \n" \
59 "*wireframe: False \n"
61 #include "xlockmore.h"
74 #define COS15 0.9659258263
75 #define SIN15 0.2588190451
76 #define COS30 0.8660254038
77 #define SIN30 0.5000000000
80 #define TEX_HEIGHT 128
81 #define TEX_GRAY 0.7, 0.7
86 #define countof(x) (sizeof((x))/sizeof((*x)))
88 #define rnd01() (random()%2)
89 #define rndcolor() (frand(0.5)+0.3)
91 /*************************************************************************/
93 static Bool wander, spin, wire, tex, rndstart, cmat;
94 static float spinspeed, tspeed, wspeed, twait, size;
95 static char *colmode_s;
98 static argtype vars[] = {
99 { &spin, "spin", "Spin", "True", t_Bool},
100 { &wander, "wander", "Wander", "True", t_Bool},
101 { &rndstart, "randomize", "Randomize", "True", t_Bool},
102 { &tex, "texture", "Texture", "True", t_Bool},
103 { &spinspeed, "spinspeed", "SpinSpeed", DEF_XYSPEED, t_Float},
104 { &tspeed, "rotspeed", "RotSpeed", DEF_TSPEED, t_Float},
105 { &wspeed, "wanderspeed", "WanderSpeed", DEF_WSPEED, t_Float},
106 { &twait, "wait", "Wait", DEF_TWAIT, t_Float},
107 { &size, "cubesize", "CubeSize", DEF_SIZE, t_Float},
108 { &colmode_s, "colormode", "ColorMode", DEF_COLMODE, t_String}
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, DEF_COLMODE }
128 ModeSpecOpt cube21_opts = {countof(opts), opts, countof(vars), vars, NULL};
131 ModStruct cube21_description =
132 { "cube21", "init_cube21", "draw_cube21", "release_cube21",
133 "draw_cube21", "change_cube21", NULL, &cube21_opts,
134 25000, 1, 1, 1, 1.0, 4, "",
135 "Shows randomly shuffling Cube 21", 0, NULL
141 CUBE21_PAUSE1 = CUBE21_STATE_BASIC,
143 CUBE21_ROT_TOP = CUBE21_ROT_BASE,
147 CUBE21_HALF1 = CUBE21_HALF_BASE,
156 CUBE21_COLOR_CLASSIC,
160 typedef int pieces_t[2][13];
161 typedef int cind_t[5][12];
162 typedef GLfloat col_t[6][3];
165 GLXContext *glx_context;
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 */
178 static cube21_conf *cube21 = NULL;
179 static unsigned char texture[TEX_HEIGHT][TEX_WIDTH];
181 static GLfloat shininess = 20.0;
182 static GLfloat ambient[] = {0.0, 0.0, 0.0, 1.0};
183 static GLfloat diffuse[] = {1.0, 1.0, 1.0, 1.0};
184 static GLfloat position0[] = {1.0, 1.0, 1.0, 0.0};
185 static GLfloat position1[] = {-1.0, -1.0, 1.0, 0.0};
186 static GLfloat lmodel_ambient[] = {0.1, 0.1, 0.1, 1.0};
187 static GLfloat material_ambient[] = {0.7, 0.7, 0.7, 1.0};
188 static GLfloat material_diffuse[] = {0.7, 0.7, 0.7, 1.0};
189 static GLfloat material_specular[] = {0.2, 0.2, 0.2, 1.0};
190 static GLfloat color_inner[] = {1.0, 1.0, 1.0};
191 static GLfloat texp = 0.0, texq, posc[6];
192 static GLfloat zpos = -18.0;
194 /*************************************************************************/
196 static void find_matches(pieces_t pieces, int matches[12], int s) {
198 for(i = 1; i<6; i++) {
199 if(pieces[s][i] && pieces[s][i+6]) {
204 for(i = 1; i<matches[0]; i++) {
205 matches[j++] = matches[i]-6;
211 static void rot_face(pieces_t pieces, cind_t colors, int s, int o) {
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];
221 for(i = 0; i<12; i++, o++) {
223 pieces[s][i] = tmp[o];
224 colors[c0][i] = tmpc[0][o];
225 colors[c1][i] = tmpc[1][o];
229 static void rot_halves(pieces_t pieces, cind_t colors, int hf[2], int s) {
230 int ss = 6*s, i, j, k, t;
231 for(i = 0; i<6; i++) {
232 j = ss+i; k = ss+6-i;
234 pieces[0][j] = pieces[1][k];
238 colors[0][j] = colors[2][k];
241 colors[1][j] = colors[3][k];
247 static void randomize(cube21_conf *cp) {
250 for(i = 0; i<SHUFFLE; i++) {
252 find_matches(cp->pieces, matches, s);
256 rot_face(cp->pieces, cp->cind, s, j);
258 rot_halves(cp->pieces, cp->cind, cp->hf, s);
262 static void finish(cube21_conf *cp) {
268 find_matches(cp->pieces, matches, s);
272 if(j==6 && rnd01()) j = -6;
273 cp->state = CUBE21_ROT_BASE+s;
274 cp->tmax = 30.0*abs(j);
275 cp->fr[0] = cp->fr[1] = 0;
280 case CUBE21_ROT_BOTTOM:
281 rot_face(cp->pieces, cp->cind, s = cp->rface, cp->ramount);
284 if(!cp->fr[s] && rnd01()) {
285 find_matches(cp->pieces, matches, s);
289 if(j==6 && rnd01()) j = -6;
290 cp->state = CUBE21_ROT_BASE+s;
291 cp->tmax = 30.0*abs(j);
296 cp->state = CUBE21_PAUSE2;
302 cp->ramount = -rnd01(); /* 0 or -1, only sign is significant in this case */
303 cp->state = CUBE21_HALF_BASE+s;
309 rot_halves(cp->pieces, cp->cind, cp->hf, cp->rface);
310 cp->state = CUBE21_PAUSE1;
317 static void draw_narrow_piece(GLfloat s, int c1, int c2, col_t colors) {
318 GLfloat s1 = posc[0]*s;
319 glBegin(GL_TRIANGLES);
320 glNormal3f(0.0, 0.0, s);
321 if(cmat) glColor3fv(colors[c1]);
322 glTexCoord2f(0.5, 0.5); glVertex3f(0.0, 0.0, s);
323 glTexCoord2f(texq, 0.0); glVertex3f(posc[1], 0.0, s);
324 glTexCoord2f(texp, 0.0); glVertex3f(posc[2], posc[3], s);
325 glNormal3f(0.0, 0.0, -s);
326 if(cmat) glColor3fv(color_inner);
327 glTexCoord2f(TEX_GRAY);
328 glVertex3f(0.0, 0.0, s1);
329 glVertex3f(posc[1], 0.0, s1);
330 glVertex3f(posc[2], posc[3], s1);
333 glNormal3f(0.0, -1.0, 0.0);
334 if(cmat) glColor3fv(color_inner);
335 glTexCoord2f(TEX_GRAY);
336 glVertex3f(0.0, 0.0, s);
337 glVertex3f(posc[1], 0.0, s);
338 glVertex3f(posc[1], 0.0, s1);
339 glVertex3f(0.0, 0.0, s1);
340 glNormal3f(COS15, SIN15, 0.0);
341 if(cmat) glColor3fv(colors[c2]);
342 glTexCoord2f(texq, texq); glVertex3f(posc[1], 0.0, s);
343 glTexCoord2f(texq, texp); glVertex3f(posc[2], posc[3], s);
344 glTexCoord2f(1.0, texp); glVertex3f(posc[2], posc[3], s1);
345 glTexCoord2f(1.0, texq); glVertex3f(posc[1], 0.0, s1);
346 glNormal3f(-SIN30, COS30, 0.0);
347 if(cmat) glColor3fv(color_inner);
348 glTexCoord2f(TEX_GRAY);
349 glVertex3f(0.0, 0.0, s);
350 glVertex3f(posc[2], posc[3], s);
351 glVertex3f(posc[2], posc[3], s1);
352 glVertex3f(0.0, 0.0, s1);
354 glRotatef(30.0, 0.0, 0.0, 1.0);
357 static void draw_wide_piece(GLfloat s, int c1, int c2, int c3, col_t colors) {
358 GLfloat s1 = posc[0]*s;
359 glBegin(GL_TRIANGLES);
360 glNormal3f(0.0, 0.0, s);
361 if(cmat) glColor3fv(colors[c1]);
362 glTexCoord2f(0.5, 0.5); glVertex3f(0.0, 0.0, s);
363 glTexCoord2f(texp, 0.0); glVertex3f(posc[1], 0.0, s);
364 glTexCoord2f(0.0, 0.0); glVertex3f(posc[4], posc[5], s);
365 glTexCoord2f(0.0, 0.0); glVertex3f(posc[4], posc[5], s);
366 glTexCoord2f(0.0, texp); glVertex3f(posc[3], posc[2], s);
367 glTexCoord2f(0.5, 0.5); glVertex3f(0.0, 0.0, s);
368 glNormal3f(0.0, 0.0, -s);
369 if(cmat) glColor3fv(color_inner);
370 glTexCoord2f(TEX_GRAY);
371 glVertex3f(0.0, 0.0, s1);
372 glVertex3f(posc[1], 0.0, s1);
373 glVertex3f(posc[4], posc[5], s1);
374 glVertex3f(posc[4], posc[5], s1);
375 glVertex3f(posc[3], posc[2], s1);
376 glVertex3f(0.0, 0.0, s1);
379 glNormal3f(0.0, -1.0, 0);
380 if(cmat) glColor3fv(color_inner);
381 glTexCoord2f(TEX_GRAY);
382 glVertex3f(0.0, 0.0, s);
383 glVertex3f(posc[1], 0.0, s);
384 glVertex3f(posc[1], 0.0, s1);
385 glVertex3f(0.0, 0.0, s1);
386 glNormal3f(COS15, -SIN15, 0.0);
387 if(cmat) glColor3fv(colors[c2]);
388 glTexCoord2f(texq, texp); glVertex3f(posc[1], 0.0, s);
389 glTexCoord2f(texq, 0.0); glVertex3f(posc[4], posc[5], s);
390 glTexCoord2f(1.0, 0.0); glVertex3f(posc[4], posc[5], s1);
391 glTexCoord2f(1.0, texp); glVertex3f(posc[1], 0.0, s1);
392 glNormal3f(SIN15, COS15, 0.0);
393 if(cmat) glColor3fv(colors[c3]);
394 glTexCoord2f(texq, texp); glVertex3f(posc[4], posc[5], s);
395 glTexCoord2f(texq, 0.0); glVertex3f(posc[3], posc[2], s);
396 glTexCoord2f(1.0, 0.0); glVertex3f(posc[3], posc[2], s1);
397 glTexCoord2f(1.0, texp); glVertex3f(posc[4], posc[5], s1);
398 glNormal3f(-COS30, SIN30, 0.0);
399 if(cmat) glColor3fv(color_inner);
400 glTexCoord2f(TEX_GRAY);
401 glVertex3f(0.0, 0.0, s);
402 glVertex3f(posc[3], posc[2], s);
403 glVertex3f(posc[3], posc[2], s1);
404 glVertex3f(0.0, 0.0, s1);
406 glRotatef(60.0, 0.0, 0.0, 1.0);
409 static void draw_middle_piece(int s, cind_t cind, col_t colors) {
412 if(cmat) glColor3fv(color_inner);
413 glNormal3f(0.0, 0.0, 1.0);
414 glTexCoord2f(TEX_GRAY);
415 glVertex3f(posc[1], 0.0, posc[0]);
416 glVertex3f(posc[4], posc[5], posc[0]);
417 glVertex3f(-posc[5], posc[4], posc[0]);
418 glVertex3f(-posc[1], 0.0, posc[0]);
419 glNormal3f(0.0, 0.0, -1.0);
420 glTexCoord2f(TEX_GRAY);
421 glVertex3f(posc[1], 0.0, -posc[0]);
422 glVertex3f(posc[4], posc[5], -posc[0]);
423 glVertex3f(-posc[5], posc[4], -posc[0]);
424 glVertex3f(-posc[1], 0.0, -posc[0]);
425 glNormal3f(0.0, -1.0, 0.0);
426 glTexCoord2f(TEX_GRAY);
427 glVertex3f(-posc[1], 0.0, posc[0]);
428 glVertex3f(posc[1], 0.0, posc[0]);
429 glVertex3f(posc[1], 0.0, -posc[0]);
430 glVertex3f(-posc[1], 0.0, -posc[0]);
431 glNormal3f(COS15, -SIN15, 0.0);
432 if(cmat) glColor3fv(colors[cind[4][s]]);
433 glTexCoord2f(texq, texp); glVertex3f(posc[1], 0.0, posc[0]);
434 glTexCoord2f(1.0, texp); glVertex3f(posc[4], posc[5], posc[0]);
435 glTexCoord2f(1.0, texq); glVertex3f(posc[4], posc[5], -posc[0]);
436 glTexCoord2f(texq, texq); glVertex3f(posc[1], 0.0, -posc[0]);
437 glNormal3f(SIN15, COS15, 0.0);
438 if(cmat) glColor3fv(colors[cind[4][s+1]]);
439 glTexCoord2f(0.0, 0.5); glVertex3f(posc[4], posc[5], posc[0]);
440 glTexCoord2f(texq, 0.5); glVertex3f(-posc[5], posc[4], posc[0]);
441 glTexCoord2f(texq, 0.75); glVertex3f(-posc[5], posc[4], -posc[0]);
442 glTexCoord2f(0.0, 0.75); glVertex3f(posc[4], posc[5], -posc[0]);
443 glNormal3f(-COS15, SIN15, 0.0);
444 if(cmat) glColor3fv(colors[cind[4][s+4]]);
445 glTexCoord2f(0.0, 0.75); glVertex3f(-posc[5], posc[4], posc[0]);
446 glTexCoord2f(1.0, 0.75); glVertex3f(-posc[1], 0.0, posc[0]);
447 glTexCoord2f(1.0, 1.0); glVertex3f(-posc[1], 0.0, -posc[0]);
448 glTexCoord2f(0.0, 1.0); glVertex3f(-posc[5], posc[4], -posc[0]);
452 static void draw_middle(cube21_conf *cp) {
453 if(cp->hf[0]) glRotatef(180.0, 0.0, 1.0, 0.0);
454 draw_middle_piece(0, cp->cind, cp->colors);
455 if(cp->hf[0]) glRotatef(180.0, 0.0, 1.0, 0.0);
456 glRotatef(180.0, 0.0, 0.0, 1.0);
457 if(cp->hf[1]) glRotatef(180.0, 0.0, 1.0, 0.0);
458 draw_middle_piece(1, cp->cind, cp->colors);
459 if(cp->hf[1]) glRotatef(180.0, 0.0, 1.0, 0.0);
462 static void draw_half_face(cube21_conf *cp, int s, int o) {
463 int i, s1 = 1-s*2, s2 = s*2;
464 for(i = o; i<o+6; i++) {
465 if(cp->pieces[s][i+1])
466 draw_narrow_piece(s1, cp->cind[s2][i], cp->cind[s2+1][i], cp->colors);
468 draw_wide_piece(s1, cp->cind[s2][i], cp->cind[s2+1][i], cp->cind[s2+1][i+1], cp->colors);
474 static void draw_top_face(cube21_conf *cp) {
475 draw_half_face(cp, 0, 0);
476 draw_half_face(cp, 0, 6);
479 static void draw_bottom_face(cube21_conf *cp) {
480 draw_half_face(cp, 1, 0);
481 draw_half_face(cp, 1, 6);
484 static Bool draw_main(cube21_conf *cp) {
485 GLfloat theta = cp->ramount<0?cp->t:-cp->t;
486 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
489 glTranslatef(3.0*cp->ratio*sin(13.0*cp->posarg), 3.0*sin(17.0*cp->posarg), zpos);
491 glTranslatef(0, 0, zpos);
492 glScalef(size, size, size);
493 glRotatef(cp->xrot, 1.0, 0.0, 0.0);
494 glRotatef(cp->yrot, 0.0, 1.0, 0.0);
495 if(wire) glColor3f(0.7, 0.7, 0.7);
500 draw_bottom_face(cp);
504 glRotatef(theta, 0.0, 0.0, 1.0);
506 glRotatef(-theta, 0.0, 0.0, 1.0);
507 draw_bottom_face(cp);
510 case CUBE21_ROT_BOTTOM:
512 glRotatef(theta, 0.0, 0.0, 1.0);
513 draw_bottom_face(cp);
514 glRotatef(-theta, 0.0, 0.0, 1.0);
518 glRotatef(theta, 0.0, 1.0, 0.0);
520 draw_half_face(cp, 0, 0);
521 glRotatef(-180.0, 0.0, 0.0, 1.0);
522 draw_half_face(cp, 1, 0);
523 glRotatef(-180.0, 0.0, 0.0, 1.0);
524 if(cp->hf[0]) glRotatef(180.0, 0.0, 1.0, 0.0);
525 draw_middle_piece(0, cp->cind, cp->colors);
526 if(cp->hf[0]) glRotatef(180.0, 0.0, 1.0, 0.0);
527 if(cp->state==CUBE21_HALF1)
528 glRotatef(-theta, 0.0, 1.0, 0.0);
530 glRotatef(theta, 0.0, 1.0, 0.0);
531 glRotatef(180.0, 0.0, 0.0, 1.0);
532 draw_half_face(cp, 0, 6);
533 glRotatef(-180.0, 0.0, 0.0, 1.0);
534 draw_half_face(cp, 1, 6);
535 glRotatef(-180.0, 0.0, 0.0, 1.0);
536 if(cp->hf[1]) glRotatef(180.0, 0.0, 1.0, 0.0);
537 draw_middle_piece(1, cp->cind, cp->colors);
541 if((cp->xrot += spinspeed)>360.0) cp->xrot -= 360.0;
542 if((cp->yrot += spinspeed)>360.0) cp->yrot -= 360.0;
545 if((cp->posarg += wspeed/1000.0)>360.0) cp->posarg -= 360.0;
546 if((cp->t += tspeed)>cp->tmax) finish(cp);
550 void parse_colmode(void) {
552 colmode = CUBE21_COLOR_WHITE;
555 if(strstr(colmode_s, "se") || strstr(colmode_s, "sil")) colmode = CUBE21_COLOR_SILVER;
556 else if(strstr(colmode_s, "ce") || strstr(colmode_s, "cla")) colmode = CUBE21_COLOR_CLASSIC;
557 else if(strstr(colmode_s, "2") || strstr(colmode_s, "two")) colmode = CUBE21_COLOR_TWORND;
558 else if(strstr(colmode_s, "6") || strstr(colmode_s, "six")) colmode = CUBE21_COLOR_SIXRND;
559 else if(strstr(colmode_s, "1") || strstr(colmode_s, "ran") || strstr(colmode_s, "rnd")) colmode = CUBE21_COLOR_RANDOM;
560 else colmode = CUBE21_COLOR_WHITE;
563 static void init_posc(void) {
564 texp = (1.0-tan(Pi/12.0))/2.0;
566 /* Some significant non-trivial coordinates
567 * of the object. We need them exactly at GLfloat precision
568 * for the edges to line up perfectly. */
569 posc[0] = tan(Pi/12); /* 0.268 */
570 posc[1] = 1.0/cos(Pi/12); /* 1.035 */
571 posc[2] = cos(Pi/6)/cos(Pi/12); /* 0.897 */
572 posc[3] = sin(Pi/6)/cos(Pi/12); /* 0.518 */
573 posc[4] = sqrt(2)*cos(Pi/6); /* 1.225 */
574 posc[5] = sqrt(2)*sin(Pi/6); /* 0.707 = 1/sqrt(2) */
577 static void draw_horz_line(int x1, int x2, int y) {
581 for(; y<BORDER; y++) {
582 if(y0+y>=TEX_HEIGHT) break;
584 for(x=x1; x<=x2; x++)
585 if(texture[y0+y][x]>w) texture[y0+y][x] = w;
589 static void draw_vert_line(int x, int y1, int y2) {
593 for(; x<BORDER; x++) {
594 if(x0+x>=TEX_WIDTH) break;
596 for(y=y1; y<=y2; y++)
597 if(texture[y][x0+x]>w) texture[y][x0+x] = w;
601 static void draw_slanted_horz(int x1, int y1, int x2, int y2) {
602 int x, y, dx = x2-x1, dy = y2-y1, y0, w;
603 for(x=x1; x<=x2; x++) {
604 y0 = y1+(y2-y1)*(x-x1)/(x2-x1);
605 for(y=-1-BORDER; y<2+BORDER; y++) {
606 w = dx*(y0+y-y1)-dy*(x-x1);
607 w = w*w/(dx*dx+dy*dy);
609 if(texture[y0+y][x]>w) texture[y0+y][x] = w;
614 static void draw_slanted_vert(int x1, int y1, int x2, int y2) {
615 int x, y, dx = x2-x1, dy = y2-y1, x0, w;
616 for(y=y1; y<=y2; y++) {
617 x0 = x1+(x2-x1)*(y-y1)/(y2-y1);
618 for(x=-1-BORDER; x<2+BORDER; x++) {
619 w = dy*(x0+x-x1)-dx*(y-y1);
620 w = w*w/(dy*dy+dx*dx);
622 if(texture[y][x0+x]>w) texture[y][x0+x] = w;
627 static void make_texture(void) {
629 float grayp[2] = {TEX_GRAY};
630 for(y=0; y<TEX_HEIGHT; y++)
631 for(x=0; x<TEX_WIDTH; x++)
633 draw_horz_line(0, TEX_WIDTH-1, 0);
634 draw_horz_line(texq*TEX_WIDTH, TEX_WIDTH-1, texp*TEX_HEIGHT);
635 draw_horz_line(texq*TEX_WIDTH, TEX_WIDTH-1, texq*TEX_HEIGHT);
636 draw_horz_line(0, texq*TEX_WIDTH, TEX_HEIGHT/2);
637 draw_horz_line(0, TEX_WIDTH-1, TEX_HEIGHT*3/4);
638 draw_horz_line(0, TEX_WIDTH-1, TEX_HEIGHT-1);
639 draw_vert_line(0, 0, TEX_HEIGHT-1);
640 draw_vert_line(texq*TEX_WIDTH, 0, TEX_HEIGHT*3/4);
641 draw_vert_line(TEX_WIDTH-1, 0, TEX_HEIGHT-1);
642 draw_slanted_horz(0, texp*TEX_HEIGHT, TEX_WIDTH/2, TEX_HEIGHT/2);
643 draw_slanted_vert(texp*TEX_WIDTH, 0, TEX_WIDTH/2, TEX_HEIGHT/2);
644 draw_slanted_vert(texq*TEX_WIDTH, 0, TEX_WIDTH/2, TEX_HEIGHT/2);
645 x0 = grayp[0]*TEX_WIDTH;
646 y0 = grayp[1]*TEX_HEIGHT;
649 texture[y0+y][x0+x] = 100;
652 /* It doesn't look good */
655 static void init_gl(ModeInfo *mi) {
660 wire = MI_IS_WIREFRAME(mi);
661 cmat = !wire && (colmode != CUBE21_COLOR_WHITE);
667 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
670 if(!tex) color_inner[0] = color_inner[1] = color_inner[2] = 0.4;
672 glClearColor(0.0, 0.0, 0.0, 1.0);
673 glDrawBuffer(GL_BACK);
674 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
675 glShadeModel(GL_FLAT);
676 glDepthFunc(GL_LESS);
677 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
678 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
679 glLightfv(GL_LIGHT0, GL_POSITION, position0);
680 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
681 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
682 glLightfv(GL_LIGHT1, GL_POSITION, position1);
683 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
684 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
685 glEnable(GL_DEPTH_TEST);
688 glEnable(GL_LIGHTING);
689 glEnable(GL_NORMALIZE);
690 glEnable(GL_COLOR_MATERIAL);
691 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material_ambient);
692 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
693 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_specular);
694 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
696 glEnable(GL_TEXTURE_2D);
699 status = gluBuild2DMipmaps(GL_TEXTURE_2D, 1, TEX_WIDTH, TEX_HEIGHT,
700 GL_LUMINANCE, GL_UNSIGNED_BYTE, texture);
702 const char *s = gluErrorString(status);
703 fprintf (stderr, "%s: error mipmapping texture: %s\n", progname, (s?s:"(unknown)"));
706 check_gl_error("mipmapping");
708 glTexImage2D(GL_TEXTURE_2D, 0, 1, TEX_WIDTH, TEX_HEIGHT,
709 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, texture);
711 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
712 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
713 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
714 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
716 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
718 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
722 static void init_cp(cube21_conf *cp) {
724 GLfloat ce_colors[6][3] = {
732 cp->state = CUBE21_STATE_BASIC;
733 cp->xrot = -65.0; cp->yrot = 185.0;
734 cp->posarg = (wspeed?random()%360:0.0);
735 cp->t = 0.0; cp->tmax = twait;
736 cp->hf[0] = cp->hf[1] = 0;
737 cp->fr[0] = cp->fr[1] = 0;
739 cp->pieces[0][i] = cp->pieces[1][i] = (i%3==1?0:1);
741 case CUBE21_COLOR_RANDOM:
742 case CUBE21_COLOR_TWORND:
743 case CUBE21_COLOR_SIXRND:
746 cp->colors[i][j] = rndcolor();
748 case CUBE21_COLOR_SILVER:
749 cp->colors[0][0] = 1.0;
750 cp->colors[0][1] = 1.0;
751 cp->colors[0][2] = 1.0;
752 cp->colors[1][0] = rndcolor();
753 cp->colors[1][1] = rndcolor();
754 cp->colors[1][2] = rndcolor();
756 case CUBE21_COLOR_CLASSIC:
759 cp->colors[i][j] = 0.2+0.7*ce_colors[i][j];
763 case CUBE21_COLOR_SILVER:
764 case CUBE21_COLOR_TWORND:
767 if(i==0) cp->cind[i][j] = 0;
768 else if(i==2) cp->cind[i][j] = 1;
769 else cp->cind[i][j] = ((j+5)%12)>=6?1:0;
771 case CUBE21_COLOR_CLASSIC:
772 case CUBE21_COLOR_SIXRND:
775 if(i==0) cp->cind[i][j] = 4;
776 else if(i==2) cp->cind[i][j] = 5;
777 else cp->cind[i][j] = ((j+5)%12)/3;
779 case CUBE21_COLOR_RANDOM:
785 if(rndstart) randomize(cp);
788 /*************************************************************************/
790 void reshape_cube21(ModeInfo *mi, int width, int height) {
791 cube21_conf *cp = &cube21[MI_SCREEN(mi)];
792 if(!height) height = 1;
793 cp->ratio = (GLfloat)width/(GLfloat)height;
794 glViewport(0, 0, (GLint) width, (GLint) height);
795 glMatrixMode(GL_PROJECTION);
797 gluPerspective(30.0, cp->ratio, 1.0, 100.0);
798 glMatrixMode(GL_MODELVIEW);
799 glClear(GL_COLOR_BUFFER_BIT);
802 void release_cube21(ModeInfo *mi) {
803 if (cube21 != NULL) {
805 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
806 cube21_conf *cp = &cube21[screen];
807 if (cp->glx_context) {
808 cp->glx_context = NULL;
811 free((void *)cube21);
817 void init_cube21(ModeInfo *mi) {
820 cube21 = (cube21_conf *)calloc(MI_NUM_SCREENS(mi), sizeof(cube21_conf));
827 cp = &cube21[MI_SCREEN(mi)];
828 if ((cp->glx_context = init_GL(mi)) != NULL) {
831 reshape_cube21(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
837 void draw_cube21(ModeInfo * mi) {
838 Display *display = MI_DISPLAY(mi);
839 Window window = MI_WINDOW(mi);
842 cp = &cube21[MI_SCREEN(mi)];
843 MI_IS_DRAWN(mi) = True;
844 if (!cp->glx_context) return;
845 glXMakeCurrent(display, window, *(cp->glx_context));
846 if (!draw_main(cp)) {
850 if (MI_IS_FPS(mi)) do_fps (mi);
852 glXSwapBuffers(display, window);
855 void change_cube21(ModeInfo * mi) {
856 cube21_conf *cp = &cube21[MI_SCREEN(mi)];
857 if (!cp->glx_context) return;
858 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cp->glx_context));