2005-04-15 05:41:28 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield
2003-01-22 00:45:36 +08:00
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
2002-06-05 20:44:55 +08:00
//osgParticle - Copyright (C) 2002 Marco Jez
2005-04-29 17:47:57 +08:00
#ifndef OSGPARTICLE_PARTICLESYSTEMUPDATER
#define OSGPARTICLE_PARTICLESYSTEMUPDATER 1
2002-06-05 20:44:55 +08:00
#include <osgParticle/Export>
#include <osgParticle/ParticleSystem>
#include <vector>
#include <osg/ref_ptr>
#include <osg/CopyOp>
#include <osg/Object>
#include <osg/Node>
#include <osg/NodeVisitor>
#include <osgUtil/CullVisitor>
namespace osgParticle
{
/** A useful node class for updating particle systems automatically.
2005-05-21 05:01:57 +08:00
When a ParticleSystemUpdater is traversed by a cull visitor, it calls the
update() method on the specified particle systems. You should place this updater
AFTER other nodes like emitters and programs.
2002-06-05 20:44:55 +08:00
*/
class OSGPARTICLE_EXPORT ParticleSystemUpdater: public osg::Node {
public:
ParticleSystemUpdater();
2005-04-29 17:47:57 +08:00
ParticleSystemUpdater(const ParticleSystemUpdater& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
2002-06-05 20:44:55 +08:00
2002-06-06 21:25:36 +08:00
META_Node(osgParticle,ParticleSystemUpdater);
2002-06-05 20:44:55 +08:00
/// Add a particle system to the list.
2005-04-29 17:47:57 +08:00
inline bool addParticleSystem(ParticleSystem* ps);
2004-03-18 04:31:55 +08:00
/// Remove a particle system from the list (by pointer).
2005-04-29 17:47:57 +08:00
inline bool removeParticleSystem(ParticleSystem* ps);
2004-03-18 04:31:55 +08:00
/// Remove a particle system(s) from the list (by index).
inline bool removeParticleSystem(unsigned int i, unsigned int numParticleSystemsToRemove=1);
2002-06-05 20:44:55 +08:00
2004-03-18 04:31:55 +08:00
/// Replace ParticleSystem with another ParticleSystem.
2005-04-29 17:47:57 +08:00
inline bool replaceParticleSystem(ParticleSystem* origPS, ParticleSystem* newPS);
2004-03-18 04:31:55 +08:00
/// set a particle system by index.
inline bool setParticleSystem( unsigned int i, ParticleSystem* ps );
/// Return the number of particle systems on the list.
inline unsigned int getNumParticleSystems() const;
2002-06-05 20:44:55 +08:00
/// Get a particle system from the list.
2004-03-18 04:31:55 +08:00
inline ParticleSystem* getParticleSystem(unsigned int i);
2002-06-05 20:44:55 +08:00
2004-03-18 04:31:55 +08:00
/// Get a particle system from the list.
inline const ParticleSystem* getParticleSystem(unsigned int i) const;
/// return true if ParticleSystem is contained within ParticlsSystemUpdater.
inline bool containsParticleSystem( const ParticleSystem* ps ) const;
/// get index number of ParticleSystem.
inline unsigned int getParticleSystemIndex( const ParticleSystem* ps ) const;
2002-06-05 20:44:55 +08:00
2005-04-29 17:47:57 +08:00
virtual void traverse(osg::NodeVisitor& nv);
2002-06-05 20:44:55 +08:00
2005-05-12 22:03:22 +08:00
virtual osg::BoundingSphere computeBound() const;
2002-06-05 20:44:55 +08:00
protected:
virtual ~ParticleSystemUpdater() {}
ParticleSystemUpdater &operator=(const ParticleSystemUpdater &) { return *this; }
private:
typedef std::vector<osg::ref_ptr<ParticleSystem> > ParticleSystem_Vector;
2005-04-29 17:47:57 +08:00
ParticleSystem_Vector _psv;
double _t0;
2002-06-05 20:44:55 +08:00
};
// INLINE FUNCTIONS
2005-04-29 17:47:57 +08:00
inline bool ParticleSystemUpdater::addParticleSystem(ParticleSystem* ps)
2002-06-05 20:44:55 +08:00
{
2005-04-29 17:47:57 +08:00
_psv.push_back(ps);
2004-03-18 04:31:55 +08:00
return true;
2002-06-05 20:44:55 +08:00
}
2005-04-29 17:47:57 +08:00
inline bool ParticleSystemUpdater::removeParticleSystem(ParticleSystem* ps)
2002-06-05 20:44:55 +08:00
{
2004-03-18 04:31:55 +08:00
unsigned int i = getParticleSystemIndex( ps );
2005-04-29 17:47:57 +08:00
if( i >= _psv.size() ) return false;
2004-03-18 04:31:55 +08:00
removeParticleSystem( i );
return true;
}
inline bool ParticleSystemUpdater::removeParticleSystem(unsigned int pos, unsigned int numParticleSystemsToRemove)
{
2005-04-29 17:47:57 +08:00
if( (pos < _psv.size()) && (numParticleSystemsToRemove > 0) )
2004-03-18 04:31:55 +08:00
{
unsigned int endOfRemoveRange = pos + numParticleSystemsToRemove;
2005-04-29 17:47:57 +08:00
if( endOfRemoveRange > _psv.size() )
2004-03-18 04:31:55 +08:00
{
osg::notify(osg::DEBUG_INFO)<<"Warning: ParticleSystem::removeParticleSystem(i,numParticleSystemsToRemove) has been passed an excessive number"<<std::endl;
osg::notify(osg::DEBUG_INFO)<<" of ParticleSystems to remove, trimming just to end of ParticleSystem list."<<std::endl;
2005-04-29 17:47:57 +08:00
endOfRemoveRange = _psv.size();
2004-03-18 04:31:55 +08:00
}
2005-04-29 17:47:57 +08:00
_psv.erase(_psv.begin()+pos, _psv.begin()+endOfRemoveRange);
2004-03-18 04:31:55 +08:00
return true;
}
return false;
}
2005-04-29 17:47:57 +08:00
inline bool ParticleSystemUpdater::replaceParticleSystem( ParticleSystem* origPS, ParticleSystem* newPS )
2004-03-18 04:31:55 +08:00
{
if( (newPS == NULL) || (origPS == newPS) ) return false;
unsigned int pos = getParticleSystemIndex( origPS );
2005-04-29 17:47:57 +08:00
if( pos < _psv.size() )
2004-03-18 04:31:55 +08:00
{
return setParticleSystem( pos, newPS );
}
return false;
}
inline bool ParticleSystemUpdater::setParticleSystem( unsigned int i, ParticleSystem* ps )
{
2005-04-29 17:47:57 +08:00
if( (i < _psv.size()) && ps )
2004-03-18 04:31:55 +08:00
{
2005-04-29 17:47:57 +08:00
_psv[i] = ps;
2004-03-18 04:31:55 +08:00
return true;
}
return false;
}
inline unsigned int ParticleSystemUpdater::getNumParticleSystems() const
{
2005-04-29 17:47:57 +08:00
return static_cast<int>(_psv.size());
2002-06-05 20:44:55 +08:00
}
2004-03-18 04:31:55 +08:00
inline ParticleSystem* ParticleSystemUpdater::getParticleSystem(unsigned int i)
2002-06-05 20:44:55 +08:00
{
2005-04-29 17:47:57 +08:00
return _psv[i].get();
2002-06-05 20:44:55 +08:00
}
2004-03-18 04:31:55 +08:00
inline const ParticleSystem* ParticleSystemUpdater::getParticleSystem(unsigned int i) const
2002-06-05 20:44:55 +08:00
{
2005-04-29 17:47:57 +08:00
return _psv[i].get();
2002-06-05 20:44:55 +08:00
}
2004-03-18 04:31:55 +08:00
inline bool ParticleSystemUpdater::containsParticleSystem( const ParticleSystem* ps ) const
2002-06-05 20:44:55 +08:00
{
2005-04-29 17:47:57 +08:00
for( ParticleSystem_Vector::const_iterator itr=_psv.begin();
itr!=_psv.end();
2004-03-18 04:31:55 +08:00
++itr )
{
if( itr->get() == ps ) return true;
}
return false;
2002-06-05 20:44:55 +08:00
}
2004-03-18 04:31:55 +08:00
inline unsigned int ParticleSystemUpdater::getParticleSystemIndex( const ParticleSystem* ps ) const
2002-06-05 20:44:55 +08:00
{
2005-04-29 17:47:57 +08:00
for( unsigned int particleSystemNum=0; particleSystemNum<_psv.size(); ++particleSystemNum )
2004-03-18 04:31:55 +08:00
{
2005-04-29 17:47:57 +08:00
if( _psv[particleSystemNum] == ps ) return particleSystemNum;
2004-03-18 04:31:55 +08:00
}
2005-04-29 17:47:57 +08:00
return _psv.size(); // node not found.
2002-06-05 20:44:55 +08:00
}
}
#endif