收藏 分销(赏)

太阳系行星运行模拟C++程序语言.doc

上传人:二*** 文档编号:4514360 上传时间:2024-09-26 格式:DOC 页数:11 大小:304KB
下载 相关 举报
太阳系行星运行模拟C++程序语言.doc_第1页
第1页 / 共11页
亲,该文档总共11页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

1、. .太阳系行星运行模拟组员: 2011年9月2日概述太阳系 (Solar System)就是我们现在所在的恒星系统。它是以太阳为中心,和所有受到太阳引力约束的天体的集合体:8颗行星冥王星已被开除、至少165颗已知的卫星,和数以亿计的太阳系小天体。这些小天体包括小行星、柯伊伯带的天体、彗星和星际尘埃。广义上,太阳系的领域包括太阳、4颗像地球的内行星、由许多小岩石组成的小行星带、4颗充满气体的巨大外行星、充满冰冻小岩石、被称为柯伊伯带的第二个小天体区。在柯伊伯带之外还有黄道离散盘面、太阳圈和依然属于假设的奥尔特云。模拟太阳系不仅仅是完成作业,也能让我们更近一步的了解太阳系,拓广知识面,对行星的运

2、行有基本的感性和理性感知。增加我们对宇宙探索的渴望,可能培养出又一批天文学家。 OpenGL(全写Open Graphics Library)是个定义了一个跨编程语言、跨平台的编程接口的规格,它用于三维图象(二维的亦可)。OpenGL是个专业的图形程序接口,是一个功能强大,调用方便的底层图形库。OpenGL是一个开放的三维图形软件包,它独立于窗口系统和操作系统,以它为基础开发的应用程序可以十分方便地在各种平台间移植;OpenGL可以与Visual C+紧密接口,便于实现机械手的有关计算和图形算法,可保证算法的正确性和可靠性;OpenGL使用简便,效率高。它具有七大功能: 1.建模:OpenGL

3、图形库除了提供基本的点、线、多边形的绘制函数外,还提供了复杂的三维物体(球、锥、多面体、茶壶等)以及复杂曲线和曲面绘制函数。 2.变换:OpenGL图形库的变换包括基本变换和投影变换。基本变换有平移、旋转、变比镜像四种变换,投影变换有平行投影(又称正射投影)和透视投 影两种变换。其变换方法有利于减少算法的运行时间,提高三维图形的显示速度。 3.颜色模式设置:OpenGL颜色模式有两种,即RGBA模式和颜色索引(Color Index)。 4.光照和材质和镜面光(Specular Light)。材质是用光反射率来表示。场景(Scene)中物体最终反映到人眼的颜色是光的红绿蓝分量与材质红绿蓝分量的

4、反射率相乘后形成的颜色。 5:纹理映射(Texture Mapping)。利用OpenGL纹理映射功能可以十分逼真地表达物体表面细节。 6:位图显示和图象增强图象功能除了基本的拷贝和像素读写外,还提供融合(Blending)、反走样(Antialiasing)和雾(fog)的特殊图象效果处理。以上三条可使被仿真物更具真实感,增强图形显示的效果。 7:双缓存动画(Double Buffering)双缓存即前台缓存和后台缓存,简言之,后台缓存计算场景、生成画面,前台缓存显示后台缓存已画好的画面。 此外,利用OpenGL还能实现深度暗示(Depth Cue)、运动模糊(Motion Blur)等特殊

5、效果。从而实现了消隐算法。具体实现利用VC6.0和OpenGL绘制太阳系模拟,以最简单的方式,新建一个win32 console application,加入以下代码:#include #include #include #include gl/glut.h/行星GLfloat rot0 = 30.0;GLfloat rot1 = 0.0;GLfloat rot2 = 0.0;GLfloat rot3 = 0.0;GLfloat rot4 = 0.0;GLfloat rot5 = 0.0;GLfloat rot6 = 0.0;GLfloat rot7 = 0.0;GLfloat rot8 =

6、0.0;/卫星GLfloat rot9 = 0.0;GLfloat rot10 = 0.0;GLfloat rot11 = 0.0;void init()glClearColor(0.0,0.0,0.0,0.0);glClearDepth(1.0);glShadeModel(GL_FLAT);void display()glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glColor3f(1.0,1.0,1.0);glLoadIdentity();/gluLookAt(0, 10, 10, 0,0,0,0, 1,0);/glRotatef(4

7、5.0,0.0,0.0,1.0);glTranslatef(0.0,0.0,-20.0);glRotatef(90.0,1.0,0.0,0);glPushMatrix();/绘制太阳glColor3f(1.0,0.0,0.0);glutSolidSphere(2.0,32,32);/绘制地球glPushMatrix();glColor3f(0.0,0.0,1.0);/ 绘制辅助轨道glRotatef(90,1.0,0,0.0);glutSolidTorus(0.02, 5.0, 10, 64);glRotatef(-90,1.0,0,0.0);/ 设置地球公转速度glRotatef(rot0,

8、0.0,1.0,0.0);/ 设置地球半径glTranslatef(5.0,0.0,0.0);/ 设置地球自转速度/glRotatef(rot1,0.0,1.0,0.0);/ 绘制地球glutSolidSphere(0.4,32,32);/ 绘制地球的卫星-月亮glColor3f(0.5,0.6,0.5);/ 抵消地球自转影响/glRotatef(-rot1,0.0,1.0,0.0);/ 绘制辅助轨道glRotatef(90,1.0,0,0.0);glRotatef(-90,1.0,0,0.0);/ 设置月亮公转速度glRotatef(rot9,0.0,1.0,0.0);/ 设置月亮公转半径g

