2012-03-22 01:36:20 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2005-10-13 02:42:36 +08:00
|
|
|
*
|
2012-03-22 01:36:20 +08:00
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
2005-10-13 02:42:36 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2012-03-22 01:36:20 +08:00
|
|
|
*
|
2005-10-13 02:42:36 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-03-22 01:36:20 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2005-10-13 02:42:36 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OSGPARTICLE_CONSTANTRATECOUNTER
|
|
|
|
#define OSGPARTICLE_CONSTANTRATECOUNTER 1
|
|
|
|
|
|
|
|
#include <osgParticle/Counter>
|
|
|
|
|
|
|
|
#include <osg/Object>
|
|
|
|
#include <osg/Math>
|
|
|
|
|
|
|
|
namespace osgParticle
|
|
|
|
{
|
|
|
|
|
|
|
|
class ConstantRateCounter: public Counter {
|
|
|
|
public:
|
|
|
|
ConstantRateCounter():
|
2012-03-22 01:36:20 +08:00
|
|
|
Counter(),
|
2005-10-13 02:42:36 +08:00
|
|
|
_minimumNumberOfParticlesToCreate(0),
|
2006-07-04 17:18:04 +08:00
|
|
|
_numberOfParticlesPerSecondToCreate(0),
|
|
|
|
_carryOver(0)
|
2005-10-13 02:42:36 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstantRateCounter(const ConstantRateCounter& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY):
|
2012-03-22 01:36:20 +08:00
|
|
|
Counter(copy, copyop),
|
2005-10-13 02:42:36 +08:00
|
|
|
_minimumNumberOfParticlesToCreate(copy._minimumNumberOfParticlesToCreate),
|
2006-07-04 17:18:04 +08:00
|
|
|
_numberOfParticlesPerSecondToCreate(copy._numberOfParticlesPerSecondToCreate),
|
|
|
|
_carryOver(copy._carryOver)
|
2005-10-13 02:42:36 +08:00
|
|
|
{
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
|
|
|
|
2005-10-13 02:42:36 +08:00
|
|
|
META_Object(osgParticle, ConstantRateCounter);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-10-13 02:42:36 +08:00
|
|
|
void setMinimumNumberOfParticlesToCreate(int minNumToCreate) { _minimumNumberOfParticlesToCreate = minNumToCreate; }
|
|
|
|
int getMinimumNumberOfParticlesToCreate() const { return _minimumNumberOfParticlesToCreate; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-10-13 02:42:36 +08:00
|
|
|
void setNumberOfParticlesPerSecondToCreate(double numPerSecond) { _numberOfParticlesPerSecondToCreate = numPerSecond; }
|
|
|
|
double getNumberOfParticlesPerSecondToCreate() const { return _numberOfParticlesPerSecondToCreate; }
|
|
|
|
|
|
|
|
/// Return the number of particles to be created in this frame
|
|
|
|
virtual int numParticlesToCreate(double dt) const
|
|
|
|
{
|
|
|
|
double v = (dt*_numberOfParticlesPerSecondToCreate);
|
|
|
|
int i = (int)(v);
|
|
|
|
_carryOver += (v-(double)i);
|
|
|
|
if (_carryOver>1.0)
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
_carryOver -= 1.0;
|
|
|
|
}
|
|
|
|
return osg::maximum(_minimumNumberOfParticlesToCreate, i);
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2016-09-02 00:14:03 +08:00
|
|
|
|
|
|
|
virtual int getEstimatedMaxNumOfParticles(double lifeTime) const
|
|
|
|
{
|
|
|
|
int minNumParticles = static_cast<int>(_minimumNumberOfParticlesToCreate*60.0f*lifeTime);
|
|
|
|
int baseNumPartciles = static_cast<int>(_numberOfParticlesPerSecondToCreate * lifeTime);
|
|
|
|
return osg::maximum(minNumParticles, baseNumPartciles);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-13 02:42:36 +08:00
|
|
|
protected:
|
|
|
|
virtual ~ConstantRateCounter() {}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-10-13 02:42:36 +08:00
|
|
|
int _minimumNumberOfParticlesToCreate;
|
|
|
|
double _numberOfParticlesPerSecondToCreate;
|
|
|
|
mutable double _carryOver;
|
|
|
|
};
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-10-13 02:42:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|