OpenSceneGraph/examples/osgparticleeffects/osgparticleeffects.cpp

461 lines
17 KiB
C++
Raw Normal View History

/* OpenSceneGraph example, osgparticleeffects.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <osgViewer/Viewer>
2005-04-04 16:47:21 +08:00
#include <osg/Group>
#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osg/Texture2D>
#include <osg/PositionAttitudeTransform>
#include <osg/MatrixTransform>
#include <osg/io_utils>
2005-04-04 16:47:21 +08:00
#include <osgUtil/Optimizer>
#include <osgDB/ReadFile>
#include <osgText/Text>
#include <osgParticle/ExplosionEffect>
#include <osgParticle/ExplosionDebrisEffect>
2005-04-04 16:47:21 +08:00
#include <osgParticle/SmokeEffect>
#include <osgParticle/SmokeTrailEffect>
2005-04-04 16:47:21 +08:00
#include <osgParticle/FireEffect>
// for the grid data..
#include "../osghangglide/terrain_coords.h"
2014-05-14 18:19:43 +08:00
osg::Vec3 wind(1.0f,0.0f,0.0f);
2005-04-04 16:47:21 +08:00
osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime)
{
2014-05-14 18:19:43 +08:00
// set up the animation path
2005-04-04 16:47:21 +08:00
osg::AnimationPath* animationPath = new osg::AnimationPath;
animationPath->setLoopMode(osg::AnimationPath::LOOP);
2014-05-14 18:19:43 +08:00
2005-04-04 16:47:21 +08:00
int numSamples = 40;
float yaw = 0.0f;
float yaw_delta = 2.0f*osg::PI/((float)numSamples-1.0f);
float roll = osg::inDegrees(30.0f);
2014-05-14 18:19:43 +08:00
2005-04-04 16:47:21 +08:00
double time=0.0f;
double time_delta = looptime/(double)numSamples;
for(int i=0;i<numSamples;++i)
{
osg::Vec3 position(center+osg::Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0f));
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)));
2014-05-14 18:19:43 +08:00
2005-04-04 16:47:21 +08:00
animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation));
yaw += yaw_delta;
time += time_delta;
}
2014-05-14 18:19:43 +08:00
return animationPath;
2005-04-04 16:47:21 +08:00
}
osg::Node* createMovingModel(const osg::Vec3& center, float radius)
{
float animationLength = 10.0f;
osg::AnimationPath* animationPath = createAnimationPath(center,radius,animationLength);
Introduced CMake option OSG_PROVIDE_READFILE option that defaults to ON, but when switched to OFF disables the building of the osgDB::read*File() methods, forcing users to use osgDB::readRef*File() methods. The later is preferable as it closes a potential threading bug when using paging databases in conjunction with the osgDB::Registry Object Cache. This threading bug occurs when one thread gets an object from the Cache via an osgDB::read*File() call where only a pointer to the object is passed back, so taking a reference to the object is delayed till it gets reassigned to a ref_ptr<>, but at the same time another thread calls a flush of the Object Cache deleting this object as it's referenceCount is now zero. Using osgDB::readREf*File() makes sure the a ref_ptr<> is passed back and the referenceCount never goes to zero. To ensure the OSG builds when OSG_PROVIDE_READFILE is to OFF the many cases of osgDB::read*File() usage had to be replaced with a ref_ptr<> osgDB::readRef*File() usage. The avoid this change causing lots of other client code to be rewritten to handle the use of ref_ptr<> in place of C pointer I introduced a serious of templte methods in various class to adapt ref_ptr<> to the underly C pointer to be passed to old OSG API's, example of this is found in include/osg/Group: bool addChild(Node* child); // old method which can only be used with a Node* tempalte<class T> bool addChild(const osg::ref_ptr<T>& child) { return addChild(child.get()); } // adapter template method These changes together cover 149 modified files, so it's a large submission. This extent of changes are warrent to make use of the Object Cache and multi-threaded loaded more robust. git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@15164 16af8721-9629-0410-8352-f15c8da7e697
2015-10-22 21:42:19 +08:00
osg::ref_ptr<osg::Group> model = new osg::Group;
2005-04-04 16:47:21 +08:00
Introduced CMake option OSG_PROVIDE_READFILE option that defaults to ON, but when switched to OFF disables the building of the osgDB::read*File() methods, forcing users to use osgDB::readRef*File() methods. The later is preferable as it closes a potential threading bug when using paging databases in conjunction with the osgDB::Registry Object Cache. This threading bug occurs when one thread gets an object from the Cache via an osgDB::read*File() call where only a pointer to the object is passed back, so taking a reference to the object is delayed till it gets reassigned to a ref_ptr<>, but at the same time another thread calls a flush of the Object Cache deleting this object as it's referenceCount is now zero. Using osgDB::readREf*File() makes sure the a ref_ptr<> is passed back and the referenceCount never goes to zero. To ensure the OSG builds when OSG_PROVIDE_READFILE is to OFF the many cases of osgDB::read*File() usage had to be replaced with a ref_ptr<> osgDB::readRef*File() usage. The avoid this change causing lots of other client code to be rewritten to handle the use of ref_ptr<> in place of C pointer I introduced a serious of templte methods in various class to adapt ref_ptr<> to the underly C pointer to be passed to old OSG API's, example of this is found in include/osg/Group: bool addChild(Node* child); // old method which can only be used with a Node* tempalte<class T> bool addChild(const osg::ref_ptr<T>& child) { return addChild(child.get()); } // adapter template method These changes together cover 149 modified files, so it's a large submission. This extent of changes are warrent to make use of the Object Cache and multi-threaded loaded more robust. git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@15164 16af8721-9629-0410-8352-f15c8da7e697
2015-10-22 21:42:19 +08:00
osg::ref_ptr<osg::Node> glider = osgDB::readRefNodeFile("glider.osgt");
2005-04-04 16:47:21 +08:00
if (glider)
{
const osg::BoundingSphere& bs = glider->getBound();
float size = radius/bs.radius()*0.15f;
2005-04-04 16:47:21 +08:00
osg::MatrixTransform* positioned = new osg::MatrixTransform;
positioned->setDataVariance(osg::Object::STATIC);
positioned->setMatrix(osg::Matrix::translate(-bs.center())*
osg::Matrix::scale(size,size,size)*
osg::Matrix::rotate(osg::inDegrees(-90.0f),0.0f,0.0f,1.0f));
2014-05-14 18:19:43 +08:00
2005-04-04 16:47:21 +08:00
positioned->addChild(glider);
2014-05-14 18:19:43 +08:00
osg::PositionAttitudeTransform* xform = new osg::PositionAttitudeTransform;
xform->setDataVariance(osg::Object::DYNAMIC);
2005-04-04 16:47:21 +08:00
xform->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0,0.5));
xform->addChild(positioned);
model->addChild(xform);
}
2014-05-14 18:19:43 +08:00
Introduced CMake option OSG_PROVIDE_READFILE option that defaults to ON, but when switched to OFF disables the building of the osgDB::read*File() methods, forcing users to use osgDB::readRef*File() methods. The later is preferable as it closes a potential threading bug when using paging databases in conjunction with the osgDB::Registry Object Cache. This threading bug occurs when one thread gets an object from the Cache via an osgDB::read*File() call where only a pointer to the object is passed back, so taking a reference to the object is delayed till it gets reassigned to a ref_ptr<>, but at the same time another thread calls a flush of the Object Cache deleting this object as it's referenceCount is now zero. Using osgDB::readREf*File() makes sure the a ref_ptr<> is passed back and the referenceCount never goes to zero. To ensure the OSG builds when OSG_PROVIDE_READFILE is to OFF the many cases of osgDB::read*File() usage had to be replaced with a ref_ptr<> osgDB::readRef*File() usage. The avoid this change causing lots of other client code to be rewritten to handle the use of ref_ptr<> in place of C pointer I introduced a serious of templte methods in various class to adapt ref_ptr<> to the underly C pointer to be passed to old OSG API's, example of this is found in include/osg/Group: bool addChild(Node* child); // old method which can only be used with a Node* tempalte<class T> bool addChild(const osg::ref_ptr<T>& child) { return addChild(child.get()); } // adapter template method These changes together cover 149 modified files, so it's a large submission. This extent of changes are warrent to make use of the Object Cache and multi-threaded loaded more robust. git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@15164 16af8721-9629-0410-8352-f15c8da7e697
2015-10-22 21:42:19 +08:00
osg::ref_ptr<osg::Node> cessna = osgDB::readRefNodeFile("cessna.osgt");
2005-04-04 16:47:21 +08:00
if (cessna)
{
const osg::BoundingSphere& bs = cessna->getBound();
float size = radius/bs.radius()*0.15f;
2005-04-04 16:47:21 +08:00
osg::MatrixTransform* positioned = new osg::MatrixTransform;
positioned->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
positioned->setDataVariance(osg::Object::STATIC);
positioned->setMatrix(osg::Matrix::translate(-bs.center())*
osg::Matrix::scale(size,size,size)*
osg::Matrix::rotate(osg::inDegrees(180.0f),0.0f,0.0f,1.0f));
2014-05-14 18:19:43 +08:00
2005-04-04 16:47:21 +08:00
//positioned->addChild(cessna);
positioned->addChild(cessna);
2014-05-14 18:19:43 +08:00
2005-04-04 16:47:21 +08:00
osg::MatrixTransform* xform = new osg::MatrixTransform;
xform->setDataVariance(osg::Object::DYNAMIC);
2005-04-04 16:47:21 +08:00
xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0f,1.0));
xform->addChild(positioned);
model->addChild(xform);
}
2014-05-14 18:19:43 +08:00
Introduced CMake option OSG_PROVIDE_READFILE option that defaults to ON, but when switched to OFF disables the building of the osgDB::read*File() methods, forcing users to use osgDB::readRef*File() methods. The later is preferable as it closes a potential threading bug when using paging databases in conjunction with the osgDB::Registry Object Cache. This threading bug occurs when one thread gets an object from the Cache via an osgDB::read*File() call where only a pointer to the object is passed back, so taking a reference to the object is delayed till it gets reassigned to a ref_ptr<>, but at the same time another thread calls a flush of the Object Cache deleting this object as it's referenceCount is now zero. Using osgDB::readREf*File() makes sure the a ref_ptr<> is passed back and the referenceCount never goes to zero. To ensure the OSG builds when OSG_PROVIDE_READFILE is to OFF the many cases of osgDB::read*File() usage had to be replaced with a ref_ptr<> osgDB::readRef*File() usage. The avoid this change causing lots of other client code to be rewritten to handle the use of ref_ptr<> in place of C pointer I introduced a serious of templte methods in various class to adapt ref_ptr<> to the underly C pointer to be passed to old OSG API's, example of this is found in include/osg/Group: bool addChild(Node* child); // old method which can only be used with a Node* tempalte<class T> bool addChild(const osg::ref_ptr<T>& child) { return addChild(child.get()); } // adapter template method These changes together cover 149 modified files, so it's a large submission. This extent of changes are warrent to make use of the Object Cache and multi-threaded loaded more robust. git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@15164 16af8721-9629-0410-8352-f15c8da7e697
2015-10-22 21:42:19 +08:00
return model.release();
2005-04-04 16:47:21 +08:00
}
osg::Vec3 computeTerrainIntersection(osg::Node* subgraph,float x,float y)
{
const osg::BoundingSphere& bs = subgraph->getBound();
float zMax = bs.center().z()+bs.radius();
float zMin = bs.center().z()-bs.radius();
2014-05-14 18:19:43 +08:00
osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector =
new osgUtil::LineSegmentIntersector(osg::Vec3(x,y,zMin),osg::Vec3(x,y,zMax));
osgUtil::IntersectionVisitor iv(intersector.get());
2005-04-04 16:47:21 +08:00
subgraph->accept(iv);
if (intersector->containsIntersections())
2005-04-04 16:47:21 +08:00
{
return intersector->getFirstIntersection().getWorldIntersectPoint();
2005-04-04 16:47:21 +08:00
}
return osg::Vec3(x,y,0.0f);
}
//////////////////////////////////////////////////////////////////////////////
// MAIN SCENE GRAPH BUILDING FUNCTION
//////////////////////////////////////////////////////////////////////////////
void build_world(osg::Group *root)
{
osg::Geode* terrainGeode = new osg::Geode;
// create terrain
{
Introduced CMake option OSG_PROVIDE_READFILE option that defaults to ON, but when switched to OFF disables the building of the osgDB::read*File() methods, forcing users to use osgDB::readRef*File() methods. The later is preferable as it closes a potential threading bug when using paging databases in conjunction with the osgDB::Registry Object Cache. This threading bug occurs when one thread gets an object from the Cache via an osgDB::read*File() call where only a pointer to the object is passed back, so taking a reference to the object is delayed till it gets reassigned to a ref_ptr<>, but at the same time another thread calls a flush of the Object Cache deleting this object as it's referenceCount is now zero. Using osgDB::readREf*File() makes sure the a ref_ptr<> is passed back and the referenceCount never goes to zero. To ensure the OSG builds when OSG_PROVIDE_READFILE is to OFF the many cases of osgDB::read*File() usage had to be replaced with a ref_ptr<> osgDB::readRef*File() usage. The avoid this change causing lots of other client code to be rewritten to handle the use of ref_ptr<> in place of C pointer I introduced a serious of templte methods in various class to adapt ref_ptr<> to the underly C pointer to be passed to old OSG API's, example of this is found in include/osg/Group: bool addChild(Node* child); // old method which can only be used with a Node* tempalte<class T> bool addChild(const osg::ref_ptr<T>& child) { return addChild(child.get()); } // adapter template method These changes together cover 149 modified files, so it's a large submission. This extent of changes are warrent to make use of the Object Cache and multi-threaded loaded more robust. git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@15164 16af8721-9629-0410-8352-f15c8da7e697
2015-10-22 21:42:19 +08:00
osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet();
osg::ref_ptr<osg::Image> image = osgDB::readRefImageFile("Images/lz.rgb");
2005-04-04 16:47:21 +08:00
if (image)
{
2005-11-18 04:22:55 +08:00
osg::Texture2D* texture = new osg::Texture2D;
texture->setImage(image);
stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
2005-04-04 16:47:21 +08:00
}
terrainGeode->setStateSet( stateset );
float size = 1000; // 10km;
float scale = size/39.0f; // 10km;
float z_scale = scale*3.0f;
osg::HeightField* grid = new osg::HeightField;
grid->allocate(38,39);
2005-04-04 16:47:21 +08:00
grid->setXInterval(scale);
grid->setYInterval(scale);
for(unsigned int r=0;r<39;++r)
{
2005-11-18 04:22:55 +08:00
for(unsigned int c=0;c<38;++c)
{
grid->setHeight(c,r,z_scale*vertex[r+c*39][2]);
}
2005-04-04 16:47:21 +08:00
}
terrainGeode->addDrawable(new osg::ShapeDrawable(grid));
2014-05-14 18:19:43 +08:00
2005-04-04 16:47:21 +08:00
root->addChild(terrainGeode);
2014-05-14 18:19:43 +08:00
}
2005-04-04 16:47:21 +08:00
// create particle effects
2014-05-14 18:19:43 +08:00
{
2005-04-04 16:47:21 +08:00
osg::Vec3 position = computeTerrainIntersection(terrainGeode,100.0f,100.0f);
osgParticle::ExplosionEffect* explosion = new osgParticle::ExplosionEffect(position, 10.0f);
osgParticle::ExplosionDebrisEffect* explosionDebri = new osgParticle::ExplosionDebrisEffect(position, 10.0f);
2005-04-04 16:47:21 +08:00
osgParticle::SmokeEffect* smoke = new osgParticle::SmokeEffect(position, 10.0f);
osgParticle::FireEffect* fire = new osgParticle::FireEffect(position, 10.0f);
explosion->setWind(wind);
explosionDebri->setWind(wind);
smoke->setWind(wind);
fire->setWind(wind);
root->addChild(explosion);
root->addChild(explosionDebri);
root->addChild(smoke);
root->addChild(fire);
}
2014-05-14 18:19:43 +08:00
2005-04-04 16:47:21 +08:00
// create particle effects
2014-05-14 18:19:43 +08:00
{
2005-04-04 16:47:21 +08:00
osg::Vec3 position = computeTerrainIntersection(terrainGeode,200.0f,100.0f);
osgParticle::ExplosionEffect* explosion = new osgParticle::ExplosionEffect(position, 1.0f);
osgParticle::ExplosionDebrisEffect* explosionDebri = new osgParticle::ExplosionDebrisEffect(position, 1.0f);
2005-04-04 16:47:21 +08:00
osgParticle::SmokeEffect* smoke = new osgParticle::SmokeEffect(position, 1.0f);
osgParticle::FireEffect* fire = new osgParticle::FireEffect(position, 1.0f);
explosion->setWind(wind);
explosionDebri->setWind(wind);
smoke->setWind(wind);
fire->setWind(wind);
root->addChild(explosion);
root->addChild(explosionDebri);
root->addChild(smoke);
root->addChild(fire);
}
// create the moving models.
{
root->addChild(createMovingModel(osg::Vec3(500.0f,500.0f,500.0f),300.0f));
2005-04-04 16:47:21 +08:00
}
}
// class to handle events with a pick
class PickHandler : public osgGA::GUIEventHandler {
2014-05-14 18:19:43 +08:00
public:
PickHandler() {}
2005-04-04 16:47:21 +08:00
bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
{
switch(ea.getEventType())
{
case(osgGA::GUIEventAdapter::PUSH):
{
osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
2005-04-04 16:47:21 +08:00
pick(viewer,ea);
}
return false;
default:
return false;
}
}
void pick(osgViewer::Viewer* viewer, const osgGA::GUIEventAdapter& ea)
2005-04-04 16:47:21 +08:00
{
2014-05-14 18:19:43 +08:00
osg::Group* root = dynamic_cast<osg::Group*>(viewer->getSceneData());
2005-04-04 16:47:21 +08:00
if (!root) return;
2014-05-14 18:19:43 +08:00
osgUtil::LineSegmentIntersector::Intersections intersections;
if (viewer->computeIntersections(ea,intersections))
2005-04-04 16:47:21 +08:00
{
const osgUtil::LineSegmentIntersector::Intersection& hit = *intersections.begin();
2005-04-04 16:47:21 +08:00
bool handleMovingModels = false;
const osg::NodePath& nodePath = hit.nodePath;
2005-04-04 16:47:21 +08:00
for(osg::NodePath::const_iterator nitr=nodePath.begin();
nitr!=nodePath.end();
++nitr)
{
const osg::Transform* transform = dynamic_cast<const osg::Transform*>(*nitr);
if (transform)
{
if (transform->getDataVariance()==osg::Object::DYNAMIC) handleMovingModels=true;
}
}
2014-05-14 18:19:43 +08:00
2005-04-04 16:47:21 +08:00
osg::Vec3 position = handleMovingModels ? hit.getLocalIntersectPoint() : hit.getWorldIntersectPoint();
float scale = 10.0f * ((float)rand() / (float)RAND_MAX);
float intensity = 1.0f;
2005-04-04 16:47:21 +08:00
osgParticle::ExplosionEffect* explosion = new osgParticle::ExplosionEffect(position, scale, intensity);
osgParticle::ExplosionDebrisEffect* explosionDebri = new osgParticle::ExplosionDebrisEffect(position, scale, intensity);
2005-04-04 16:47:21 +08:00
osgParticle::FireEffect* fire = new osgParticle::FireEffect(position, scale, intensity);
osgParticle::ParticleEffect* smoke = 0;
if (handleMovingModels)
smoke = new osgParticle::SmokeTrailEffect(position, scale, intensity);
else
smoke = new osgParticle::SmokeEffect(position, scale, intensity);
2014-05-14 18:19:43 +08:00
2005-04-04 16:47:21 +08:00
explosion->setWind(wind);
explosionDebri->setWind(wind);
smoke->setWind(wind);
fire->setWind(wind);
osg::Group* effectsGroup = new osg::Group;
effectsGroup->addChild(explosion);
effectsGroup->addChild(explosionDebri);
effectsGroup->addChild(smoke);
effectsGroup->addChild(fire);
2014-05-14 18:19:43 +08:00
2005-04-04 16:47:21 +08:00
if (handleMovingModels)
{
// insert particle effects alongside the hit node, therefore able to track that nodes movement,
// however, this does require us to insert the ParticleSystem itself into the root of the scene graph
// separately from the main particle effects group which contains the emitters and programs.
2018-04-21 00:18:22 +08:00
// the follow code block implements this, note the path for handling particle effects which aren't attached to
2005-04-04 16:47:21 +08:00
// moving models is easy - just a single line of code!
2014-05-14 18:19:43 +08:00
// tell the effects not to attach to the particle system locally for rendering, as we'll handle add it into the
2005-04-04 16:47:21 +08:00
// scene graph ourselves.
explosion->setUseLocalParticleSystem(false);
explosionDebri->setUseLocalParticleSystem(false);
smoke->setUseLocalParticleSystem(false);
fire->setUseLocalParticleSystem(false);
// find a place to insert the particle effects group alongside the hit node.
// there are two possible ways that this can be done, either insert it into
// a pre-existing group along side the hit node, or if no pre existing group
// is found then this needs to be inserted above the hit node, and then the
// particle effect can be inserted into this.
osg::ref_ptr<osg::Node> hitNode = hit.nodePath.back();
2014-05-14 18:19:43 +08:00
osg::Node::ParentList parents = hitNode->getParents();
osg::Group* insertGroup = 0;
2005-04-04 16:47:21 +08:00
unsigned int numGroupsFound = 0;
for(osg::Node::ParentList::iterator itr=parents.begin();
itr!=parents.end();
++itr)
{
osg::Group* parent = (*itr);
if (typeid(*parent)==typeid(osg::Group))
2005-04-04 16:47:21 +08:00
{
++numGroupsFound;
insertGroup = parent;
2005-04-04 16:47:21 +08:00
}
2014-05-14 18:19:43 +08:00
}
2005-04-04 16:47:21 +08:00
if (numGroupsFound==parents.size() && numGroupsFound==1 && insertGroup)
{
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;
2005-04-04 16:47:21 +08:00
// just reuse the existing group.
insertGroup->addChild(effectsGroup);
}
else
2014-05-14 18:19:43 +08:00
{
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;
2005-04-04 16:47:21 +08:00
insertGroup = new osg::Group;
for(osg::Node::ParentList::iterator itr=parents.begin();
itr!=parents.end();
++itr)
{
(*itr)->replaceChild(hit.nodePath.back(),insertGroup);
2005-04-04 16:47:21 +08:00
}
insertGroup->addChild(hitNode.get());
2005-04-04 16:47:21 +08:00
insertGroup->addChild(effectsGroup);
}
// finally insert the particle systems into a Geode and attach to the root of the scene graph so the particle system
// can be rendered.
osg::Geode* geode = new osg::Geode;
geode->addDrawable(explosion->getParticleSystem());
geode->addDrawable(explosionDebri->getParticleSystem());
geode->addDrawable(smoke->getParticleSystem());
geode->addDrawable(fire->getParticleSystem());
2014-05-14 18:19:43 +08:00
2005-04-04 16:47:21 +08:00
root->addChild(geode);
}
else
{
// when we don't have moving models we can simple insert the particle effect into the root of the scene graph
osg::notify(osg::INFO)<<"PickHandler::pick(,) adding particle effects to root node."<<std::endl;
2005-04-04 16:47:21 +08:00
root->addChild(effectsGroup);
}
2014-05-14 18:19:43 +08:00
#if 0
2005-04-04 16:47:21 +08:00
osg::Geode* geode = new osg::Geode;
geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(position,scale)));
group->addChild(geode);
#endif
2014-05-14 18:19:43 +08:00
2005-04-04 16:47:21 +08:00
}
}
2014-05-14 18:19:43 +08:00
2005-04-04 16:47:21 +08:00
protected:
virtual ~PickHandler() {}
};
// function used in debugging
void insertParticle(osg::Group* root, const osg::Vec3& center, float radius)
{
bool handleMovingModels = false;
2014-05-14 18:19:43 +08:00
osg::Vec3 position = center +
osg::Vec3( radius * (((float)rand() / (float)RAND_MAX)-0.5)*2.0,
radius * (((float)rand() / (float)RAND_MAX)-0.5)*2.0,
0.0f);
float scale = 10.0f * ((float)rand() / (float)RAND_MAX);
float intensity = 1.0f;
osgParticle::ExplosionEffect* explosion = new osgParticle::ExplosionEffect(position, scale, intensity);
osgParticle::ExplosionDebrisEffect* explosionDebri = new osgParticle::ExplosionDebrisEffect(position, scale, intensity);
osgParticle::FireEffect* fire = new osgParticle::FireEffect(position, scale, intensity);
osgParticle::ParticleEffect* smoke = 0;
if (handleMovingModels)
smoke = new osgParticle::SmokeTrailEffect(position, scale, intensity);
else
smoke = new osgParticle::SmokeEffect(position, scale, intensity);
explosion->setWind(wind);
explosionDebri->setWind(wind);
smoke->setWind(wind);
fire->setWind(wind);
osg::Group* effectsGroup = new osg::Group;
effectsGroup->addChild(explosion);
effectsGroup->addChild(explosionDebri);
effectsGroup->addChild(smoke);
effectsGroup->addChild(fire);
root->addChild(effectsGroup);
}
2005-04-04 16:47:21 +08:00
//////////////////////////////////////////////////////////////////////////////
// main()
//////////////////////////////////////////////////////////////////////////////
int main(int, char **)
2005-04-04 16:47:21 +08:00
{
// construct the viewer.
osgViewer::Viewer viewer;
2005-04-04 16:47:21 +08:00
// register the pick handler
viewer.addEventHandler(new PickHandler());
2014-05-14 18:19:43 +08:00
2005-04-04 16:47:21 +08:00
osg::Group *root = new osg::Group;
build_world(root);
osgUtil::Optimizer optimizer;
optimizer.optimize(root);
2014-05-14 18:19:43 +08:00
2005-04-04 16:47:21 +08:00
// add a viewport to the viewer and attach the scene graph.
viewer.setSceneData(root);
2014-05-14 18:19:43 +08:00
return viewer.run();
2005-04-04 16:47:21 +08:00
}