源码:
// CMyOSGParticle.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include //事件监听
#include //事件响应类,对渲染状态进行控制
#include //简化几何体
#include
#include
#include
#include
#include < osgParticle/RandomRateCounter>
#include
#include
#include
#include
#include < osgParticle/ModularProgram>
#pragma comment(lib, "OpenThreadsd.lib")
#pragma comment(lib, "osgd.lib")
#pragma comment(lib, "osgDBd.lib")
#pragma comment(lib, "osgUtild.lib")
#pragma comment(lib, "osgGAd.lib")
#pragma comment(lib, "osgViewerd.lib")
#pragma comment(lib, "osgTextd.lib")
#pragma comment(lib, "osgParticled.lib")
//创建火球(燃烧)
void createFireBall(osg::MatrixTransform* smokeNode)
{
// 创建粒子对象,设置其属性并交由粒子系统使用。
osgParticle::Particle particleTempalte;
particleTempalte.setShape(osgParticle::Particle::QUAD);
particleTempalte.setLifeTime(1.5); // 单位:秒
particleTempalte.setSizeRange(osgParticle::rangef(3.0f, 1.0f)); // 单位:米
particleTempalte.setAlphaRange(osgParticle::rangef(1, 0));
particleTempalte.setColorRange(osgParticle::rangev4(osg::Vec4(1.0f, 0.2f, 0.0f, 1.0f),//0.1f,0.3f,0.4f,1.0f
osg::Vec4(0.1f, 0.1f, 0.1f, 0)//0.95f,0.75f,0,1(1,1,1,1)
));
particleTempalte.setPosition(osg::Vec3(0.0f, 0.0f, 0.0f));
particleTempalte.setVelocity(osg::Vec3(0.0f, 0.0f, 0.0f));
particleTempalte.setMass(0.1f); //单位:千克
particleTempalte.setRadius(0.2f);
particleTempalte.setSizeInterpolator(new osgParticle::LinearInterpolator);
particleTempalte.setAlphaInterpolator(new osgParticle::LinearInterpolator);
particleTempalte.setColorInterpolator(new osgParticle::LinearInterpolator);
// 创建并初始化粒子系统。
osgParticle::ParticleSystem* particleSystem = new osgParticle::ParticleSystem;
particleSystem->setDataVariance(osg::Node::STATIC);
// 设置材质,是否放射粒子,以及是否使用光照。
particleSystem->setDefaultAttributes("D:\\data\\Images\\smoke.rgb", true, false);
osg::Geode* geode = new osg::Geode;
geode->addDrawable(particleSystem);
smokeNode->addChild(geode);
//设置为粒子系统的缺省粒子对象。
particleSystem->setDefaultParticleTemplate(particleTempalte);
//获取放射极中缺省计数器的句柄,调整每帧增加的新粒子数目
osgParticle::RandomRateCounter* particleGenerateRate = new osgParticle::RandomRateCounter();
particleGenerateRate->setRateRange(30, 50);
// 每秒新生成的粒子范围
particleGenerateRate->setDataVariance(osg::Node::DYNAMIC);
// 自定义一个放置器,这里创建并初始化一个点放置器
osgParticle::PointPlacer* particlePlacer = new osgParticle::PointPlacer;
particlePlacer->setCenter(osg::Vec3(0.0f, 0.0f, 0.0f));
particlePlacer->setDataVariance(osg::Node::DYNAMIC);
// 自定义一个弧度发射器
osgParticle::RadialShooter* particleShooter = new osgParticle::RadialShooter;
// 设置发射器的属性
particleShooter->setDataVariance(osg::Node::DYNAMIC);
particleShooter->setThetaRange(-0.1f, 0.1f);
// 弧度值,与Z 轴夹角
particleShooter->setPhiRange(-0.1f, 0.1f);
particleShooter->setInitialSpeedRange(5, 7.5f);//单位:米/秒
//创建标准放射极对象
osgParticle::ModularEmitter* emitter = new osgParticle::ModularEmitter;
emitter->setDataVariance(osg::Node::DYNAMIC);
emitter->setCullingActive(false);
// 将放射极对象与粒子系统关联。
emitter->setParticleSystem(particleSystem);
// 设置计数器
emitter->setCounter(particleGenerateRate);
// 设置放置器
emitter->setPlacer(particlePlacer);
// 设置发射器
emitter->setShooter(particleShooter);
// 把放射极添加为变换节点
smokeNode->addChild(emitter);
// 添加更新器,以实现每帧的粒子管理。
osgParticle::ParticleSystemUpdater* particleSystemUpdater = new osgParticle::ParticleSystemUpdater;
// 将更新器与粒子系统对象关联。
particleSystemUpdater->addParticleSystem(particleSystem);
// 将更新器节点添加到场景中。
smokeNode->addChild(particleSystemUpdater);
// 创建标准编程器对象并与粒子系统相关联。
osgParticle::ModularProgram* particleMoveProgram = new osgParticle::ModularProgram;
particleMoveProgram->setParticleSystem(particleSystem);
// 最后,将编程器添加到场景中。
smokeNode->addChild(particleMoveProgram);
}
//创建浓烟
void createDarkSmoke(osg::MatrixTransform* smokeNode)
{
// 创建粒子对象,设置其属性并交由粒子系统使用。
osgParticle::Particle particleTempalte;
particleTempalte.setShape(osgParticle::Particle::QUAD);
particleTempalte.setLifeTime(10); // 单位:秒
particleTempalte.setSizeRange(osgParticle::rangef(1.0f, 12.0f)); // 单位:米
particleTempalte.setAlphaRange(osgParticle::rangef(1, 0));
particleTempalte.setColorRange(osgParticle::rangev4(
osg::Vec4(0.0f, 0.0f, 0.0f, 0.5f),//(0.1f,0.1f,0.1f,0.5f)
osg::Vec4(0.5f, 0.5f, 0.5f, 1.5f)//0.95f,0.75f,0,1
));
particleTempalte.setPosition(osg::Vec3(0.0f, 0.0f, 0.0f));
particleTempalte.setVelocity(osg::Vec3(0.0f, 0.0f, 0.0f));
particleTempalte.setMass(0.1f); //单位:千克
particleTempalte.setRadius(0.2f);
particleTempalte.setSizeInterpolator(new osgParticle::LinearInterpolator);
particleTempalte.setAlphaInterpolator(new osgParticle::LinearInterpolator);
particleTempalte.setColorInterpolator(new osgParticle::LinearInterpolator);
// 创建并初始化粒子系统。
osgParticle::ParticleSystem* particleSystem = new osgParticle::ParticleSystem;
particleSystem->setDataVariance(osg::Node::STATIC);
// 设置材质,是否放射粒子,以及是否使用光照。
particleSystem->setDefaultAttributes("D:\\data\\Images\\smoke.rgb", false, false);
osg::Geode* geode = new osg::Geode;
geode->addDrawable(particleSystem);
smokeNode->addChild(geode);
//设置为粒子系统的缺省粒子对象。
particleSystem->setDefaultParticleTemplate(particleTempalte);
//获取放射极中缺省计数器的句柄,调整每帧增加的新粒
//子数目
osgParticle::RandomRateCounter* particleGenerateRate = new osgParticle::RandomRateCounter();
particleGenerateRate->setRateRange(30, 50);
// 每秒新生成的粒子范围
particleGenerateRate->setDataVariance(osg::Node::DYNAMIC);
// 自定义一个放置器,这里我们创建并初始化一个点放置器
osgParticle::PointPlacer* particlePlacer = new osgParticle::PointPlacer;
particlePlacer->setCenter(osg::Vec3(0.0f, 0.0f, 0.05f));
particlePlacer->setDataVariance(osg::Node::DYNAMIC);
// 自定义一个弧度发射器
osgParticle::RadialShooter* particleShooter = new osgParticle::RadialShooter;
// 设置发射器的属性
particleShooter->setDataVariance(osg::Node::DYNAMIC);
particleShooter->setThetaRange(-0.1f, 0.1f);
// 弧度值,与Z 轴夹角0.392699f
particleShooter->setPhiRange(-0.1f, 0.1f);
particleShooter->setInitialSpeedRange(10, 15);
//单位:米/秒
//创建标准放射极对象
osgParticle::ModularEmitter* emitter = new osgParticle::
ModularEmitter;
emitter->setDataVariance(osg::Node::DYNAMIC);
emitter->setCullingActive(false);
// 将放射极对象与粒子系统关联。
emitter->setParticleSystem(particleSystem);
// 设置计数器
emitter->setCounter(particleGenerateRate);
// 设置放置器
emitter->setPlacer(particlePlacer);
// 设置发射器
emitter->setShooter(particleShooter);
// 把放射极添加为变换节点
smokeNode->addChild(emitter);
// 添加更新器,以实现每帧的粒子管理。
osgParticle::ParticleSystemUpdater* particleSystemUpdater = new osgParticle::ParticleSystemUpdater;
// 将更新器与粒子系统对象关联。
particleSystemUpdater->addParticleSystem(particleSystem);
// 将更新器节点添加到场景中。
smokeNode->addChild(particleSystemUpdater);
osgParticle::ModularProgram* particleMoveProgram = new osgParticle::ModularProgram;
particleMoveProgram->setParticleSystem(particleSystem);
// 最后,将编程器添加到场景中。
smokeNode->addChild(particleMoveProgram);
}
void WriteFireballAndSmoke()
{
osg::ref_ptr<osg::Group> root = new osg::Group();
osg::MatrixTransform* flightTransform = new osg::MatrixTransform();
root->addChild(flightTransform);
//引擎烟雾
osg::MatrixTransform* fireballAndSmoke = new osg::MatrixTransform();
createFireBall(fireballAndSmoke);
fireballAndSmoke->setMatrix(osg::Matrix::rotate(-osg::PI / 2, 1, 0, 0) * osg::Matrix::translate(-8, -10, -3));
flightTransform->addChild(fireballAndSmoke);
createDarkSmoke(fireballAndSmoke);
osgDB::writeNodeFile(*(root.get()), "D:\\data\\Images\\MyScene.osg");
}
void ReadFireballAndSmoke()
{
osg::ref_ptr<osg::Group> root = new osg::Group();
osg::Node* flightNode = osgDB::readNodeFile("D:\\data\\Images\\MyScene.osg");
if (!flightNode)
{
return;
}
osg::MatrixTransform* flightTransform = new osg::MatrixTransform();
flightTransform->addChild(flightNode);
root->addChild(flightTransform);
osgViewer::Viewer viewer;
osgUtil::Simplifier simplifier(0.3f, 4.0f);
osgUtil::Optimizer optimzer;
optimzer.optimize(root.get());
viewer.setSceneData(root.get());
//添加一个事件句柄 相当于添加一个响应 响应鼠标或是键盘 响应L键(控制灯光开关)
viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
//窗口大小变化事件 添加窗口大小改变的句柄 这里响应的是F键
viewer.addEventHandler(new osgViewer::WindowSizeHandler);
//添加一些常用状态设置 添加常用的状态操作,这里会响应S键、W键等等
viewer.addEventHandler(new osgViewer::StatsHandler);
viewer.realize();
viewer.run();
}
//陨石坠落
//int main()
//{
// WriteFireballAndSmoke();
// ReadFireballAndSmoke();
//
// return 0;
//}
int main()
{
osg::ref_ptr<osg::Group> root = new osg::Group();
osg::Node* flightNode = osgDB::readNodeFile("D:\\data\\cessna.osgt");//需要经模型放在项目根目录下(而不是在cpp所在目录)
if (!flightNode)
{
return -1;
}
//飞机变换节点
osg::MatrixTransform* flightTransform = new osg::MatrixTransform();
flightTransform->addChild(flightNode);
root->addChild(flightTransform);
//引擎烟雾
osg::MatrixTransform* fireballAndSmoke = new osg::MatrixTransform();
createFireBall(fireballAndSmoke);
fireballAndSmoke->setMatrix(osg::Matrix::rotate(-osg::PI / 2, 1, 0, 0) * osg::Matrix::translate(-8, -10, -3));
flightTransform->addChild(fireballAndSmoke);
createDarkSmoke(fireballAndSmoke);
osgViewer::Viewer viewer;
osgUtil::Simplifier simplifier(0.3f, 4.0f);
osgUtil::Optimizer optimzer;
optimzer.optimize(root.get());
viewer.setSceneData(root.get());
//添加一个事件句柄 相当于添加一个响应 响应鼠标或是键盘 响应L键(控制灯光开关)
viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
//窗口大小变化事件 添加窗口大小改变的句柄 这里响应的是F键
viewer.addEventHandler(new osgViewer::WindowSizeHandler);
//添加一些常用状态设置 添加常用的状态操作,这里会响应S键、W键等等
viewer.addEventHandler(new osgViewer::StatsHandler);
viewer.realize();
viewer.run();
return 0;
}
#include
#include
#include
#include
#include
//创建爆炸效果
osg::ref_ptr<osg::Node> createExplode()
{
osg::ref_ptr<osg::Group> explode = new osg::Group();
//风向
osg::Vec3 wind(1.0f, 0.0f, 0.0f);
osg::Vec3 position(0.0f, 0.0f, -1.0f);
//爆炸模拟,10.0f为缩放比,默认为1.0f,不缩放
osg::ref_ptr<osgParticle::ExplosionEffect> explosion = new osgParticle::ExplosionEffect(position, 10.0f);
osg::ref_ptr<osgParticle::ExplosionDebrisEffect> explosionDebri =
new osgParticle::ExplosionDebrisEffect(position, 10.0f);
//烟模拟
osg::ref_ptr<osgParticle::SmokeEffect> smoke = new osgParticle::SmokeEffect(position, 10.0f);
//火焰模拟
osg::ref_ptr<osgParticle::FireEffect> fire = new osgParticle::FireEffect(position, 10.0f);
//设置风向
explosion->setWind(wind);
explosionDebri->setWind(wind);
smoke->setWind(wind);
fire->setWind(wind);
//添加子节点
explode->addChild(explosion.get());
explode->addChild(explosionDebri.get());
explode->addChild(smoke.get());
explode->addChild(fire.get());
return explode.get();
}
int main()
{
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
osg::ref_ptr<osg::Group> root = new osg::Group();
//添加爆炸效果
root->addChild(createExplode());
//优化场景数据
osgUtil::Optimizer optimizer;
optimizer.optimize(root.get());
//设置场景数据
viewer->setSceneData(root.get());
//初始化并创建窗口
viewer->realize();
//viewer->setUpViewInWindow(200, 200, 800, 800);
viewer->run();
}
源码:
#include
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::ref_ptr <osg::Group> pRoot = new osg::Group();
//把雪花加入到场景结点
pRoot->addChild(precipitationEffect.get());
osg::ref_ptr<osg::Node> glider = osgDB::readNodeFile("D:\\data\\cessna.osgt");
pRoot->addChild(glider.get());
viewer.setSceneData(pRoot);
//添加一个事件句柄 相当于添加一个响应 响应鼠标或是键盘 响应L键(控制灯光开关)
viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
//窗口大小变化事件 添加窗口大小改变的句柄 这里响应的是F键
viewer.addEventHandler(new osgViewer::WindowSizeHandler);
//添加一些常用状态设置 添加常用的状态操作,这里会响应S键、W键等等
viewer.addEventHandler(new osgViewer::StatsHandler);
viewer.realize();
viewer.run();
}