1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<glut.h>

GLfloat sunr = 0.0f;
GLfloat earthr = 0.0f;
GLfloat moonr = 0.0f;
GLfloat times = 5.0f;
GLfloat year = 0.0f;
GLfloat month = 0.0f;

void init()
{
    glClearColor(0,0,0,1);
    glShadeModel (GL_FLAT);
}

void timer(int p)
{
    sunr = sunr + 0.037f * times;
    earthr = earthr + 1.0f * times;
    moonr = moonr + 0.33f * times;
    year = year + 0.0027397 * times;
    month = month + 0.0366 * times;
    if(sunr > 360)
        sunr -= 360;
    if(earthr > 360)
        earthr -= 360;
    if(moonr > 360)
        moonr -= 360;
    if(year > 360)
        year -= 360;
    if(month > 360)
        month -= 360;
    glutPostRedisplay();
    glutTimerFunc(33,timer,1);
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    //Draw Start
    glColor3f(1.0,0.0,0.0);
    glPushMatrix();
        glRotatef(sunr,0.0f,0.0f,1.0f);//Sun Rotate
        glutWireSphere(2.0f,7,7);
    glPopMatrix();
    //Sun Done

    //Draw Start
    glPushMatrix();
        glColor3f(0.0,0.0,1.0);
        glRotatef(year,0.0f,0.0f,1.0f);//Revolution
        glTranslatef(7.0f,0.0f,0.0f);//Distance from Sun to Earth
        glRotatef(23.0f,0.0f,1.0f,0.0f);//Earth Axis
        glRotatef(earthr,0.0f,0.0f,1.0f);//Rotation
        glutWireSphere(1.0f,7,7);//Draw Earth
    //Earth Done
        glRotatef(-earthr,0.0f,0.0f,1.0f);//Anti-Rotation
        glRotatef(-23.0f,0.0f,1.0f,0.0f);//Reverse Earth Axis
    //Moon Start
        glColor3f(1.0f,1.0f,1.0f);
        glRotatef(month,0.0f,0.0f,1.0f);//Revolution
        glTranslatef(1.5f,0.0f,0.0f);//Distance from Earth to Moon
        glRotatef(moonr,0.0f,0.0f,1.0f);//Rotation
        glutWireSphere(0.3f,7,7);//Draw Moon
    glPopMatrix();
    //Moon Done
    glFlush();
}

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, 100.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt (0.0, 20.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE| GLUT_RGBA);
    glutInitWindowSize (600, 600);
    glutInitWindowPosition(100,100);      
    glutCreateWindow("Solar System");  
    init();
    glutDisplayFunc(display);
    glutTimerFunc(33, timer, 1);
    glutReshapeFunc(reshape);
    glutMainLoop();      
    return 0;
}

void reshape是照抄的,还没研究明白。星球比例肯定不会。公转轨道目前是圆周。转速比例基本对的。times变量为时间比例设置,越大,时间过得越快。