/* box6.c * Ray S. Babcock, CS425, F'02 * * This program demonstrates the use of the GL viewing call * and simple idle loop animation. * * A pillar is drawn (cube scaled, translated, and rotated CW). * A cube is drawn translated and rotated CCW. * Both rotations are the same speed, but opposite directions. */ #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(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 pillar (cube scaled, rotated CW, & translated) */ glPopMatrix(); /* retrieve the original viewing matrix values */ glPushMatrix(); /* save the matrix for later use */ glScalef(0.5, 1.5, 0.5); /* scale the modeling coord system */ /* rotate the modeling coord system CW an increasing amount around Y) */ glRotatef(rot_deg*(-1.0), 0, 1, 0); /* translate the modeling coord system in the negative X direction. (After the rotation, this will be in a different direction each time) */ glTranslatef(-4.0, 0.0, 0.0); glCallList(theCube); /* draw the cube scaled, translated, and rotated */ /* draw cube translated & rotated CCW */ glPopMatrix(); /* retrieve the original viewing matrix values */ glPushMatrix(); /* save the matrix for later use */ glTranslatef(2.0, 0.0, 0.0); /* translate the modeling coord system in positive X */ /* rotate the world CCW around Y by an increasing amount */ glRotatef(rot_deg, 0, 1, 0); glCallList(theCube); /* draw the cube translated and rotated */ } void display (void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); draw_scene(); glFlush(); glutSwapBuffers(); } void myReshape(int w, int h) { /* minimal support for reshape */ 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. */ }