////////////////////////////////////////////////////////////////// // Robert C. Duvall // Fall 2001 // #include // also includes glu and gl correctly #include // for exit #include // for printf ////////////////////////////////////////////////////////////////// // Globals // // Constants // const int WINDOW_WIDTH = 800; // size of glut window const int WINDOW_HEIGHT = 600; ////////////////////////////////////////////////////////////////// // Utility functions // void printVector (char * title, GLint vec[4]) { printf("%s\n", title); printf(" |"); for (int k = 0; k < 4; k++) { printf(" %6d", vec[k]); } printf(" |\n"); printf("\n"); } void printMatrix (char * title, GLdouble mat[16]) { printf("%s\n", title); for (int r = 0; r < 4; r++) { printf(" |"); for (int c = 0; c < 4; c++) { printf(" %6.2f", mat[r + c * 4]); } printf(" |\n"); } printf("\n"); } ////////////////////////////////////////////////////////////////// // User Functions // These are the functions you will mostly be changing. // void initModel () { } void drawModel () { GLdouble mat[16]; glPushMatrix(); glColor3f(1.0, 1.0, 1.0); glTranslatef(0.0, 0.0, -1.0); glGetDoublev(GL_MODELVIEW_MATRIX, mat); printMatrix("After translate:", mat); glRotatef(60, 0.0, 1.0, 0.0); glGetDoublev(GL_MODELVIEW_MATRIX, mat); printMatrix("After rotate:", mat); glScalef(2.0, 2.0, 2.0); glGetDoublev(GL_MODELVIEW_MATRIX, mat); printMatrix("After scale:", mat); glutWireCube(1.0); glPopMatrix(); } ////////////////////////////////////////////////////////////////// // Callback Functions // These functions are registered with the glut window and called // when certain events occur. // void onInit () // pre: glut window has been initialized // post: model has been initialized { initModel(); glClearColor(0.0, 0.0, 0.0, 0.0); glEnable(GL_DEPTH_TEST); } void onRedraw () // pre: glut window needs to be refreshed // post: model is drawn { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); drawModel(); glutSwapBuffers(); } void onResize (int width, int height) // pre: glut window has been resized // post: resets cameras location and aspect to match window { GLint vec[4]; GLdouble mat[16]; glViewport(0, 0, GLint(width), GLint(height)); glGetIntegerv(GL_VIEWPORT, vec); printVector("After veiwport:", vec); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, GLfloat(width) / GLfloat(height), 0.5, 10.0); glGetDoublev(GL_PROJECTION_MATRIX, mat); printMatrix("After perspective:", mat); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0, 2.0, 4.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glGetDoublev(GL_MODELVIEW_MATRIX, mat); printMatrix("After lookat:", mat); } void onKeyPressed (unsigned char key, int /* mouseX */, int /* mouseY */) // pre: key has been pressed while mouse is within glut window // post: animation may be paused or started, program may be exited { switch (key) { case 'q': case 27: // ESCAPE key exit(1); break; } } ////////////////////////////////////////////////////////////////// // Main Function // int main (int argc, char *argv[]) { glutInit(&argc, argv); glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA); glutCreateWindow(argv[0]); onInit(); glutDisplayFunc(onRedraw); glutReshapeFunc(onResize); glutKeyboardFunc(onKeyPressed); glutMainLoop(); return 0; // program should never get here }