多媒体技术实验报告.doc

上传人:仙人指路1688 文档编号:2888336 上传时间:2023-03-01 格式:DOC 页数:24 大小:125.50KB
返回 下载 相关 举报
多媒体技术实验报告.doc_第1页
第1页 / 共24页
多媒体技术实验报告.doc_第2页
第2页 / 共24页
多媒体技术实验报告.doc_第3页
第3页 / 共24页
多媒体技术实验报告.doc_第4页
第4页 / 共24页
多媒体技术实验报告.doc_第5页
第5页 / 共24页
点击查看更多>>
资源描述

《多媒体技术实验报告.doc》由会员分享,可在线阅读,更多相关《多媒体技术实验报告.doc(24页珍藏版)》请在三一办公上搜索。

1、1q实验 粒子系统讲授时数:0.5学时。一、 实验类型研究创新型实验 二、 实验目的 通过本实验让学生熟练掌握三维图形程序编写,掌握粒子系统的设计与实现,加深对粒子系统的认识。 本实验要实现一个基于粒子系统的烟花效果模拟。粒子系统包括:粒子初始化、粒子状态控制、粒子的绘制。 实现本系统实现之后,学生可以在此基础上进行扩展,例如模拟喷泉效果。喷泉效果的实现包括:粒子颜色保持蓝色,粒子运动受到重力作用。三、 实验要求 三维图形绘制工程配置和环境设置。 粒子系统设计,实现粒子系统的数据结构和管理的设计模式。 各个粒子位置、颜色计算。 粒子的绘制。 提交程序使用手册(可简单到为一个readme文件)。

2、四、 实验内容利用OpenGL实现一个粒子系统,模拟烟花效果。五、 开设方式小组实验。六、 学时数8学时。七、 参考资料1 VC+编程指南,2 NeHe OpenGL框架程序3 OpenGL SDK下载http:/www.opengl.org/八、 实验后记电 子 科 技 大 学实 验 报 告学生姓名: 学 号: 指导教师:实验地点: 实验时间:一、实验室名称:计算机学院软件实验室二、实验项目名称:粒子系统三、实验学时:8学时四、实验原理粒子系统在计算机动画中被广泛用于模拟爆炸、喷泉、流星等效果。在本实验中,粒子系统被用来模拟不断喷射的烟花。实现粒子系统包括:(1)粒子初始化。(2)粒子运动和

3、颜色模拟。(3)粒子生命周期模拟。粒子的初始化给所有的粒子的位置、速度、生命和衰减周期赋值;运动和颜色模拟是计算粒子的速度、位置在每一时间步长之后变化的值;粒子的生命周期模拟是在粒子生命完结之后重新初始化该粒子。为了实现这样的效果,粒子应该具有以下属性:(1)boolactive;粒子的活动状态,为true时,才模拟粒子。(2)floatlife;粒子的生命值,在模拟过程中不断减少,如果为0就重新初始化粒子。(3)floatfade;粒子的生命值减少的速度。(4)floatr;粒子颜色R分量。(5)floatg;粒子颜色G分量。(6)floatb;粒子颜色B分量。(7)floatx;粒子位置X

4、分量。(8)floaty;粒子位置Y分量。(9)floatz;粒子位置Z分量。(10)floatxi;粒子速度X分量。(11)floatyi;粒子速度Y分量。(12)floatzi;粒子速度Z分量。(13)floatxg;粒子加速度X分量。(14)floatyg;粒子加速度X分量。(15)floatzg;粒子加速度X分量。在具体的模拟过程中,需要创建一个数组叫particle数组存MAX_PARTICLES个元素。也就是说我们创建1000(MAX_PARTICLES)个粒子,存储空间为每个粒子提供相应的信息。使用光滑的阴影,清除背景为黑色,关闭深度测试,绑定并映射纹理。启用映射位图后我们选择粒

