85 static float lightDir[4] =
86 {-2.0, -4.0, -2.0, 1.0};
96 {0.0, 1.0, 0.0, 1.499};
98 {0.0, 0.0, 1.0, 0.899};
101 {0.0, 0.0, 0.0, 0.0};
107 0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
108 0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
109 0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
110 0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
111 0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
112 0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
113 0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
114 0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
115 0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
116 0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
117 0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
118 0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
119 0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
120 0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
121 0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
122 0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55
127 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
128 0xff, 0xbd, 0xff, 0x83, 0xff, 0x5a, 0xff, 0xef,
129 0xfe, 0xdb, 0x7f, 0xef, 0xfd, 0xdb, 0xbf, 0xef,
130 0xfb, 0xdb, 0xdf, 0xef, 0xf7, 0xdb, 0xef, 0xef,
131 0xfb, 0xdb, 0xdf, 0xef, 0xfd, 0xdb, 0xbf, 0x83,
132 0xce, 0xdb, 0x73, 0xff, 0xb7, 0x5a, 0xed, 0xff,
133 0xbb, 0xdb, 0xdd, 0xc7, 0xbd, 0xdb, 0xbd, 0xbb,
134 0xbe, 0xbd, 0x7d, 0xbb, 0xbf, 0x7e, 0xfd, 0xb3,
135 0xbe, 0xe7, 0x7d, 0xbf, 0xbd, 0xdb, 0xbd, 0xbf,
136 0xbb, 0xbd, 0xdd, 0xbb, 0xb7, 0x7e, 0xed, 0xc7,
137 0xce, 0xdb, 0x73, 0xff, 0xfd, 0xdb, 0xbf, 0xff,
138 0xfb, 0xdb, 0xdf, 0x87, 0xf7, 0xdb, 0xef, 0xfb,
139 0xf7, 0xdb, 0xef, 0xfb, 0xfb, 0xdb, 0xdf, 0xfb,
140 0xfd, 0xdb, 0xbf, 0xc7, 0xfe, 0xdb, 0x7f, 0xbf,
141 0xff, 0x5a, 0xff, 0xbf, 0xff, 0xbd, 0xff, 0xc3,
142 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
148 {-1.0, -1.0, -1.0, 1.0},
149 {-1.0, -1.0, 1.0, 1.0},
150 {-1.0, 1.0, 1.0, 1.0},
151 {-1.0, 1.0, -1.0, 1.0}},
154 {1.0, 1.0, 1.0, 1.0},
155 {1.0, -1.0, 1.0, 1.0},
156 {1.0, -1.0, -1.0, 1.0},
157 {1.0, 1.0, -1.0, 1.0}},
160 {-1.0, -1.0, -1.0, 1.0},
161 {1.0, -1.0, -1.0, 1.0},
162 {1.0, -1.0, 1.0, 1.0},
163 {-1.0, -1.0, 1.0, 1.0}},
166 {1.0, 1.0, 1.0, 1.0},
167 {1.0, 1.0, -1.0, 1.0},
168 {-1.0, 1.0, -1.0, 1.0},
169 {-1.0, 1.0, 1.0, 1.0}},
172 {-1.0, -1.0, -1.0, 1.0},
173 {-1.0, 1.0, -1.0, 1.0},
174 {1.0, 1.0, -1.0, 1.0},
175 {1.0, -1.0, -1.0, 1.0}},
178 {1.0, 1.0, 1.0, 1.0},
179 {-1.0, 1.0, 1.0, 1.0},
180 {-1.0, -1.0, 1.0, 1.0},
181 {1.0, -1.0, 1.0, 1.0}}
186 {-1.0, 0.0, 0.0, 0.0},
187 {1.0, 0.0, 0.0, 0.0},
188 {0.0, -1.0, 0.0, 0.0},
189 {0.0, 1.0, 0.0, 0.0},
190 {0.0, 0.0, -1.0, 0.0},
198 printf(
"usage: scube [options]\n");
200 printf(
" display a spinning cube and its shadow\n");
202 printf(
" Options:\n");
203 printf(
" -geometry window size and location\n");
204 printf(
" -c toggle color index mode\n");
205 printf(
" -l toggle lighting\n");
206 printf(
" -f toggle fog\n");
207 printf(
" -db toggle double buffering\n");
208 printf(
" -logo toggle sgi logo for the shadow pattern\n");
209 printf(
" -quads toggle use of GL_QUADS to draw the checkerboard\n");
213 #define EXIT_FAILURE 1
224 int mapSize = 1 << glutGet(GLUT_WINDOW_BUFFER_SIZE);
225 int rampSize = mapSize / 8;
229 for (entry = 0; entry < mapSize; ++entry) {
230 int hue = entry / rampSize;
231 GLfloat val = (entry % rampSize) * (1.0 / (rampSize - 1));
232 GLfloat red, green, blue;
234 red = (hue == 0 || hue == 1 || hue == 5 || hue == 6) ? val : 0;
235 green = (hue == 0 || hue == 2 || hue == 4 || hue == 6) ? val : 0;
236 blue = (hue == 0 || hue == 3 || hue == 4 || hue == 5) ? val : 0;
238 glutSetColor(entry, red, green, blue);
241 for (i = 0; i < 8; ++i) {
248 fogIndex[0] = -0.2 * (rampSize - 1);
257 glMaterialfv(GL_FRONT_AND_BACK,
260 glMaterialfv(GL_FRONT_AND_BACK,
279 for (i = 0; i < 6; ++i) {
293 static int initialized = 0;
294 static int usedLighting = 0;
295 static GLuint checklist = 0;
297 if (!initialized || (usedLighting !=
useLighting)) {
298 static float square_normal[4] =
299 {0.0, 0.0, 1.0, 0.0};
300 static float square[4][4];
304 checklist = glGenLists(1);
306 glNewList(checklist, GL_COMPILE_AND_EXECUTE);
309 glNormal3fv(square_normal);
312 for (j = 0; j < h; ++j) {
313 for (i = 0; i < w; ++i) {
314 square[0][0] = -1.0 + 2.0 / w * i;
315 square[0][1] = -1.0 + 2.0 / h * (j + 1);
319 square[1][0] = -1.0 + 2.0 / w * i;
320 square[1][1] = -1.0 + 2.0 / h * j;
324 square[2][0] = -1.0 + 2.0 / w * (i + 1);
325 square[2][1] = -1.0 + 2.0 / h * j;
329 square[3][0] = -1.0 + 2.0 / w * (i + 1);
330 square[3][1] = -1.0 + 2.0 / h * (j + 1);
343 glVertex4fv(&square[0][0]);
344 glVertex4fv(&square[1][0]);
345 glVertex4fv(&square[2][0]);
346 glVertex4fv(&square[3][0]);
361 glCallList(checklist);
369 float shadowMat[4][4];
371 dot = ground[0] * light[0] +
372 ground[1] * light[1] +
373 ground[2] * light[2] +
374 ground[3] * light[3];
376 shadowMat[0][0] = dot - light[0] * ground[0];
377 shadowMat[1][0] = 0.0 - light[0] * ground[1];
378 shadowMat[2][0] = 0.0 - light[0] * ground[2];
379 shadowMat[3][0] = 0.0 - light[0] * ground[3];
381 shadowMat[0][1] = 0.0 - light[1] * ground[0];
382 shadowMat[1][1] = dot - light[1] * ground[1];
383 shadowMat[2][1] = 0.0 - light[1] * ground[2];
384 shadowMat[3][1] = 0.0 - light[1] * ground[3];
386 shadowMat[0][2] = 0.0 - light[2] * ground[0];
387 shadowMat[1][2] = 0.0 - light[2] * ground[1];
388 shadowMat[2][2] = dot - light[2] * ground[2];
389 shadowMat[3][2] = 0.0 - light[2] * ground[3];
391 shadowMat[0][3] = 0.0 - light[3] * ground[0];
392 shadowMat[1][3] = 0.0 - light[3] * ground[1];
393 shadowMat[2][3] = 0.0 - light[3] * ground[2];
394 shadowMat[3][3] = dot - light[3] * ground[3];
396 glMultMatrixf((
const GLfloat *) shadowMat);
426 glDisable(GL_LIGHTING);
432 useFog ? glEnable(GL_FOG) : glDisable(GL_FOG);
436 glFogf(GL_FOG_MODE, GL_LINEAR);
440 glFogf(GL_FOG_MODE, GL_EXP);
444 glFogf(GL_FOG_MODE, GL_EXP2);
458 GLfloat cubeXform[4][4];
460 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
463 glTranslatef(0.0, -1.5, 0.0);
464 glRotatef(-90.0, 1, 0, 0);
465 glScalef(2.0, 2.0, 2.0);
471 glTranslatef(0.0, 0.0, -0.9);
472 glScalef(2.0, 2.0, 2.0);
478 glTranslatef(0.0, 0.2, 0.0);
479 glScalef(0.3, 0.3, 0.3);
480 glRotatef((360.0 / (30 * 1)) *
tick, 1, 0, 0);
481 glRotatef((360.0 / (30 * 2)) *
tick, 0, 1, 0);
482 glRotatef((360.0 / (30 * 4)) *
tick, 0, 0, 1);
483 glScalef(1.0, 2.0, 1.0);
484 glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat *) cubeXform);
489 glDepthMask(GL_FALSE);
493 glEnable(GL_POLYGON_STIPPLE);
500 glTranslatef(0.0, 0.0, 2.0);
501 glMultMatrixf((
const GLfloat *) cubeXform);
508 glTranslatef(0.0, 0.0, 2.0);
509 glMultMatrixf((
const GLfloat *) cubeXform);
514 glDepthMask(GL_TRUE);
518 glDisable(GL_POLYGON_STIPPLE);
533 glFogf(GL_FOG_MODE, fog);
551 useFog ? glEnable(GL_FOG) : glDisable(GL_FOG);
557 glDisable(GL_LIGHTING);
569 if (state == GLUT_VISIBLE) {
581 int width = 350, height = 350;
586 glutInitWindowSize(width, height);
587 glutInit(&
argc, argv);
589 for (i = 1; i <
argc; ++i) {
590 if (!strcmp(
"-c", argv[i])) {
592 }
else if (!strcmp(
"-l", argv[i])) {
594 }
else if (!strcmp(
"-f", argv[i])) {
596 }
else if (!strcmp(
"-db", argv[i])) {
598 }
else if (!strcmp(
"-logo", argv[i])) {
600 }
else if (!strcmp(
"-quads", argv[i])) {
610 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
613 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
618 glutInitDisplayMode(GLUT_DOUBLE | GLUT_INDEX | GLUT_DEPTH);
621 glutInitDisplayMode(GLUT_SINGLE | GLUT_INDEX | GLUT_DEPTH);
626 glutCreateWindow(name);
635 glutAddMenuEntry(
"Linear fog", GL_LINEAR);
636 glutAddMenuEntry(
"Exp fog", GL_EXP);
637 glutAddMenuEntry(
"Exp^2 fog", GL_EXP2);
640 glutAddMenuEntry(
"Start motion", 1);
641 glutAddMenuEntry(
"Stop motion", 2);
642 glutAddMenuEntry(
"Toggle fog", 3);
643 glutAddMenuEntry(
"Toggle lighting", 4);
644 glutAddSubMenu(
"Fog type", fog_menu);
645 glutAddMenuEntry(
"Quit", 5);
646 glutAttachMenu(GLUT_RIGHT_BUTTON);
649 glMatrixMode(GL_PROJECTION);
651 glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 3.0);
653 glMatrixMode(GL_MODELVIEW);
655 glTranslatef(0.0, 0.0, -2.0);
657 glEnable(GL_DEPTH_TEST);
660 glEnable(GL_LIGHTING);
663 glLightfv(GL_LIGHT0, GL_POSITION,
lightPos);
664 glLightfv(GL_LIGHT0, GL_AMBIENT,
lightAmb);
665 glLightfv(GL_LIGHT0, GL_DIFFUSE,
lightDiff);
666 glLightfv(GL_LIGHT0, GL_SPECULAR,
lightSpec);
668 glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, lightDir);
669 glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 80);
670 glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 25);
673 glEnable(GL_NORMALIZE);
680 glFogf(GL_FOG_MODE, GL_EXP);
681 glFogf(GL_FOG_DENSITY, 0.5);
682 glFogf(GL_FOG_START, 1.0);
683 glFogf(GL_FOG_END, 3.0);
685 glEnable(GL_CULL_FACE);
688 glShadeModel(GL_SMOOTH);
690 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
692 glPolygonStipple((
const GLubyte *)
sgiPattern);
697 glClearColor(0.0, 0.0, 0.0, 1);