图形学综合实验.doc

上传人:文库蛋蛋多 文档编号:2888222 上传时间:2023-03-01 格式:DOC 页数:38 大小:4.92MB
返回 下载 相关 举报
图形学综合实验.doc_第1页
第1页 / 共38页
图形学综合实验.doc_第2页
第2页 / 共38页
图形学综合实验.doc_第3页
第3页 / 共38页
图形学综合实验.doc_第4页
第4页 / 共38页
图形学综合实验.doc_第5页
第5页 / 共38页
点击查看更多>>
资源描述

《图形学综合实验.doc》由会员分享,可在线阅读,更多相关《图形学综合实验.doc(38页珍藏版)》请在三一办公上搜索。

1、期末时上交的图形学实验报告,虽然只得了良,但用心去做了,供大家参考(实验报告+源程序+图片素材): 数学与计算机科学学院实验报告计算机图形综合实验程序名称:Fantasy Solar System程序设计者:*学 号:*所在学院、班级:数学与计算机科学(软件)学院08级综合班电子邮件:fzu_xxs2010指导教师:*完稿日期:二 一 一 年 六 月 十四 日 实验名称:太阳系的模拟 实验内容:1、绘制太阳系8大行星2、显示行星的运动轨道;3、实现8大行星、太空等的纹理映射;4、实现各大行星的公转和自转;5、设置合适的灯光,模拟太阳、月亮、地球、星星等光照效果;6、通过控制摄像机的漫游与环视,

2、实现太阳系的漫游;7、显示各行星的简介资料;8、利用融合、雾化等方法,增强太空的真实感效果;9、设计合理、友好的交互界面。通过鼠标、键盘(包括功能键、组合键)、菜单等,控制各种参数的变化。比如:(1)行星的运转速度、运动轨迹显示的开关;(2)灯光的开启、位置、颜色以及材质;(3)漫游的方向控制;(4)雾化、融合效果的控制。10、其它功能(自定,选做)。 实验环境1.软件环境:(1)操作系统:Windows 7 (2)编译器:Microsoft Visual Studio 20082.硬件环境:(1)主板:技嘉 GA-MA785GT-UT3H (2)CPU:AMD Athlon(tm) II 4

3、 630 Processor (3)内存:Kingston DDR3,2G(4)硬盘:SATA 500G(5)屏幕分辨率:1024768 实验方案与程序设计1.设置初始化函数init()实现对自转、公转速度的初始,字符显示列表的初始,深度信息的设置,视见体的初始,投影矩阵的设置,以及灯光、纹理、材质的设置。2.显示回调函数myDisplay()实现对场景的总体绘制:通过myFog()实现雾化;draw_back_ground()实现背景绘制;draw_path()实现轨道绘制;draw_planet()实现星球的绘制;show_info()则实现了星体信息的显示。3.通过键盘回调函数、功能键回

4、调函数和右键菜单更改各个参数以实现多种特殊效果,如漫游、星体的单独显示、雾的颜色和浓度的更改、自转和公转的速度设置、灯光的开户关闭和颜色的设置、重置场景以及退出程序等。4.详细信息见程序代码及注释。 实验结果与分析(包括功能说明、操作说明)1.通过f键开启和关闭雾化效果;2.通过l(字母L)键开启和关闭太阳光源;3.通过r键重置场景;4.通过q键退出程序;5.通过数字键0到9实现各星体的单独显示和全部显示;6.当单独显示某一星体时,可通过w,s键加快或减缓该星体的自转速度,通过a,d键加快或减缓该星体的公转速度;7.通过z,x,c,v键移动视点的焦点;8.通过四个方向键和Page Up,Pag

5、e Down键移动视点的坐标;9.单击右键弹出右键菜单,各菜单项说明如下:u Path: show moving path表示显示运动轨迹和坐标轴,hide moving path表示隐藏运动轨迹和坐标轴u Rotation:start rotation表示开启自转,stop rotation表示停止自转u Revolution:start revolution表示开启公转,stop revolution表示停止公转u Fog Density: 设置雾的浓度,thin表示小雾,medium表示普通雾,thick大雾u Fog Color:设置雾的颜色(灰色、红色、蓝色、绿色)u Sun Lig

