#include #include #include #include #include #include #include #define X .525731112119133606 #define Z .850650808352039932 #define SETRANDCOLOR() \ int r = rand() % 256; \ int g = rand() % 256; \ int b = rand() % 256; \ glColor3ub(r, g, b); const int EXIT_OK = 0; const int EXIT_RESTART = 1; const int EXIT_ERROR = 2; const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; static GLfloat vdata[12][3] = { {-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z}, {X, 0.0, -Z}, {0.0, Z, X}, {0.0, Z, -X}, {0.0, -Z, X}, {0.0, -Z, -X}, {Z, X, 0.0}, {-Z, X, 0.0}, {Z, -X, 0.0}, {-Z, -X, 0.0} }; static GLuint tindices[20][3] = { {0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1}, {8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3}, {7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6}, {6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11} }; void normalize(float v[3]) { GLfloat d = sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]); if (d == 0.0) { exit(EXIT_ERROR); return; } v[0] /= d; v[1] /= d; v[2] /= d; } void normcrossprod(float v1[3], float v2[3], float out[3]) { GLint i, j; GLfloat length; out[0] = v1[1]*v2[2] - v1[2]*v2[1]; out[1] = v1[2]*v2[0] - v1[0]*v2[2]; out[2] = v1[0]*v2[1] - v1[1]*v2[0]; normalize(out); } GLfloat LightAmbient[] = { 0.0f, 0.0f, 0.0f, 0.0f }; GLfloat LightDiffuse[] = { 1.0f, 1.0f, 1.0f, 0.0f }; GLfloat LightPosition[] = { 0.0f, 0.0f, 20.0f, 0.0f }; GLfloat specular[] = { 0.2f, 0.2f, 0.2f, 0.0f }; GLfloat emission[] = { 0.0f, 0.0f, 0.0f, 0.0f }; GLfloat lightmodel[] = { 0.2f, 0.2f, 0.2f, 0.0f }; void initlight() { glEnable(GL_LIGHTING); glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient); glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse); glLightfv(GL_LIGHT0, GL_POSITION, LightPosition); glEnable(GL_LIGHT0); glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular); glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, emission); glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lightmodel); glShadeModel(GL_SMOOTH); } int main(void) { allegro_init(); install_allegro_gl(); allegro_gl_clear_settings(); allegro_gl_set (AGL_COLOR_DEPTH, 32); if (set_gfx_mode(GFX_OPENGL_WINDOWED, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0) < 0) { printf("Couldn't init gfx mode.\n"); exit(1); } install_keyboard(); initlight(); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); /* Depth buffer setup */ glClearDepth(100.0f); glViewport(0, 0, (GLsizei)SCREEN_WIDTH, (GLsizei)SCREEN_HEIGHT); /* change to the projection matrix and set our viewing volume. */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); /* Set our perspective */ GLfloat ratio = (GLfloat)SCREEN_WIDTH / (GLfloat)SCREEN_HEIGHT; gluPerspective(45.0f, ratio, 0.1f, 100.0f); /* Make sure we're chaning the model view and not the projection */ glMatrixMode(GL_MODELVIEW); /* Reset The View */ glLoadIdentity(); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); int i; double angle = 0.5; SETRANDCOLOR(); glTranslatef(0.0f, 0.0f, -5.0f); for (;;) { glRotatef(angle, 1.0f, 1.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBegin(GL_TRIANGLES); for (i = 0; i < 20; i++) { GLfloat d1[3], d2[3], norm[3]; for (int j = 0; j < 3; j++) { d1[j] = vdata[tindices[i][0]][j] - vdata[tindices[i][1]][j]; d2[j] = vdata[tindices[i][1]][j] - vdata[tindices[i][2]][j]; } normcrossprod(d1, d2, norm); glNormal3fv(norm); glVertex3fv(&vdata[tindices[i][0]][0]); glVertex3fv(&vdata[tindices[i][1]][0]); glVertex3fv(&vdata[tindices[i][2]][0]); } glEnd(); glFlush(); allegro_gl_flip(); rest(20); if (keypressed()) break; } } END_OF_MAIN()