RandomRateCounter 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_RANDOMRATE_COUNTER
  15. #define OSGPARTICLE_RANDOMRATE_COUNTER 1
  16. #include <osgParticle/VariableRateCounter>
  17. #include <osg/CopyOp>
  18. #include <osg/Object>
  19. namespace osgParticle
  20. {
  21. class RandomRateCounter: public VariableRateCounter {
  22. public:
  23. inline RandomRateCounter();
  24. inline RandomRateCounter(const RandomRateCounter& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
  25. META_Object(osgParticle, RandomRateCounter);
  26. /// Return the number of particles to be created in this frame
  27. inline int numParticlesToCreate(double dt) const;
  28. protected:
  29. virtual ~RandomRateCounter() {}
  30. mutable float _np;
  31. };
  32. // INLINE FUNCTIONS
  33. inline RandomRateCounter::RandomRateCounter()
  34. : VariableRateCounter(), _np(0)
  35. {
  36. }
  37. inline RandomRateCounter::RandomRateCounter(const RandomRateCounter& copy, const osg::CopyOp& copyop)
  38. : VariableRateCounter(copy, copyop), _np(copy._np)
  39. {
  40. }
  41. inline int RandomRateCounter::numParticlesToCreate(double dt) const
  42. {
  43. // compute the number of new particles, clamping it to 1 second of particles at the maximum rate
  44. float numNewParticles = osg::minimum(static_cast<float>(dt * getRateRange().get_random()), getRateRange().maximum);
  45. // add the number of new particles to value carried over from the previous call
  46. _np += numNewParticles;
  47. // round down the number of particles.
  48. int n = static_cast<int>(_np);
  49. // take away the number of rounded number of particles leaving the decimal place
  50. // this is done so that two frames of 0.5's will results in first frame 0 new particles, second frame 1
  51. _np -= n;
  52. // return the rounded number of particles to be created
  53. return n;
  54. }
  55. }
  56. #endif