9、lTranslatef(0.6,0.0,0.0);/ 绘制月亮glutSolidSphere(0.1,10,8);glPopMatrix();/ 绘制水星glPushMatrix();glColor3f(0.0,1.0,1.0);/ 绘制辅助轨道glRotatef(90,1.0,0,0.0);glutSolidTorus(0.02, 2.5, 10, 64);glRotatef(-90,1.0,0,0.0);/ 设置水星公转速度glRotatef(rot1,0.0,1.0,0.0);/ 设置水星公转半径glTranslatef(2.5,0.0,0.0);/ 设置水星自传glRotatef(ro

10、t3,0.0,1.0,0.0);/ 绘制水星glutSolidSphere(0.2,32,32);glPopMatrix();/绘制金星glPushMatrix();glColor3f(0.0,1.0,0.0);/绘制辅助轨道glRotatef(90,1.0,0,0.0);glutSolidTorus(0.02, 3.4, 10, 64);glRotatef(-90,1.0,0,0.0);/ 设置金星公转速度glRotatef(rot2,0.0,3.0,0.0);/ 设置金星公转半径glTranslatef(3.4,0.0,0.0);/ 设置金星自传glRotatef(rot0,0.0,1.0

11、,0.0);/ 绘制金星glutSolidSphere(0.3,32,32);glPopMatrix();/绘制火星glPushMatrix();glColor3f(1.0,0.0,0.0);/绘制辅助轨道glRotatef(90,1.0,0,0.0);glutSolidTorus(0.02, 6.6, 10, 64);glRotatef(-90,1.0,0,0.0);/ 设置火星公转速度glRotatef(rot3,0.0,4.0,0.0);/ 设置火星公转半径glTranslatef(6.6,0.0,0.0);/ 设置火星自传glRotatef(rot7,0.0,2.0,0.0);/ 绘制

12、火星glutSolidSphere(0.5,32,32);glPopMatrix();/绘制木星glPushMatrix();glColor3f(2.0,0.1,1.0);/绘制辅助轨道glRotatef(90,1.0,0,0.0);glutSolidTorus(0.02, 8.5, 10, 64);glRotatef(-90,1.0,0,0.0);/ 设置木星公转速度glRotatef(rot4,0.0,0.4,0.0);/ 设置木星公转半径glTranslatef(8.5,0.0,0.0);/ 设置木星自传glRotatef(rot3,0.0,0.3,0.0);/ 绘制木星glutSoli

13、dSphere(1.0,32,32);/ 绘制木星卫星-木卫1glColor3f(0.4,0.3,0.5);/ 抵消地球自转影响/glRotatef(-rot1,0.0,1.0,0.0);/ 绘制辅助轨道glRotatef(90,1.0,0,0.0);glRotatef(-90,1.0,0,0.0);/ 设置木卫1公转速度glRotatef(rot10,0.0,1.0,0.0);/ 设置木卫1公转半径glTranslatef(1.3,0.0,0.0);/ 绘制木卫1glutSolidSphere(0.1,10,8);/ 绘制木星卫星-木卫2glColor3f(0.5f,0.5f,0.5f);/

14、 抵消地球自转影响/glRotatef(-rot1,0.0,1.0,0.0);/ 绘制辅助轨道glRotatef(90,1.0,0,0.0);glRotatef(-90,1.0,0,0.0);/ 设置木卫2公转速度glRotatef(rot11,0.0,1.0,0.0);/ 设置木卫2公转半径glTranslatef(1.2,0.0,0.0);/ 绘制木卫2glutSolidSphere(0.08,10,8);glPopMatrix();/绘制土星glPushMatrix();glColor3f(1.0f, 1.0f, 0.0f);/绘制辅助轨道glRotatef(90,1.0,0,0.0);

15、glutSolidTorus(0.02, 12.5, 10, 64);glRotatef(-90,1.0,0,0.0);/ 设置土星公转速度glRotatef(rot5,0.0,0.4,0.0);/ 设置土星公转半径glTranslatef(12.5,0.0,0.0);/ 设置土星自传glRotatef(rot3,0.0,0.3,0.0);/ 绘制土星glutSolidSphere(0.85,32,32);/绘制土星光环glRotatef(90,1.0,0,0.0);glutSolidTorus(0.1, 1.25, 10, 64);glRotatef(-90,1.0,0,0.0);glRot

