46 #define M_PI 3.14159265 75 static GLfloat
body[][2] = { {0, 3}, {1, 1}, {5, 1}, {8, 4}, {10, 4}, {11, 5},
76 {11, 11.5}, {13, 12}, {13, 13}, {10, 13.5}, {13, 14}, {13, 15}, {11, 16},
77 {8, 16}, {7, 15}, {7, 13}, {8, 12}, {7, 11}, {6, 6}, {4, 3}, {3, 2},
79 static GLfloat
arm[][2] = { {8, 10}, {9, 9}, {10, 9}, {13, 8}, {14, 9}, {16, 9},
80 {15, 9.5}, {16, 10}, {15, 10}, {15.5, 11}, {14.5, 10}, {14, 11}, {14, 10},
81 {13, 9}, {11, 11}, {9, 11} };
82 static GLfloat
leg[][2] = { {8, 6}, {8, 4}, {9, 3}, {9, 2}, {8, 1}, {8, 0.5}, {9, 0},
83 {12, 0}, {10, 1}, {10, 2}, {12, 4}, {11, 6}, {10, 7}, {9, 7} };
84 static GLfloat
eye[][2] = { {8.75, 15}, {9, 14.7}, {9.6, 14.7}, {10.1, 15},
85 {9.6, 15.25}, {9, 15.25} };
114 GLubyte floorTexture[16][16][3];
119 loc = (GLubyte*) floorTexture;
120 for (t = 0; t < 16; t++) {
121 for (s = 0; s < 16; s++) {
137 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
140 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
141 GL_LINEAR_MIPMAP_LINEAR);
142 gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 16, 16,
143 GL_RGB, GL_UNSIGNED_BYTE, floorTexture);
146 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
148 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
150 glTexImage2D(GL_TEXTURE_2D, 0, 3, 16, 16, 0,
151 GL_RGB, GL_UNSIGNED_BYTE, floorTexture);
165 GLfloat groundplane[4],
171 dot = groundplane[
X] * lightpos[
X] +
172 groundplane[
Y] * lightpos[
Y] +
173 groundplane[
Z] * lightpos[
Z] +
174 groundplane[
W] * lightpos[
W];
176 shadowMat[0][0] = dot - lightpos[
X] * groundplane[
X];
177 shadowMat[1][0] = 0.f - lightpos[
X] * groundplane[
Y];
178 shadowMat[2][0] = 0.f - lightpos[
X] * groundplane[
Z];
179 shadowMat[3][0] = 0.f - lightpos[
X] * groundplane[
W];
181 shadowMat[
X][1] = 0.f - lightpos[
Y] * groundplane[
X];
182 shadowMat[1][1] = dot - lightpos[
Y] * groundplane[
Y];
183 shadowMat[2][1] = 0.f - lightpos[
Y] * groundplane[
Z];
184 shadowMat[3][1] = 0.f - lightpos[
Y] * groundplane[
W];
186 shadowMat[
X][2] = 0.f - lightpos[
Z] * groundplane[
X];
187 shadowMat[1][2] = 0.f - lightpos[
Z] * groundplane[
Y];
188 shadowMat[2][2] = dot - lightpos[
Z] * groundplane[
Z];
189 shadowMat[3][2] = 0.f - lightpos[
Z] * groundplane[
W];
191 shadowMat[
X][3] = 0.f - lightpos[
W] * groundplane[
X];
192 shadowMat[1][3] = 0.f - lightpos[
W] * groundplane[
Y];
193 shadowMat[2][3] = 0.f - lightpos[
W] * groundplane[
Z];
194 shadowMat[3][3] = dot - lightpos[
W] * groundplane[
W];
201 GLfloat v0[3], GLfloat v1[3], GLfloat v2[3])
203 GLfloat vec0[3], vec1[3];
206 vec0[
X] = v1[
X] - v0[
X];
207 vec0[
Y] = v1[
Y] - v0[
Y];
208 vec0[
Z] = v1[
Z] - v0[
Z];
210 vec1[
X] = v2[
X] - v0[
X];
211 vec1[
Y] = v2[
Y] - v0[
Y];
212 vec1[
Z] = v2[
Z] - v0[
Z];
215 plane[
A] = vec0[
Y] * vec1[
Z] - vec0[
Z] * vec1[
Y];
216 plane[
B] = -(vec0[
X] * vec1[
Z] - vec0[
Z] * vec1[
X]);
217 plane[
C] = vec0[
X] * vec1[
Y] - vec0[
Y] * vec1[
X];
219 plane[
D] = -(plane[
A] * v0[
X] + plane[
B] * v0[
Y] + plane[
C] * v0[
Z]);
224 GLdouble thickness, GLuint side, GLuint
edge, GLuint whole)
226 static GLUtriangulatorObj *tobj =
NULL;
227 GLdouble vertex[3], dx, dy, len;
229 int count = (int) (dataSize / (2 *
sizeof(GLfloat)));
234 gluTessCallback(tobj, GLU_BEGIN, glBegin);
235 gluTessCallback(tobj, GLU_VERTEX, glVertex2fv);
236 gluTessCallback(tobj, GLU_END, glEnd);
238 glNewList(side, GL_COMPILE);
239 glShadeModel(GL_SMOOTH);
241 gluBeginPolygon(tobj);
242 for (i = 0; i <
count; i++) {
243 vertex[0] = data[i][0];
244 vertex[1] = data[i][1];
246 gluTessVertex(tobj, vertex, data[i]);
250 glNewList(edge, GL_COMPILE);
251 glShadeModel(GL_FLAT);
253 glBegin(GL_QUAD_STRIP);
254 for (i = 0; i <=
count; i++) {
256 glVertex3f(data[i % count][0], data[i % count][1], 0.0);
257 glVertex3f(data[i % count][0], data[i % count][1], thickness);
262 dx = data[(i + 1) % count][1] - data[i % count][1];
263 dy = data[i %
count][0] - data[(i + 1) % count][0];
264 len =
sqrt(dx * dx + dy * dy);
265 glNormal3f(dx / len, dy / len, 0.0);
269 glNewList(whole, GL_COMPILE);
272 glNormal3f(0.0, 0.0, -1.0);
275 glTranslatef(0.0, 0.0, thickness);
277 glNormal3f(0.0, 0.0, 1.0);
309 glTranslatef(0.0,
jump, 0.0);
310 glMaterialfv(GL_FRONT, GL_DIFFUSE,
skinColor);
319 glTranslatef(0.0, 0.0,
bodyWidth / 2 - 0.1);
320 glMaterialfv(GL_FRONT, GL_DIFFUSE,
eyeColor);
326 { -20.0, 0.0, 20.0 },
328 { 20.0, 0.0, -20.0 },
329 { -20.0, 0.0, -20.0 },
336 glDisable(GL_LIGHTING);
339 glEnable(GL_TEXTURE_2D);
343 glTexCoord2f(0.0, 0.0);
345 glTexCoord2f(0.0, 16.0);
347 glTexCoord2f(16.0, 16.0);
349 glTexCoord2f(16.0, 0.0);
354 glDisable(GL_TEXTURE_2D);
357 glEnable(GL_LIGHTING);
369 start = glutGet(GLUT_ELAPSED_TIME);
374 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
377 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
394 glRotatef(
angle2, 1.0, 0.0, 0.0);
395 glRotatef(
angle, 0.0, 1.0, 0.0);
411 glDisable(GL_DEPTH_TEST);
412 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
415 glEnable(GL_STENCIL_TEST);
416 glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
417 glStencilFunc(GL_ALWAYS, 1, 0xffffffff);
423 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
424 glEnable(GL_DEPTH_TEST);
427 glStencilFunc(GL_EQUAL, 1, 0xffffffff);
428 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
435 glScalef(1.0, -1.0, 1.0);
442 glEnable(GL_NORMALIZE);
443 glCullFace(GL_FRONT);
449 glDisable(GL_NORMALIZE);
458 glDisable(GL_STENCIL_TEST);
469 glColor4f(0.1, 0.1, 0.7, 1.0);
478 glEnable(GL_STENCIL_TEST);
479 glStencilFunc(GL_ALWAYS, 3, 0xffffffff);
480 glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
486 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
487 glColor4f(0.7, 0.0, 0.0, 0.3);
488 glColor4f(1.0, 1.0, 1.0, 0.3);
507 glStencilFunc(GL_LESS, 2, 0xffffffff);
508 glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
517 #ifdef GL_EXT_polygon_offset 518 glEnable(GL_POLYGON_OFFSET_EXT);
521 #ifdef GL_VERSION_1_1 523 glEnable(GL_POLYGON_OFFSET_FILL);
535 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
536 glDisable(GL_LIGHTING);
537 glColor4f(0.0, 0.0, 0.0, 0.5);
546 glEnable(GL_LIGHTING);
550 #ifdef GL_EXT_polygon_offset 552 glDisable(GL_POLYGON_OFFSET_EXT);
555 #ifdef GL_VERSION_1_1 557 glDisable(GL_POLYGON_OFFSET_FILL);
566 glDisable(GL_STENCIL_TEST);
571 glDisable(GL_LIGHTING);
572 glColor3f(1.0, 1.0, 0.0);
575 glDisable(GL_CULL_FACE);
579 glBegin(GL_TRIANGLE_FAN);
582 glVertex3f(2, -1, 1);
583 glVertex3f(2, -1, -1);
584 glVertex3f(2, 1, -1);
588 glColor3f(1.0, 1.0, 1.0);
593 glEnable(GL_CULL_FACE);
597 glutSolidSphere(1.0, 5, 5);
599 glEnable(GL_LIGHTING);
606 end = glutGet(GLUT_ELAPSED_TIME);
607 printf(
"Speed %.3g frames/sec (%d ms)\n", 1000.0/(end-start), end-start);
615 mouse(
int button,
int state,
int x,
int y)
617 if (button == GLUT_LEFT_BUTTON) {
618 if (state == GLUT_DOWN) {
623 if (state == GLUT_UP) {
627 if (button == GLUT_MIDDLE_BUTTON) {
628 if (state == GLUT_DOWN) {
633 if (state == GLUT_UP) {
663 static float time = 0.0;
665 time = glutGet(GLUT_ELAPSED_TIME) / 500.0;
667 jump = 4.0 * fabs(
sin(time)*0.5);
699 glDisable(GL_LIGHT0);
740 if (vis == GLUT_VISIBLE) {
753 key(
unsigned char c,
int x,
int y)
776 version = (
char *) glGetString(GL_VERSION);
777 if (sscanf(version,
"%d.%d", &major, &minor) == 2)
778 return major >= 1 && minor >= 1;
787 glutInit(&argc, argv);
789 for (i=1; i<
argc; i++) {
790 if (!strcmp(
"-linear", argv[i])) {
792 }
else if (!strcmp(
"-mipmap", argv[i])) {
794 }
else if (!strcmp(
"-ext", argv[i])) {
799 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL | GLUT_MULTISAMPLE);
804 glutInitDisplayString(
"samples stencil>=2 rgb double depth");
807 glutCreateWindow(
"Shadowy Leapin' Lizards");
809 if (glutGet(GLUT_WINDOW_STENCIL_SIZE) <= 1) {
810 printf(
"dinoshade: Sorry, I need at least 2 bits of stencil.\n");
816 glutMouseFunc(
mouse);
819 glutKeyboardFunc(
key);
824 glutAddMenuEntry(
"Toggle motion",
M_MOTION);
825 glutAddMenuEntry(
"-----------------------",
M_NONE);
826 glutAddMenuEntry(
"Toggle light",
M_LIGHT);
827 glutAddMenuEntry(
"Toggle texture",
M_TEXTURE);
828 glutAddMenuEntry(
"Toggle shadows",
M_SHADOWS);
830 glutAddMenuEntry(
"Toggle dinosaur",
M_DINOSAUR);
831 glutAddMenuEntry(
"-----------------------",
M_NONE);
835 glutAddMenuEntry(
"----------------------",
M_NONE);
838 glutAddMenuEntry(
"-----------------------",
M_NONE);
840 glutAttachMenu(GLUT_RIGHT_BUTTON);
843 #ifdef GL_VERSION_1_1 846 glPolygonOffset(-2.0, -1.0);
850 #ifdef GL_EXT_polygon_offset 852 if (glutExtensionSupported(
"GL_EXT_polygon_offset")) {
854 glPolygonOffsetEXT(-0.1, -0.002);
859 printf(
"\ndinoshine: Missing polygon offset.\n");
860 printf(
" Expect shadow depth aliasing artifacts.\n\n");
864 glEnable(GL_CULL_FACE);
865 glEnable(GL_DEPTH_TEST);
866 glEnable(GL_TEXTURE_2D);
869 glMatrixMode(GL_PROJECTION);
870 gluPerspective( 40.0,
873 glMatrixMode(GL_MODELVIEW);
874 gluLookAt(0.0, 8.0, 60.0,
878 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
880 glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1);
881 glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05);
883 glEnable(GL_LIGHTING);
static void drawFloor(void)
int main(int argc, char **argv)
static void makeDinosaur(void)
static GLfloat floorShadow[4][4]
static void drawDinosaur(void)
static GLfloat lightColor[]
static int directionalLight
static GLfloat lightPosition[4]
static void controlLights(int value)
static void visible(int vis)
static void special(int k, int x, int y)
static GLfloat floorVertices[4][3]
static int renderReflection
static GLdouble bodyWidth
static void key(unsigned char c, int x, int y)
void extrudeSolidFromPolygon(GLfloat data[][2], unsigned int dataSize, GLdouble thickness, GLuint side, GLuint edge, GLuint whole)
static int linearFiltering
static void makeFloorTexture(void)
void findPlane(GLfloat plane[4], GLfloat v0[3], GLfloat v1[3], GLfloat v2[3])
static int supportsOneDotOne(void)
static GLfloat floorPlane[4]
static void motion(int x, int y)
static time_stamp_t start
static int renderDinosaur
static GLboolean lightSwitch
void shadowMatrix(GLfloat shadowMat[4][4], GLfloat groundplane[4], GLfloat lightpos[4])
static GLfloat eyeColor[]
static void mouse(int button, int state, int x, int y)
static int stencilReflection
static GLfloat skinColor[]
edge(MATRIX f, int n, unsigned int planetab, unsigned int planework, int r, linetab, int m)
static int forceExtension