| 1 | #include <osgParticle/ParticleSystemUpdater> |
|---|
| 2 | |
|---|
| 3 | #include <osg/CopyOp> |
|---|
| 4 | #include <osg/Node> |
|---|
| 5 | |
|---|
| 6 | using namespace osg; |
|---|
| 7 | |
|---|
| 8 | osgParticle::ParticleSystemUpdater::ParticleSystemUpdater() |
|---|
| 9 | : osg::Node(), _t0(-1), _frameNumber(0) |
|---|
| 10 | { |
|---|
| 11 | setCullingActive(false); |
|---|
| 12 | } |
|---|
| 13 | |
|---|
| 14 | osgParticle::ParticleSystemUpdater::ParticleSystemUpdater(const ParticleSystemUpdater& copy, const osg::CopyOp& copyop) |
|---|
| 15 | : osg::Node(copy, copyop), _t0(copy._t0), _frameNumber(0) |
|---|
| 16 | { |
|---|
| 17 | ParticleSystem_Vector::const_iterator i; |
|---|
| 18 | for (i=copy._psv.begin(); i!=copy._psv.end(); ++i) { |
|---|
| 19 | _psv.push_back(static_cast<ParticleSystem* >(copyop(i->get()))); |
|---|
| 20 | } |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | void osgParticle::ParticleSystemUpdater::traverse(osg::NodeVisitor& nv) |
|---|
| 24 | { |
|---|
| 25 | osgUtil::CullVisitor *cv = dynamic_cast<osgUtil::CullVisitor *>(&nv); |
|---|
| 26 | if (cv) |
|---|
| 27 | { |
|---|
| 28 | if (nv.getFrameStamp()) |
|---|
| 29 | { |
|---|
| 30 | |
|---|
| 31 | if( _frameNumber < nv.getFrameStamp()->getFrameNumber()) |
|---|
| 32 | { |
|---|
| 33 | _frameNumber = nv.getFrameStamp()->getFrameNumber(); |
|---|
| 34 | |
|---|
| 35 | double t = nv.getFrameStamp()->getSimulationTime(); |
|---|
| 36 | if (_t0 != -1.0) |
|---|
| 37 | { |
|---|
| 38 | ParticleSystem_Vector::iterator i; |
|---|
| 39 | for (i=_psv.begin(); i!=_psv.end(); ++i) |
|---|
| 40 | { |
|---|
| 41 | ParticleSystem* ps = i->get(); |
|---|
| 42 | |
|---|
| 43 | ParticleSystem::ScopedWriteLock lock(*(ps->getReadWriteMutex())); |
|---|
| 44 | |
|---|
| 45 | if (!ps->isFrozen() && (ps->getLastFrameNumber() >= (nv.getFrameStamp()->getFrameNumber() - 1) || !ps->getFreezeOnCull())) |
|---|
| 46 | { |
|---|
| 47 | ps->update(t - _t0); |
|---|
| 48 | } |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | _t0 = t; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | } |
|---|
| 55 | else |
|---|
| 56 | { |
|---|
| 57 | osg::notify(osg::WARN) << "osgParticle::ParticleSystemUpdater::traverse(NodeVisitor&) requires a valid FrameStamp to function, particles not updated.\n"; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | } |
|---|
| 61 | Node::traverse(nv); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | osg::BoundingSphere osgParticle::ParticleSystemUpdater::computeBound() const |
|---|
| 65 | { |
|---|
| 66 | return osg::BoundingSphere(); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | bool osgParticle::ParticleSystemUpdater::addParticleSystem(ParticleSystem* ps) |
|---|
| 70 | { |
|---|
| 71 | _psv.push_back(ps); |
|---|
| 72 | return true; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | bool osgParticle::ParticleSystemUpdater::removeParticleSystem(ParticleSystem* ps) |
|---|
| 76 | { |
|---|
| 77 | unsigned int i = getParticleSystemIndex( ps ); |
|---|
| 78 | if( i >= _psv.size() ) return false; |
|---|
| 79 | |
|---|
| 80 | removeParticleSystem( i ); |
|---|
| 81 | return true; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | bool osgParticle::ParticleSystemUpdater::removeParticleSystem(unsigned int pos, unsigned int numParticleSystemsToRemove) |
|---|
| 85 | { |
|---|
| 86 | if( (pos < _psv.size()) && (numParticleSystemsToRemove > 0) ) |
|---|
| 87 | { |
|---|
| 88 | unsigned int endOfRemoveRange = pos + numParticleSystemsToRemove; |
|---|
| 89 | if( endOfRemoveRange > _psv.size() ) |
|---|
| 90 | { |
|---|
| 91 | osg::notify(osg::DEBUG_INFO)<<"Warning: ParticleSystem::removeParticleSystem(i,numParticleSystemsToRemove) has been passed an excessive number"<<std::endl; |
|---|
| 92 | osg::notify(osg::DEBUG_INFO)<<" of ParticleSystems to remove, trimming just to end of ParticleSystem list."<<std::endl; |
|---|
| 93 | endOfRemoveRange = _psv.size(); |
|---|
| 94 | } |
|---|
| 95 | _psv.erase(_psv.begin()+pos, _psv.begin()+endOfRemoveRange); |
|---|
| 96 | return true; |
|---|
| 97 | } |
|---|
| 98 | return false; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | bool osgParticle::ParticleSystemUpdater::replaceParticleSystem( ParticleSystem* origPS, ParticleSystem* newPS ) |
|---|
| 102 | { |
|---|
| 103 | if( (newPS == NULL) || (origPS == newPS) ) return false; |
|---|
| 104 | |
|---|
| 105 | unsigned int pos = getParticleSystemIndex( origPS ); |
|---|
| 106 | if( pos < _psv.size() ) |
|---|
| 107 | { |
|---|
| 108 | return setParticleSystem( pos, newPS ); |
|---|
| 109 | } |
|---|
| 110 | return false; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | bool osgParticle::ParticleSystemUpdater::setParticleSystem( unsigned int i, ParticleSystem* ps ) |
|---|
| 114 | { |
|---|
| 115 | if( (i < _psv.size()) && ps ) |
|---|
| 116 | { |
|---|
| 117 | _psv[i] = ps; |
|---|
| 118 | return true; |
|---|
| 119 | } |
|---|
| 120 | return false; |
|---|
| 121 | } |
|---|