5、子纹理。唯一的改变就是禁用深度测试和初始化粒子。并且创建一个循环loop.这个环将会更新每一个粒子。每次循环时首先检查粒子是否活跃。如果不活跃,则不被更新。在这个程序中,它们始终活跃。五、实验目的本实验要实现粒子系统。粒子系统包括以下模块:粒子初始化;位置、速度、颜色实时计算;生命周期控制。基本系统实现之后,可以在此基础上扩展为喷泉效果。实现喷泉效果需要一下几个模块:(1)粒子水平方向的随机初速度。(2)粒子运动模拟。(3)位置低于地面,重新初始化。六、实验内容利用OpenGL实现一个粒子系统。七、实验器材(设备、元器件)1. 操作系统:Windows XP2. 开发工具:VC2005,Ope

6、nGL库3. 普通PC即可八、实验步骤(1) 创建工程启动VC+2005,选择菜单中的“文件”-“新建”-“项目”。在弹出的对话框中,左边的“项目类型”框中,选择“Visual C+”,在右边框中,选择“Win32项目”。在对话框下边,选择工程文件存放目录及输入名称,如Particle,单击“确定”。在“Win32应用程序向导”中单击“下一步”,在“应用程序类型”中选择“windows应用程序”;在“附加选项”中选择“空项目”。点击“完成”。(2) 设置此工程所需的OpenGL库选择菜单中的“项目”-“属性”。在弹出的“Particle属性页”对话框中,展开“链接器”选择“输入”。选择右边框中

7、的“附加依赖项”,单击最右端的“”小按钮,弹出“附加依赖项”对话框,输入“OPENGL32.lib GLUT.lib GLAUX.lib GLU32.lib”(注意,输入双引号中的内容,各个库用空格分开;否则会出现链接错误。如果编译时提示找不到以上的lib文件,把“链接器-常规-附加库目录”选择为OpenGL SDK所在目录,或者从OPENGL SDK中把这几个lib文件放到Microsoft Visual Studio 8VClib)。“配置属性-常规-字符集”设置为“未设置”。“C/C+-预编译头-创建/使用预编译头”设置为“不使用预编译头”。单击“确定”结束。项目属性设置完成。(3) 在

8、工程项目中添加源文件在“解决方案资源管理器”中,选中Particle项目下的“源文件”筛选器。右键点击后选择“添加”,然后选择新建项。在“添加新项”的窗口中选择“C+文件(.cpp)”模板,并命名为main.cpp。(4) 录入源程序程序使用到了资源文件“Particle.bmp”作为粒子的贴图,该文件如下:图1 Particle.bmp将该文件保存在工程目录“ParticleData”目录下。将源程序写入到工程中。新建好main.cpp文件后,将代码写入该文件。参考源代码如下:Main.cpp#include / Header File For Windows#include / Heade

9、r File For Standard Input/Output#include / Header File For The OpenGL32 Library#include / Header File For The GLu32 Library#include / Header File For The Glaux Library#defineMAX_PARTICLES1000/ Number Of Particles To CreateHDChDC=NULL;/ Private GDI Device ContextHGLRChRC=NULL;/ Permanent Rendering Co

10、ntextHWNDhWnd=NULL;/ Holds Our Window HandleHINSTANCEhInstance;/ Holds The Instance Of The Applicationboolkeys256;/ Array Used For The Keyboard Routineboolactive=TRUE;/ Window Active Flag Set To TRUE By Defaultboolfullscreen=TRUE;/ Fullscreen Flag Set To Fullscreen Mode By Defaultboolrainbow=true;/

11、Rainbow Mode?boolsp;/ Spacebar Pressed?boolrp;/ Enter Key Pressed?floatslowdown=2.0f;/ Slow Down Particlesfloatxspeed;/ Base X Speed (To Allow Keyboard Direction Of Tail)floatyspeed;/ Base Y Speed (To Allow Keyboard Direction Of Tail)floatzoom=-40.0f;/ Used To Zoom OutGLuintloop;/ Misc Loop Variable

