1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
13//osgParticle - Copyright (C) 2002 Marco Jez
15#ifndef OSGPARTICLE_RANDOMRATE_COUNTER
16#define OSGPARTICLE_RANDOMRATE_COUNTER 1
18#include <osgParticle/VariableRateCounter>
26 class RandomRateCounter: public VariableRateCounter {
28 inline RandomRateCounter();
29 inline RandomRateCounter(const RandomRateCounter& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
31 META_Object(osgParticle, RandomRateCounter);
33 /// Return the number of particles to be created in this frame
34 inline int numParticlesToCreate(double dt) const;
37 virtual ~RandomRateCounter() {}
44 inline RandomRateCounter::RandomRateCounter()
45 : VariableRateCounter(), _np(0)
49 inline RandomRateCounter::RandomRateCounter(const RandomRateCounter& copy, const osg::CopyOp& copyop)
50 : VariableRateCounter(copy, copyop), _np(copy._np)
54 inline int RandomRateCounter::numParticlesToCreate(double dt) const
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);
59 // add the number of new particles to value carried over from the previous call
60 _np += numNewParticles;
62 // round down the number of particles.
63 int n = static_cast<int>(_np);
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
69 // return the rounded number of particles to be created