[cc lang=’c’]
#include
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;
}
[/cc]
void reshape是照抄的,还没研究明白。星球比例肯定不会。公转轨道目前是圆周。转速比例基本对的。times变量为时间比例设置,越大,时间过得越快。
近期评论