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_SPIN "True"
55 #define DEF_WANDER "True"
56 #define DEF_TEXTURE "True"
57 #define DEF_RANDOMIZE "True"
58 #define DEF_SPINSPEED "1.0"
59 #define DEF_ROTSPEED "3.0"
60 #define DEF_WANDERSPEED "0.02"
61 #define DEF_WAIT "40.0"
62 #define DEF_CUBESIZE "0.7"
63 #define DEF_COLORMODE "six"
72 #define COS15 0.9659258263
73 #define SIN15 0.2588190451
74 #define COS30 0.8660254038
75 #define SIN30 0.5000000000
78 #define TEX_HEIGHT 128
79 #define TEX_GRAY 0.7, 0.7
84 #define countof(x) (sizeof((x))/sizeof((*x)))
86 #define rnd01() (random()%2)
87 #define rndcolor() (frand(0.5)+0.3)
89 /*************************************************************************/
91 static Bool spin, wander, rndstart, tex;
92 static float spinspeed, tspeed, wspeed, twait, size;
93 static char *colmode_s;
96 static argtype vars[] = {
97 { &spin, "spin", "Spin", DEF_SPIN, t_Bool},
98 { &wander, "wander", "Wander", DEF_WANDER, t_Bool},
99 { &rndstart, "randomize", "Randomize", DEF_RANDOMIZE, t_Bool},
100 { &tex, "texture", "Texture", DEF_TEXTURE, t_Bool},
101 { &spinspeed, "spinspeed", "SpinSpeed", DEF_SPINSPEED, t_Float},
102 { &tspeed, "rotspeed", "RotSpeed", DEF_ROTSPEED, t_Float},
103 { &wspeed, "wanderspeed", "WanderSpeed", DEF_WANDERSPEED, t_Float},
104 { &twait, "wait", "Wait", DEF_WAIT, t_Float},
105 { &size, "cubesize", "CubeSize", DEF_CUBESIZE, t_Float},
106 { &colmode_s, "colormode", "ColorMode", DEF_COLORMODE, t_String}
109 static XrmOptionDescRec opts[] = {
110 { "-spin", ".spin", XrmoptionNoArg, "True" },
111 { "+spin", ".spin", XrmoptionNoArg, "False" },
112 { "-wander", ".wander", XrmoptionNoArg, "True" },
113 { "+wander", ".wander", XrmoptionNoArg, "False" },
114 { "-randomize", ".randomize", XrmoptionNoArg, "True" },
115 { "+randomize", ".randomize", XrmoptionNoArg, "False" },
116 { "-texture", ".texture", XrmoptionNoArg, "True" },
117 { "+texture", ".texture", XrmoptionNoArg, "False" },
118 { "-spinspeed", ".spinspeed", XrmoptionSepArg, 0 },
119 { "-wanderspeed", ".wanderspeed", XrmoptionSepArg, 0 },
120 { "-rotspeed", ".rotspeed", XrmoptionSepArg, 0 },
121 { "-wait", ".wait", XrmoptionSepArg, 0 },
122 { "-cubesize", ".cubesize", XrmoptionSepArg, 0 },
123 { "-colormode", ".colormode", XrmoptionSepArg, 0 }
126 ENTRYPOINT ModeSpecOpt cube21_opts = {countof(opts), opts, countof(vars), vars, NULL};
129 ModStruct cube21_description =
130 { "cube21", "init_cube21", "draw_cube21", "release_cube21",
131 "draw_cube21", "change_cube21", NULL, &cube21_opts,
132 25000, 1, 1, 1, 1.0, 4, "",
133 "Shows randomly shuffling Cube 21", 0, NULL
139 CUBE21_PAUSE1 = CUBE21_STATE_BASIC,
141 CUBE21_ROT_TOP = CUBE21_ROT_BASE,
145 CUBE21_HALF1 = CUBE21_HALF_BASE,
154 CUBE21_COLOR_CLASSIC,
158 typedef int pieces_t[2][13];
159 typedef int cind_t[5][12];
160 typedef GLfloat col_t[6][3];
163 GLXContext *glx_context;
165 cube21_state state; /* type of "rotation" - shuffling */
166 GLfloat xrot, yrot; /* "spin" - object rotation around axis */
167 GLfloat posarg; /* position argument (for sine function) */
168 GLfloat t, tmax; /* rotation clock */
169 int hf[2], fr[2]; /* half flipped / face rotated flags */
170 int rface, ramount; /* selected face and amount of rotation in multiplies of 30deg */
171 int pieces[2][13]; /* locations of "narrow" and "wide" pieces */
172 int cind[5][12]; /* color indices */
173 GLfloat colors[6][3]; /* color map */
176 unsigned char texture[TEX_HEIGHT][TEX_WIDTH];
178 GLfloat texp, texq, posc[6];
179 GLfloat color_inner[4];
183 static cube21_conf *cube21 = NULL;
185 static const GLfloat shininess = 20.0;
186 static const GLfloat ambient[] = {0.0, 0.0, 0.0, 1.0};
187 static const GLfloat diffuse[] = {1.0, 1.0, 1.0, 1.0};
188 static const GLfloat position0[] = {1.0, 1.0, 1.0, 0.0};
189 static const GLfloat position1[] = {-1.0, -1.0, 1.0, 0.0};
190 static const GLfloat lmodel_ambient[] = {0.1, 0.1, 0.1, 1.0};
191 static const GLfloat material_ambient[] = {0.7, 0.7, 0.7, 1.0};
192 static const GLfloat material_diffuse[] = {0.7, 0.7, 0.7, 1.0};
193 static const GLfloat material_specular[] = {0.2, 0.2, 0.2, 1.0};
194 static const GLfloat zpos = -18.0;
196 /*************************************************************************/
198 static void find_matches(pieces_t pieces, int matches[12], int s)
201 for(i = 1; i<6; i++) {
202 if(pieces[s][i] && pieces[s][i+6]) {
207 for(i = 1; i<matches[0]; i++) {
208 matches[j++] = matches[i]-6;
214 static void rot_face(pieces_t pieces, cind_t colors, int s, int o)
217 int tmp[12], tmpc[2][12];
218 int c0 = 2*s, c1 = c0+1;
219 for(i = 0; i<12; i++) {
220 tmp[i] = pieces[s][i];
221 tmpc[0][i] = colors[c0][i];
222 tmpc[1][i] = colors[c1][i];
225 for(i = 0; i<12; i++, o++) {
227 pieces[s][i] = tmp[o];
228 colors[c0][i] = tmpc[0][o];
229 colors[c1][i] = tmpc[1][o];
233 static void rot_halves(pieces_t pieces, cind_t colors, int hf[2], int s)
235 int ss = 6*s, i, j, k, t;
236 for(i = 0; i<6; i++) {
237 j = ss+i; k = ss+6-i;
239 pieces[0][j] = pieces[1][k];
243 colors[0][j] = colors[2][k];
246 colors[1][j] = colors[3][k];
252 static void randomize(cube21_conf *cp)
256 for(i = 0; i<SHUFFLE; i++) {
258 find_matches(cp->pieces, matches, s);
262 rot_face(cp->pieces, cp->cind, s, j);
264 rot_halves(cp->pieces, cp->cind, cp->hf, s);
268 static void finish(cube21_conf *cp)
275 find_matches(cp->pieces, matches, s);
279 if(j==6 && rnd01()) j = -6;
280 cp->state = CUBE21_ROT_BASE+s;
281 cp->tmax = 30.0*abs(j);
282 cp->fr[0] = cp->fr[1] = 0;
287 case CUBE21_ROT_BOTTOM:
288 rot_face(cp->pieces, cp->cind, s = cp->rface, cp->ramount);
291 if(!cp->fr[s] && rnd01()) {
292 find_matches(cp->pieces, matches, s);
296 if(j==6 && rnd01()) j = -6;
297 cp->state = CUBE21_ROT_BASE+s;
298 cp->tmax = 30.0*abs(j);
303 cp->state = CUBE21_PAUSE2;
309 cp->ramount = -rnd01(); /* 0 or -1, only sign is significant in this case */
310 cp->state = CUBE21_HALF_BASE+s;
316 rot_halves(cp->pieces, cp->cind, cp->hf, cp->rface);
317 cp->state = CUBE21_PAUSE1;
324 static void draw_narrow_piece(ModeInfo *mi, cube21_conf *cp, GLfloat s, int c1, int c2, col_t colors)
326 GLfloat s1 = cp->posc[0]*s;
327 glBegin(GL_TRIANGLES);
328 glNormal3f(0.0, 0.0, s);
329 if(cp->cmat) glColor3fv(colors[c1]);
330 glTexCoord2f(0.5, 0.5); glVertex3f(0.0, 0.0, s);
331 glTexCoord2f(cp->texq, 0.0); glVertex3f(cp->posc[1], 0.0, s);
332 glTexCoord2f(cp->texp, 0.0); glVertex3f(cp->posc[2], cp->posc[3], s);
334 glNormal3f(0.0, 0.0, -s);
335 if(cp->cmat) glColor3fv(cp->color_inner);
336 glTexCoord2f(TEX_GRAY);
337 glVertex3f(0.0, 0.0, s1);
338 glVertex3f(cp->posc[1], 0.0, s1);
339 glVertex3f(cp->posc[2], cp->posc[3], s1);
343 glNormal3f(0.0, -1.0, 0.0);
344 if(cp->cmat) glColor3fv(cp->color_inner);
345 glTexCoord2f(TEX_GRAY);
346 glVertex3f(0.0, 0.0, s);
347 glVertex3f(cp->posc[1], 0.0, s);
348 glVertex3f(cp->posc[1], 0.0, s1);
349 glVertex3f(0.0, 0.0, s1);
351 glNormal3f(COS15, SIN15, 0.0);
352 if(cp->cmat) glColor3fv(colors[c2]);
353 glTexCoord2f(cp->texq, cp->texq); glVertex3f(cp->posc[1], 0.0, s);
354 glTexCoord2f(cp->texq, cp->texp); glVertex3f(cp->posc[2], cp->posc[3], s);
355 glTexCoord2f(1.0, cp->texp); glVertex3f(cp->posc[2], cp->posc[3], s1);
356 glTexCoord2f(1.0, cp->texq); glVertex3f(cp->posc[1], 0.0, s1);
358 glNormal3f(-SIN30, COS30, 0.0);
359 if(cp->cmat) glColor3fv(cp->color_inner);
360 glTexCoord2f(TEX_GRAY);
361 glVertex3f(0.0, 0.0, s);
362 glVertex3f(cp->posc[2], cp->posc[3], s);
363 glVertex3f(cp->posc[2], cp->posc[3], s1);
364 glVertex3f(0.0, 0.0, s1);
367 glRotatef(30.0, 0.0, 0.0, 1.0);
370 static void draw_wide_piece(ModeInfo *mi, cube21_conf *cp, GLfloat s, int c1, int c2, int c3, col_t colors)
372 GLfloat s1 = cp->posc[0]*s;
373 glBegin(GL_TRIANGLES);
374 glNormal3f(0.0, 0.0, s);
375 if(cp->cmat) glColor3fv(colors[c1]);
376 glTexCoord2f(0.5, 0.5); glVertex3f(0.0, 0.0, s);
377 glTexCoord2f(cp->texp, 0.0); glVertex3f(cp->posc[1], 0.0, s);
378 glTexCoord2f(0.0, 0.0); glVertex3f(cp->posc[4], cp->posc[5], s);
379 glTexCoord2f(0.0, 0.0); glVertex3f(cp->posc[4], cp->posc[5], s);
380 glTexCoord2f(0.0, cp->texp); glVertex3f(cp->posc[3], cp->posc[2], s);
381 glTexCoord2f(0.5, 0.5); glVertex3f(0.0, 0.0, s);
382 glNormal3f(0.0, 0.0, -s);
383 if(cp->cmat) glColor3fv(cp->color_inner);
384 glTexCoord2f(TEX_GRAY);
385 glVertex3f(0.0, 0.0, s1);
386 glVertex3f(cp->posc[1], 0.0, s1);
387 glVertex3f(cp->posc[4], cp->posc[5], s1);
388 glVertex3f(cp->posc[4], cp->posc[5], s1);
389 glVertex3f(cp->posc[3], cp->posc[2], s1);
390 glVertex3f(0.0, 0.0, s1);
393 glNormal3f(0.0, -1.0, 0);
394 if(cp->cmat) glColor3fv(cp->color_inner);
395 glTexCoord2f(TEX_GRAY);
396 glVertex3f(0.0, 0.0, s);
397 glVertex3f(cp->posc[1], 0.0, s);
398 glVertex3f(cp->posc[1], 0.0, s1);
399 glVertex3f(0.0, 0.0, s1);
400 glNormal3f(COS15, -SIN15, 0.0);
401 if(cp->cmat) glColor3fv(colors[c2]);
402 glTexCoord2f(cp->texq, cp->texp); glVertex3f(cp->posc[1], 0.0, s);
403 glTexCoord2f(cp->texq, 0.0); glVertex3f(cp->posc[4], cp->posc[5], s);
404 glTexCoord2f(1.0, 0.0); glVertex3f(cp->posc[4], cp->posc[5], s1);
405 glTexCoord2f(1.0, cp->texp); glVertex3f(cp->posc[1], 0.0, s1);
406 glNormal3f(SIN15, COS15, 0.0);
407 if(cp->cmat) glColor3fv(colors[c3]);
408 glTexCoord2f(cp->texq, cp->texp); glVertex3f(cp->posc[4], cp->posc[5], s);
409 glTexCoord2f(cp->texq, 0.0); glVertex3f(cp->posc[3], cp->posc[2], s);
410 glTexCoord2f(1.0, 0.0); glVertex3f(cp->posc[3], cp->posc[2], s1);
411 glTexCoord2f(1.0, cp->texp); glVertex3f(cp->posc[4], cp->posc[5], s1);
412 glNormal3f(-COS30, SIN30, 0.0);
413 if(cp->cmat) glColor3fv(cp->color_inner);
414 glTexCoord2f(TEX_GRAY);
415 glVertex3f(0.0, 0.0, s);
416 glVertex3f(cp->posc[3], cp->posc[2], s);
417 glVertex3f(cp->posc[3], cp->posc[2], s1);
418 glVertex3f(0.0, 0.0, s1);
420 glRotatef(60.0, 0.0, 0.0, 1.0);
423 static void draw_middle_piece(cube21_conf *cp, int s, cind_t cind, col_t colors)
427 if(cp->cmat) glColor3fv(cp->color_inner);
428 glNormal3f(0.0, 0.0, 1.0);
429 glTexCoord2f(TEX_GRAY);
430 glVertex3f(cp->posc[1], 0.0, cp->posc[0]);
431 glVertex3f(cp->posc[4], cp->posc[5], cp->posc[0]);
432 glVertex3f(-cp->posc[5], cp->posc[4], cp->posc[0]);
433 glVertex3f(-cp->posc[1], 0.0, cp->posc[0]);
434 glNormal3f(0.0, 0.0, -1.0);
435 glTexCoord2f(TEX_GRAY);
436 glVertex3f(cp->posc[1], 0.0, -cp->posc[0]);
437 glVertex3f(cp->posc[4], cp->posc[5], -cp->posc[0]);
438 glVertex3f(-cp->posc[5], cp->posc[4], -cp->posc[0]);
439 glVertex3f(-cp->posc[1], 0.0, -cp->posc[0]);
440 glNormal3f(0.0, -1.0, 0.0);
441 glTexCoord2f(TEX_GRAY);
442 glVertex3f(-cp->posc[1], 0.0, cp->posc[0]);
443 glVertex3f(cp->posc[1], 0.0, cp->posc[0]);
444 glVertex3f(cp->posc[1], 0.0, -cp->posc[0]);
445 glVertex3f(-cp->posc[1], 0.0, -cp->posc[0]);
446 glNormal3f(COS15, -SIN15, 0.0);
447 if(cp->cmat) glColor3fv(colors[cind[4][s]]);
448 glTexCoord2f(cp->texq, cp->texp); glVertex3f(cp->posc[1], 0.0, cp->posc[0]);
449 glTexCoord2f(1.0, cp->texp); glVertex3f(cp->posc[4], cp->posc[5], cp->posc[0]);
450 glTexCoord2f(1.0, cp->texq); glVertex3f(cp->posc[4], cp->posc[5], -cp->posc[0]);
451 glTexCoord2f(cp->texq, cp->texq); glVertex3f(cp->posc[1], 0.0, -cp->posc[0]);
452 glNormal3f(SIN15, COS15, 0.0);
453 if(cp->cmat) glColor3fv(colors[cind[4][s+1]]);
454 glTexCoord2f(0.0, 0.5); glVertex3f(cp->posc[4], cp->posc[5], cp->posc[0]);
455 glTexCoord2f(cp->texq, 0.5); glVertex3f(-cp->posc[5], cp->posc[4], cp->posc[0]);
456 glTexCoord2f(cp->texq, 0.75); glVertex3f(-cp->posc[5], cp->posc[4], -cp->posc[0]);
457 glTexCoord2f(0.0, 0.75); glVertex3f(cp->posc[4], cp->posc[5], -cp->posc[0]);
458 glNormal3f(-COS15, SIN15, 0.0);
459 if(cp->cmat) glColor3fv(colors[cind[4][s+4]]);
460 glTexCoord2f(0.0, 0.75); glVertex3f(-cp->posc[5], cp->posc[4], cp->posc[0]);
461 glTexCoord2f(1.0, 0.75); glVertex3f(-cp->posc[1], 0.0, cp->posc[0]);
462 glTexCoord2f(1.0, 1.0); glVertex3f(-cp->posc[1], 0.0, -cp->posc[0]);
463 glTexCoord2f(0.0, 1.0); glVertex3f(-cp->posc[5], cp->posc[4], -cp->posc[0]);
467 static void draw_middle(cube21_conf *cp)
469 if(cp->hf[0]) glRotatef(180.0, 0.0, 1.0, 0.0);
470 draw_middle_piece(cp, 0, cp->cind, cp->colors);
471 if(cp->hf[0]) glRotatef(180.0, 0.0, 1.0, 0.0);
472 glRotatef(180.0, 0.0, 0.0, 1.0);
473 if(cp->hf[1]) glRotatef(180.0, 0.0, 1.0, 0.0);
474 draw_middle_piece(cp, 1, cp->cind, cp->colors);
475 if(cp->hf[1]) glRotatef(180.0, 0.0, 1.0, 0.0);
478 static void draw_half_face(ModeInfo *mi, cube21_conf *cp, int s, int o)
480 int i, s1 = 1-s*2, s2 = s*2;
481 for(i = o; i<o+6; i++) {
482 if(cp->pieces[s][i+1])
483 draw_narrow_piece(mi, cp, s1, cp->cind[s2][i], cp->cind[s2+1][i], cp->colors);
485 draw_wide_piece(mi, cp, s1, cp->cind[s2][i], cp->cind[s2+1][i], cp->cind[s2+1][i+1], cp->colors);
491 static void draw_top_face(ModeInfo *mi, cube21_conf *cp)
493 draw_half_face(mi, cp, 0, 0);
494 draw_half_face(mi, cp, 0, 6);
497 static void draw_bottom_face(ModeInfo *mi, cube21_conf *cp)
499 draw_half_face(mi, cp, 1, 0);
500 draw_half_face(mi, cp, 1, 6);
503 static Bool draw_main(ModeInfo *mi, cube21_conf *cp)
505 GLfloat theta = cp->ramount<0?cp->t:-cp->t;
506 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
509 glTranslatef(3.0*cp->ratio*sin(13.0*cp->posarg), 3.0*sin(17.0*cp->posarg), zpos);
511 glTranslatef(0, 0, zpos);
512 glScalef(size, size, size);
513 glRotatef(cp->xrot, 1.0, 0.0, 0.0);
514 glRotatef(cp->yrot, 0.0, 1.0, 0.0);
515 if(cp->wire) glColor3f(0.7, 0.7, 0.7);
519 draw_top_face(mi, cp);
520 draw_bottom_face(mi, cp);
524 glRotatef(theta, 0.0, 0.0, 1.0);
525 draw_top_face(mi, cp);
526 glRotatef(-theta, 0.0, 0.0, 1.0);
527 draw_bottom_face(mi, cp);
530 case CUBE21_ROT_BOTTOM:
531 draw_top_face(mi, cp);
532 glRotatef(theta, 0.0, 0.0, 1.0);
533 draw_bottom_face(mi, cp);
534 glRotatef(-theta, 0.0, 0.0, 1.0);
538 glRotatef(theta, 0.0, 1.0, 0.0);
540 draw_half_face(mi, cp, 0, 0);
541 glRotatef(-180.0, 0.0, 0.0, 1.0);
542 draw_half_face(mi, cp, 1, 0);
543 glRotatef(-180.0, 0.0, 0.0, 1.0);
544 if(cp->hf[0]) glRotatef(180.0, 0.0, 1.0, 0.0);
545 draw_middle_piece(cp, 0, cp->cind, cp->colors);
546 if(cp->hf[0]) glRotatef(180.0, 0.0, 1.0, 0.0);
547 if(cp->state==CUBE21_HALF1)
548 glRotatef(-theta, 0.0, 1.0, 0.0);
550 glRotatef(theta, 0.0, 1.0, 0.0);
551 glRotatef(180.0, 0.0, 0.0, 1.0);
552 draw_half_face(mi, cp, 0, 6);
553 glRotatef(-180.0, 0.0, 0.0, 1.0);
554 draw_half_face(mi, cp, 1, 6);
555 glRotatef(-180.0, 0.0, 0.0, 1.0);
556 if(cp->hf[1]) glRotatef(180.0, 0.0, 1.0, 0.0);
557 draw_middle_piece(cp, 1, cp->cind, cp->colors);
561 if((cp->xrot += spinspeed)>360.0) cp->xrot -= 360.0;
562 if((cp->yrot += spinspeed)>360.0) cp->yrot -= 360.0;
565 if((cp->posarg += wspeed/1000.0)>360.0) cp->posarg -= 360.0;
566 if((cp->t += tspeed)>cp->tmax) finish(cp);
570 static void parse_colmode(void)
573 colmode = CUBE21_COLOR_WHITE;
576 if(strstr(colmode_s, "se") || strstr(colmode_s, "sil")) colmode = CUBE21_COLOR_SILVER;
577 else if(strstr(colmode_s, "ce") || strstr(colmode_s, "cla")) colmode = CUBE21_COLOR_CLASSIC;
578 else if(strstr(colmode_s, "2") || strstr(colmode_s, "two")) colmode = CUBE21_COLOR_TWORND;
579 else if(strstr(colmode_s, "6") || strstr(colmode_s, "six")) colmode = CUBE21_COLOR_SIXRND;
580 else if(strstr(colmode_s, "1") || strstr(colmode_s, "ran") || strstr(colmode_s, "rnd")) colmode = CUBE21_COLOR_RANDOM;
581 else colmode = CUBE21_COLOR_WHITE;
584 static void init_posc(cube21_conf *cp)
586 cp->texp = (1.0-tan(Pi/12.0))/2.0;
587 cp->texq = 1.0-cp->texp;
588 /* Some significant non-trivial coordinates
589 * of the object. We need them exactly at GLfloat precision
590 * for the edges to line up perfectly. */
591 cp->posc[0] = tan(Pi/12); /* 0.268 */
592 cp->posc[1] = 1.0/cos(Pi/12); /* 1.035 */
593 cp->posc[2] = cos(Pi/6)/cos(Pi/12); /* 0.897 */
594 cp->posc[3] = sin(Pi/6)/cos(Pi/12); /* 0.518 */
595 cp->posc[4] = sqrt(2)*cos(Pi/6); /* 1.225 */
596 cp->posc[5] = sqrt(2)*sin(Pi/6); /* 0.707 = 1/sqrt(2) */
599 static void draw_horz_line(cube21_conf *cp, int x1, int x2, int y)
604 for(; y<BORDER; y++) {
605 if(y0+y>=TEX_HEIGHT) break;
607 for(x=x1; x<=x2; x++)
608 if(cp->texture[y0+y][x]>w) cp->texture[y0+y][x] = w;
612 static void draw_vert_line(cube21_conf *cp, int x, int y1, int y2)
617 for(; x<BORDER; x++) {
618 if(x0+x>=TEX_WIDTH) break;
620 for(y=y1; y<=y2; y++)
621 if(cp->texture[y][x0+x]>w) cp->texture[y][x0+x] = w;
625 static void draw_slanted_horz(cube21_conf *cp, int x1, int y1, int x2, int y2)
627 int x, y, dx = x2-x1, dy = y2-y1, y0, w;
628 for(x=x1; x<=x2; x++) {
629 y0 = y1+(y2-y1)*(x-x1)/(x2-x1);
630 for(y=-1-BORDER; y<2+BORDER; y++) {
631 w = dx*(y0+y-y1)-dy*(x-x1);
632 w = w*w/(dx*dx+dy*dy);
634 if(cp->texture[y0+y][x]>w) cp->texture[y0+y][x] = w;
639 static void draw_slanted_vert(cube21_conf *cp, int x1, int y1, int x2, int y2)
641 int x, y, dx = x2-x1, dy = y2-y1, x0, w;
642 for(y=y1; y<=y2; y++) {
643 x0 = x1+(x2-x1)*(y-y1)/(y2-y1);
644 for(x=-1-BORDER; x<2+BORDER; x++) {
645 w = dy*(x0+x-x1)-dx*(y-y1);
646 w = w*w/(dy*dy+dx*dx);
648 if(cp->texture[y][x0+x]>w) cp->texture[y][x0+x] = w;
653 static void make_texture(cube21_conf *cp)
656 float grayp[2] = {TEX_GRAY};
657 for(y=0; y<TEX_HEIGHT; y++)
658 for(x=0; x<TEX_WIDTH; x++)
659 cp->texture[y][x] = 255;
660 draw_horz_line(cp, 0, TEX_WIDTH-1, 0);
661 draw_horz_line(cp, cp->texq*TEX_WIDTH, TEX_WIDTH-1, cp->texp*TEX_HEIGHT);
662 draw_horz_line(cp, cp->texq*TEX_WIDTH, TEX_WIDTH-1, cp->texq*TEX_HEIGHT);
663 draw_horz_line(cp, 0, cp->texq*TEX_WIDTH, TEX_HEIGHT/2);
664 draw_horz_line(cp, 0, TEX_WIDTH-1, TEX_HEIGHT*3/4);
665 draw_horz_line(cp, 0, TEX_WIDTH-1, TEX_HEIGHT-1);
666 draw_vert_line(cp, 0, 0, TEX_HEIGHT-1);
667 draw_vert_line(cp, cp->texq*TEX_WIDTH, 0, TEX_HEIGHT*3/4);
668 draw_vert_line(cp, TEX_WIDTH-1, 0, TEX_HEIGHT-1);
669 draw_slanted_horz(cp, 0, cp->texp*TEX_HEIGHT, TEX_WIDTH/2, TEX_HEIGHT/2);
670 draw_slanted_vert(cp, cp->texp*TEX_WIDTH, 0, TEX_WIDTH/2, TEX_HEIGHT/2);
671 draw_slanted_vert(cp, cp->texq*TEX_WIDTH, 0, TEX_WIDTH/2, TEX_HEIGHT/2);
672 x0 = grayp[0]*TEX_WIDTH;
673 y0 = grayp[1]*TEX_HEIGHT;
676 cp->texture[y0+y][x0+x] = 100;
679 /* It doesn't look good */
682 static void init_gl(ModeInfo *mi)
684 cube21_conf *cp = &cube21[MI_SCREEN(mi)];
689 cp->wire = MI_IS_WIREFRAME(mi);
690 cp->cmat = !cp->wire && (colmode != CUBE21_COLOR_WHITE);
696 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
700 cp->color_inner[0] = cp->color_inner[1] = cp->color_inner[2] = 0.4;
702 cp->color_inner[0] = cp->color_inner[1] = cp->color_inner[2] = 1.0;
705 glClearColor(0.0, 0.0, 0.0, 1.0);
706 glDrawBuffer(GL_BACK);
707 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
708 glShadeModel(GL_FLAT);
709 glDepthFunc(GL_LESS);
710 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
711 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
712 glLightfv(GL_LIGHT0, GL_POSITION, position0);
713 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
714 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
715 glLightfv(GL_LIGHT1, GL_POSITION, position1);
716 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
717 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
718 glEnable(GL_DEPTH_TEST);
721 glEnable(GL_LIGHTING);
722 glEnable(GL_NORMALIZE);
723 glEnable(GL_COLOR_MATERIAL);
724 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material_ambient);
725 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
726 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_specular);
727 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
729 glEnable(GL_TEXTURE_2D);
732 status = gluBuild2DMipmaps(GL_TEXTURE_2D, 1, TEX_WIDTH, TEX_HEIGHT,
733 GL_LUMINANCE, GL_UNSIGNED_BYTE, texture);
735 const char *s = gluErrorString(status);
736 fprintf (stderr, "%s: error mipmapping texture: %s\n", progname, (s?s:"(unknown)"));
739 check_gl_error("mipmapping");
741 glTexImage2D(GL_TEXTURE_2D, 0, 1, TEX_WIDTH, TEX_HEIGHT,
742 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, cp->texture);
744 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
745 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
746 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
747 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
749 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
751 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
755 static void init_cp(cube21_conf *cp)
758 GLfloat ce_colors[6][3] = {
766 cp->state = CUBE21_STATE_BASIC;
767 cp->xrot = -65.0; cp->yrot = 185.0;
768 cp->posarg = (wspeed?random()%360:0.0);
769 cp->t = 0.0; cp->tmax = twait;
770 cp->hf[0] = cp->hf[1] = 0;
771 cp->fr[0] = cp->fr[1] = 0;
773 cp->pieces[0][i] = cp->pieces[1][i] = (i%3==1?0:1);
775 case CUBE21_COLOR_RANDOM:
776 case CUBE21_COLOR_TWORND:
777 case CUBE21_COLOR_SIXRND:
780 cp->colors[i][j] = rndcolor();
782 case CUBE21_COLOR_SILVER:
783 cp->colors[0][0] = 1.0;
784 cp->colors[0][1] = 1.0;
785 cp->colors[0][2] = 1.0;
786 cp->colors[1][0] = rndcolor();
787 cp->colors[1][1] = rndcolor();
788 cp->colors[1][2] = rndcolor();
790 case CUBE21_COLOR_CLASSIC:
793 cp->colors[i][j] = 0.2+0.7*ce_colors[i][j];
797 case CUBE21_COLOR_SILVER:
798 case CUBE21_COLOR_TWORND:
801 if(i==0) cp->cind[i][j] = 0;
802 else if(i==2) cp->cind[i][j] = 1;
803 else cp->cind[i][j] = ((j+5)%12)>=6?1:0;
805 case CUBE21_COLOR_CLASSIC:
806 case CUBE21_COLOR_SIXRND:
809 if(i==0) cp->cind[i][j] = 4;
810 else if(i==2) cp->cind[i][j] = 5;
811 else cp->cind[i][j] = ((j+5)%12)/3;
813 case CUBE21_COLOR_RANDOM:
819 if(rndstart) randomize(cp);
822 /*************************************************************************/
824 ENTRYPOINT void reshape_cube21(ModeInfo *mi, int width, int height)
826 cube21_conf *cp = &cube21[MI_SCREEN(mi)];
827 if(!height) height = 1;
828 cp->ratio = (GLfloat)width/(GLfloat)height;
829 glViewport(0, 0, (GLint) width, (GLint) height);
830 glMatrixMode(GL_PROJECTION);
832 gluPerspective(30.0, cp->ratio, 1.0, 100.0);
833 glMatrixMode(GL_MODELVIEW);
834 glClear(GL_COLOR_BUFFER_BIT);
837 ENTRYPOINT void release_cube21(ModeInfo *mi)
839 if (cube21 != NULL) {
841 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
842 cube21_conf *cp = &cube21[screen];
843 if (cp->glx_context) {
844 cp->glx_context = NULL;
847 free((void *)cube21);
853 ENTRYPOINT void init_cube21(ModeInfo *mi)
857 cube21 = (cube21_conf *)calloc(MI_NUM_SCREENS(mi), sizeof(cube21_conf));
860 cp = &cube21[MI_SCREEN(mi)];
867 if ((cp->glx_context = init_GL(mi)) != NULL) {
870 reshape_cube21(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
876 ENTRYPOINT void draw_cube21(ModeInfo * mi)
878 Display *display = MI_DISPLAY(mi);
879 Window window = MI_WINDOW(mi);
882 cp = &cube21[MI_SCREEN(mi)];
883 MI_IS_DRAWN(mi) = True;
884 if (!cp->glx_context) return;
885 mi->polygon_count = 0;
886 glXMakeCurrent(display, window, *(cp->glx_context));
887 if (!draw_main(mi, cp)) {
891 if (MI_IS_FPS(mi)) do_fps (mi);
893 glXSwapBuffers(display, window);
897 ENTRYPOINT void change_cube21(ModeInfo * mi)
899 cube21_conf *cp = &cube21[MI_SCREEN(mi)];
900 if (!cp->glx_context) return;
901 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cp->glx_context));
904 #endif /* !STANDALONE */
907 XSCREENSAVER_MODULE ("Cube21", cube21)