#include #include #include static int year = 0, month = 0; static double pi = 3.1415926; void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glColor3f (1.0, 0.0, 0.0); glutWireSphere(1.0, 50, 50); /* draw sun */ glLoadIdentity(); glRotatef((GLfloat) year, 0.0, 1.0, 0.0); glTranslatef (2.0, 0.0, 0.0); glColor3f(0.0,0.0,1.0); glutWireSphere(0.2, 20, 20); /* draw earth */ glLoadIdentity(); glTranslatef(2.0*cos(year*2*pi/360),0, 2.0*sin(-year*2*pi/360)); glRotatef((GLfloat) month, 0.0, 1.0, 0.0); glTranslatef(-2.0*cos(year*2*pi/360),0, -2.0*sin(-year*2*pi/360)); glRotatef((GLfloat) year, 0.0, 1.0, 0.0); glTranslatef (2.5, 0.0, 0.0); glColor3f(0.0,1.0,0.0); glutWireSphere(0.1, 10, 10); /* draw moon */ glFlush(); glutSwapBuffers(); } void myReshape(GLsizei w, GLsizei h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(90.0, (GLfloat) w/(GLfloat) h, 1.0, 40.0); gluLookAt(0,2,2.5, 0,0,0, 0,1,0); } void keyboard(unsigned char key, int x, int y){ switch(key) {case 'd': year = (year + 1) % 360; month = (month + 5) % 360; break; } } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA); glutInitWindowPosition(0, 0); glutInitWindowSize(800, 800); glutCreateWindow("A solar system"); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutIdleFunc(display); glutKeyboardFunc(keyboard); glutMainLoop(); }