12、GLuintcol;/ Current Color SelectionGLuintdelay;/ Rainbow Effect DelayGLuinttexture1;/ Storage For Our Particle Texturetypedef struct/ Create A Structure For Particleboolactive;/ Active (Yes/No)floatlife;/ Particle Lifefloatfade;/ Fade Speedfloatr;/ Red Valuefloatg;/ Green Valuefloatb;/ Blue Valueflo

13、atx;/ X Positionfloaty;/ Y Positionfloatz;/ Z Positionfloatxi;/ X Directionfloatyi;/ Y Directionfloatzi;/ Z Directionfloatxg;/ X Gravityfloatyg;/ Y Gravityfloatzg;/ Z Gravityparticles;/ Particles Structureparticles particleMAX_PARTICLES;/ Particle Array (Room For Particle Info)static GLfloat colors1

14、23=/ Rainbow Of Colors1.0f,0.5f,0.5f,1.0f,0.75f,0.5f,1.0f,1.0f,0.5f,0.75f,1.0f,0.5f,0.5f,1.0f,0.5f,0.5f,1.0f,0.75f,0.5f,1.0f,1.0f,0.5f,0.75f,1.0f,0.5f,0.5f,1.0f,0.75f,0.5f,1.0f,1.0f,0.5f,1.0f,1.0f,0.5f,0.75f;LRESULTCALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);/ Declaration For WndProcAUX_RGBImageRec

15、 *LoadBMP(char *Filename)/ Loads A Bitmap Image FILE *File=NULL;/ File Handle if (!Filename)/ Make Sure A Filename Was Given return NULL;/ If Not Return NULL File=fopen(Filename,r);/ Check To See If The File Exists if (File)/ Does The File Exist? fclose(File);/ Close The Handlereturn auxDIBImageLoad

16、(Filename);/ Load The Bitmap And Return A Pointer return NULL;/ If Load Failed Return NULLint LoadGLTextures()/ Load Bitmap And Convert To A Texture int Status=FALSE;/ Status Indicator AUX_RGBImageRec *TextureImage1;/ Create Storage Space For The Textures memset(TextureImage,0,sizeof(void *)*1);/ Se

17、t The Pointer To NULL if (TextureImage0=LoadBMP(Data/Particle.bmp)/ Load Particle Texture Status=TRUE;/ Set The Status To TRUEglGenTextures(1, &texture0);/ Create One TextureglBindTexture(GL_TEXTURE_2D, texture0);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);glTexParameteri(GL_TEXTU

18、RE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage0-sizeX, TextureImage0-sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage0-data); if (TextureImage0)/ If Texture Existsif (TextureImage0-data)/ If Texture Image Existsfree(TextureImage0-data);/ Free The Texture Image

19、Memoryfree(TextureImage0);/ Free The Image Structure return Status;/ Return The StatusGLvoid ReSizeGLScene(GLsizei width, GLsizei height)/ Resize And Initialize The GL Windowif (height=0)/ Prevent A Divide By Zero Byheight=1;/ Making Height Equal OneglViewport(0,0,width,height);/ Reset The Current V

20、iewportglMatrixMode(GL_PROJECTION);/ Select The Projection MatrixglLoadIdentity();/ Reset The Projection Matrix/ Calculate The Aspect Ratio Of The WindowgluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,200.0f);glMatrixMode(GL_MODELVIEW);/ Select The Modelview MatrixglLoadIdentity();/ Reset T

21、he Modelview Matrixint InitGL(GLvoid)/ All Setup For OpenGL Goes Hereif (!LoadGLTextures()/ Jump To Texture Loading Routinereturn FALSE;/ If Texture Didnt Load Return FALSEglShadeModel(GL_SMOOTH);/ Enable Smooth ShadingglClearColor(0.0f,0.0f,0.0f,0.0f);/ Black BackgroundglClearDepth(1.0f);/ Depth Bu

22、ffer SetupglDisable(GL_DEPTH_TEST);/ Disable Depth TestingglEnable(GL_BLEND);/ Enable BlendingglBlendFunc(GL_SRC_ALPHA,GL_ONE);/ Type Of Blending To PerformglHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);/ Really Nice Perspective CalculationsglHint(GL_POINT_SMOOTH_HINT,GL_NICEST);/ Really Nice Poin

