/* box11.c * Ray S. Babcock, CS425, F'02 * * This program demonstrates the use of the GL viewing call * and simple idle loop animation. * * A teapot and sphere are added to the objects being displayed. */ #include #include #include /* DELTA_DEG is the increment for rotation, useful for * different speed computers. Set lower to slow down rotation. */ #define DELTA_DEG 1.0 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(1.0, 6.0, 12.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() { /* draw the pillar, (cube rotated CW twice as fast as the increment * specifies then scaled and translated). */ glPopMatrix(); /* retrieve the original values */ glPushMatrix(); /* save for later use */ /* rotate the modeling coord system CW twice the increment around X */ glRotatef(rot_deg*(-2.0), 1, 0, 0); glScalef(0.5, 1.5, 0.5); /* scale the modeling coord system */ glTranslatef(-4.0, 0.0, 0.0); /* translate the modeling coord system in negative X */ glCallList(theCube); /* draw pillar (cube rotated, scaled, and translated)*/ /* draw the cube translated and rotated CCW three times as fast * as the increment specifies. */ glPopMatrix(); /* retrieve the original values */ glPushMatrix(); /* save for later use */ /* rotate the modeling coord system CCW three times the increment around X */ glRotatef(rot_deg*3.0, 1, 0, 0); glTranslatef(2.0, 0.0, 0.0); /* translate the modeling coord system in positive X */ glCallList(theCube); /* draw the cube (rotated and translated) */ /* draw a white GLUT solid sphere, rotated CCW twice * as fast as the increment specifies, and translated in positive Y. */ glColor3f(1.0, 1.0, 1.0); /* set color to white */ glPopMatrix(); /* retrieve original values */ glPushMatrix(); /* save for later use */ /* Rotate modeling coord system twice as much as increment specifies CCW around X */ glRotatef(rot_deg*2.0, 1, 0, 0); glTranslatef(0.0, 2.0, 0.0); /* translate modeling coord system in positive y */ glutSolidSphere(1.0,20,20); /* draw a solid sphere */ /* draw a gray GLUT solid teapot, rotated CCW about * the Y axis at the same speed as the increment (for this program * the teapot rotates the slowest) and translated in positive Y. */ glColor3f(0.6, 0.6, 0.6); /* set color to a gray */ glPopMatrix(); /* retrieve the original values */ glPushMatrix(); /* save for possible use later */ glRotatef(rot_deg*1.0, 0, 1, 0); /* rotate CCW around Y */ glTranslatef(0.0, 2.0, 0.0); /* translate in positive y */ /* this causes the teapot to rotate around its center y axis */ glutSolidTeapot(1.0); /* draw the teapot */ } 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. */ }