osg 实现 实例烟雾粒子效果飞机

 代码中有使用。 osgGA::GUIEventHandler 事件 和  节点回调osg::NodeCallback事件

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
1 #include <QApplication>
  2 
  3 #include <osg/Node>
  4 #include <osg/Group>
  5 #include <osg/Geode>
  6 #include <osg/Geometry>
  7 #include <osg/Texture2D>
  8 #include <osg/StateSet>
  9 #include <osg/PositionAttitudeTransform>
 10 #include <osgViewer/Viewer>
 11 #include <osgDB/ReadFile>
 12 #include <osgParticle/PrecipitationEffect>
 13 // 雨雪效果
 14 #include <osg/MatrixTransform>
 15 // 粒子效果
 16 #include <osgParticle/PrecipitationEffect>
 17 #include <osgParticle/Particle>
 18 #include <osgParticle/LinearInterpolator>
 19 #include <osgParticle/ParticleSystem>
 20 #include <osgParticle/RandomRateCounter>
 21 #include <osgParticle/PointPlacer>
 22 #include <osgParticle/RadialShooter>
 23 #include <osgParticle/ModularEmitter>
 24 #include <osgParticle/ParticleSystemUpdater>
 25 #include <osgParticle/ModularProgram>
 26 #include <osgUtil/Optimizer>
 27 #include <osgUtil/Simplifier>
 28 #include <osgParticle/FireEffect>
 29 // 雾
 30 #include <osg/Fog>
 31 #include <osgDB/ReadFile>
 32 #include <osgViewer/Viewer>
 33 #include <osg/StateSet>
 34 #include <osg/StateAttribute>
 35 #include <osgViewer/ViewerEventHandlers>
 36 #include <osgWidget/ViewerEventHandlers>
 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<osg::MatrixTransform*>(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<osg::MatrixTransform> mt,osg::ref_ptr<osgParticle::FireEffect> _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<osgViewer::View*>(&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<osgParticle::FireEffect> fire;
 83     osg::ref_ptr<osg::MatrixTransform> mat;
 84 };
 85 
 86 int main(int argc, char *argv[])
 87 {
 88     // ------------------粒子效果-----------------------
 89     osg::ref_ptr<osg::Group> root = new osg::Group;
 90     osg::ref_ptr<osg::Node> node = osgDB::readNodeFile("D:\\osgFiles\\cessna.osg");
 91     osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform;
 92     mt->setUpdateCallback(new transCallback);
 93     mt->addChild(node);
 94 
 95     osg::ref_ptr<osgParticle::FireEffect> 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<osg::MatrixTransform> 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 }

猜你喜欢

转载自blog.csdn.net/vcit102/article/details/131897729