/* box9.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, rotated CW and translated) * (note the distortion because scaled is done first) * A cube is drawn rotated and translated. */ #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() { /* Pillar (cube scaled, rotated CW & translated */ glPopMatrix(); /* retrieve original values */ glPushMatrix(); /* save for later */ glScalef(0.5, 1.5, 0.5); /* scale modeling coord system */ glRotatef(rot_deg*(-1.0), 1, 0, 0); /* rotate modeling coord system CW around X */ glTranslatef(-4.0, 0.0, 0.0); /* translate in negative X */ glCallList(theCube); /* draw cube scaled, rotated, and translated */ /* Cube translated, rotated CCW */ glPopMatrix(); /* retrieve original values */ glPushMatrix(); /* save for possible use later */ /* rotate modeling coord system around X (doesn't change direction of X) */ glRotatef(rot_deg, 1, 0, 0); glTranslatef(2.0, 0.0, 0.0); /* translate in positive X */ glCallList(theCube); /* draw the cube rotated and translated */ } 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. */ }