#include #include #include #include #include #include using namespace std; void Projection(); // moving the objects about GLfloat Sun_X_POS, Sun_Y_POS, Sun_Z_POS, Sun_Radius, Sun_Angle1, Sun_Angle2, Sun_Rotation, Planet_X_POS, Planet_Z_POS, Planet_Radius, Planet_Angle, Planet_Rotation; // moving the camera about GLfloat EyeX, EyeY, EyeZ; // controling the projection GLfloat FOV, ASPECT; void DisplayString(void * font, string s) { int i; for(i=0; i < s.size(); i++) { glutStrokeCharacter(font, s[i]); } return; } void display() { stringstream s; s << "Current Position (" << EyeX << ", " << EyeY << ", " << EyeZ << ")"; glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); glPushMatrix(); glTranslatef(-1.3,1.3,-3); glScalef(0.001, 0.001, 0.001); DisplayString(GLUT_STROKE_ROMAN,s.str()); glPopMatrix(); gluLookAt(EyeX,EyeY,EyeZ,0,0,0,0,1,0); glColor3f(1,0,0); // put the sun up in the sky glPushMatrix(); glTranslatef(Sun_X_POS,Sun_Y_POS,Sun_Z_POS); // spin the sun. glRotatef(Sun_Rotation, 0,1,0); // I don't like the original orientation of the sun, so spin it. glRotatef(60, 1,1,0); glutWireSphere(2,12,12); // put a planet near the sun glTranslatef(Planet_X_POS,0,Planet_Z_POS); glRotatef(Planet_Rotation, 0,1,0); glutWireSphere(.5,5,5); glPopMatrix(); glColor3f(0.2, 0.8, 0.5); glutWireTeapot(.25); glColor3f(0.7, 0.2, 0.9); glutWireCube(10); glColor3f(0.7, 0.2, 0.9); glScalef(.001, .001, .001); glPushMatrix(); DisplayString(GLUT_STROKE_ROMAN,"This is the X axis"); glPopMatrix(); glPushMatrix(); glRotatef(90,0,0,1); DisplayString(GLUT_STROKE_ROMAN,"This is the Y axis"); glPopMatrix(); glFlush(); return; } void reset() { Sun_Rotation = 0; Sun_Angle1 = 90; Sun_Angle2 = 90; Sun_Radius = 8; Sun_X_POS = Sun_Radius*cos(Sun_Angle1)*sin(Sun_Angle2); Sun_Y_POS = Sun_Radius*sin(Sun_Angle1)*sin(Sun_Angle2); Sun_Z_POS = Sun_Radius*cos(Sun_Angle2); Planet_Angle = 0; Planet_Radius = 3; Planet_X_POS = Planet_Radius * cos(Planet_Angle); Planet_Z_POS = Planet_Radius * sin(Planet_Angle); Planet_Rotation = 0; EyeX = 0; EyeY = 0; EyeZ = 10; FOV = 60; ASPECT = 1; Projection(); return; } void idle() { Sun_X_POS = Sun_Radius*cos(Sun_Angle1)*sin(Sun_Angle2); Sun_Y_POS = Sun_Radius*sin(Sun_Angle1)*sin(Sun_Angle2); Sun_Z_POS = Sun_Radius*cos(Sun_Angle2); Sun_Angle1 += .001; Sun_Angle2 += .002; Sun_Rotation += 0.1; if (Sun_Rotation > 360) { Sun_Rotation -= 360; } Planet_Angle += 0.01; Planet_X_POS = Planet_Radius*cos(Planet_Angle); Planet_Z_POS = Planet_Radius*sin(Planet_Angle); Planet_Rotation += 0.5; if (Planet_Rotation > 360) { Planet_Rotation -= 360; } glutPostRedisplay(); return; } void setup() { reset(); glClearColor(1,1,1,1); return; } void Projection() { cout << "Aspect " << ASPECT << endl; cout << "FOV " << FOV << endl; cout << endl; glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(FOV,ASPECT,1, 30); //glFrustum(-1.0, 1.0, -1.0, 1.0, 1, 20); glMatrixMode(GL_MODELVIEW); return; } void resize(int w, int h) { glViewport(0, 0, w, h); Projection(); glLoadIdentity(); return; } void keypress (unsigned char key, int x, int y) { GLfloat increment = 0.25; switch(key) { case 'a': ASPECT -= 0.1; Projection(); break; case 'A': ASPECT += 0.1; Projection(); break; case 'f': FOV -= 2; Projection(); break; case 'F': FOV += 2; Projection(); break; case 'i': EyeY += increment; break; case 'n': EyeY -= increment; break; case 'h': EyeX -= increment; break; case 'j': EyeZ -= increment; break; case 'k': EyeZ += increment; break; case 'l': EyeX += increment; break; case 'r': reset(); 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); glutIdleFunc(idle); glewExperimental = GL_TRUE; glewInit(); setup(); glutMainLoop(); }