Osg-Osg实例烟雾粒子效果飞机(Qt5.14.2+osgEarht3.6.5+win10)-No6-EParticlePlane


相关资料:

https://blog.csdn.net/qq_35225556/article/details/109730460?spm=1001.2101.3001.6650.10&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~default-10.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~default-10.no_search_link     osg粒子飞行尾迹

代码实例:

.pro

 1 QT       += core gui widgets
 2 TARGET = TestOsgQt
 3 TEMPLATE = app
 4 DEFINES += QT_DEPRECATED_WARNINGS
 5 CONFIG += c++11
 6 
 7 SOURCES += \
 8         main.cpp
 9 
10 HEADERS +=
11 
12 OsgDir = D:\\RuanJian\\osg365R
13 CONFIG(release, debug|release) {
14         LIBS += -L$${OsgDir}/lib/ -losgDB -losgViewer -lOpenThreads -losgAnimation -losg \
15                                   -losgEarth -losgEarthAnnotation -losgEarthFeatures -losgEarthSymbology -losgEarthUtil \
16                                   -losgQOpenGL -losgUtil -losgText -losgTerrain -losgSim \
17                                   -losgShadow -losgParticle -losgManipulator -losgGA -losgFX \
18                                   -losgWidget
19 } else {
20         LIBS += -L$${OsgDir}/debug/lib/ -losgDBd -losgViewerd -lOpenThreadsd -losgAnimationd -losgd \
21                                   -losgEarthd -losgEarthAnnotationd -losgEarthFeaturesd -losgEarthSymbologyd -losgEarthUtild \
22                                   -losgQOpenGLd -losgUtild -losgTextd -losgTerraind -losgSimd \
23                                   -losgShadowd -losgParticled -losgManipulatord -losgGAd -losgFXd \
24 }
25 
26 
27 INCLUDEPATH += $${OsgDir}/include
28 DEPENDPATH += $${OsgDir}/include

main.cpp

  1 #include 
  2 
  3 #include 
  4 #include 
  5 #include 
  6 #include 
  7 #include 
  8 #include 
  9 #include 
 10 #include 
 11 #include 
 12 #include 
 13 // 雨雪效果
 14 #include 
 15 // 粒子效果
 16 #include 
 17 #include 
 18 #include 
 19 #include 
 20 #include 
 21 #include 
 22 #include 
 23 #include 
 24 #include 
 25 #include 
 26 #include 
 27 #include 
 28 #include 
 29 //
 30 #include 
 31 #include 
 32 #include 
 33 #include 
 34 #include 
 35 #include 
 36 #include 
 37 
 38 //设置飞机动态移动
 39 class transCallback : public osg::NodeCallback
 40 {
 41 public:
 42     transCallback():delta(0.0){}
 43     virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
 44     {
 45         osg::MatrixTransform* mt = dynamic_cast(node);
 46         if(mt)
 47         {
 48             mt->setMatrix(osg::Matrix::translate(0,delta,0));
 49             delta += 0.1;
 50             traverse(node,nv);
 51         }
 52     }
 53 private:
 54     float delta;
 55 };
 56 
 57 //设置飞机尾焰的位置
 58 class firePosCallback : public osgGA::GUIEventHandler
 59 {
 60 public:
 61     firePosCallback(osg::ref_ptr mt,osg::ref_ptr _fire):mat(mt),fire(_fire){}
 62     ~firePosCallback(){}
 63 
 64     bool handle(const osgGA::GUIEventAdapter &ea   ,osgGA::GUIActionAdapter &aa)
 65     {
 66         osgViewer::View *viewer = dynamic_cast(&aa);
 67         switch (ea.getEventType())
 68         {
 69             case osgGA::GUIEventAdapter::FRAME:
 70                 {
 71                     osg::Vec3 position = mat->getMatrix().getTrans();
 72                     fire->setPosition(position);
 73                 }
 74                 break;
 75             default:
 76                 return false;
 77         }
 78         return false;
 79     }
 80 
 81 private:
 82     osg::ref_ptr fire;
 83     osg::ref_ptr mat;
 84 };
 85 
 86 int main(int argc, char *argv[])
 87 {
 88     // ------------------粒子效果-----------------------
 89     osg::ref_ptr root = new osg::Group;
 90     osg::ref_ptr node = osgDB::readNodeFile("D:\\osgFiles\\cessna.osg");
 91     osg::ref_ptr mt = new osg::MatrixTransform;
 92     mt->setUpdateCallback(new transCallback);
 93     mt->addChild(node);
 94 
 95     osg::ref_ptr fire = new osgParticle::FireEffect(osg::Vec3(0,0,0),50,1);
 96     fire->setWind(osg::Vec3(1.0f,0.0f,0.0f));
 97     osg::ref_ptr mtfire = new osg::MatrixTransform;
 98     mtfire->setMatrix(osg::Matrix::rotate(osg::PI_2,osg::X_AXIS));
 99     mtfire->addChild(fire);
100 
101     root->addChild(mt);
102     root->addChild(node);
103     root->addChild(mtfire);
104 
105     osgViewer::Viewer viewer;
106     viewer.addEventHandler(new firePosCallback(mt,fire));
107     viewer.addEventHandler(new osgViewer::StatsHandler);
108     viewer.setUpViewInWindow(50,50,500,400);
109     viewer.setSceneData( root.get() );
110     return viewer.run();
111 }

相关