From e122b34858b40eaf70bb9888bc85b14e450fa38d Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 2 Aug 2004 07:25:28 +0000 Subject: [PATCH] From Tom Jolly. a couple of small changes to osgParticle. "The first is with Particle.cpp. I made a change so that when the lifetime is 0 (lasts forever) the sizeRange, colorRange, and alphaRange are used to create a random size, color, and alpha." "The second change is with range and SectorPlacer. The distribution of particles across the sector was not uniform. I added get_random_sqrtf() function where it is used in SectorPlacer::Place(). This seems to make the distribution uniform (at least when minimum radius is 0)." --- include/osgParticle/SectorPlacer | 2 +- include/osgParticle/range | 6 ++++++ src/osgParticle/Particle.cpp | 14 +++++++++++--- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/include/osgParticle/SectorPlacer b/include/osgParticle/SectorPlacer index 9d31357cd..5e79516f2 100644 --- a/include/osgParticle/SectorPlacer +++ b/include/osgParticle/SectorPlacer @@ -116,7 +116,7 @@ namespace osgParticle inline void SectorPlacer::place(Particle *P) const { - float rad = rad_range_.get_random(); + float rad = rad_range_.get_random_sqrtf(); float phi = phi_range_.get_random(); osg::Vec3 pos( diff --git a/include/osgParticle/range b/include/osgParticle/range index 03f5c0255..f48087afb 100644 --- a/include/osgParticle/range +++ b/include/osgParticle/range @@ -64,6 +64,12 @@ namespace osgParticle return minimum + (maximum - minimum) * rand() / RAND_MAX; } + /// Get a random square root value between min and max. + T_ get_random_sqrtf() const + { + return minimum + (maximum - minimum) * sqrtf( static_cast(rand()) / static_cast(RAND_MAX) ); + } + }; /// Range of floats. diff --git a/src/osgParticle/Particle.cpp b/src/osgParticle/Particle.cpp index 06307aa88..3ad314505 100644 --- a/src/osgParticle/Particle.cpp +++ b/src/osgParticle/Particle.cpp @@ -86,9 +86,17 @@ bool osgParticle::Particle::update(double dt) } // compute the current values for size, alpha and color. - current_size_ = si_.get()->interpolate(x, sr_); - current_alpha_ = ai_.get()->interpolate(x, ar_); - current_color_ = ci_.get()->interpolate(x, cr_); + if (lifetime_ <= 0) { + if (dt == t0_) { + current_size_ = sr_.get_random(); + current_alpha_ = ar_.get_random(); + current_color_ = cr_.get_random(); + } + } else { + current_size_ = si_.get()->interpolate(x, sr_); + current_alpha_ = ai_.get()->interpolate(x, ar_); + current_color_ = ci_.get()->interpolate(x, cr_); + } // update position prev_pos_ = position_;