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