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"
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 #include "xlockmore.h"
51 #include "gltrackball.h"
55 #define DEF_SPIN "True"
56 #define DEF_WANDER "True"
57 #define DEF_TEXTURE "True"
58 #define DEF_RANDOMIZE "True"
59 #define DEF_SPINSPEED "1.0"
60 #define DEF_ROTSPEED "3.0"
61 #define DEF_WANDERSPEED "0.02"
62 #define DEF_WAIT "40.0"
63 #define DEF_CUBESIZE "0.7"
64 #define DEF_COLORMODE "six"
73 #define COS15 0.9659258263
74 #define SIN15 0.2588190451
75 #define COS30 0.8660254038
76 #define SIN30 0.5000000000
79 #define TEX_HEIGHT 128
80 #define TEX_GRAY 0.7, 0.7
85 #define countof(x) (sizeof((x))/sizeof((*x)))
87 #define rnd01() (random()%2)
88 #define rndcolor() (frand(0.5)+0.3)
90 /*************************************************************************/
92 static Bool spin, wander, rndstart, tex;
93 static float spinspeed, tspeed, wspeed, twait, size;
94 static char *colmode_s;
97 static argtype vars[] = {
98 { &spin, "spin", "Spin", DEF_SPIN, t_Bool},
99 { &wander, "wander", "Wander", DEF_WANDER, t_Bool},
100 { &rndstart, "randomize", "Randomize", DEF_RANDOMIZE, t_Bool},
101 { &tex, "texture", "Texture", DEF_TEXTURE, t_Bool},
102 { &spinspeed, "spinspeed", "SpinSpeed", DEF_SPINSPEED, t_Float},
103 { &tspeed, "rotspeed", "RotSpeed", DEF_ROTSPEED, t_Float},
104 { &wspeed, "wanderspeed", "WanderSpeed", DEF_WANDERSPEED, t_Float},
105 { &twait, "wait", "Wait", DEF_WAIT, t_Float},
106 { &size, "cubesize", "CubeSize", DEF_CUBESIZE, t_Float},
107 { &colmode_s, "colormode", "ColorMode", DEF_COLORMODE, t_String}
110 static XrmOptionDescRec opts[] = {
111 { "-spin", ".spin", XrmoptionNoArg, "True" },
112 { "+spin", ".spin", XrmoptionNoArg, "False" },
113 { "-wander", ".wander", XrmoptionNoArg, "True" },
114 { "+wander", ".wander", XrmoptionNoArg, "False" },
115 { "-randomize", ".randomize", XrmoptionNoArg, "True" },
116 { "+randomize", ".randomize", XrmoptionNoArg, "False" },
117 { "-texture", ".texture", XrmoptionNoArg, "True" },
118 { "+texture", ".texture", XrmoptionNoArg, "False" },
119 { "-spinspeed", ".spinspeed", XrmoptionSepArg, 0 },
120 { "-wanderspeed", ".wanderspeed", XrmoptionSepArg, 0 },
121 { "-rotspeed", ".rotspeed", XrmoptionSepArg, 0 },
122 { "-wait", ".wait", XrmoptionSepArg, 0 },
123 { "-cubesize", ".cubesize", XrmoptionSepArg, 0 },
124 { "-colormode", ".colormode", XrmoptionSepArg, 0 }
127 ENTRYPOINT ModeSpecOpt cube21_opts = {countof(opts), opts, countof(vars), vars, NULL};
130 ModStruct cube21_description =
131 { "cube21", "init_cube21", "draw_cube21", "release_cube21",
132 "draw_cube21", "change_cube21", NULL, &cube21_opts,
133 25000, 1, 1, 1, 1.0, 4, "",
134 "Shows randomly shuffling Cube 21", 0, NULL
140 CUBE21_PAUSE1 = CUBE21_STATE_BASIC,
142 CUBE21_ROT_TOP = CUBE21_ROT_BASE,
146 CUBE21_HALF1 = CUBE21_HALF_BASE,
155 CUBE21_COLOR_CLASSIC,
159 typedef int pieces_t[2][13];
160 typedef int cind_t[5][12];
161 typedef GLfloat col_t[6][3];
164 GLXContext *glx_context;
166 cube21_state state; /* type of "rotation" - shuffling */
167 GLfloat xrot, yrot; /* "spin" - object rotation around axis */
168 GLfloat posarg; /* position argument (for sine function) */
169 GLfloat t, tmax; /* rotation clock */
170 int hf[2], fr[2]; /* half flipped / face rotated flags */
171 int rface, ramount; /* selected face and amount of rotation in multiplies of 30deg */
172 int pieces[2][13]; /* locations of "narrow" and "wide" pieces */
173 int cind[5][12]; /* color indices */
174 GLfloat colors[6][3]; /* color map */
177 unsigned char texture[TEX_HEIGHT][TEX_WIDTH];
179 GLfloat texp, texq, posc[6];
180 GLfloat color_inner[4];
183 trackball_state *trackball;
187 static cube21_conf *cube21 = NULL;
189 static const GLfloat shininess = 20.0;
190 static const GLfloat ambient[] = {0.0, 0.0, 0.0, 1.0};
191 static const GLfloat diffuse[] = {1.0, 1.0, 1.0, 1.0};
192 static const GLfloat position0[] = {1.0, 1.0, 1.0, 0.0};
193 static const GLfloat position1[] = {-1.0, -1.0, 1.0, 0.0};
194 static const GLfloat lmodel_ambient[] = {0.1, 0.1, 0.1, 1.0};
195 static const GLfloat material_ambient[] = {0.7, 0.7, 0.7, 1.0};
196 static const GLfloat material_diffuse[] = {0.7, 0.7, 0.7, 1.0};
197 static const GLfloat material_specular[] = {0.2, 0.2, 0.2, 1.0};
198 static const GLfloat zpos = -18.0;
200 /*************************************************************************/
202 static void find_matches(pieces_t pieces, int matches[12], int s)
205 for(i = 1; i<6; i++) {
206 if(pieces[s][i] && pieces[s][i+6]) {
211 for(i = 1; i<matches[0]; i++) {
212 matches[j++] = matches[i]-6;
218 static void rot_face(pieces_t pieces, cind_t colors, int s, int o)
221 int tmp[12], tmpc[2][12];
222 int c0 = 2*s, c1 = c0+1;
223 for(i = 0; i<12; i++) {
224 tmp[i] = pieces[s][i];
225 tmpc[0][i] = colors[c0][i];
226 tmpc[1][i] = colors[c1][i];
229 for(i = 0; i<12; i++, o++) {
231 pieces[s][i] = tmp[o];
232 colors[c0][i] = tmpc[0][o];
233 colors[c1][i] = tmpc[1][o];
237 static void rot_halves(pieces_t pieces, cind_t colors, int hf[2], int s)
239 int ss = 6*s, i, j, k, t;
240 for(i = 0; i<6; i++) {
241 j = ss+i; k = ss+6-i;
243 pieces[0][j] = pieces[1][k];
247 colors[0][j] = colors[2][k];
250 colors[1][j] = colors[3][k];
256 static void randomize(cube21_conf *cp)
260 for(i = 0; i<SHUFFLE; i++) {
262 find_matches(cp->pieces, matches, s);
266 rot_face(cp->pieces, cp->cind, s, j);
268 rot_halves(cp->pieces, cp->cind, cp->hf, s);
272 static void finish(cube21_conf *cp)
279 find_matches(cp->pieces, matches, s);
283 if(j==6 && rnd01()) j = -6;
284 cp->state = CUBE21_ROT_BASE+s;
285 cp->tmax = 30.0*abs(j);
286 cp->fr[0] = cp->fr[1] = 0;
291 case CUBE21_ROT_BOTTOM:
292 rot_face(cp->pieces, cp->cind, s = cp->rface, cp->ramount);
295 if(!cp->fr[s] && rnd01()) {
296 find_matches(cp->pieces, matches, s);
300 if(j==6 && rnd01()) j = -6;
301 cp->state = CUBE21_ROT_BASE+s;
302 cp->tmax = 30.0*abs(j);
307 cp->state = CUBE21_PAUSE2;
313 cp->ramount = -rnd01(); /* 0 or -1, only sign is significant in this case */
314 cp->state = CUBE21_HALF_BASE+s;
320 rot_halves(cp->pieces, cp->cind, cp->hf, cp->rface);
321 cp->state = CUBE21_PAUSE1;
328 static void draw_narrow_piece(ModeInfo *mi, cube21_conf *cp, GLfloat s, int c1, int c2, col_t colors)
330 GLfloat s1 = cp->posc[0]*s;
331 glBegin(GL_TRIANGLES);
332 glNormal3f(0.0, 0.0, s);
333 if(cp->cmat) glColor3fv(colors[c1]);
334 glTexCoord2f(0.5, 0.5); glVertex3f(0.0, 0.0, s);
335 glTexCoord2f(cp->texq, 0.0); glVertex3f(cp->posc[1], 0.0, s);
336 glTexCoord2f(cp->texp, 0.0); glVertex3f(cp->posc[2], cp->posc[3], s);
338 glNormal3f(0.0, 0.0, -s);
339 if(cp->cmat) glColor3fv(cp->color_inner);
340 glTexCoord2f(TEX_GRAY);
341 glVertex3f(0.0, 0.0, s1);
342 glVertex3f(cp->posc[1], 0.0, s1);
343 glVertex3f(cp->posc[2], cp->posc[3], s1);
347 glNormal3f(0.0, -1.0, 0.0);
348 if(cp->cmat) glColor3fv(cp->color_inner);
349 glTexCoord2f(TEX_GRAY);
350 glVertex3f(0.0, 0.0, s);
351 glVertex3f(cp->posc[1], 0.0, s);
352 glVertex3f(cp->posc[1], 0.0, s1);
353 glVertex3f(0.0, 0.0, s1);
355 glNormal3f(COS15, SIN15, 0.0);
356 if(cp->cmat) glColor3fv(colors[c2]);
357 glTexCoord2f(cp->texq, cp->texq); glVertex3f(cp->posc[1], 0.0, s);
358 glTexCoord2f(cp->texq, cp->texp); glVertex3f(cp->posc[2], cp->posc[3], s);
359 glTexCoord2f(1.0, cp->texp); glVertex3f(cp->posc[2], cp->posc[3], s1);
360 glTexCoord2f(1.0, cp->texq); glVertex3f(cp->posc[1], 0.0, s1);
362 glNormal3f(-SIN30, COS30, 0.0);
363 if(cp->cmat) glColor3fv(cp->color_inner);
364 glTexCoord2f(TEX_GRAY);
365 glVertex3f(0.0, 0.0, s);
366 glVertex3f(cp->posc[2], cp->posc[3], s);
367 glVertex3f(cp->posc[2], cp->posc[3], s1);
368 glVertex3f(0.0, 0.0, s1);
371 glRotatef(30.0, 0.0, 0.0, 1.0);
374 static void draw_wide_piece(ModeInfo *mi, cube21_conf *cp, GLfloat s, int c1, int c2, int c3, col_t colors)
376 GLfloat s1 = cp->posc[0]*s;
377 glBegin(GL_TRIANGLES);
378 glNormal3f(0.0, 0.0, s);
379 if(cp->cmat) glColor3fv(colors[c1]);
380 glTexCoord2f(0.5, 0.5); glVertex3f(0.0, 0.0, s);
381 glTexCoord2f(cp->texp, 0.0); glVertex3f(cp->posc[1], 0.0, s);
382 glTexCoord2f(0.0, 0.0); glVertex3f(cp->posc[4], cp->posc[5], s);
383 glTexCoord2f(0.0, 0.0); glVertex3f(cp->posc[4], cp->posc[5], s);
384 glTexCoord2f(0.0, cp->texp); glVertex3f(cp->posc[3], cp->posc[2], s);
385 glTexCoord2f(0.5, 0.5); glVertex3f(0.0, 0.0, s);
386 glNormal3f(0.0, 0.0, -s);
387 if(cp->cmat) glColor3fv(cp->color_inner);
388 glTexCoord2f(TEX_GRAY);
389 glVertex3f(0.0, 0.0, s1);
390 glVertex3f(cp->posc[1], 0.0, s1);
391 glVertex3f(cp->posc[4], cp->posc[5], s1);
392 glVertex3f(cp->posc[4], cp->posc[5], s1);
393 glVertex3f(cp->posc[3], cp->posc[2], s1);
394 glVertex3f(0.0, 0.0, s1);
397 glNormal3f(0.0, -1.0, 0);
398 if(cp->cmat) glColor3fv(cp->color_inner);
399 glTexCoord2f(TEX_GRAY);
400 glVertex3f(0.0, 0.0, s);
401 glVertex3f(cp->posc[1], 0.0, s);
402 glVertex3f(cp->posc[1], 0.0, s1);
403 glVertex3f(0.0, 0.0, s1);
404 glNormal3f(COS15, -SIN15, 0.0);
405 if(cp->cmat) glColor3fv(colors[c2]);
406 glTexCoord2f(cp->texq, cp->texp); glVertex3f(cp->posc[1], 0.0, s);
407 glTexCoord2f(cp->texq, 0.0); glVertex3f(cp->posc[4], cp->posc[5], s);
408 glTexCoord2f(1.0, 0.0); glVertex3f(cp->posc[4], cp->posc[5], s1);
409 glTexCoord2f(1.0, cp->texp); glVertex3f(cp->posc[1], 0.0, s1);
410 glNormal3f(SIN15, COS15, 0.0);
411 if(cp->cmat) glColor3fv(colors[c3]);
412 glTexCoord2f(cp->texq, cp->texp); glVertex3f(cp->posc[4], cp->posc[5], s);
413 glTexCoord2f(cp->texq, 0.0); glVertex3f(cp->posc[3], cp->posc[2], s);
414 glTexCoord2f(1.0, 0.0); glVertex3f(cp->posc[3], cp->posc[2], s1);
415 glTexCoord2f(1.0, cp->texp); glVertex3f(cp->posc[4], cp->posc[5], s1);
416 glNormal3f(-COS30, SIN30, 0.0);
417 if(cp->cmat) glColor3fv(cp->color_inner);
418 glTexCoord2f(TEX_GRAY);
419 glVertex3f(0.0, 0.0, s);
420 glVertex3f(cp->posc[3], cp->posc[2], s);
421 glVertex3f(cp->posc[3], cp->posc[2], s1);
422 glVertex3f(0.0, 0.0, s1);
424 glRotatef(60.0, 0.0, 0.0, 1.0);
427 static void draw_middle_piece(cube21_conf *cp, int s, cind_t cind, col_t colors)
431 if(cp->cmat) glColor3fv(cp->color_inner);
432 glNormal3f(0.0, 0.0, 1.0);
433 glTexCoord2f(TEX_GRAY);
434 glVertex3f(cp->posc[1], 0.0, cp->posc[0]);
435 glVertex3f(cp->posc[4], cp->posc[5], cp->posc[0]);
436 glVertex3f(-cp->posc[5], cp->posc[4], cp->posc[0]);
437 glVertex3f(-cp->posc[1], 0.0, cp->posc[0]);
438 glNormal3f(0.0, 0.0, -1.0);
439 glTexCoord2f(TEX_GRAY);
440 glVertex3f(cp->posc[1], 0.0, -cp->posc[0]);
441 glVertex3f(cp->posc[4], cp->posc[5], -cp->posc[0]);
442 glVertex3f(-cp->posc[5], cp->posc[4], -cp->posc[0]);
443 glVertex3f(-cp->posc[1], 0.0, -cp->posc[0]);
444 glNormal3f(0.0, -1.0, 0.0);
445 glTexCoord2f(TEX_GRAY);
446 glVertex3f(-cp->posc[1], 0.0, cp->posc[0]);
447 glVertex3f(cp->posc[1], 0.0, cp->posc[0]);
448 glVertex3f(cp->posc[1], 0.0, -cp->posc[0]);
449 glVertex3f(-cp->posc[1], 0.0, -cp->posc[0]);
450 glNormal3f(COS15, -SIN15, 0.0);
451 if(cp->cmat) glColor3fv(colors[cind[4][s]]);
452 glTexCoord2f(cp->texq, cp->texp); glVertex3f(cp->posc[1], 0.0, cp->posc[0]);
453 glTexCoord2f(1.0, cp->texp); glVertex3f(cp->posc[4], cp->posc[5], cp->posc[0]);
454 glTexCoord2f(1.0, cp->texq); glVertex3f(cp->posc[4], cp->posc[5], -cp->posc[0]);
455 glTexCoord2f(cp->texq, cp->texq); glVertex3f(cp->posc[1], 0.0, -cp->posc[0]);
456 glNormal3f(SIN15, COS15, 0.0);
457 if(cp->cmat) glColor3fv(colors[cind[4][s+1]]);
458 glTexCoord2f(0.0, 0.5); glVertex3f(cp->posc[4], cp->posc[5], cp->posc[0]);
459 glTexCoord2f(cp->texq, 0.5); glVertex3f(-cp->posc[5], cp->posc[4], cp->posc[0]);
460 glTexCoord2f(cp->texq, 0.75); glVertex3f(-cp->posc[5], cp->posc[4], -cp->posc[0]);
461 glTexCoord2f(0.0, 0.75); glVertex3f(cp->posc[4], cp->posc[5], -cp->posc[0]);
462 glNormal3f(-COS15, SIN15, 0.0);
463 if(cp->cmat) glColor3fv(colors[cind[4][s+4]]);
464 glTexCoord2f(0.0, 0.75); glVertex3f(-cp->posc[5], cp->posc[4], cp->posc[0]);
465 glTexCoord2f(1.0, 0.75); glVertex3f(-cp->posc[1], 0.0, cp->posc[0]);
466 glTexCoord2f(1.0, 1.0); glVertex3f(-cp->posc[1], 0.0, -cp->posc[0]);
467 glTexCoord2f(0.0, 1.0); glVertex3f(-cp->posc[5], cp->posc[4], -cp->posc[0]);
471 static void draw_middle(cube21_conf *cp)
473 if(cp->hf[0]) glRotatef(180.0, 0.0, 1.0, 0.0);
474 draw_middle_piece(cp, 0, cp->cind, cp->colors);
475 if(cp->hf[0]) glRotatef(180.0, 0.0, 1.0, 0.0);
476 glRotatef(180.0, 0.0, 0.0, 1.0);
477 if(cp->hf[1]) glRotatef(180.0, 0.0, 1.0, 0.0);
478 draw_middle_piece(cp, 1, cp->cind, cp->colors);
479 if(cp->hf[1]) glRotatef(180.0, 0.0, 1.0, 0.0);
482 static void draw_half_face(ModeInfo *mi, cube21_conf *cp, int s, int o)
484 int i, s1 = 1-s*2, s2 = s*2;
485 for(i = o; i<o+6; i++) {
486 if(cp->pieces[s][i+1])
487 draw_narrow_piece(mi, cp, s1, cp->cind[s2][i], cp->cind[s2+1][i], cp->colors);
489 draw_wide_piece(mi, cp, s1, cp->cind[s2][i], cp->cind[s2+1][i], cp->cind[s2+1][i+1], cp->colors);
495 static void draw_top_face(ModeInfo *mi, cube21_conf *cp)
497 draw_half_face(mi, cp, 0, 0);
498 draw_half_face(mi, cp, 0, 6);
501 static void draw_bottom_face(ModeInfo *mi, cube21_conf *cp)
503 draw_half_face(mi, cp, 1, 0);
504 draw_half_face(mi, cp, 1, 6);
507 static Bool draw_main(ModeInfo *mi, cube21_conf *cp)
509 GLfloat theta = cp->ramount<0?cp->t:-cp->t;
510 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
513 glTranslatef(3.0*cp->ratio*sin(13.0*cp->posarg), 3.0*sin(17.0*cp->posarg), zpos);
515 glTranslatef(0, 0, zpos);
516 glScalef(size, size, size);
518 /* Do it twice because we don't track the device's orientation. */
519 glRotatef( current_device_rotation(), 0, 0, 1);
520 gltrackball_rotate (cp->trackball);
521 glRotatef(-current_device_rotation(), 0, 0, 1);
523 glRotatef(cp->xrot, 1.0, 0.0, 0.0);
524 glRotatef(cp->yrot, 0.0, 1.0, 0.0);
525 if(cp->wire) glColor3f(0.7, 0.7, 0.7);
529 draw_top_face(mi, cp);
530 draw_bottom_face(mi, cp);
534 glRotatef(theta, 0.0, 0.0, 1.0);
535 draw_top_face(mi, cp);
536 glRotatef(-theta, 0.0, 0.0, 1.0);
537 draw_bottom_face(mi, cp);
540 case CUBE21_ROT_BOTTOM:
541 draw_top_face(mi, cp);
542 glRotatef(theta, 0.0, 0.0, 1.0);
543 draw_bottom_face(mi, cp);
544 glRotatef(-theta, 0.0, 0.0, 1.0);
548 glRotatef(theta, 0.0, 1.0, 0.0);
550 draw_half_face(mi, cp, 0, 0);
551 glRotatef(-180.0, 0.0, 0.0, 1.0);
552 draw_half_face(mi, cp, 1, 0);
553 glRotatef(-180.0, 0.0, 0.0, 1.0);
554 if(cp->hf[0]) glRotatef(180.0, 0.0, 1.0, 0.0);
555 draw_middle_piece(cp, 0, cp->cind, cp->colors);
556 if(cp->hf[0]) glRotatef(180.0, 0.0, 1.0, 0.0);
557 if(cp->state==CUBE21_HALF1)
558 glRotatef(-theta, 0.0, 1.0, 0.0);
560 glRotatef(theta, 0.0, 1.0, 0.0);
561 glRotatef(180.0, 0.0, 0.0, 1.0);
562 draw_half_face(mi, cp, 0, 6);
563 glRotatef(-180.0, 0.0, 0.0, 1.0);
564 draw_half_face(mi, cp, 1, 6);
565 glRotatef(-180.0, 0.0, 0.0, 1.0);
566 if(cp->hf[1]) glRotatef(180.0, 0.0, 1.0, 0.0);
567 draw_middle_piece(cp, 1, cp->cind, cp->colors);
571 if((cp->xrot += spinspeed)>360.0) cp->xrot -= 360.0;
572 if((cp->yrot += spinspeed)>360.0) cp->yrot -= 360.0;
575 if((cp->posarg += wspeed/1000.0)>360.0) cp->posarg -= 360.0;
576 if((cp->t += tspeed)>cp->tmax) finish(cp);
580 static void parse_colmode(void)
583 colmode = CUBE21_COLOR_WHITE;
586 if(strstr(colmode_s, "se") || strstr(colmode_s, "sil")) colmode = CUBE21_COLOR_SILVER;
587 else if(strstr(colmode_s, "ce") || strstr(colmode_s, "cla")) colmode = CUBE21_COLOR_CLASSIC;
588 else if(strstr(colmode_s, "2") || strstr(colmode_s, "two")) colmode = CUBE21_COLOR_TWORND;
589 else if(strstr(colmode_s, "6") || strstr(colmode_s, "six")) colmode = CUBE21_COLOR_SIXRND;
590 else if(strstr(colmode_s, "1") || strstr(colmode_s, "ran") || strstr(colmode_s, "rnd")) colmode = CUBE21_COLOR_RANDOM;
591 else colmode = CUBE21_COLOR_WHITE;
594 static void init_posc(cube21_conf *cp)
596 cp->texp = (1.0-tan(Pi/12.0))/2.0;
597 cp->texq = 1.0-cp->texp;
598 /* Some significant non-trivial coordinates
599 * of the object. We need them exactly at GLfloat precision
600 * for the edges to line up perfectly. */
601 cp->posc[0] = tan(Pi/12); /* 0.268 */
602 cp->posc[1] = 1.0/cos(Pi/12); /* 1.035 */
603 cp->posc[2] = cos(Pi/6)/cos(Pi/12); /* 0.897 */
604 cp->posc[3] = sin(Pi/6)/cos(Pi/12); /* 0.518 */
605 cp->posc[4] = sqrt(2)*cos(Pi/6); /* 1.225 */
606 cp->posc[5] = sqrt(2)*sin(Pi/6); /* 0.707 = 1/sqrt(2) */
609 static void draw_horz_line(cube21_conf *cp, int x1, int x2, int y)
614 for(; y<BORDER; y++) {
615 if(y0+y>=TEX_HEIGHT) break;
617 for(x=x1; x<=x2; x++)
618 if(cp->texture[y0+y][x]>w) cp->texture[y0+y][x] = w;
622 static void draw_vert_line(cube21_conf *cp, int x, int y1, int y2)
627 for(; x<BORDER; x++) {
628 if(x0+x>=TEX_WIDTH) break;
630 for(y=y1; y<=y2; y++)
631 if(cp->texture[y][x0+x]>w) cp->texture[y][x0+x] = w;
635 static void draw_slanted_horz(cube21_conf *cp, int x1, int y1, int x2, int y2)
637 int x, y, dx = x2-x1, dy = y2-y1, y0, w;
638 for(x=x1; x<=x2; x++) {
639 y0 = y1+(y2-y1)*(x-x1)/(x2-x1);
640 for(y=-1-BORDER; y<2+BORDER; y++) {
641 w = dx*(y0+y-y1)-dy*(x-x1);
642 w = w*w/(dx*dx+dy*dy);
644 if(cp->texture[y0+y][x]>w) cp->texture[y0+y][x] = w;
649 static void draw_slanted_vert(cube21_conf *cp, int x1, int y1, int x2, int y2)
651 int x, y, dx = x2-x1, dy = y2-y1, x0, w;
652 for(y=y1; y<=y2; y++) {
653 x0 = x1+(x2-x1)*(y-y1)/(y2-y1);
654 for(x=-1-BORDER; x<2+BORDER; x++) {
655 w = dy*(x0+x-x1)-dx*(y-y1);
656 w = w*w/(dy*dy+dx*dx);
658 if(cp->texture[y][x0+x]>w) cp->texture[y][x0+x] = w;
663 static void make_texture(cube21_conf *cp)
666 float grayp[2] = {TEX_GRAY};
667 for(y=0; y<TEX_HEIGHT; y++)
668 for(x=0; x<TEX_WIDTH; x++)
669 cp->texture[y][x] = 255;
670 draw_horz_line(cp, 0, TEX_WIDTH-1, 0);
671 draw_horz_line(cp, cp->texq*TEX_WIDTH, TEX_WIDTH-1, cp->texp*TEX_HEIGHT);
672 draw_horz_line(cp, cp->texq*TEX_WIDTH, TEX_WIDTH-1, cp->texq*TEX_HEIGHT);
673 draw_horz_line(cp, 0, cp->texq*TEX_WIDTH, TEX_HEIGHT/2);
674 draw_horz_line(cp, 0, TEX_WIDTH-1, TEX_HEIGHT*3/4);
675 draw_horz_line(cp, 0, TEX_WIDTH-1, TEX_HEIGHT-1);
676 draw_vert_line(cp, 0, 0, TEX_HEIGHT-1);
677 draw_vert_line(cp, cp->texq*TEX_WIDTH, 0, TEX_HEIGHT*3/4);
678 draw_vert_line(cp, TEX_WIDTH-1, 0, TEX_HEIGHT-1);
679 draw_slanted_horz(cp, 0, cp->texp*TEX_HEIGHT, TEX_WIDTH/2, TEX_HEIGHT/2);
680 draw_slanted_vert(cp, cp->texp*TEX_WIDTH, 0, TEX_WIDTH/2, TEX_HEIGHT/2);
681 draw_slanted_vert(cp, cp->texq*TEX_WIDTH, 0, TEX_WIDTH/2, TEX_HEIGHT/2);
682 x0 = grayp[0]*TEX_WIDTH;
683 y0 = grayp[1]*TEX_HEIGHT;
686 cp->texture[y0+y][x0+x] = 100;
689 /* It doesn't look good */
692 static void init_gl(ModeInfo *mi)
694 cube21_conf *cp = &cube21[MI_SCREEN(mi)];
699 cp->wire = MI_IS_WIREFRAME(mi);
700 cp->cmat = !cp->wire && (colmode != CUBE21_COLOR_WHITE);
706 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
710 cp->color_inner[0] = cp->color_inner[1] = cp->color_inner[2] = 0.4;
712 cp->color_inner[0] = cp->color_inner[1] = cp->color_inner[2] = 1.0;
715 glDrawBuffer(GL_BACK);
716 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
717 glShadeModel(GL_FLAT);
718 glDepthFunc(GL_LESS);
719 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
720 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
721 glLightfv(GL_LIGHT0, GL_POSITION, position0);
722 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
723 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
724 glLightfv(GL_LIGHT1, GL_POSITION, position1);
725 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
726 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
727 glEnable(GL_DEPTH_TEST);
730 glEnable(GL_LIGHTING);
731 glEnable(GL_NORMALIZE);
732 glEnable(GL_COLOR_MATERIAL);
733 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material_ambient);
734 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
735 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_specular);
736 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
738 glEnable(GL_TEXTURE_2D);
741 status = gluBuild2DMipmaps(GL_TEXTURE_2D, 1, TEX_WIDTH, TEX_HEIGHT,
742 GL_LUMINANCE, GL_UNSIGNED_BYTE, texture);
744 const char *s = gluErrorString(status);
745 fprintf (stderr, "%s: error mipmapping texture: %s\n", progname, (s?s:"(unknown)"));
748 check_gl_error("mipmapping");
750 glTexImage2D(GL_TEXTURE_2D, 0, 1, TEX_WIDTH, TEX_HEIGHT,
751 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, cp->texture);
753 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
754 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
755 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
756 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
758 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
760 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
764 static void init_cp(cube21_conf *cp)
767 GLfloat ce_colors[6][3] = {
775 cp->state = CUBE21_STATE_BASIC;
776 cp->xrot = -65.0; cp->yrot = 185.0;
777 cp->posarg = (wspeed?random()%360:0.0);
778 cp->t = 0.0; cp->tmax = twait;
779 cp->hf[0] = cp->hf[1] = 0;
780 cp->fr[0] = cp->fr[1] = 0;
782 cp->pieces[0][i] = cp->pieces[1][i] = (i%3==1?0:1);
784 case CUBE21_COLOR_RANDOM:
785 case CUBE21_COLOR_TWORND:
786 case CUBE21_COLOR_SIXRND:
789 cp->colors[i][j] = rndcolor();
791 case CUBE21_COLOR_SILVER:
792 cp->colors[0][0] = 1.0;
793 cp->colors[0][1] = 1.0;
794 cp->colors[0][2] = 1.0;
795 cp->colors[1][0] = rndcolor();
796 cp->colors[1][1] = rndcolor();
797 cp->colors[1][2] = rndcolor();
799 case CUBE21_COLOR_CLASSIC:
802 cp->colors[i][j] = 0.2+0.7*ce_colors[i][j];
806 case CUBE21_COLOR_SILVER:
807 case CUBE21_COLOR_TWORND:
810 if(i==0) cp->cind[i][j] = 0;
811 else if(i==2) cp->cind[i][j] = 1;
812 else cp->cind[i][j] = ((j+5)%12)>=6?1:0;
814 case CUBE21_COLOR_CLASSIC:
815 case CUBE21_COLOR_SIXRND:
818 if(i==0) cp->cind[i][j] = 4;
819 else if(i==2) cp->cind[i][j] = 5;
820 else cp->cind[i][j] = ((j+5)%12)/3;
822 case CUBE21_COLOR_RANDOM:
828 if(rndstart) randomize(cp);
831 /*************************************************************************/
833 ENTRYPOINT void reshape_cube21(ModeInfo *mi, int width, int height)
835 cube21_conf *cp = &cube21[MI_SCREEN(mi)];
836 if(!height) height = 1;
837 cp->ratio = (GLfloat)width/(GLfloat)height;
838 glViewport(0, 0, (GLint) width, (GLint) height);
839 glMatrixMode(GL_PROJECTION);
841 gluPerspective(30.0, cp->ratio, 1.0, 100.0);
842 glMatrixMode(GL_MODELVIEW);
843 glClear(GL_COLOR_BUFFER_BIT);
847 cube21_handle_event (ModeInfo *mi, XEvent *event)
849 cube21_conf *cp = &cube21[MI_SCREEN(mi)];
851 if (event->xany.type == ButtonPress &&
852 event->xbutton.button == Button1)
854 cp->button_down_p = True;
855 gltrackball_start (cp->trackball,
856 event->xbutton.x, event->xbutton.y,
857 MI_WIDTH (mi), MI_HEIGHT (mi));
860 else if (event->xany.type == ButtonRelease &&
861 event->xbutton.button == Button1)
863 cp->button_down_p = False;
866 else if (event->xany.type == ButtonPress &&
867 (event->xbutton.button == Button4 ||
868 event->xbutton.button == Button5 ||
869 event->xbutton.button == Button6 ||
870 event->xbutton.button == Button7))
872 gltrackball_mousewheel (cp->trackball, event->xbutton.button, 10,
873 !!event->xbutton.state);
876 else if (event->xany.type == MotionNotify &&
879 gltrackball_track (cp->trackball,
880 event->xmotion.x, event->xmotion.y,
881 MI_WIDTH (mi), MI_HEIGHT (mi));
889 ENTRYPOINT void release_cube21(ModeInfo *mi)
891 if (cube21 != NULL) {
893 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
894 cube21_conf *cp = &cube21[screen];
895 if (cp->glx_context) {
896 cp->glx_context = NULL;
899 free((void *)cube21);
905 ENTRYPOINT void init_cube21(ModeInfo *mi)
909 cube21 = (cube21_conf *)calloc(MI_NUM_SCREENS(mi), sizeof(cube21_conf));
912 cp = &cube21[MI_SCREEN(mi)];
914 cp->trackball = gltrackball_init ();
921 if ((cp->glx_context = init_GL(mi)) != NULL) {
924 reshape_cube21(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
930 ENTRYPOINT void draw_cube21(ModeInfo * mi)
932 Display *display = MI_DISPLAY(mi);
933 Window window = MI_WINDOW(mi);
936 cp = &cube21[MI_SCREEN(mi)];
937 MI_IS_DRAWN(mi) = True;
938 if (!cp->glx_context) return;
939 mi->polygon_count = 0;
940 glXMakeCurrent(display, window, *(cp->glx_context));
941 if (!draw_main(mi, cp)) {
945 if (MI_IS_FPS(mi)) do_fps (mi);
947 glXSwapBuffers(display, window);
951 ENTRYPOINT void change_cube21(ModeInfo * mi)
953 cube21_conf *cp = &cube21[MI_SCREEN(mi)];
954 if (!cp->glx_context) return;
955 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cp->glx_context));
958 #endif /* !STANDALONE */
961 XSCREENSAVER_MODULE ("Cube21", cube21)