ParticleSystemUpdater 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. //osgParticle - Copyright (C) 2002 Marco Jez
  14. #ifndef OSGPARTICLE_PARTICLESYSTEMUPDATER
  15. #define OSGPARTICLE_PARTICLESYSTEMUPDATER 1
  16. #include <osgParticle/Export>
  17. #include <osgParticle/ParticleSystem>
  18. #include <vector>
  19. #include <osg/ref_ptr>
  20. #include <osg/CopyOp>
  21. #include <osg/Object>
  22. #include <osg/Geode>
  23. #include <osg/NodeVisitor>
  24. #include <osgUtil/CullVisitor>
  25. namespace osgParticle
  26. {
  27. /** A useful node class for updating particle systems automatically.
  28. When a ParticleSystemUpdater is traversed by a cull visitor, it calls the
  29. update() method on the specified particle systems. You should place this updater
  30. AFTER other nodes like emitters and programs.
  31. */
  32. class OSGPARTICLE_EXPORT ParticleSystemUpdater: public osg::Node {
  33. public:
  34. ParticleSystemUpdater();
  35. ParticleSystemUpdater(const ParticleSystemUpdater& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
  36. META_Node(osgParticle,ParticleSystemUpdater);
  37. /// Add a particle system to the list.
  38. virtual bool addParticleSystem(ParticleSystem* ps);
  39. /// Remove a particle system from the list (by pointer).
  40. virtual bool removeParticleSystem(ParticleSystem* ps);
  41. /// Remove a particle system(s) from the list (by index).
  42. virtual bool removeParticleSystem(unsigned int i, unsigned int numParticleSystemsToRemove=1);
  43. /// Replace ParticleSystem with another ParticleSystem.
  44. virtual bool replaceParticleSystem(ParticleSystem* origPS, ParticleSystem* newPS);
  45. /// set a particle system by index.
  46. virtual bool setParticleSystem( unsigned int i, ParticleSystem* ps );
  47. /// Return the number of particle systems on the list.
  48. inline unsigned int getNumParticleSystems() const;
  49. /// Get a particle system from the list.
  50. inline ParticleSystem* getParticleSystem(unsigned int i);
  51. /// Get a particle system from the list.
  52. inline const ParticleSystem* getParticleSystem(unsigned int i) const;
  53. /// return true if ParticleSystem is contained within ParticlsSystemUpdater.
  54. inline bool containsParticleSystem( const ParticleSystem* ps ) const;
  55. /// get index number of ParticleSystem.
  56. inline unsigned int getParticleSystemIndex( const ParticleSystem* ps ) const;
  57. virtual void traverse(osg::NodeVisitor& nv);
  58. virtual osg::BoundingSphere computeBound() const;
  59. protected:
  60. virtual ~ParticleSystemUpdater() {}
  61. ParticleSystemUpdater &operator=(const ParticleSystemUpdater &) { return *this; }
  62. private:
  63. typedef std::vector<osg::ref_ptr<ParticleSystem> > ParticleSystem_Vector;
  64. ParticleSystem_Vector _psv;
  65. double _t0;
  66. //added 1/17/06- bgandere@nps.edu
  67. //a var to keep from doing multiple updates per frame
  68. unsigned int _frameNumber;
  69. };
  70. // INLINE FUNCTIONS
  71. inline unsigned int ParticleSystemUpdater::getNumParticleSystems() const
  72. {
  73. return static_cast<int>(_psv.size());
  74. }
  75. inline ParticleSystem* ParticleSystemUpdater::getParticleSystem(unsigned int i)
  76. {
  77. return _psv[i].get();
  78. }
  79. inline const ParticleSystem* ParticleSystemUpdater::getParticleSystem(unsigned int i) const
  80. {
  81. return _psv[i].get();
  82. }
  83. inline bool ParticleSystemUpdater::containsParticleSystem( const ParticleSystem* ps ) const
  84. {
  85. for( ParticleSystem_Vector::const_iterator itr=_psv.begin();
  86. itr!=_psv.end();
  87. ++itr )
  88. {
  89. if( itr->get() == ps ) return true;
  90. }
  91. return false;
  92. }
  93. inline unsigned int ParticleSystemUpdater::getParticleSystemIndex( const ParticleSystem* ps ) const
  94. {
  95. for( unsigned int particleSystemNum=0; particleSystemNum<_psv.size(); ++particleSystemNum )
  96. {
  97. if( _psv[particleSystemNum] == ps ) return particleSystemNum;
  98. }
  99. return _psv.size(); // node not found.
  100. }
  101. }
  102. #endif