#include <windows.h>
#include <GL/glut.h>
#include <winbase.h>
static int sun_rotation;
static int earth_rotation, earth_revolution, moon_rotation, moon_revolution;
static int mars_rotation, mars_revolution;
static int venus_rotation, venus_revolution;
static GLfloat camera_x = 8.0, camera_y, camera_z;
static GLfloat camera_obj_x, camera_obj_y, camera_obj_z;
static GLfloat camera_step = 0.5;
void Init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void Display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 0.0, 0.0);
glPushMatrix();
glRotatef((GLfloat)sun_rotation, 0.0, -1.0, 2.0);
glRotatef(30.0, 1.0, 0.0, 0.0); // angle of the sun's axis
glutWireSphere(1.0, 20, 16);
glPopMatrix();
glPushMatrix();
glColor3f(0.0, 0.0, 1.0);
glRotatef((GLfloat)earth_revolution, 0.0, 0.0, 1.0);
glTranslatef(4.0, 0.0, 0.0); // distance between earth and sun
glPushMatrix();
glRotatef((GLfloat)earth_rotation, 0.0, -1.0, 2.0);
glRotatef(30, 1.0, 0.0, 0.0); // angle of the earth's axis
glutWireSphere(0.2, 10, 8);
glPopMatrix();
glColor3f(1.0, 1.0, 1.0);
glRotatef((GLfloat)moon_revolution, 0.0, 1.0, 1.0);
glTranslatef(0.5, 0.0, 0.0); // distance between earth and moon
glutWireSphere(0.05, 5, 4);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 0.5, 0.2);
glRotatef((GLfloat)mars_revolution, 0.0, 0.0, 1.0);
glTranslatef(6.0, 0.0, 0.0); // distance between sun and mars
glRotatef((GLfloat)mars_rotation, 0.0, -1.0, 1.0);
glRotatef(45.0, 1.0, 0.0, 0.0);
glutWireSphere(0.15, 8, 6);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 1.0, 0.2);
glRotatef((GLfloat)venus_revolution, 0.0, 0.0, 1.0);
glTranslatef(3.0, 0, 0);
glRotatef((GLfloat)venus_rotation, 0.0, 0.0, 1.0);
glutWireSphere(0.2, 9, 7);
glPopMatrix();
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(camera_x, camera_y, camera_z, camera_obj_x, camera_obj_y, camera_obj_z, 0.0, 0.0, 1.0);
}
void Keyboard(unsigned char key,int x,int y)
{
switch (key)
{
case 'w':
camera_x -= camera_step;
glLoadIdentity();
gluLookAt(camera_x, camera_y, camera_z, camera_obj_x, camera_obj_y, camera_obj_z, 0.0, 0.0, 1.0);
glutPostRedisplay();
break;
case 's':
camera_x += camera_step;
glLoadIdentity();
gluLookAt(camera_x, camera_y, camera_z, camera_obj_x, camera_obj_y, camera_obj_z, 0.0, 0.0, 1.0);
glutPostRedisplay();
break;
case 'a':
camera_obj_y -= camera_step;
glLoadIdentity();
gluLookAt(camera_x, camera_y, camera_z, camera_obj_x, camera_obj_y, camera_obj_z, 0.0, 0.0, 1.0);
glutPostRedisplay();
break;
case 'd':
camera_obj_y += camera_step;
glLoadIdentity();
gluLookAt(camera_x, camera_y, camera_z, camera_obj_x, camera_obj_y, camera_obj_z, 0.0, 0.0, 1.0);
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}
void SolarDisplay(void)
{
sun_rotation = (sun_rotation + 1) % 360;
earth_rotation = (earth_rotation + 8) % 360;
earth_revolution = (earth_revolution + 4) % 360;
moon_revolution = (moon_revolution + 9) % 360;
mars_revolution = (mars_revolution + 3) % 360;
mars_rotation = (venus_rotation + 10) % 360;
venus_revolution = (venus_revolution - 4) % 360;
venus_rotation = (venus_rotation + 8) % 360;
glutPostRedisplay();
Sleep(100);
}
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);
glutReshapeFunc(Reshape);
glutKeyboardFunc(Keyboard);
glutIdleFunc(SolarDisplay);
glutMainLoop();
return 0;
}