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.
14#ifndef OSGPARTICLE_CONSTANTRATECOUNTER
15#define OSGPARTICLE_CONSTANTRATECOUNTER 1
17#include <osgParticle/Counter>
25 class ConstantRateCounter: public Counter {
27 ConstantRateCounter():
29 _minimumNumberOfParticlesToCreate(0),
30 _numberOfParticlesPerSecondToCreate(0),
35 ConstantRateCounter(const ConstantRateCounter& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY):
36 Counter(copy, copyop),
37 _minimumNumberOfParticlesToCreate(copy._minimumNumberOfParticlesToCreate),
38 _numberOfParticlesPerSecondToCreate(copy._numberOfParticlesPerSecondToCreate),
39 _carryOver(copy._carryOver)
44 META_Object(osgParticle, ConstantRateCounter);
46 void setMinimumNumberOfParticlesToCreate(int minNumToCreate) { _minimumNumberOfParticlesToCreate = minNumToCreate; }
47 int getMinimumNumberOfParticlesToCreate() const { return _minimumNumberOfParticlesToCreate; }
49 void setNumberOfParticlesPerSecondToCreate(double numPerSecond) { _numberOfParticlesPerSecondToCreate = numPerSecond; }
50 double getNumberOfParticlesPerSecondToCreate() const { return _numberOfParticlesPerSecondToCreate; }
52 /// Return the number of particles to be created in this frame
53 virtual int numParticlesToCreate(double dt) const
55 double v = (dt*_numberOfParticlesPerSecondToCreate);
57 _carryOver += (v-(double)i);
63 return osg::maximum(_minimumNumberOfParticlesToCreate, i);
67 virtual int getEstimatedMaxNumOfParticles(double lifeTime) const
69 int minNumParticles = static_cast<int>(_minimumNumberOfParticlesToCreate*60.0f*lifeTime);
70 int baseNumPartciles = static_cast<int>(_numberOfParticlesPerSecondToCreate * lifeTime);
71 return osg::maximum(minNumParticles, baseNumPartciles);
76 virtual ~ConstantRateCounter() {}
78 int _minimumNumberOfParticlesToCreate;
79 double _numberOfParticlesPerSecondToCreate;
80 mutable double _carryOver;