23、t SmoothingglEnable(GL_TEXTURE_2D);/ Enable Texture MappingglBindTexture(GL_TEXTURE_2D,texture0);/ Select Our Texturefor (loop=0;loopMAX_PARTICLES;loop+)/ Initials All The Texturesparticleloop.active=true;/ Make All The Particles Activeparticleloop.life=1.0f;/ Give All The Particles Full Lifeparticl

24、eloop.fade=float(rand()%100)/1000.0f+0.003f;/ Random Fade Speedparticleloop.r=colorsloop*(12/MAX_PARTICLES)0;/ Select Red Rainbow Colorparticleloop.g=colorsloop*(12/MAX_PARTICLES)1;/ Select Red Rainbow Colorparticleloop.b=colorsloop*(12/MAX_PARTICLES)2;/ Select Red Rainbow Colorparticleloop.xi=float

25、(rand()%50)-26.0f)*10.0f;/ Random Speed On X Axisparticleloop.yi=float(rand()%50)-25.0f)*10.0f;/ Random Speed On Y Axisparticleloop.zi=float(rand()%50)-25.0f)*10.0f;/ Random Speed On Z Axisparticleloop.xg=0.0f;/ Set Horizontal Pull To Zeroparticleloop.yg=-0.8f;/ Set Vertical Pull Downwardparticleloo

26、p.zg=0.0f;/ Set Pull On Z Axis To Zeroreturn TRUE;/ Initialization Went OKint DrawGLScene(GLvoid)/ Heres Where We Do All The DrawingglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);/ Clear Screen And Depth BufferglLoadIdentity();/ Reset The ModelView Matrixfor (loop=0;loopMAX_PARTICLES;loop+)/ Loo

27、p Through All The Particlesif (particleloop.active)/ If The Particle Is Activefloat x=particleloop.x;/ Grab Our Particle X Positionfloat y=particleloop.y;/ Grab Our Particle Y Positionfloat z=particleloop.z+zoom;/ Particle Z Pos + Zoom/ Draw The Particle Using Our RGB Values, Fade The Particle Based

28、 On Its LifeglColor4f(particleloop.r,particleloop.g,particleloop.b,particleloop.life);glBegin(GL_TRIANGLE_STRIP);/ Build Quad From A Triangle Strip glTexCoord2d(1,1); glVertex3f(x+0.5f,y+0.5f,z); / Top RightglTexCoord2d(0,1); glVertex3f(x-0.5f,y+0.5f,z); / Top LeftglTexCoord2d(1,0); glVertex3f(x+0.5

29、f,y-0.5f,z); / Bottom RightglTexCoord2d(0,0); glVertex3f(x-0.5f,y-0.5f,z); / Bottom LeftglEnd();/ Done Building Triangle Stripparticleloop.x+=particleloop.xi/(slowdown*1000);/ Move On The X Axis By X Speedparticleloop.y+=particleloop.yi/(slowdown*1000);/ Move On The Y Axis By Y Speedparticleloop.z+=

30、particleloop.zi/(slowdown*1000);/ Move On The Z Axis By Z Speedparticleloop.xi+=particleloop.xg;/ Take Pull On X Axis Into Accountparticleloop.yi+=particleloop.yg;/ Take Pull On Y Axis Into Accountparticleloop.zi+=particleloop.zg;/ Take Pull On Z Axis Into Accountparticleloop.life-=particleloop.fade

