root/OpenSceneGraph/trunk/examples/osgparticleeffects/osgparticleeffects.cpp @ 6941

Revision 6941, 17.2 kB (checked in by robert, 7 years ago)

From Martin Lavery and Robert Osfield, Updated examples to use a variation of the MIT License

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgparticleeffects.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19
20#include <osgViewer/Viewer>
21
22#include <osg/Group>
23#include <osg/Geode>
24#include <osg/ShapeDrawable>
25#include <osg/Texture2D>
26#include <osg/PositionAttitudeTransform>
27#include <osg/MatrixTransform>
28#include <osg/io_utils>
29
30#include <osgUtil/Optimizer>
31#include <osgUtil/IntersectVisitor>
32
33#include <osgDB/ReadFile>
34
35#include <osgText/Text>
36
37#include <osgParticle/ExplosionEffect>
38#include <osgParticle/ExplosionDebrisEffect>
39#include <osgParticle/SmokeEffect>
40#include <osgParticle/SmokeTrailEffect>
41#include <osgParticle/FireEffect>
42
43// for the grid data..
44#include "../osghangglide/terrain_coords.h"
45
46osg::Vec3 wind(1.0f,0.0f,0.0f);           
47
48osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime)
49{
50    // set up the animation path
51    osg::AnimationPath* animationPath = new osg::AnimationPath;
52    animationPath->setLoopMode(osg::AnimationPath::LOOP);
53   
54    int numSamples = 40;
55    float yaw = 0.0f;
56    float yaw_delta = 2.0f*osg::PI/((float)numSamples-1.0f);
57    float roll = osg::inDegrees(30.0f);
58   
59    double time=0.0f;
60    double time_delta = looptime/(double)numSamples;
61    for(int i=0;i<numSamples;++i)
62    {
63        osg::Vec3 position(center+osg::Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0f));
64        osg::Quat rotation(osg::Quat(roll,osg::Vec3(0.0,1.0,0.0))*osg::Quat(-(yaw+osg::inDegrees(90.0f)),osg::Vec3(0.0,0.0,1.0)));
65       
66        animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation));
67
68        yaw += yaw_delta;
69        time += time_delta;
70
71    }
72    return animationPath;   
73}
74
75osg::Node* createMovingModel(const osg::Vec3& center, float radius)
76{
77    float animationLength = 10.0f;
78
79    osg::AnimationPath* animationPath = createAnimationPath(center,radius,animationLength);
80
81    osg::Group* model = new osg::Group;
82
83    osg::Node* glider = osgDB::readNodeFile("glider.osg");
84    if (glider)
85    {
86        const osg::BoundingSphere& bs = glider->getBound();
87        float size = radius/bs.radius()*0.15f;
88
89        osg::MatrixTransform* positioned = new osg::MatrixTransform;
90        positioned->setDataVariance(osg::Object::STATIC);
91        positioned->setMatrix(osg::Matrix::translate(-bs.center())*
92                                     osg::Matrix::scale(size,size,size)*
93                                     osg::Matrix::rotate(osg::inDegrees(-90.0f),0.0f,0.0f,1.0f));
94   
95        positioned->addChild(glider);
96   
97        osg::PositionAttitudeTransform* xform = new osg::PositionAttitudeTransform;   
98        xform->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
99        xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0,0.5));
100        xform->addChild(positioned);
101
102        model->addChild(xform);
103    }
104 
105    osg::Node* cessna = osgDB::readNodeFile("cessna.osg");
106    if (cessna)
107    {
108        const osg::BoundingSphere& bs = cessna->getBound();
109        float size = radius/bs.radius()*0.15f;
110
111        osg::MatrixTransform* positioned = new osg::MatrixTransform;
112        positioned->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
113        positioned->setDataVariance(osg::Object::STATIC);
114        positioned->setMatrix(osg::Matrix::translate(-bs.center())*
115                                     osg::Matrix::scale(size,size,size)*
116                                     osg::Matrix::rotate(osg::inDegrees(180.0f),0.0f,0.0f,1.0f));
117   
118        //positioned->addChild(cessna);
119        positioned->addChild(cessna);
120   
121        osg::MatrixTransform* xform = new osg::MatrixTransform;
122        xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0f,1.0));
123        xform->addChild(positioned);
124
125        model->addChild(xform);
126    }
127   
128    return model;
129}
130
131
132osg::Vec3 computeTerrainIntersection(osg::Node* subgraph,float x,float y)
133{
134    osgUtil::IntersectVisitor iv;
135    osg::ref_ptr<osg::LineSegment> segDown = new osg::LineSegment;
136
137    const osg::BoundingSphere& bs = subgraph->getBound();
138    float zMax = bs.center().z()+bs.radius();
139    float zMin = bs.center().z()-bs.radius();
140   
141    segDown->set(osg::Vec3(x,y,zMin),osg::Vec3(x,y,zMax));
142    iv.addLineSegment(segDown.get());
143
144    subgraph->accept(iv);
145
146    if (iv.hits())
147    {
148        osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(segDown.get());
149        if (!hitList.empty())
150        {
151            osg::Vec3 ip = hitList.front().getWorldIntersectPoint();
152            return  ip;
153        }
154    }
155
156    return osg::Vec3(x,y,0.0f);
157}
158
159
160//////////////////////////////////////////////////////////////////////////////
161// MAIN SCENE GRAPH BUILDING FUNCTION
162//////////////////////////////////////////////////////////////////////////////
163
164void build_world(osg::Group *root)
165{
166
167    osg::Geode* terrainGeode = new osg::Geode;
168    // create terrain
169    {
170        osg::StateSet* stateset = new osg::StateSet();
171        osg::Image* image = osgDB::readImageFile("Images/lz.rgb");
172        if (image)
173        {
174            osg::Texture2D* texture = new osg::Texture2D;
175            texture->setImage(image);
176            stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
177        }
178
179        terrainGeode->setStateSet( stateset );
180
181        float size = 1000; // 10km;
182        float scale = size/39.0f; // 10km;
183        float z_scale = scale*3.0f;
184
185        osg::HeightField* grid = new osg::HeightField;
186        grid->allocate(38,39);
187        grid->setXInterval(scale);
188        grid->setYInterval(scale);
189
190        for(unsigned int r=0;r<39;++r)
191        {
192            for(unsigned int c=0;c<38;++c)
193            {
194                grid->setHeight(c,r,z_scale*vertex[r+c*39][2]);
195            }
196        }
197        terrainGeode->addDrawable(new osg::ShapeDrawable(grid));
198       
199        root->addChild(terrainGeode);
200    }   
201
202
203    // create particle effects
204    {   
205        osg::Vec3 position = computeTerrainIntersection(terrainGeode,100.0f,100.0f);
206
207        osgParticle::ExplosionEffect* explosion = new osgParticle::ExplosionEffect(position, 10.0f);
208        osgParticle::ExplosionDebrisEffect* explosionDebri = new osgParticle::ExplosionDebrisEffect(position, 10.0f);
209        osgParticle::SmokeEffect* smoke = new osgParticle::SmokeEffect(position, 10.0f);
210        osgParticle::FireEffect* fire = new osgParticle::FireEffect(position, 10.0f);
211
212        explosion->setWind(wind);
213        explosionDebri->setWind(wind);
214        smoke->setWind(wind);
215        fire->setWind(wind);
216
217        root->addChild(explosion);
218        root->addChild(explosionDebri);
219        root->addChild(smoke);
220        root->addChild(fire);
221    }
222   
223    // create particle effects
224    {   
225        osg::Vec3 position = computeTerrainIntersection(terrainGeode,200.0f,100.0f);
226
227        osgParticle::ExplosionEffect* explosion = new osgParticle::ExplosionEffect(position, 1.0f);
228        osgParticle::ExplosionDebrisEffect* explosionDebri = new osgParticle::ExplosionDebrisEffect(position, 1.0f);
229        osgParticle::SmokeEffect* smoke = new osgParticle::SmokeEffect(position, 1.0f);
230        osgParticle::FireEffect* fire = new osgParticle::FireEffect(position, 1.0f);
231
232        explosion->setWind(wind);
233        explosionDebri->setWind(wind);
234        smoke->setWind(wind);
235        fire->setWind(wind);
236
237        root->addChild(explosion);
238        root->addChild(explosionDebri);
239        root->addChild(smoke);
240        root->addChild(fire);
241    }
242
243    // create the moving models.
244    {
245        root->addChild(createMovingModel(osg::Vec3(500.0f,500.0f,500.0f),300.0f));
246    }
247}
248
249
250// class to handle events with a pick
251class PickHandler : public osgGA::GUIEventHandler {
252public:
253
254    PickHandler() {}       
255   
256    bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
257    {
258        switch(ea.getEventType())
259        {
260            case(osgGA::GUIEventAdapter::PUSH):
261            {
262                osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
263                pick(viewer,ea);
264            }
265            return false;
266
267        default:
268            return false;
269        }
270    }
271
272    void pick(osgViewer::Viewer* viewer, const osgGA::GUIEventAdapter& ea)
273    {
274        osg::Group* root = dynamic_cast<osg::Group*>(viewer->getSceneData());       
275        if (!root) return;
276
277        osgUtil::LineSegmentIntersector::Intersections intersections;
278        if (viewer->computeIntersections(ea.getX(),ea.getY(),intersections))
279        {
280            const osgUtil::LineSegmentIntersector::Intersection& hit = *intersections.begin();
281
282            bool handleMovingModels = false;
283            const osg::NodePath& nodePath = hit.nodePath;
284            for(osg::NodePath::const_iterator nitr=nodePath.begin();
285                nitr!=nodePath.end();
286                ++nitr)
287            {
288                const osg::Transform* transform = dynamic_cast<const osg::Transform*>(*nitr);
289                if (transform)
290                {
291                    if (transform->getDataVariance()==osg::Object::DYNAMIC) handleMovingModels=true;
292                }
293            }
294           
295            osg::Vec3 position = handleMovingModels ? hit.getLocalIntersectPoint() : hit.getWorldIntersectPoint();
296            float scale = 10.0f * ((float)rand() / (float)RAND_MAX);
297            float intensity = 1.0f;
298
299            osgParticle::ExplosionEffect* explosion = new osgParticle::ExplosionEffect(position, scale, intensity);
300            osgParticle::ExplosionDebrisEffect* explosionDebri = new osgParticle::ExplosionDebrisEffect(position, scale, intensity);
301            osgParticle::FireEffect* fire = new osgParticle::FireEffect(position, scale, intensity);
302            osgParticle::ParticleEffect* smoke = 0;
303            if (handleMovingModels)
304                smoke =  new osgParticle::SmokeTrailEffect(position, scale, intensity);
305            else
306                smoke =  new osgParticle::SmokeEffect(position, scale, intensity);
307           
308            explosion->setWind(wind);
309            explosionDebri->setWind(wind);
310            smoke->setWind(wind);
311            fire->setWind(wind);
312
313            osg::Group* effectsGroup = new osg::Group;
314            effectsGroup->addChild(explosion);
315            effectsGroup->addChild(explosionDebri);
316            effectsGroup->addChild(smoke);
317            effectsGroup->addChild(fire);
318           
319
320            if (handleMovingModels)
321            {
322                // insert particle effects alongside the hit node, therefore able to track that nodes movement,
323                // however, this does require us to insert the ParticleSystem itself into the root of the scene graph
324                // seperately from the the main particle effects group which contains the emitters and programs.
325                // the follow code block implements this, note the path for handling particle effects which arn't attached to
326                // moving models is easy - just a single line of code!
327           
328                // tell the effects not to attach to the particle system locally for rendering, as we'll handle add it into the
329                // scene graph ourselves.
330                explosion->setUseLocalParticleSystem(false);
331                explosionDebri->setUseLocalParticleSystem(false);
332                smoke->setUseLocalParticleSystem(false);
333                fire->setUseLocalParticleSystem(false);
334
335                // find a place to insert the particle effects group alongside the hit node.
336                // there are two possible ways that this can be done, either insert it into
337                // a pre-existing group along side the hit node, or if no pre existing group
338                // is found then this needs to be inserted above the hit node, and then the
339                // particle effect can be inserted into this.
340                osg::ref_ptr<osg::Node> hitNode = hit.nodePath.back();
341                osg::Node::ParentList parents = hitNode->getParents();               
342                osg::Group* insertGroup = 0;
343                unsigned int numGroupsFound = 0;
344                for(osg::Node::ParentList::iterator itr=parents.begin();
345                    itr!=parents.end();
346                    ++itr)
347                {
348                    if (typeid(*(*itr))==typeid(osg::Group))
349                    {
350                        ++numGroupsFound;
351                        insertGroup = *itr;
352                    }
353                }               
354                if (numGroupsFound==parents.size() && numGroupsFound==1 && insertGroup)
355                {
356                    osg::notify(osg::INFO)<<"PickHandler::pick(,) hit node's parent is a single osg::Group so we can simple the insert the particle effects group here."<<std::endl;
357
358                    // just reuse the existing group.
359                    insertGroup->addChild(effectsGroup);
360                }
361                else
362                {           
363                    osg::notify(osg::INFO)<<"PickHandler::pick(,) hit node doesn't have an appropriate osg::Group node to insert particle effects into, inserting a new osg::Group."<<std::endl;
364                    insertGroup = new osg::Group;
365                    for(osg::Node::ParentList::iterator itr=parents.begin();
366                        itr!=parents.end();
367                        ++itr)
368                    {
369                        (*itr)->replaceChild(hit.nodePath.back(),insertGroup);
370                    }
371                    insertGroup->addChild(hitNode.get());
372                    insertGroup->addChild(effectsGroup);
373                }
374
375                // finally insert the particle systems into a Geode and attach to the root of the scene graph so the particle system
376                // can be rendered.
377                osg::Geode* geode = new osg::Geode;
378                geode->addDrawable(explosion->getParticleSystem());
379                geode->addDrawable(explosionDebri->getParticleSystem());
380                geode->addDrawable(smoke->getParticleSystem());
381                geode->addDrawable(fire->getParticleSystem());
382               
383                root->addChild(geode);
384
385            }
386            else
387            {
388                // when we don't have moving models we can simple insert the particle effect into the root of the scene graph
389                osg::notify(osg::INFO)<<"PickHandler::pick(,) adding particle effects to root node."<<std::endl;
390                root->addChild(effectsGroup);
391            }
392
393#if 0           
394            osg::Geode* geode = new osg::Geode;
395            geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(position,scale)));
396            group->addChild(geode);
397#endif
398 
399        }
400    }
401   
402protected:
403    virtual ~PickHandler() {}
404};
405
406// function used in debugging
407void insertParticle(osg::Group* root, const osg::Vec3& center, float radius)
408{
409    bool handleMovingModels = false;
410
411    osg::Vec3 position = center +
412               osg::Vec3( radius * (((float)rand() / (float)RAND_MAX)-0.5)*2.0,
413                          radius * (((float)rand() / (float)RAND_MAX)-0.5)*2.0,
414                          0.0f);
415
416    float scale = 10.0f * ((float)rand() / (float)RAND_MAX);
417    float intensity = 1.0f;
418
419    osgParticle::ExplosionEffect* explosion = new osgParticle::ExplosionEffect(position, scale, intensity);
420    osgParticle::ExplosionDebrisEffect* explosionDebri = new osgParticle::ExplosionDebrisEffect(position, scale, intensity);
421    osgParticle::FireEffect* fire = new osgParticle::FireEffect(position, scale, intensity);
422    osgParticle::ParticleEffect* smoke = 0;
423    if (handleMovingModels)
424        smoke =  new osgParticle::SmokeTrailEffect(position, scale, intensity);
425    else
426        smoke =  new osgParticle::SmokeEffect(position, scale, intensity);
427
428    explosion->setWind(wind);
429    explosionDebri->setWind(wind);
430    smoke->setWind(wind);
431    fire->setWind(wind);
432
433    osg::Group* effectsGroup = new osg::Group;
434    effectsGroup->addChild(explosion);
435    effectsGroup->addChild(explosionDebri);
436    effectsGroup->addChild(smoke);
437    effectsGroup->addChild(fire);
438
439    root->addChild(effectsGroup);
440}
441
442//////////////////////////////////////////////////////////////////////////////
443// main()
444//////////////////////////////////////////////////////////////////////////////
445
446int main(int, char **)
447{
448    // construct the viewer.
449    osgViewer::Viewer viewer;
450
451    // register the pick handler
452    viewer.addEventHandler(new PickHandler());
453   
454    osg::Group *root = new osg::Group;
455    build_world(root);
456
457    osgUtil::Optimizer optimizer;
458    optimizer.optimize(root);
459   
460    // add a viewport to the viewer and attach the scene graph.
461    viewer.setSceneData(root);
462       
463    return viewer.run();
464}
Note: See TracBrowser for help on using the browser.