#include #include #include #include #include using namespace std; GLfloat Left, Right, Top, Bottom, Near, Far; void DisplayString(void * font, string s) { int i; for(i=0; i < s.size(); i++) { glutStrokeCharacter(font, s[i]); } return; } void display() { glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); glColor3f(0,.87, .68); glutWireTeapot(0.25); glFlush(); return; } void reset() { Left = -1; Right = 1; Bottom = -1; Top = 1; Near = -1; Far = 1; return; } void setup() { reset(); glClearColor(1,1,1,1); return; } void ReportFromGL(){ GLfloat matrix[16]; int i,j; glGetFloatv(GL_PROJECTION_MATRIX, matrix); cout << "The matrix from OpenGL is " << endl; for(i=0;i<4;i++) { for (j=0;j<4;j++) { cout << setw(10) << matrix[i+j*4]; } cout << endl; } return; } void ReportFromMatrix(){ GLfloat sx, sy, sz; GLfloat tx, ty, tz; sx = 2.0/(Right-Left); sy = 2.0/(Top - Bottom); sz = 2.0/(Near - Far); tx = -(Right+Left)/(Right-Left); ty = -(Top+Bottom)/(Top-Bottom); tz = -(Far+Near)/(Far-Near); cout << endl ; cout << "The derived matrix is " << endl; cout << setw(10) << sx << setw(10) << 0 << setw(10) << 0 << setw(10) << tx << endl; cout << setw(10) << 0 << setw(10) << sy << setw(10) << 0 << setw(10) << ty << endl; cout << setw(10) << 0 << setw(10) << 0 << setw(10) << sz << setw(10) << tz << endl; cout << setw(10) << 0 << setw(10) << 0 << setw(10) << 0 << setw(10) << 1 << endl; } void Ortho() { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(Left, Right, Bottom, Top, Near, Far); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); cout << endl ; cout << "The box is now " << endl; cout << "\tLeft = " << Left << endl; cout << "\tRight = " << Right << endl; cout << "\tBottom = " << Bottom << endl; cout << "\tTop = " << Top << endl; cout << "\tNear = " << Near << endl; cout << "\tFar = " << Far << endl; cout << endl; ReportFromGL(); ReportFromMatrix(); return; } void resize(int w, int h) { glViewport(0, 0, w, h); Ortho(); return; } void keypress (unsigned char key, int x, int y) { const GLfloat INCREMENT = 0.5; switch(key) { case 'l': Left -=INCREMENT; Ortho(); break; case 'L': Left +=INCREMENT; Ortho(); break; case 'r': Right -=INCREMENT; Ortho(); break; case 'R': Right +=INCREMENT; Ortho(); break; case 't': Top -=INCREMENT; Ortho(); break; case 'T': Top +=INCREMENT; Ortho(); break; case 'b': Bottom -=INCREMENT; Ortho(); break; case 'B': Bottom +=INCREMENT; Ortho(); break; case 'n': Near -=INCREMENT; Ortho(); break; case 'N': Near +=INCREMENT; Ortho(); break; case 'f': Far -=INCREMENT; Ortho(); break; case 'F': Far +=INCREMENT; Ortho(); break; case 'q': exit(0); } glutPostRedisplay(); return; } int main(int argc, char * argv[]) { reset(); glutInit(&argc, argv); glutInitContextVersion(3, 0); glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA); glutInitWindowSize(500, 500); glutCreateWindow("A Demo"); glutDisplayFunc(display); glutReshapeFunc(resize); glutKeyboardFunc(keypress); glewExperimental = GL_TRUE; glewInit(); setup(); glutMainLoop(); }