6、ht Color:设置太阳光的颜色,四个可选项依次为白色、红色、绿色、蓝色 实验中遇到的问题及解决方法1.Q:生成的可执行文件在其它计算机上不能运行。S:首先是犯了一个低级错误,就是生成可执行文件时没将debug改为release;其次在没有安装opengl库文件的机子上,是没有glut32.dll的,所以要将可执行文件和glut32.dll一起拷贝到其它计算机上。2.Q:处理纹理的函数auxDIBImageLoadW在生成可执行文件时出错,提示信息为error C2664: “auxDIBImageLoadW”: 不能将参数 1 从“char *”转换为“LPCWSTR”。 S: 这是由于V

7、S2008默认使用Unicode字符,LPCWSTR即是Unicode字符的一种。解决方法为更改工程的属性:项目-Fantasy Solar System属性-配置属性-常规,在其中的字符集选项中把“使用Unicode字符集”更改为“未设置”。3.Q:在实验室的机子上,星体的贴图无法正常显示,出现时隐时现的情况,同时灯光也随之出错。S:视见体的前截面的值设得太小了,将其从0.01改为10,问题解决。之所以在自己宿舍的计算机上可以显示,可能原因是计算能力相对机房的机子强一些故可处理的精度更高一些。4.Q: 在加入了键盘回调函数之后,字符无法正常显示。S: 原因是键盘回调函数本身会将从键盘输入的字

8、符读入程序中,从而使显示字符列表的初始化失败。解决方案为在键盘事件发生前调用传递空串给显示字符函数进行字符显示列表的初始化。5.Q: 启用雾化效果时,显示结果为一大片不透明灰色。S: 最后发现是雾化的起始位置和结束位置问题,将起始和结束位置设为与视见体初始相同,问题解决。同时雾化效果必须有灯光的配合。 实验心得(自我评价)与建议这个Solar System写得好累,1081行的代码敲得都憔悴了。实验提交时限又正好赶上考试,所以上交时间有些迟。虽然有理论课作为基础,但实践时发现各种矩阵之间的相互影响有时搞得程序跑出来的结果莫名其妙,许多次不得不重写所有代码,先后经历了好几个版本的修改。总结前期失

9、败的原因,主要是程序的结构不够清晰,各功能的实现相互交叉,导致耦合度过高,引起许多隐含错误。后面学乖了,首先设计整体的框架,抽象出各个相对独立的部件,再在这些部件内实现功能。同时在启用纹理绘制完对象后立即关闭纹理,以减少潜在的对其它矩阵的影响。这种方式写出来的代码条理清楚,思路清晰则易于查错,经过注释方面的美化之后,不得不说其实挺漂亮的。虽然最终实现的效果没有那么炫丽,但在完成实验的过程中学到了许多理论课上所没有的知识。有时出现的错误自己觉得是毫无道理的,求助Google无果后,自己只能一步一步地调试,一行一行地注释,一个变量一个变量地修改参数,一个位置一个位置地安放函数。最终的结果是错误不是

10、没有道理的,而是自己本身考虑事情不够全面。错误不单是代码方面的,还有自己逻辑上的和系统编译方面等,有些还是openGL自身固有的。面对Bug调试很久没有解决,有时会相当沮丧,甚至觉得自己根本没有能力解决。但静下心来认真查资料,问题是会逐渐明朗化的,所以对自己要有信心。写完这个代码,终于可以脸不红心不跳地说自己已经完成openGL编程入门了。附录一(源程序):/* File: Fantasy Solar System.cpp* Version: 4.0 aleph* Compiler: Visual Studio 2008* Author: s030802435* E-mail: fzu_xxs

11、2010* Function: This is an OpenGL program to imitate solar system* Usage:* #1.Press the key f to turn on or turn off the fog* #2.Press the key l to turn on or turn off the sun light* #3.Press the key r to reset the scene* #4.Press the key q to exit the program.* #5.Press the number button to show pl

12、anet alone:* 0: show all planet* 1: show Sun* 2: show Mercury* 3: show Venus* 4: show Earth and Moon* 5: show Mars* 6: show Jupiter* 7: show Saturn* 8: show Uranus* 9: show Neptune* #6.When you show planet alone, you can change the speed of rotation and revolution:*Press w to increase the speed of r

13、otation*Press s to decrease the speed of rotation*Press d to increase the speed of revolution*Press a to decrease the speed of revolution* #7.Press z, x, c or v to move the focus of view*z-left x-right c-up v-down* #8.the position of view point can be changed by:*up-arrow: towards near*down-arrow: t

14、owards far*left-arrow: towards left*right-arrow: towards right*Page Up key: towards upside*Page Down key: towards downside* #9.Click right button to pop the main menu:*Path: show path or not*Rotation: start or stop rotation *Revolution: start or stop revolution*Fog Density: set the density of fog(th

15、in, medium or thick)*Fog Color: set the color of fog(grey, red, blue or green)*Sun Light Color: set the color of sun light(white, red, green or blue)* Attention: You should set your project dont use Unicode characters* Last modify: 2011-6-14*/* Head File*/#include#include#include#include#include#inc