31、;/ Reduce Particles Life By Fadeif (particleloop.life0.0f)/ If Particle Is Burned Outparticleloop.life=1.0f;/ Give It New Lifeparticleloop.fade=float(rand()%100)/1000.0f+0.003f;/ Random Fade Valueparticleloop.x=0.0f;/ Center On X Axisparticleloop.y=0.0f;/ Center On Y Axisparticleloop.z=0.0f;/ Center

32、 On Z Axisparticleloop.xi=xspeed+float(rand()%60)-32.0f);/ X Axis Speed And Directionparticleloop.yi=yspeed+float(rand()%60)-30.0f);/ Y Axis Speed And Directionparticleloop.zi=float(rand()%60)-30.0f);/ Z Axis Speed And Directionparticleloop.r=colorscol0;/ Select Red From Color Tableparticleloop.g=co

33、lorscol1;/ Select Green From Color Tableparticleloop.b=colorscol2;/ Select Blue From Color Table/ If Number Pad 8 And Y Gravity Is Less Than 1.5 Increase Pull Upwardsif (keysVK_NUMPAD8 & (particleloop.yg-1.5f) particleloop.yg-=0.01f;/ If Number Pad 6 And X Gravity Is Less Than 1.5 Increase Pull Righ

34、tif (keysVK_NUMPAD6 & (particleloop.xg-1.5f) particleloop.xg-=0.01f;if (keysVK_TAB)/ Tab Key Causes A Burstparticleloop.x=0.0f;/ Center On X Axisparticleloop.y=0.0f;/ Center On Y Axisparticleloop.z=0.0f;/ Center On Z Axisparticleloop.xi=float(rand()%50)-26.0f)*10.0f;/ Random Speed On X Axisparticlel

35、oop.yi=float(rand()%50)-25.0f)*10.0f;/ Random Speed On Y Axisparticleloop.zi=float(rand()%50)-25.0f)*10.0f;/ Random Speed On Z Axis return TRUE;/ Everything Went OKGLvoid KillGLWindow(GLvoid)/ Properly Kill The Windowif (fullscreen)/ Are We In Fullscreen Mode?ChangeDisplaySettings(NULL,0);/ If So Sw

36、itch Back To The DesktopShowCursor(TRUE);/ Show Mouse Pointerif (hRC)/ Do We Have A Rendering Context?if (!wglMakeCurrent(NULL,NULL)/ Are We Able To Release The DC And RC Contexts?MessageBox(NULL,Release Of DC And RC Failed.,SHUTDOWN ERROR,MB_OK | MB_ICONINFORMATION);if (!wglDeleteContext(hRC)/ Are

37、We Able To Delete The RC?MessageBox(NULL,Release Rendering Context Failed.,SHUTDOWN ERROR,MB_OK | MB_ICONINFORMATION);hRC=NULL;/ Set RC To NULLif (hDC & !ReleaseDC(hWnd,hDC)/ Are We Able To Release The DCMessageBox(NULL,Release Device Context Failed.,SHUTDOWN ERROR,MB_OK | MB_ICONINFORMATION);hDC=NU

38、LL;/ Set DC To NULLif (hWnd & !DestroyWindow(hWnd)/ Are We Able To Destroy The Window?MessageBox(NULL,Could Not Release hWnd.,SHUTDOWN ERROR,MB_OK | MB_ICONINFORMATION);hWnd=NULL;/ Set hWnd To NULLif (!UnregisterClass(OpenGL,hInstance)/ Are We Able To Unregister ClassMessageBox(NULL,Could Not Unregi

39、ster Class.,SHUTDOWN ERROR,MB_OK | MB_ICONINFORMATION);hInstance=NULL;/ Set hInstance To NULL/*This Code Creates Our OpenGL Window. Parameters Are:* *title- Title To Appear At The Top Of The Window* *width- Width Of The GL Window Or Fullscreen Mode* *height- Height Of The GL Window Or Fullscreen Mode* *bits- Number Of Bits To Use For Color (8/16/24/32)* *fullscreenflag- Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE)*

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

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


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号