OSG粒子系统特效-----雨雪、爆炸、烟雾

2023-10-19 00:12

本文主要是介绍OSG粒子系统特效-----雨雪、爆炸、烟雾,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、烟雾效果

飞机坠毁飞机坠毁
在这里插入图片描述
陨石坠落

源码:

// CMyOSGParticle.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <windows.h>
#include <osgViewer/Viewer>#include <osg/Node>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Group>
#include <osg/Billboard>
#include <osg/Texture2D>
#include <osg/Image>
#include <osg/Vec3>
#include <osg/Vec2>
#include <osg/PositionAttitudeTransform>
#include <osg/MatrixTransform>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgUtil/Optimizer>#include <osgViewer/ViewerEventHandlers> //事件监听
#include <osgGA/StateSetManipulator> //事件响应类,对渲染状态进行控制
#include <osgUtil/Simplifier> //简化几何体#include <osgParticle/PrecipitationEffect>
#include <osgParticle/Particle>#include <osgParticle/LinearInterpolator>
#include <osgParticle/ParticleSystem>
#include < osgParticle/RandomRateCounter>
#include <osgParticle/PointPlacer>
#include <osgParticle/RadialShooter>
#include <osgParticle/ModularEmitter>
#include <osgParticle/ParticleSystemUpdater>
#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.0fosg::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.392699fparticleShooter->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;
}

2、爆炸效果

#include <osgParticle/ExplosionDebrisEffect>
#include <osgParticle/ExplosionEffect>
#include <osgParticle/SmokeEffect>
#include <osgParticle/FireEffect>
#include <osgUtil/Optimizer>//创建爆炸效果
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();
}

3、雨雪效果

在这里插入图片描述
源码:

#include <osgParticle/PrecipitationEffect>
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();
}

这篇关于OSG粒子系统特效-----雨雪、爆炸、烟雾的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/236022

相关文章

『功能项目』战士的平A特效【35】

我们打开上一篇34武器的切换实例的项目, 本章要做的事情是在战士的每次按A键时在指定位置生成一个平A特效 首先将之前下载的技能拖拽至场景中 完全解压缩后重命名为AEffect 拖拽至预制体文件夹 进入主角动画的战士动画层级 双击第一次攻击 选择Animation 创建事件 创建的动画事件帧放在攻击动画挥剑指定处 命名为PerpetualAtt

第49课 Scratch入门篇:骇客任务背景特效

骇客任务背景特效 故事背景:   骇客帝国特色背景在黑色中慢慢滚动着! 程序原理:  1 、 角色的设计技巧  2 、克隆体的应用及特效的使用 开始编程   1、使用 黑色的背景: ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/7d74c872f06b4d9fbc88aecee634b074.png#pic_center)   2

【生日视频制作】酒吧一群美女车展模特大屏幕视频改字AE模板修改文字软件生成器教程特效素材【AE模板】

生日视频制作教程酒吧一群美女车展模特大屏幕视频改字AE模板修改文字特效广软件告生成神器素材祝福玩法AE模板工程 怎么如何做的【生日视频制作】酒吧一群美女车展模特大屏幕视频改字AE模板修改文字软件生成器教程特效素材【AE模板】 生日视频制作步骤: 安装AE软件 下载AE模板 把AE模板导入AE软件 修改图片或文字 渲染出视频

【生日视频制作】劳斯莱斯库里南中控改名软件AE模板修改文字软件生成器教程特效素材【AE模板】

生日视频制作教程豪车劳斯莱斯库里南中控改名软件AE模板修改文字特效广告生成神器素材祝福玩法AE模板工程 怎么如何做的【生日视频制作】劳斯莱斯库里南中控改名软件AE模板修改文字软件生成器教程特效素材【AE模板】 生日视频制作步骤: 下载AE模板 安装AE软件 把AE模板导入AE软件 修改图片或文字 渲染出视频

印度再现超级大片,豪华阵容加顶级特效

最近,印度影坛再次掀起了风潮,一部名为《毗湿奴降临》的神话大片强势登陆各大影院,上映首周票房就飙升至105亿卢比,成功占据了票房榜首的位置。之后,这部电影也在北美上映,海外市场的表现同样不俗,收获了相当亮眼的票房成绩。作为一部印度神话科幻大片,《毗湿奴降临》不仅在本土大火,在国际市场上也引发了不小的关注。 《毗湿奴降临》由印度著名导演纳格·阿什温执导,卡司阵容极其豪华,集结了迪皮卡·帕度柯妮

【生日视频制作】海上绿色摩托艇汽车艇车身AE模板修改文字软件生成器教程特效素材【AE模板】

生日视频制作教程海上绿色摩托艇汽车艇车身AE模板修改文字特效广软件告生成神器素材祝福玩法AE模板替换工程 怎么如何做的【生日视频制作】海上绿色摩托艇汽车艇车身AE模板修改文字软件生成器教程特效素材【AE模板】 生日视频制作步骤: 安装AE软件 下载AE模板 把AE模板导入AE软件 修改图片或文字 渲染出视频

【生日视频制作】黑板写文字美女跳舞2版AE模板修改文字软件生成器教程特效素材【AE模板】

生日视频制作教程黑板写文字美女跳舞2版AE模板修改文字特效广软件告生成神器素材祝福玩法AE模板替换工程 怎么如何做的【生日视频制作】黑板写文字美女跳舞2版AE模板修改文字软件生成器教程特效素材【AE模板】 生日视频制作步骤: 安装AE软件 下载AE模板 把AE模板导入AE软件 修改图片或文字 渲染出视频

可以进行非机动车违停、人员聚集、临街摆摊、垃圾满溢、烟雾火情等城市治理场景的智能识别的智慧城管开源了

智慧城管视觉监控平台是一款功能强大且简单易用的实时算法视频监控系统。它的愿景是最底层打通各大芯片厂商相互间的壁垒,省去繁琐重复的适配流程,实现芯片、算法、应用的全流程组合,从而大大减少企业级应用约95%的开发成本。 基于深度学习技术及强大的专家团队,针对多个工业垂类场景进行算法优化,打造最优的工业AI算法模型,提供更加精准的工业AI模型库,客户可直接选择适合自己业务场景的模型,快速实现业务落地

【生日视频制作】酒吧DJ三美女跳舞大屏幕墙字AE模板修改文字软件生成器教程特效素材【AE模板】

生日视频制作教程酒吧DJ三美女跳舞大屏幕墙字AE模板修改文字特效广软件告生成神器素材祝福玩法AE模板替换工程 怎么如何做的【生日视频制作】酒吧DJ三美女跳舞大屏幕墙字AE模板修改文字软件生成器教程特效素材【AE模板】 生日视频制作步骤: 安装AE软件 下载AE模板 把AE模板导入AE软件 修改图片或文字 渲染出视频

QT+OSG+osg-earth显示一个球

目录 1、环境配置 2、在QT  Creator导入相关的库 3、代码部分 4、运行过程中的问题 5、相关参考 重要衔接:QT+OSG显示一个三维模型-CSDN博客 1、环境配置  系统:windows10系统 QT:版本5.15.2        编译器:MSVC2019_64bit        编辑器:QT  Creator OSG版本:3.7.0   64位