实验八:粒子系统
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验⼋:粒⼦系统
实验⼋:粒⼦系统
姓名:班级学号:
⼀、实验⽬的
掌握粒⼦系统的的原理,熟悉Ogre中粒⼦系统的结构和使⽤⽅法.
⼆、实验仪器
Pc,visual studio 2010
三、实验原理及过程
//利⽤Ogre实现⼀个粒⼦系统效果,并描述程序实现时的思路包括对每个调⽤的API以及脚本
进⾏详细说明
1、简述
粒⼦系统的基本单元是粒⼦,每个粒⼦⼀般具有形状、⼤⼩、颜⾊、透明度、位置、速度、加速度、⽅向、⽣命周期等属性。
每⼀帧,粒⼦系统⼀般都会执⾏如下步骤:
(a)产⽣新的粒⼦,并赋予相应的属性;
(b)删除超过⽣命周期的粒⼦;
(c)更新粒⼦的动态属性;
(d)渲染所有有效粒⼦。
2、组成
它由ParticleSystemManage(粒⼦系统管理器)、ParticleSystem(粒⼦系统)、ParticleSystemRenderer(粒⼦渲染器)、ParticleEmitter(粒⼦发射器)、ParticleAffector(粒⼦影响器)、Particle(粒⼦)等组成。
(1)ParticleSystemManager(粒⼦系统管理器)
此类类似于OGRE中的SceneManager,单件模式,内含粒⼦系统⼯⼚集合,粒⼦发射器⼯⼚集合,粒⼦影响器⼯⼚集合,以及粒⼦渲染器⼯⼚集合,⽤于创建和管理粒⼦系统、粒⼦发射器、粒⼦影响器、粒⼦渲染器。
该类还负责加载和分析粒⼦系统脚本。
(2)ParticleSystem(粒⼦系统)
具体的粒⼦系统类,它需要包含具体的ParticleEmitter 集合、ParticleAffector集合、ParticleSystemRenderer,Particle集合,⽤于粒⼦的创建、更新、渲染。
(3)ParticleEmitter(粒⼦发射器)
⽤于创建粒⼦,赋予粒⼦初始属性赋值,最重要的⽅法是_initParticle(),该类为抽象类,OGRE中提供了如下派⽣类,⾃⼰也可以根据需要编写⾃⼰的Emitter:
AreaEmitter
BoxEmitter
CylinderEmitter
EllipsoidEmitter
HollowEllipsoidEmitter
PointEmitter
RingEmitter
(4)ParticleAffector(粒⼦影响器)
⽤于在粒⼦的⽣命周期内更新粒⼦的属性,最重要的⽅法是_affectParticles(),OGRE中提供了如下派⽣类,⾃⼰也可以根据需要编写⾃⼰的Affector:
ColourFaderAffector
ColourImageAffector
ColourInterpolatorAffector
DeflectorPlaneAffector
DirectionRandomiserAffector
LinearForceAffector
RotationAffector
ScaleAffector
(6)ParticleSystemRenderer(粒⼦渲染器)
该类⽤于渲染粒⼦系统,该类为抽象类,ORGE提供了BillboardParticleRenderer具体类,⾃⼰也可以根据需要编写⾃⼰的粒⼦渲染类。
(7) Particle(粒⼦)
粒⼦系统的基本元素,粒⼦,存储了粒⼦的所有属性和状态。
3、OGRE粒⼦系统渲染流程
(1)初始化
ParticleSystemManager::addEmitterFactory()
ParticleSystemManager::addAffectorFactory()
ParticleSystemManager ::addRendererFactory()
ParticleSystemManager::createTemplate()
ParticleSystem::setRenderer()
ParticleSystemManager::_createRenderer()
ParticleSystem::addEmitter()
ParticleSystemManager::_createEmitter()
ParticleSystem::addAffector()
ParticleSystemManager::_createAffector()
(2) 每⼀帧,执⾏如下更新
ParticleSystem::_update()
ParticleSystem::_triggerAffectors()
Affector::_affectParticles()
ParticleSystem ::_triggerEmitters()
Emitter::_initParticle()
ParticleSystem::_updateRenderQueue()
ParticleRenderer::_updateRenderQueue()
四、实验结果
五、实验⼼得
六、主要代码
// ParticleFX.h
#ifndef __ParticleFX_H__
#define __ParticleFX_H__
#include "SdkSample.h"
using namespace Ogre;
using namespace OgreBites;
class _OgreSampleClassExport Sample_ParticleFX : public SdkSample
{
public :
Sample_ParticleFX()
{
mInfo["Title"] = "Particle Effects";
mInfo["Description"] = "Demonstrates the creation and usage of particle effects.";
mInfo["Thumbnail"] = "thumb_particles.png";
mInfo["Category"] = "Effects";
mInfo["Help"] = "Use the checkboxes to toggle visibility of the individual particle systems."; }
bool frameRenderingQueued(const FrameEvent& evt)
{
mFountainPivot->yaw(Degree(evt.timeSinceLastFrame * 30)); // spin the fountains around return SdkSample::frameRenderingQueued(evt); // don't forget the parent class updates! }
void checkBoxToggled(CheckBox* box)
{
// show or hide the particle system with the same name as the check box
mSceneMgr->getParticleSystem(box->getName())->setVisible(box->isChecked());
}
protected:
void setupContent()
{
// setup some basic lighting for our scene
mSceneMgr->setAmbientLight(ColourValue(0.3, 0.3, 0.3));
mSceneMgr->createLight()->setPosition(20, 80, 50);
// set our camera to orbit around the origin and show cursor
mCameraMan->setStyle(CS_ORBIT);
mCameraMan->setYawPitchDist(Degree(0), Degree(15), 250);
mTrayMgr->showCursor();
// create an ogre head entity and place it at the origin
Entity* ent = mSceneMgr->createEntity("Head", "ogrehead.mesh");
mSceneMgr->getRootSceneNode()->attachObject(ent);
setupParticles(); // setup particles
setupTogglers(); // setup particle togglers
}
void setupParticles()
{
ParticleSystem::setDefaultNonVisibleUpdateTimeout(5); // set nonvisible timeout
ParticleSystem* ps;
// create some nice fireworks and place it at the origin
ps = mSceneMgr->createParticleSystem("Fireworks", "Examples/Fireworks");
mSceneMgr->getRootSceneNode()->attachObject(ps);
// create a green nimbus around the ogre head
ps = mSceneMgr->createParticleSystem("Nimbus", "Examples/GreenyNimbus");
mSceneMgr->getRootSceneNode()->attachObject(ps);
ps = mSceneMgr->createParticleSystem("Rain", "Examples/Rain"); // create a rainstorm
ps->fastForward(5); // fast-forward the rain so it looks more natural
mSceneMgr->getRootSceneNode()->createChildSceneNode(Vector3(0, 1000,
0))->attachObject(ps);
// create aureola around ogre head perpendicular to the ground
ps = mSceneMgr->createParticleSystem("Aureola", "Examples/Aureola");
mSceneMgr->getRootSceneNode()->attachObject(ps);
// create shared pivot node for spinning the fountains
mFountainPivot = mSceneMgr->getRootSceneNode()->createChildSceneNode();
ps = mSceneMgr->createParticleSystem("Fountain1", "Examples/PurpleFountain"); // create fountain 1
// attach the fountain to a child node of the pivot at a distance and angle
mFountainPivot->createChildSceneNode(Vector3(200, -100, 0), Quaternion(Degree(20), Vector3::UNIT_Z))->attachObject(ps);
ps = mSceneMgr->createParticleSystem("Fountain2", "Examples/PurpleFountain"); // create fountain 2
// attach the fountain to a child node of the pivot at a distance and angle
mFountainPivot->createChildSceneNode(Vector3(-200, -100, 0), Quaternion(Degree(-20), Vector3::UNIT_Z))->attachObject(ps);
}
void setupTogglers()
{
// create check boxes to toggle the visibility of our particle systems
mTrayMgr->createLabel(TL_TOPLEFT, "VisLabel", "Particles");
mTrayMgr->createCheckBox(TL_TOPLEFT, "Fireworks", "Fireworks",
130)->setChecked(true);
mTrayMgr->createCheckBox(TL_TOPLEFT, "Fountain1", "Fountain A",
130)->setChecked(true);
mTrayMgr->createCheckBox(TL_TOPLEFT, "Fountain2", "Fountain B",
130)->setChecked(true);
mTrayMgr->createCheckBox(TL_TOPLEFT, "Aureola", "Aureola",
130)->setChecked(false);
mTrayMgr->createCheckBox(TL_TOPLEFT, "Nimbus", "Nimbus",
130)->setChecked(false);
mTrayMgr->createCheckBox(TL_TOPLEFT, "Rain", "Rain", 130)->setChecked(false);
}
SceneNode* mFountainPivot;
};
#endif
// ParticleFX.cpp
#include"SamplePlugin.h"
#include"ParticleFX.h"
using namespace Ogre;
using namespace OgreBites;
#ifndef OGRE_STATIC_LIB
SamplePlugin* sp;
Sample* s;
extern"C" _OgreSampleExport void dllStartPlugin()
{
s = new Sample_ParticleFX;
sp = OGRE_NEW SamplePlugin(s->getInfo()["Title"] + " Sample");
sp->addSample(s);
Root::getSingleton().installPlugin(sp);
}
extern"C" _OgreSampleExport void dllStopPlugin() {
Root::getSingleton().uninstallPlugin(sp);
OGRE_DELETE sp;
delete s;
}
#endif。