创建烟雾的例子效果,并附在飞机上;

#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>//创建火球(燃烧)
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("G:\\YCThirdParty\\Osg\\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("G:\\YCThirdParty\\Osg\\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 main()
{osg::ref_ptr<osg::Group> root = new osg::Group();osg::Node* flightNode = osgDB::readNodeFile("cessna.osg");//需要经模型放在项目根目录下(而不是在cpp所在目录)if (!flightNode){std::cout << "no flight" << std::endl;return  ;}
//飞机变换节点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();
}

效果如下:

image.png

单独将“火球”“浓烟”以.osg形式输出

void main()
{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()), "MyScene.osg");
}

验证输出的osg烟雾效果:

void main()
{osg::ref_ptr<osg::Group> root = new osg::Group();osg::Node* flightNode = osgDB::readNodeFile("MyScene.osg");if (!flightNode){std::cout << "no flight" << std::endl;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();
}

效果展示:

image.png


http://www.taodudu.cc/news/show-1639516.html

相关文章:

  • OSG仿真案例(3)——cmake创建工程
  • OSG仿真案例(5)——模型的平移、缩放、旋转
  • OSG仿真案例(5)——创建火光、爆炸(碎片)
  • OpenCV图像处理(2)——形态学操作
  • OpenCV图像处理(3)——盒维数计算
  • OpenCV图像处理(4)——去除小面积
  • OpenCV图像处理(6)——轮廓标记
  • OpenCV图像处理(7)——图像上写TEXT
  • C++ 常见错误(00) —— C#调用c++做的dll是报错
  • C++ 常见错误(01) —— error LNK1104: 无法打开文件“avcodec.lib”
  • (1)癌症
  • C++ 常见错误(02) —— 将dll(用c++写的)处理的结果展示在界面上
  • C++ 常见错误(03) —— cout输出图像路径
  • OpenCV图像处理(9)——边缘提取方法对比
  • C++ 配置笔记
  • 深度学习(01)——安装anaconda
  • 深度学习(00)——GPU版本的TensorFlow
  • (0)图像处理界面——C#调用C++图像处理的DLL
  • OpenCV图像处理(12)——保存图像到指定位置
  • (0)C#开发环境构建——史上最容易理解的C#界面搭建
  • OpenCV图像处理(13)——指定区域截取和指定区域复制
  • (1)非对称加密
  • (1)非对称加密——RSA——史上最通俗的小白可看懂!
  • OSG仿真案例(7)——osg自动驾驶
  • OpenCV图像处理(14)——文件夹下所有图像转灰度
  • OSG仿真案例(8)——读取FBX格式文件并显示(无动画)
  • OSG仿真案例(9)——JY61陀螺仪控制飞机姿态
  • who I am ?
  • OSG仿真案例(10)——osg仿真录屏抓取图像,自定义修改路径,程序控制(而不是按键控制)...
  • C++相对路径下新建文件夹

OSG仿真案例(4)——创建烟雾粒子效果相关推荐

  1. OSG仿真案例(0)

    //实现基于颜色的材质映射,只有一个地球//调用dll,而不是使用自源程序 int Metr_ClassificationOnlyOneEarth() {osgViewer::Viewer *view ...

  2. OSG仿真案例(1)

    整体代码的主函数部分如下: /*********************************************************main.cpp******************** ...

  3. OSG仿真案例(2)

    从路径中读取一个运动路径(本代码中,是从程序中直接输入三个点),系统自动计算之间插值,达到连续运动的目的.(控制相机的运动的动画漫游) int wavaModule() {int argc = 2;c ...

  4. OSG仿真案例(5)——创建火光、爆炸(碎片)

    创建火光.爆炸(碎片) 所使用的头文件#include "Include_mymap.h" #pragma once #include <osgGA/CameraManipu ...

  5. OSG仿真案例(3)——cmake创建工程

    通过cmake新建一个osg的工程 1.新建一个CMakeLists.txt文件,内容如下: cmake_policy(VERSION 3.9) cmake_minimum_required(VERS ...

  6. OSG仿真案例(8)——读取FBX格式文件并显示(无动画)

    包含的头 #include <iostream> #include <Windows.h> #include <osgDB/ReadFile> #include & ...

  7. OSG仿真案例(5)——模型的平移、缩放、旋转

    这个程序显示的是一头牛,牛的身子朝向屏幕. image.png 旋转后,让牛头朝向屏幕外.如下图所示. image.png OSG中通过旋转模型来改变节点的姿态,使用类 MatrixTransform ...

  8. OSG仿真案例(9)——JY61陀螺仪控制飞机姿态

    前言 在调试osg中模型运动姿态时,总觉得直观性不够强.所以有了想买个硬件陀螺仪(当时并不知道这个硬件应该叫什么名字,在淘宝搜索角度传感器的). 几个驱动 1.CH340驱动 这个驱动在自带资源包里面 ...

  9. OSG仿真案例(7)——osg自动驾驶

    由于在运行时.需要配置各种环境,所以将项目使用cmake生成,确实非常方便. cmake文件: cmake_policy(VERSION 3.9) project(你项目想取的名字 VERSION 0 ...

最新文章

  1. python中利用lxml模块解析xml文件报错XMLSyntaxError: Opening and ending tag mismatch
  2. 如何将Unix时间戳转换为DateTime,反之亦然?
  3. 删除SmartAuditor日记信息报错问题
  4. python中id函数的用法_用id函数做几个测试
  5. hbase动态更改行键设计_谈笑间学会Hbase Rowkey设计
  6. 系统学习 TypeScript(二)——开发流程和语法规则
  7. 捕捉动物游戏java代码_Java Swing打猎射击游戏源码
  8. tomcat未自动解压war包原因分析
  9. MDCC2013会议笔记
  10. 红米9A android版本,红米9a和红米9哪一款手机好?有什么区别存在?
  11. 通俗的解释docker
  12. 多智能体强化学习——相关论文
  13. Java工程师是做什么的?学习java能干什么?
  14. 微信h5页面图片预览
  15. starUML使用教程
  16. Eclipse 安装 Jrebel插件
  17. 已知IP地址,如何计算其子网掩码,默认网关地址,网络地址等。
  18. 驱动篇:底层驱动移植(四)(摘录)
  19. java数据结构队列
  20. linux系统程序下载,Linux平台的下载程序

热门文章

  1. 知名互联网公司系统架构图[第1期]
  2. -----表达式-处理字符串表达式// A(A>0.5)
  3. Python处理Excel文档之openpyxl (三)简单的使用
  4. Django-(分页器,中间介)
  5. ios之JavaScript
  6. [转]oracle分页用两层循环还是三层循环?
  7. this 自引用指针
  8. 深蓝词库转换1.6发布
  9. 更改SAP的字段翻译
  10. [转载] dataframe中有关inf的处理技巧