计算机图形学(OpenGL技术)太阳系动画
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
#include
float fEarth = 2.0f;
float fMood = 24.0f;
void Init()
{
glEnable(GL_DEPTH_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 0.8f);
}
void Display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//画太阳
glTranslated(0.0f, 0.0f, -500.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glutSolidSphere(50,20,20);
//画地球
glColor3f(0.0f, 0.0f, 1.0f);
glRotatef(23.27, 0.0f, 0.0f, 1.0f);
glRotatef(fEarth,0.0f,1.0f,0.0f);
glTranslated(200.0f, 0.0f, 0.0f);
glutSolidSphere(20.0, 10, 16);
//画月球
glPopMatrix();
glPopMatrix();
glRotatef(6.0, 1.0f, 1.0f, 1.0f);
glRotatef(fMood, 0.0f, 1.0f, 0.0f);
glTranslated(30.0f, 0.0f, 0.0f);
glColor3f(1.0f, 1.0f, 0.0f);
glutSolidSphere(5, 20, 20);
glLoadIdentity();
glFlush();
glutSwapBuffers();
}
void myIdle()
{
fEarth += 0.03;
if (fEarth > 360.0f)
fEarth = 0.2f;
fMood += 0.24f;
if (fMood > 360.0f)
fMood = 24.0f;
Display();
}
void reshape(int w,int h)
{
if (h == 0)
h = 0;
glViewport(0, 0, w, h); //设置视区大小
glMatrixMode(GL_PROJECTION); //
glLoadIdentity();
gluPerspective(45.0f,w/h,1.0f,1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE| GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("太阳系动画");
glutDisplayFunc(Display);
glutReshapeFunc(reshape);
glutIdleFunc(&myIdle);
Init();
glutMainLoop();
return 0;
}