16、atef(90,1.0,0,0.0);glutSolidTorus(0.07, 1.65, 10, 64);glRotatef(-90,1.0,0,0.0);glPopMatrix();glPopMatrix();/绘制天王星glPushMatrix();glColor3f(0.0f, 1.0f, 1.0f);/绘制辅助轨道glRotatef(90,1.0,0,0.0);glutSolidTorus(0.02, 15.5, 10, 64);glRotatef(-90,1.0,0,0.0);/ 设置天王星公转速度glRotatef(rot6,0.0,0.4,0.0);/ 设置天王星公转半径glT

17、ranslatef(15.5,0.0,0.0);/ 设置天王星自传glRotatef(rot1,0.0,0.3,0.0);/ 绘制天王星glutSolidSphere(0.15,32,32);glPopMatrix();/绘制海王星glPushMatrix();glColor3f(0.0f, 0.0, 8.0f);/绘制辅助轨道glRotatef(90,1.0,0,0.0);glutSolidTorus(0.02, 17.5, 10, 64);glRotatef(-90,1.0,0,0.0);/ 设置海王星公转速度glRotatef(rot7,0.0,0.4,0.0);/ 设置海王星公转半径g

18、lTranslatef(17.5,0.0,0.0);/ 设置海王星自传glRotatef(rot3,0.0,0.3,0.0);/ 绘制海王星glutSolidSphere(0.145,32,32);glPopMatrix();/绘制冥王星glPushMatrix();glColor3f(0.5f, 0.5f, 0.5f);/绘制辅助轨道glRotatef(90,1.0,0,0.0);glutSolidTorus(0.02, 19.5, 10, 64);glRotatef(-90,1.0,0,0.0);/ 设置冥王星公转速度glRotatef(rot8,0.0,0.4,0.0);/ 设置冥王星公

19、转半径glTranslatef(19.5,0.0,0.0);/ 设置冥王星自传glRotatef(rot2,0.0,0.3,0.0);/ 绘制冥王星glutSolidSphere(0.145,32,32);glPopMatrix();glutSwapBuffers();glFlush();void idle()rot0+=0.1;if(rot0=360.0)rot0-=360.0;rot1+=0.416;if(rot1=360.0)rot1-=360.0;rot2+=0.1631;if(rot2=360.0)rot2-=360.0;rot3+=0.053;if(rot3=360.0)rot3-

20、=360.0;rot4+=0.0083;if(rot4=360.0)rot4-=360.0;rot5+=0.0034;if(rot5=360.0)rot5-=360.0;rot6+=0.00119;if(rot6=360.0)rot6-=360.0;rot7+=0.00069;if(rot7=360.0)rot7-=360.0;rot8+=0.0008;if(rot8=360.0)rot8-=360.0;rot9+=1.0;if(rot9=360.0)rot9-=360.0;rot10+=0.005;if(rot10=360.0)rot10-=360.0;rot11+=0.01;if(rot1

21、1=360.0)rot11-=360.0;glutPostRedisplay();void reshape(int w,int h)glViewport(0,0,(GLsizei)w,(GLsizei)h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(60.0,(GLdouble)w/(GLdouble)h,1.0,100.0);glMatrixMode(GL_MODELVIEW);void main(int argc,char *argv)glutInit(&argc,argv);glutInitDisplayMod

22、e(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);glutInitWindowSize(600,400);glutInitWindowPosition(500,500);glutCreateWindow(planet sim);init();glutDisplayFunc(display);glutReshapeFunc(reshape);glutIdleFunc(idle);glutMainLoop();return;运行结果总结本次设计是基于VC+6.0平台和OpenGL,进行太阳系九大行星的运行模拟,虽说取得了比较好的效果,但是在某些方面还是不足:1. 运行的轨道是简化的圆形轨道,不是实际中的椭圆轨道,没遵循开普勒三大定律;2. 视角固定,没有实现任意视角观察;3. 由于对OpenGL不熟悉,时间不短,大部分代码是在参考别人的基础上写出;4. 没有实现纹理光照等效果。通过这次设计,对OpenGL有了一定的了解,对计算机图形学的知识也有了更深一步的理解,发现自己的不足,有利于以后的学习与工作。5. 参考书目1. 计算机图形学 Donald Hearn M.Pauline Baker 著 电子工业2.OpenGl编程基础 安吉尔著 清华大学. .word.

展开阅读全文
部分上传会员的收益排行 01、路***(¥15400+),02、曲****(¥15300+),
03、wei****016(¥13200+),04、大***流(¥12600+),
05、Fis****915(¥4200+),06、h****i(¥4100+),
07、Q**(¥3400+),08、自******点(¥2400+),
09、h*****x(¥1400+),10、c****e(¥1100+),
11、be*****ha(¥800+),12、13********8(¥800+)。
相似文档                                   自信AI助手自信AI助手
搜索标签

当前位置:首页 > 通信科技 > 开发语言

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服