/* Simple Solar system (C) Bedrich Benes 2003 */ #include #include #include #include #define R 5 static GLfloat alpha,beta; void Init(void) { glClearColor(0,0,0,0); } void Display(void) { } void Idle(void) { static GLfloat time; glClear (GL_COLOR_BUFFER_BIT); glPushMatrix(); glColor3ub(255, 255, 0.0); //Sun glutWireSphere(0.7,20,20); glColor3ub(0,255,0); //Earth glRotatef(time/180, 0, 1, 0); glTranslatef(2,0,0); glRotatef(time/10.f,0.0,1.0,0.0); glutWireSphere(0.2,10,8); glRotatef(-time/10.f,0.0,1.0,0.0); glRotatef(15,1,0.0,0.0); glRotatef(time/28,0.0,1.0,0.0); glTranslatef(1,0,0); //Moon glRotatef(time/28,0.0,1.0,0.0); glColor3ub(0,255,255); glutWireTeapot(0.1); glPopMatrix(); time +=50; glutSwapBuffers(); } void Reshape (int w, int h) { glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 1.0, 20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0, 0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); } void Kbd(unsigned char key, int x, int y) { switch (key) { case 27: exit(0);break; } } void Spec(GLint key, int xx, int yy) { switch (key) { case GLUT_KEY_UP: beta+=0.1;break; case GLUT_KEY_DOWN: beta-=0.1;break; case GLUT_KEY_LEFT: alpha+=0.1;break; case GLUT_KEY_RIGHT: alpha-=0.1;break; } glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(R*sin(alpha)*cos(beta), R*sin(beta), R*cos(alpha)*cos(beta), 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); printf("alpha=%i, beta=%i\n",alpha,beta); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); Init(); glutDisplayFunc(Display); glutIdleFunc(Idle); glutReshapeFunc(Reshape); glutKeyboardFunc(Kbd); glutSpecialFunc(Spec); glutMainLoop(); return 0; }