#include #include // function prototypes void display(void); void reshape(int width, int height); void keyboard(unsigned char key, int x, int y); void init(void); void menu(int menuItem); void printMatrix(float matrix[16]); enum {MY_MENU_PROJECTION, MY_MENU_PROJECTION_MATRIX, MY_MENU_MODELVIEW_MATRIX}; int main(int argc, char **argv) { // set up window glutInitWindowSize(400, 400); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutCreateWindow("My starter code"); // register callback functions glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); // initalize opengl parameters init(); // loop until something happens glutMainLoop(); return 0; } void init() { // initialize viewing system glMatrixMode(GL_PROJECTION); glLoadIdentity(); // we start in orthographic mode glOrtho(-1, 1, -1, 1, 1, 3); // the near plane is at -1 and the far plane at -3 // because the glOrtho call reverses the signs! glMatrixMode(GL_MODELVIEW); // initialize background color to black glClearColor(0.0,0.0,0.0,0.0); // enable depth buffering glEnable(GL_DEPTH_TEST); // build menu glutCreateMenu(menu); glutAddMenuEntry("Toggle Projection Mode", MY_MENU_PROJECTION); glutAddMenuEntry("Print Projection Matrix",MY_MENU_PROJECTION_MATRIX); glutAddMenuEntry("Print Modelview Matrix",MY_MENU_MODELVIEW_MATRIX); glutAttachMenu(GLUT_RIGHT_BUTTON); // set up printing for matrix cout.setf(ios::showpoint); cout.setf(ios::showpos); cout.precision(4); cout.setf(ios::fixed); } void reshape(int width, int height) { } void display() { // clear the buffers glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // initialize modelview matrix glLoadIdentity(); //draw a triangle glColor3f(1,0,0); glBegin(GL_TRIANGLES); glVertex3f(0,0,-1.1); glVertex3f(1,0,-1.1); glVertex3f(0.3f,0.5f,-1.1); glEnd(); //draw a triangle glColor3f(0,0,1); glBegin(GL_TRIANGLES); glVertex3f(-1,0,-1.5f); glVertex3f(1,0,-1.5f); glVertex3f(0.2f,0.5f,-1.5f); glEnd(); //draw a triangle glColor3f(0,1,0); glBegin(GL_TRIANGLES); glVertex3f(-1,0,-1.9); glVertex3f(1,0,-1.9); glVertex3f(0.1f,0.6f,-1.9); glEnd(); // draw to screen glutSwapBuffers(); } void keyboard(unsigned char key, int x, int y) { if (key == ' ') cout << "You hit the space bar." << endl; } void menu(int menuItem) { static bool perspective=false; float matrix[16]; switch(menuItem) { case MY_MENU_PROJECTION: // toggle projection mode { glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (perspective) { cout << endl << "Switching to orthographic projection." << endl; glOrtho(-1, 1, -1, 1, 1, 3); } else { cout << endl << "Switching to perspective projection." << endl; glFrustum(-1,1,-1,1,1,3); } perspective = !perspective; glMatrixMode(GL_MODELVIEW); glutPostRedisplay(); break; } case MY_MENU_PROJECTION_MATRIX: // print current projection matrix { glGetFloatv(GL_PROJECTION_MATRIX, matrix); cout << endl << "Current Projection Matrix "; if (perspective) cout << "(Perspective l=-1, r=1, b=-1, t=1, n=1, f=3):" << endl; else cout << "(Orthographic l=-1, r=1, b=-1, t=1, n=1, f=3):" << endl; printMatrix(matrix); break; } case MY_MENU_MODELVIEW_MATRIX: // print current modeling matrix { glGetFloatv(GL_MODELVIEW_MATRIX, matrix); cout << endl << "Current Modelview Matrix " << endl; printMatrix(matrix); break; } default: break; } } void printMatrix(float matrix[16]) { for (int i=0;i<4;i++) { for (int j=0; j<4; j++) cout << matrix[i+j*4] << " "; cout << endl; } }