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 DEFAULTS "*delay: 20000 \n" \
45 "*showFPS: False \n" \
46 "*wireframe: False \n"
48 # define refresh_cube21 0
49 # define cube21_handle_event 0
50 #include "xlockmore.h"
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"
68 #define COS15 0.9659258263
69 #define SIN15 0.2588190451
70 #define COS30 0.8660254038
71 #define SIN30 0.5000000000
74 #define TEX_HEIGHT 128
75 #define TEX_GRAY 0.7, 0.7
80 #define countof(x) (sizeof((x))/sizeof((*x)))
82 #define rnd01() (random()%2)
83 #define rndcolor() (frand(0.5)+0.3)
85 /*************************************************************************/
87 static Bool spin, wander, rndstart, tex;
88 static float spinspeed, tspeed, wspeed, twait, size;
89 static char *colmode_s;
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}
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 }
122 ENTRYPOINT ModeSpecOpt cube21_opts = {countof(opts), opts, countof(vars), vars, NULL};
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
135 CUBE21_PAUSE1 = CUBE21_STATE_BASIC,
137 CUBE21_ROT_TOP = CUBE21_ROT_BASE,
141 CUBE21_HALF1 = CUBE21_HALF_BASE,
150 CUBE21_COLOR_CLASSIC,
154 typedef int pieces_t[2][13];
155 typedef int cind_t[5][12];
156 typedef GLfloat col_t[6][3];
159 GLXContext *glx_context;
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 */
172 unsigned char texture[TEX_HEIGHT][TEX_WIDTH];
174 GLfloat texp, texq, posc[6];
175 GLfloat color_inner[4];
179 static cube21_conf *cube21 = NULL;
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;
192 /*************************************************************************/
194 static void find_matches(pieces_t pieces, int matches[12], int s)
197 for(i = 1; i<6; i++) {
198 if(pieces[s][i] && pieces[s][i+6]) {
203 for(i = 1; i<matches[0]; i++) {
204 matches[j++] = matches[i]-6;
210 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)
231 int ss = 6*s, i, j, k, t;
232 for(i = 0; i<6; i++) {
233 j = ss+i; k = ss+6-i;
235 pieces[0][j] = pieces[1][k];
239 colors[0][j] = colors[2][k];
242 colors[1][j] = colors[3][k];
248 static void randomize(cube21_conf *cp)
252 for(i = 0; i<SHUFFLE; i++) {
254 find_matches(cp->pieces, matches, s);
258 rot_face(cp->pieces, cp->cind, s, j);
260 rot_halves(cp->pieces, cp->cind, cp->hf, s);
264 static void finish(cube21_conf *cp)
271 find_matches(cp->pieces, matches, s);
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;
283 case CUBE21_ROT_BOTTOM:
284 rot_face(cp->pieces, cp->cind, s = cp->rface, cp->ramount);
287 if(!cp->fr[s] && rnd01()) {
288 find_matches(cp->pieces, matches, s);
292 if(j==6 && rnd01()) j = -6;
293 cp->state = CUBE21_ROT_BASE+s;
294 cp->tmax = 30.0*abs(j);
299 cp->state = CUBE21_PAUSE2;
305 cp->ramount = -rnd01(); /* 0 or -1, only sign is significant in this case */
306 cp->state = CUBE21_HALF_BASE+s;
312 rot_halves(cp->pieces, cp->cind, cp->hf, cp->rface);
313 cp->state = CUBE21_PAUSE1;
320 static void draw_narrow_piece(cube21_conf *cp, GLfloat s, int c1, int c2, col_t colors)
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);
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);
358 glRotatef(30.0, 0.0, 0.0, 1.0);
361 static void draw_wide_piece(cube21_conf *cp, GLfloat s, int c1, int c2, int c3, col_t colors)
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);
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);
411 glRotatef(60.0, 0.0, 0.0, 1.0);
414 static void draw_middle_piece(cube21_conf *cp, int s, cind_t cind, col_t colors)
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]);
458 static void draw_middle(cube21_conf *cp)
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);
469 static void draw_half_face(cube21_conf *cp, int s, int o)
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);
476 draw_wide_piece(cp, s1, cp->cind[s2][i], cp->cind[s2+1][i], cp->cind[s2+1][i+1], cp->colors);
482 static void draw_top_face(cube21_conf *cp)
484 draw_half_face(cp, 0, 0);
485 draw_half_face(cp, 0, 6);
488 static void draw_bottom_face(cube21_conf *cp)
490 draw_half_face(cp, 1, 0);
491 draw_half_face(cp, 1, 6);
494 static Bool draw_main(cube21_conf *cp)
496 GLfloat theta = cp->ramount<0?cp->t:-cp->t;
497 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
500 glTranslatef(3.0*cp->ratio*sin(13.0*cp->posarg), 3.0*sin(17.0*cp->posarg), zpos);
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);
511 draw_bottom_face(cp);
515 glRotatef(theta, 0.0, 0.0, 1.0);
517 glRotatef(-theta, 0.0, 0.0, 1.0);
518 draw_bottom_face(cp);
521 case CUBE21_ROT_BOTTOM:
523 glRotatef(theta, 0.0, 0.0, 1.0);
524 draw_bottom_face(cp);
525 glRotatef(-theta, 0.0, 0.0, 1.0);
529 glRotatef(theta, 0.0, 1.0, 0.0);
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);
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);
552 if((cp->xrot += spinspeed)>360.0) cp->xrot -= 360.0;
553 if((cp->yrot += spinspeed)>360.0) cp->yrot -= 360.0;
556 if((cp->posarg += wspeed/1000.0)>360.0) cp->posarg -= 360.0;
557 if((cp->t += tspeed)>cp->tmax) finish(cp);
561 static void parse_colmode(void)
564 colmode = CUBE21_COLOR_WHITE;
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;
575 static void init_posc(cube21_conf *cp)
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) */
590 static void draw_horz_line(cube21_conf *cp, int x1, int x2, int y)
595 for(; y<BORDER; y++) {
596 if(y0+y>=TEX_HEIGHT) break;
598 for(x=x1; x<=x2; x++)
599 if(cp->texture[y0+y][x]>w) cp->texture[y0+y][x] = w;
603 static void draw_vert_line(cube21_conf *cp, int x, int y1, int y2)
608 for(; x<BORDER; x++) {
609 if(x0+x>=TEX_WIDTH) break;
611 for(y=y1; y<=y2; y++)
612 if(cp->texture[y][x0+x]>w) cp->texture[y][x0+x] = w;
616 static void draw_slanted_horz(cube21_conf *cp, int x1, int y1, int x2, int y2)
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);
625 if(cp->texture[y0+y][x]>w) cp->texture[y0+y][x] = w;
630 static void draw_slanted_vert(cube21_conf *cp, int x1, int y1, int x2, int y2)
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);
639 if(cp->texture[y][x0+x]>w) cp->texture[y][x0+x] = w;
644 static void make_texture(cube21_conf *cp)
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;
667 cp->texture[y0+y][x0+x] = 100;
670 /* It doesn't look good */
673 static void init_gl(ModeInfo *mi)
675 cube21_conf *cp = &cube21[MI_SCREEN(mi)];
680 cp->wire = MI_IS_WIREFRAME(mi);
681 cp->cmat = !cp->wire && (colmode != CUBE21_COLOR_WHITE);
687 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
691 cp->color_inner[0] = cp->color_inner[1] = cp->color_inner[2] = 0.4;
693 cp->color_inner[0] = cp->color_inner[1] = cp->color_inner[2] = 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);
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);
720 glEnable(GL_TEXTURE_2D);
723 status = gluBuild2DMipmaps(GL_TEXTURE_2D, 1, TEX_WIDTH, TEX_HEIGHT,
724 GL_LUMINANCE, GL_UNSIGNED_BYTE, texture);
726 const char *s = gluErrorString(status);
727 fprintf (stderr, "%s: error mipmapping texture: %s\n", progname, (s?s:"(unknown)"));
730 check_gl_error("mipmapping");
732 glTexImage2D(GL_TEXTURE_2D, 0, 1, TEX_WIDTH, TEX_HEIGHT,
733 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, cp->texture);
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);
740 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
742 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
746 static void init_cp(cube21_conf *cp)
749 GLfloat ce_colors[6][3] = {
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;
764 cp->pieces[0][i] = cp->pieces[1][i] = (i%3==1?0:1);
766 case CUBE21_COLOR_RANDOM:
767 case CUBE21_COLOR_TWORND:
768 case CUBE21_COLOR_SIXRND:
771 cp->colors[i][j] = rndcolor();
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();
781 case CUBE21_COLOR_CLASSIC:
784 cp->colors[i][j] = 0.2+0.7*ce_colors[i][j];
788 case CUBE21_COLOR_SILVER:
789 case CUBE21_COLOR_TWORND:
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;
796 case CUBE21_COLOR_CLASSIC:
797 case CUBE21_COLOR_SIXRND:
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;
804 case CUBE21_COLOR_RANDOM:
810 if(rndstart) randomize(cp);
813 /*************************************************************************/
815 ENTRYPOINT void reshape_cube21(ModeInfo *mi, int width, int height)
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);
823 gluPerspective(30.0, cp->ratio, 1.0, 100.0);
824 glMatrixMode(GL_MODELVIEW);
825 glClear(GL_COLOR_BUFFER_BIT);
828 ENTRYPOINT void release_cube21(ModeInfo *mi)
830 if (cube21 != NULL) {
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;
838 free((void *)cube21);
844 ENTRYPOINT void init_cube21(ModeInfo *mi)
848 cube21 = (cube21_conf *)calloc(MI_NUM_SCREENS(mi), sizeof(cube21_conf));
851 cp = &cube21[MI_SCREEN(mi)];
858 if ((cp->glx_context = init_GL(mi)) != NULL) {
861 reshape_cube21(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
867 ENTRYPOINT void draw_cube21(ModeInfo * mi)
869 Display *display = MI_DISPLAY(mi);
870 Window window = MI_WINDOW(mi);
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)) {
881 if (MI_IS_FPS(mi)) do_fps (mi);
883 glXSwapBuffers(display, window);
887 ENTRYPOINT void change_cube21(ModeInfo * mi)
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));
894 #endif /* !STANDALONE */
897 XSCREENSAVER_MODULE ("Cube21", cube21)