16、lude/ If fail to find this head file, please try GL/glaux.h/* Macro*/#pragma comment(lib,opengl32.lib)#pragma comment(lib,glu32.lib)#pragma comment(lib,glaux.lib)#pragma comment(lib,GLUT32.LIB)#define TRUE 1#define FALSE 0#define PIE 3.1415926#define MAXTEXTURE 11#define width 1024#define height 512

17、#define MAX_CHAR 128/* Static Global Var*/static GLint show_back_ground=TRUE; / show the background bitmapstatic GLuint textureMAXTEXTURE; / the array of texturesstatic GLint cnt=0; / the index of the texturesstatic GLuint isFirstCall = TRUE; / first call character display liststatic GLuint lists; /

18、 character display listsstatic GLfloat lookx=0.0,looky=100.0,lookz=300.0; / coordinate of viewportstatic GLfloat look_x=0.0,look_y=0.0,look_z=0.0; / focus point of viewportstatic GLint show_planet = 0; / choose the planet to showstatic GLint show_path = TRUE; / show the path of planet movingstatic G

19、Lfloat myTime,myTime_2; / the computer system timestatic GLint rotation_speed9; / adjust the rotation speed of Earthstatic GLint revolution_speed9; / adjust the revolution speed of Earthstatic GLint stop_rotation=FALSE; / stop rotation of planetsstatic GLint stop_revolution=FALSE; / stop revolution

20、of planetsstatic GLfloat sun_ambient = 1.0 , 0.9 , 0.9 , 0.1 ; / ambient part of sun lightstatic GLfloat sun_diffuse = 1.0 , 0.9 , 0.9 , 1.0 ; / diffuse part of sun lightstatic GLfloat sun_specular = 1.0 , 0.9 , 0.9 , 1.0 ; / specular part of sun lightstatic GLfloat sun_position = 0.0 , 0.0 , 0.0 ,

21、1.0 ; / the position of sun lightstatic GLfloat globel_ambient = 0.3 , 0.3 , 0.3 , 1.0 ; / globel lightstatic GLfloat sun_self_emission = 0.8 , 0.8 , 0.8 , 1.0 ; / the surface light of sunstatic GLfloat planet_ambient = 0.01 , 0.01 , 0.01 , 1.0 ; / ambient part of planet surfacestatic GLfloat planet

22、_diffuse = 0.7 , 0.7 , 0.7 , 1.0 ; / diffuse part of planet surfacestatic GLfloat planet_specular = 0.5 , 0.5 , 0.5 , 0.5 ; / specular part of planet surfacestatic GLfloat planet_self_emission = 0.2 , 0.2 , 0.2 , 1.0; / the surface light of planet surfacestatic GLfloat planet_high_light = 120.0; / h

23、ighlights coefficient of planetstatic GLint sun_light_on = TRUE; / the state of sun lightstatic GLint set_fog = TRUE; / start fog effect or notstatic GLfloat fog_density = 10.0; / the density of fogstatic GLfloat fog_color4 = 0.5f,0.5f,0.5f,1.0f; / the color of fogstatic GLUquadricObj* g_text= gluNe

24、wQuadric(); / achieve texture by quadric object /* Function*/*-*/* open bitmap file */*-*/AUX_RGBImageRec *LoadImage(char *Filename) FILE *File=NULL;if(!Filename) return NULL;File = fopen(Filename,r);if(File) fclose(File);return auxDIBImageLoad(Filename);return NULL;/*-*/* load textures */*-*/int Lo

