#include <windows.h>
#include <GL/glut.h>
GLdouble speed=0.1,day,month,year,solarcycle;
//月亮、地球、太阳的半径
GLfloat mr=0.15,er=0.3,sr=0.4;
//月亮、地球的公转半径
GLfloat mrr=0.6,err=2;
void init()
{
glClearColor(0,0,0.1,1);
glEnable(GL_POINT_SMOOTH|GL_LINE_SMOOTH|GL_POLYGON_SMOOTH);
}
void keyboard(GLubyte key,GLint x,GLint y)
{
if(key==27) exit(0);
}
void timer(GLint millis)
{
day+=speed*360/0.997;
month+=speed*360/29.5;
year+=speed*360/365.2475;
solarcycle+=speed*360/27.5;
glutPostRedisplay();
glutTimerFunc(millis,timer,millis);
}
void reshape(GLint w,GLint h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30,(GLfloat)w/h,1,1000);
glTranslatef(0,0,-8);
glRotatef(30,1,0,0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void light(GLfloat r)
{
GLfloat pos[][4]=
{
{r,0,0,1},{-r,0,0,1},{0,r,0,1},
{0,-r,0,1},{0,0,r,1},{0,0,-r,1}
};
GLfloat params[]={0.9,0.9,0.9,1};
for(int i=0;i<6;i++)
{
glLightfv(GL_LIGHT0+i,GL_POSITION,pos[i]);
glLightfv(GL_LIGHT0+i,GL_DIFFUSE,params);
glEnable(GL_LIGHT0+i);
}
}
void material(GLfloat r,GLfloat g,GLfloat b)
{
GLfloat params[]={r,g,b,1};
glEnable(GL_LIGHTING);
glMaterialfv(GL_FRONT,GL_DIFFUSE,params);
}
void color(GLfloat r,GLfloat g,GLfloat b)
{
glDisable(GL_LIGHTING);
glColor3f(r,g,b);
}
// 经纬线
void wire(GLfloat r)
{
color(0.5,0.5,0.5);
glutWireSphere(1.005*r,24,12);
}
// 自转轴
void axle(GLfloat r)
{
color(0,0,0);
glBegin(GL_LINE);
glVertex3f(0,0,-r*1.2);
glVertex3f(0,0,r*1.2);
glEnd();
}
void sun(GLfloat r)
{
glPushMatrix();
glRotatef(90,-1,0,0);
color(0.9,0.1,0.1);
glutSolidSphere(r,24,12);
wire(r);
glPopMatrix();
}
void earth(GLfloat r)
{
glPushMatrix();
glRotatef(90,-1,0,0);
material(0.1,0.1,0.9);
glutSolidSphere(r,24,12);
wire(r);
glPopMatrix();
}
void moon(GLfloat r)
{
glPushMatrix();
glRotatef(90,-1,0,0);
material(0.75,0.75,0.1);
glutSolidSphere(r,24,12);
wire(r);
glPopMatrix();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// 太阳光照、太阳自转
light(sr);
glPushMatrix();
glRotatef(solarcycle,0,1,0);
sun(sr);
glPopMatrix();
// 地球公转
glRotatef(year,0,1,0);
glTranslatef(err,0,0);
glRotatef(-year,0,1,0);
glRotatef(-23.5,0,0,1);
// 地球自转
glPushMatrix();
glRotatef(day,0,1,0);
earth(er);
glPopMatrix();
// 月球公转
glRotatef(month,0,1,0);
glTranslatef(mrr,0,0);
moon(mr);
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
glutCreateWindow("Galaxy");
glutFullScreen();
glutKeyboardFunc(keyboard);
init();
glutTimerFunc(25,timer,25);
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
}
OpenGL 真实感日地月系统
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 【蝴蝶效应】 蝴蝶效应:上个世纪70年代,美国一个名叫洛伦兹的气象学家在解释空气系统理论时说,亚马逊雨林一只蝴蝶...
- 前段时间期末考,忙里偷闲锱铢积累读完了钱钟书的唯一长篇著作《围城》,读这本书让我有太多与往常不一样的体验,常常笑...
- 尊敬的新京报社领导,您好: 我叫封卫东,家住江苏省泰兴市黄桥镇横巷村4组,我的手机号是18952671212,今天...