/***************************** view3d.c This file demonstrates 3D viewing of a teapot with openGL. *******************************/ #include #include #include #include #define TRANSLATE 1 #define QUIT 2 float translate = 0.0; float current_translation = 0.0; /* * renderPerspective * This function renders a 3D perspective image * of the Utah Teapot. */ void renderPerspective() { glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); current_translation += translate; /* NOTE: this only works for situations where the DoP is */ /* orthogonal to the viewplane */ //glTranslatef(current_translation, 0.0, 0.0); gluLookAt(5+current_translation, 5, 5, current_translation, 0, 0, 0, 1, 0); //glTranslatef(current_translation, 0.0, 0.0); if(current_translation >= 3.0 || current_translation <= -3.0) translate = -translate; glutWireTeapot(2); glutSwapBuffers(); glutPostRedisplay(); } /* * reshape * This is the reshape callback function, which sets up * the window->viewport transformation and the perspective * projection transformation. */ void reshape(int w, int h) { if (w == h) glViewport(0, 0, w, h); else if (w > h) glViewport(0, 0, h, h); else glViewport(0, 0, w, w); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, 1.0, 1.0, 100.0); glMatrixMode(GL_MODELVIEW); } /* * menu_callback: the callback function when a menu item * is selected. Just a big switch statement with an entry * for each menu item. */ void menu_callback(int value) { switch(value){ case TRANSLATE: if(translate == 0.0) translate = 0.1; else translate = 0.0; break; case QUIT: glutDestroyWindow(glutGetWindow()); exit(0); break; default: break; } } /* * init_menus: subroutine to set up the pop-up menu * for this program */ void init_menus() { int my_menu; // create a menu and set callback fn. my_menu = glutCreateMenu(menu_callback); // insert the menu items in order glutAddMenuEntry("Toggle camera movement", TRANSLATE); glutAddMenuEntry("Quit", QUIT); // use the right mouse button to call up the menu glutAttachMenu(GLUT_RIGHT_BUTTON); } /* * Main function. Reads in the polygon and viewing information, * places it in data structures, and creates an interface window. */ void main(int argc, char **argv) { /* initialize */ glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); /* create the interface window */ glutInitWindowSize(300, 300); glutInitWindowPosition(0, 0); glutCreateWindow("3D Perspective Teapot"); /* set the reshape and display functions */ glutReshapeFunc(reshape); glutDisplayFunc(renderPerspective); init_menus(); /* enter the main loop */ glutMainLoop(); }