25、adGLTextures(char *Filename) GLint Status=FALSE; AUX_RGBImageRec *TextureImage1; memset(TextureImage,0,sizeof(void *)*1); if (TextureImage0=LoadImage(Filename) Status=TRUE; glGenTextures(1, &texturecnt); glBindTexture(GL_TEXTURE_2D, texturecnt+); glPixelStorei(GL_UNPACK_ALIGNMENT,1); glTexParameteri

26、(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); glTexImage2D(GL_TEXTURE_2D,0,3,TextureImage0-sizeX,TextureImage0-

27、sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,TextureImage0-data); if (TextureImage0) if (TextureImage0-data) free(TextureImage0-data); free(TextureImage0); return Status; /*-*/* load the textures which will be used */*-*/int InitTexture() if( !LoadGLTextures(Image/space.bmp) printf(Fail to load Image/space.bmpn)

28、; return -1; if( !LoadGLTextures(Image/sun.bmp)printf(Fail to load Image/sun.bmpn);return -1;if( !LoadGLTextures(Image/mercury.bmp)printf(Fail to load Image/mercury.bmpn);return -1;if( !LoadGLTextures(Image/venus.bmp)printf(Fail to load Image/venus.bmpn);return -1;if( !LoadGLTextures(Image/earth.bmp

29、)printf(Fail to load Image/earth.bmpn);return -1;if( !LoadGLTextures(Image/moon.bmp)printf(Fail to load Image/moon.bmpn);return -1;if( !LoadGLTextures(Image/mars.bmp)printf(Fail to load Image/mars.bmpn);return -1;if( !LoadGLTextures(Image/jupiter.bmp)printf(Fail to load Image/jupiter.bmpn);return -1

30、;if( !LoadGLTextures(Image/saturn.bmp)printf(Fail to load Image/saturn.bmpn);return -1;if( !LoadGLTextures(Image/uranus.bmp)printf(Fail to load Image/uranus.bmpn);return -1;if( !LoadGLTextures(Image/neptune.bmp)printf(Fail to load Image/neptune.bmpn);return -1;/else printf(success load all the bitma

31、psn); return TRUE; /*-*/* Display strings */*-*/void drawString(const char* str) if( isFirstCall ) isFirstCall = FALSE; lists = glGenLists(MAX_CHAR); wglUseFontBitmaps(wglGetCurrentDC(), 0, MAX_CHAR, lists); for(; *str!=0; +str) glCallList(lists + *str);/*-*/* Display information about the planet */

32、*-*/void show_info(char* str)glMatrixMode(GL_MODELVIEW);glDisable(GL_LIGHTING);glColor3f(1.0,0.1,0.0); glPushMatrix();glRasterPos2f(-200.0f, 100.0f);drawString(str);glEnable(GL_LIGHTING);glColor3f(1.0,1.0,1.0);glPopMatrix();/*-*/* draw fog */*-*/void myFog()glFogi(GL_FOG_MODE,GL_LINEAR);glFogfv(GL_F

33、OG_COLOR,fog_color);glFogf(GL_FOG_DENSITY,fog_density);glHint(GL_FOG_HINT,GL_DONT_CARE); glFogf(GL_FOG_START,300.0f);glFogf(GL_FOG_END,-300.0f);/*-*/* initialization */*-*/bool init()glClearColor(0.0,0.0,0.0,1.0);show_info();rotation_speed0=27;rotation_speed1=58;rotation_speed2=243;rotation_speed3=1

34、;rotation_speed4=1;rotation_speed5=10;rotation_speed6=11;rotation_speed7=8;rotation_speed8=8;revolution_speed0=0;revolution_speed1=88;revolution_speed2=225;revolution_speed3=365;revolution_speed4=686;revolution_speed5=100;revolution_speed6=129;revolution_speed7=500;revolution_speed8=550;glDepthFunc(

35、GL_LEQUAL);glClearDepth(1.0f);glEnable(GL_DEPTH_TEST);glShadeModel(GL_SMOOTH);gluLookAt (lookx, looky, lookz, look_x, look_y, look_z, 0.0, 1.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0,(GLfloat)(width)/(GLfloat)(height),10,100000); glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);glEnable(GL_TEXTURE_2D);gluQuadricTexture(g_text,GL_TRUE); InitTexture(); glEnable(GL_LIGHTING);glEnable(GL_LIGHT0); glLightModeli(GL_LIGHT_MODEL_TWO_SIDE , GL_FALSE);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号