| 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 | |
|---|
| 14 | #ifndef OSGPARTICLE_CONSTANTRATECOUNTER |
|---|
| 15 | #define OSGPARTICLE_CONSTANTRATECOUNTER 1 |
|---|
| 16 | |
|---|
| 17 | #include <osgParticle/Counter> |
|---|
| 18 | |
|---|
| 19 | #include <osg/Object> |
|---|
| 20 | #include <osg/Math> |
|---|
| 21 | |
|---|
| 22 | namespace osgParticle |
|---|
| 23 | { |
|---|
| 24 | |
|---|
| 25 | class ConstantRateCounter: public Counter { |
|---|
| 26 | public: |
|---|
| 27 | ConstantRateCounter(): |
|---|
| 28 | Counter(), |
|---|
| 29 | _minimumNumberOfParticlesToCreate(0), |
|---|
| 30 | _numberOfParticlesPerSecondToCreate(0), |
|---|
| 31 | _carryOver(0) |
|---|
| 32 | { |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 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) |
|---|
| 40 | { |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | META_Object(osgParticle, ConstantRateCounter); |
|---|
| 45 | |
|---|
| 46 | void setMinimumNumberOfParticlesToCreate(int minNumToCreate) { _minimumNumberOfParticlesToCreate = minNumToCreate; } |
|---|
| 47 | int getMinimumNumberOfParticlesToCreate() const { return _minimumNumberOfParticlesToCreate; } |
|---|
| 48 | |
|---|
| 49 | void setNumberOfParticlesPerSecondToCreate(double numPerSecond) { _numberOfParticlesPerSecondToCreate = numPerSecond; } |
|---|
| 50 | double getNumberOfParticlesPerSecondToCreate() const { return _numberOfParticlesPerSecondToCreate; } |
|---|
| 51 | |
|---|
| 52 | /// Return the number of particles to be created in this frame |
|---|
| 53 | virtual int numParticlesToCreate(double dt) const |
|---|
| 54 | { |
|---|
| 55 | double v = (dt*_numberOfParticlesPerSecondToCreate); |
|---|
| 56 | int i = (int)(v); |
|---|
| 57 | _carryOver += (v-(double)i); |
|---|
| 58 | if (_carryOver>1.0) |
|---|
| 59 | { |
|---|
| 60 | ++i; |
|---|
| 61 | _carryOver -= 1.0; |
|---|
| 62 | } |
|---|
| 63 | return osg::maximum(_minimumNumberOfParticlesToCreate, i); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | protected: |
|---|
| 67 | virtual ~ConstantRateCounter() {} |
|---|
| 68 | |
|---|
| 69 | int _minimumNumberOfParticlesToCreate; |
|---|
| 70 | double _numberOfParticlesPerSecondToCreate; |
|---|
| 71 | mutable double _carryOver; |
|---|
| 72 | }; |
|---|
| 73 | |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | #endif |
|---|