From 71782462b73231bf088f51058b31fb3703949e5d Mon Sep 17 00:00:00 2001 From: scrawl Date: Sat, 21 Mar 2015 04:28:26 +0100 Subject: [PATCH] Subclass ParticleSystem to support limit on the number of particles --- components/nifosg/nifloader.cpp | 4 +++- components/nifosg/particle.cpp | 26 ++++++++++++++++++++++++++ components/nifosg/particle.hpp | 17 +++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/components/nifosg/nifloader.cpp b/components/nifosg/nifloader.cpp index 30b3c6537..dcab80707 100644 --- a/components/nifosg/nifloader.cpp +++ b/components/nifosg/nifloader.cpp @@ -571,7 +571,7 @@ namespace NifOsg void Loader::handleParticleSystem(const Nif::Node *nifNode, osg::Group *parentNode, int animflags, int particleflags) { - osg::ref_ptr partsys (new osgParticle::ParticleSystem); + osg::ref_ptr partsys (new ParticleSystem); partsys->setSortMode(osgParticle::ParticleSystem::SORT_BACK_TO_FRONT); const Nif::NiAutoNormalParticlesData *particledata = NULL; @@ -630,6 +630,8 @@ namespace NifOsg created->setSizeRange(osgParticle::rangef(size, size)); } + partsys->setQuota(partctrl->numParticles); + partsys->getDefaultParticleTemplate().setSizeRange(osgParticle::rangef(partctrl->size, partctrl->size)); partsys->getDefaultParticleTemplate().setColorRange(osgParticle::rangev4(osg::Vec4f(1.f,1.f,1.f,1.f), osg::Vec4f(1.f,1.f,1.f,1.f))); partsys->getDefaultParticleTemplate().setAlphaRange(osgParticle::rangef(1.f, 1.f)); diff --git a/components/nifosg/particle.cpp b/components/nifosg/particle.cpp index f2f59195c..77e5f16df 100644 --- a/components/nifosg/particle.cpp +++ b/components/nifosg/particle.cpp @@ -1,5 +1,7 @@ #include "particle.hpp" +#include + #include #include @@ -9,6 +11,30 @@ namespace NifOsg { +ParticleSystem::ParticleSystem() + : osgParticle::ParticleSystem() + , mQuota(std::numeric_limits::max()) +{ +} + +ParticleSystem::ParticleSystem(const ParticleSystem ©, const osg::CopyOp ©op) + : osgParticle::ParticleSystem(copy, copyop) + , mQuota(copy.mQuota) +{ +} + +void ParticleSystem::setQuota(int quota) +{ + mQuota = quota; +} + +osgParticle::Particle* ParticleSystem::createParticle(const osgParticle::Particle *ptemplate) +{ + if (numParticles()-numDeadParticles() < mQuota) + return osgParticle::ParticleSystem::createParticle(ptemplate); + return NULL; +} + void InverseWorldMatrix::operator()(osg::Node *node, osg::NodeVisitor *nv) { if (nv && nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR) diff --git a/components/nifosg/particle.hpp b/components/nifosg/particle.hpp index 13010fbac..1f3905089 100644 --- a/components/nifosg/particle.hpp +++ b/components/nifosg/particle.hpp @@ -20,6 +20,23 @@ namespace Nif namespace NifOsg { + // Subclass ParticleSystem to support a limit on the number of active particles. + class ParticleSystem : public osgParticle::ParticleSystem + { + public: + ParticleSystem(); + ParticleSystem(const ParticleSystem& copy, const osg::CopyOp& copyop); + + META_Object(NifOsg, NifOsg::ParticleSystem) + + virtual osgParticle::Particle* createParticle(const osgParticle::Particle *ptemplate); + + void setQuota(int quota); + + private: + int mQuota; + }; + // HACK: Particle doesn't allow setting the initial age, but we need this for loading the particle system state class ParticleAgeSetter : public osgParticle::Particle {