ConstantRateCounter 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #ifndef OSGPARTICLE_CONSTANTRATECOUNTER
  14. #define OSGPARTICLE_CONSTANTRATECOUNTER 1
  15. #include <osgParticle/Counter>
  16. #include <osg/Object>
  17. #include <osg/Math>
  18. namespace osgParticle
  19. {
  20. class ConstantRateCounter: public Counter {
  21. public:
  22. ConstantRateCounter():
  23. Counter(),
  24. _minimumNumberOfParticlesToCreate(0),
  25. _numberOfParticlesPerSecondToCreate(0),
  26. _carryOver(0)
  27. {
  28. }
  29. ConstantRateCounter(const ConstantRateCounter& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY):
  30. Counter(copy, copyop),
  31. _minimumNumberOfParticlesToCreate(copy._minimumNumberOfParticlesToCreate),
  32. _numberOfParticlesPerSecondToCreate(copy._numberOfParticlesPerSecondToCreate),
  33. _carryOver(copy._carryOver)
  34. {
  35. }
  36. META_Object(osgParticle, ConstantRateCounter);
  37. void setMinimumNumberOfParticlesToCreate(int minNumToCreate) { _minimumNumberOfParticlesToCreate = minNumToCreate; }
  38. int getMinimumNumberOfParticlesToCreate() const { return _minimumNumberOfParticlesToCreate; }
  39. void setNumberOfParticlesPerSecondToCreate(double numPerSecond) { _numberOfParticlesPerSecondToCreate = numPerSecond; }
  40. double getNumberOfParticlesPerSecondToCreate() const { return _numberOfParticlesPerSecondToCreate; }
  41. /// Return the number of particles to be created in this frame
  42. virtual int numParticlesToCreate(double dt) const
  43. {
  44. double v = (dt*_numberOfParticlesPerSecondToCreate);
  45. int i = (int)(v);
  46. _carryOver += (v-(double)i);
  47. if (_carryOver>1.0)
  48. {
  49. ++i;
  50. _carryOver -= 1.0;
  51. }
  52. return osg::maximum(_minimumNumberOfParticlesToCreate, i);
  53. }
  54. virtual int getEstimatedMaxNumOfParticles(double lifeTime) const
  55. {
  56. int minNumParticles = static_cast<int>(_minimumNumberOfParticlesToCreate*60.0f*lifeTime);
  57. int baseNumPartciles = static_cast<int>(_numberOfParticlesPerSecondToCreate * lifeTime);
  58. return osg::maximum(minNumParticles, baseNumPartciles);
  59. }
  60. protected:
  61. virtual ~ConstantRateCounter() {}
  62. int _minimumNumberOfParticlesToCreate;
  63. double _numberOfParticlesPerSecondToCreate;
  64. mutable double _carryOver;
  65. };
  66. }
  67. #endif