#include #include "glut.h" void reshape(int width, int height) { static GLfloat lightPosition[4] = {0.25f, 1.0f, 0.25f, 0.0f}; static GLfloat lightDiffuse[3] = {1.0f, 1.0f, 1.0f}; static GLfloat lightAmbient[3] = {0.25f, 0.25f, 0.25f}; static GLfloat lightSpecular[3] = {1.0f, 1.0f, 1.0f}; glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glShadeModel(GL_SMOOTH); glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, (double)width / (double)height, 0.1, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.5, 1.5, 2.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse); glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient); glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular); } void display() { static GLfloat vertices [8][3] = { {-0.5f, -0.5f, 0.5f}, { 0.5f, -0.5f, 0.5f}, { 0.5f, 0.5f, 0.5f}, {-0.5f, 0.5f, 0.5f}, { 0.5f, -0.5f, -0.5f}, {-0.5f, -0.5f, -0.5f}, {-0.5f, 0.5f, -0.5f}, { 0.5f, 0.5f, -0.5f} }; static GLfloat normals[6][3] = { { 0.0f, 0.0f, 1.0f}, { 0.0f, 0.0f, -1.0f}, { 1.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}, { 0.0f, 1.0f, 0.0f}, { 0.0f, -1.0f, 0.0f} }; static GLfloat diffuse[3] = {1.0f, 0.0f, 0.0f}; static GLfloat ambient[3] = {0.25f, 0.25f, 0.25f}; static GLfloat specular[3] = {1.0f, 1.0f, 1.0f}; static GLfloat shininess[1] = {32.0f}; static float angle = 0.0f; glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse); glMaterialfv(GL_FRONT, GL_AMBIENT, ambient); glMaterialfv(GL_FRONT, GL_SPECULAR, specular); glMaterialfv(GL_FRONT, GL_SHININESS, shininess); glEnable(GL_DEPTH_TEST); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); angle += 2.5f; if (angle > 360.0f) { angle -= 360.0f; } glPushMatrix(); glRotatef(angle, 0.0f, 1.0f, 0.0f); // 前 glBegin(GL_QUADS); glNormal3fv(normals[0]); glVertex3fv(vertices[0]); glVertex3fv(vertices[1]); glVertex3fv(vertices[2]); glVertex3fv(vertices[3]); glEnd(); // 後 glBegin(GL_QUADS); glNormal3fv(normals[1]); glVertex3fv(vertices[4]); glVertex3fv(vertices[5]); glVertex3fv(vertices[6]); glVertex3fv(vertices[7]); glEnd(); // 右 glBegin(GL_QUADS); glNormal3fv(normals[2]); glVertex3fv(vertices[1]); glVertex3fv(vertices[4]); glVertex3fv(vertices[7]); glVertex3fv(vertices[2]); glEnd(); // 左 glBegin(GL_QUADS); glNormal3fv(normals[3]); glVertex3fv(vertices[5]); glVertex3fv(vertices[0]); glVertex3fv(vertices[3]); glVertex3fv(vertices[6]); glEnd(); // 上 glBegin(GL_QUADS); glNormal3fv(normals[4]); glVertex3fv(vertices[3]); glVertex3fv(vertices[2]); glVertex3fv(vertices[7]); glVertex3fv(vertices[6]); glEnd(); // 下 glBegin(GL_QUADS); glNormal3fv(normals[5]); glVertex3fv(vertices[1]); glVertex3fv(vertices[0]); glVertex3fv(vertices[5]); glVertex3fv(vertices[4]); glEnd(); glPopMatrix(); glutSwapBuffers(); } void idle() { glutPostRedisplay(); } int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); glutInitWindowPosition(100, 100); glutInitWindowSize(640, 480); glutCreateWindow("Animation Cube"); glutReshapeFunc(reshape); glutIdleFunc(idle); glutDisplayFunc(display); glutMainLoop(); return 0; }