文章目录
说明
1. 粒子系统简介
2. 雨雪效果
说明
示例来源于《OSG程序设计教程》
资源下载:
https://download.csdn.net/download/qq_35097289/10609392
https://download.csdn.net/download/qq_35097289/10488928
1. 粒子系统简介
粒子系统是用于不规则模糊物体建模及图像生成的一种方法。
粒子系统是一种过程模型,即利用各种计算过程生成模型各个体素的建模技术。它的基本思想是:采用大量不规则的、随机分布的具有一定生命和属性的 微小粒子图元作为基本元素来描述不规则的模糊物体。在粒子系统中,每一个粒子图元均具有:位置、形状、大小、颜色、透明度、运动速度和运动方向、生命期等属性,所有这些属性都是时间t的函数。
2. 雨雪效果
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgParticle/PrecipitationEffect>
#include <osg/Node>
void main() {
osgViewer::Viewer viewer;
// 设置雪花类
osg::ref_ptr<osgParticle::PrecipitationEffect> precipitationEffect = new osgParticle::PrecipitationEffect;
// 设置雪花浓度
precipitationEffect->snow(0.5);
//设置雪花颜色
precipitationEffect->setParticleColor(osg::Vec4(1, 1, 1, 1));
// 设置风向
precipitationEffect->setWind(osg::Vec3(2, 0, 0));
osg::Group *root = new osg::Group();
// 把雪花加入到场景结点
root->addChild(precipitationEffect.get());
osg::Node *ceep = osgDB::readNodeFile("ceep.ive");
root->addChild(ceep);
viewer.setSceneData(root);
viewer.realize();
viewer.run();
}
void createSnow() {
osg::ref_ptr<osg::Group> root = new osg::Group;
root->addChild(osgDB::readNodeFile("cow.osg"));
//设置雪花类
osg::ref_ptr<osgParticle::PrecipitationEffect> sn = new osgParticle::PrecipitationEffect;
//设置雪花浓度
sn->snow(0.3);
//设置雪花颜色
sn->setParticleColor(osg::Vec4(1, 1, 1, 0));
//设置风向
sn->setWind(osg::Vec3(0, 0, 0));
root->addChild(sn.get());
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
viewer->addEventHandler(new osgViewer::WindowSizeHandler());
viewer->setSceneData(root.get());
viewer->realize();
viewer->run();
}
粒子
OSG中使用osgParticle::Particle这个类,来表示粒子。
如果要产生自定义的粒子,则要自己定义一个Particle对象,然后将这个Particle对象设置为粒子产生器的模板。
这是通过函数Emitter::setParticleTemplate来实现的。
粒子产生
OSG中粒子的产生,使用的是一个叫做粒子发射器(Emiter)的东西。使用的类叫做osgParticle::Emitter。
这个粒子发射器,不停地产生新的粒子,每个粒子都从粒子产生器的初始位置发出,
然后以一定的初始发射角度和初始发射速度向外发出。
这里使用的是OSG自己内置的一个标准发射器osgParticle::ModularEmitter。
粒子管理
粒子发射器,不停地发射粒子,这些发射出来的粒子,如何管理呢?
这是通过粒子系统来进行的。
对应的类叫osgParticle::ParticleSystem。
粒子发射器,发射出来粒子后,交个粒子系统,就不用操心了。
这个转交的过程,是用过emitter->setParticleSystem(ps)实现的。
粒子显示
由于粒子系统本身,只负责管理粒子,并不负责显示粒子,所以,还需要一个东西,将粒子显示出来。
OSG中是通过osg::Geode将粒子系统显示出来。
然后通过geode->addDrawable(ps)将粒子系统与Geode结合起来。
粒子更新
由于粒子是在不停地运动,所以需要不停地更新。
粒子系统的更新是通过类osgParticle::ParticleSystemUpdater来实现的。
然后通过psu->addParticleSystem(ps);来将更新器和粒子系统进行关联。
最后,将粒子系统,加到场景中,需要添加三个东西到场景树中:发射器,显示的Geode,更新器。