/* box2.c * Ray S. Babcock, CS425, F'02 * * This program demonstrates the use of the GL viewing call * and simple idle loop animation. * * A colored cube is drawn. * A display list is used for the cube. * A draw_scene function is used to simplify the * display function. (Useful in larger programs). */ #include #include #include /* DELTA_DEG is the increment for rotation, useful for * different speed computers. Set lower to slow down rotation. */ #define DELTA_DEG 0.4 static float rot_deg = 0.0; GLuint theCube; void draw_scene(); void display(void); void rotate_scene(void); void mycube(void) { GLfloat vertices[] = { -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0}; GLubyte frontIndices[] = { 4, 5, 6, 7 }; GLubyte rightIndices[] = { 5, 1, 2, 6 }; GLubyte bottomIndices[] = { 0, 1, 5, 4 }; GLubyte backIndices[] = { 1, 0, 3, 2 }; GLubyte leftIndices[] = { 0, 4, 7, 3 }; GLubyte topIndices[] = { 7, 6, 2, 3 }; glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, 0, vertices); /* front face */ glColor3f(1.0,0.0,0.0); glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_BYTE, frontIndices); /* right face */ glColor3f(0.0,1.0,0.0); glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_BYTE, rightIndices); /* bottom face */ glColor3f(0.0,0.0,1.0); glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_BYTE, bottomIndices); /* back face */ glColor3f(1.0,1.0,0.0); glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_BYTE, backIndices); /* left face */ glColor3f(0.0,1.0,1.0); glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_BYTE, leftIndices); /* top face */ glColor3f(1.0,0.0,1.0); glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_BYTE, topIndices); } void myinit (void) { theCube = glGenLists(1); glNewList(theCube, GL_COMPILE); mycube(); glEndList(); glShadeModel (GL_FLAT); glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluPerspective(30.0, 1.0, 1.0, 200.0); glMatrixMode (GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0, 3.0, 6.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glPushMatrix(); glEnable(GL_DEPTH_TEST); } void rotate_scene() { rot_deg += DELTA_DEG; if(rot_deg >= 360.0)rot_deg=0.0; glutPostRedisplay(); } void draw_scene() { glPopMatrix(); /* retrieve the original view matrix */ glPushMatrix(); /* save it again for later */ /* rotate the modeling coordinate system around Y by an increasing amount */ glRotatef(rot_deg, 0, 1, 0); glCallList(theCube); } void display (void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); draw_scene(); glFlush(); glutSwapBuffers(); } void myReshape(int w, int h) { /* minimal reshape support */ glViewport (0, 0, w, h); } void keyboard(unsigned char key, int x, int y) { switch (key) { case 27: /* escape key */ exit(0); break; } } /* Main Loop * Open window with initial window size, title bar, * RGB display mode, and handle input events. */ int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize (500, 500); glutCreateWindow (argv[0]); myinit (); glutIdleFunc(rotate_scene); glutReshapeFunc (myReshape); glutDisplayFunc(display); glutKeyboardFunc (keyboard); glutMainLoop(); return 0; /* ANSI C requires main to return int. */ }