hi!
im my code:
bool BaseParticleObject::setParticleSetLifeTime(float value)
{
ptemplate->setLifeTime((double)value);
ps->setDefaultParticleTemplate(*ptemplate);
return true;
}
//
//osgParticle::Particle* ptemplate;
//osg::ref_ptr<osgParticle::ParticleSystem> ps;
2009/5/20 Iban Arriola <ezku...@hotmail.com>:
> Hi,
>
> Hi I´m trying to change my particle systems life time in run-time. To do
> that, I started making a function setUp called once and a function update
> called every frame. Inside the setUp function I create the particle system: I
> set the shape, life time, size, mass, initial speed and so on.
>
>
>
> Code:
>
> void setUp(osg::Group *pRoot)
> {
> m_ParticleSystem = new osgParticle::ParticleSystem;
> m_ParticleSystem->setDefaultAttributes("Fire.dds", false, false);
>
> m_Geode = new osg::Geode;
> m_Geode->addDrawable(m_ParticleSystem.get());
> mRoot->addChild(m_Geode.get());
>
> m_pSystemUpdater = new osgParticle::ParticleSystemUpdater;
> m_pSystemUpdater->addParticleSystem(m_ParticleSystem.get());
> mRoot->addChild(m_SystemUpdater.get());
>
>
> m_myParticle = new osgParticle::Particle;
>
> m_myParticle->setShape(osgParticle::Particle::QUAD);
> m_myParticle->setSizeRange(osgParticle::rangef(0.2,0.2));
> m_myParticle->setLifeTime(4);
> m_myParticle->setMass(0.1);
>
>
> m_ParticleSystem->setDefaultParticleTemplate(*m_myParticle);
>
>
> m_Emitter = new osgParticle::ModularEmitter;
> m_Emitter->setParticleSystem(m_ParticleSystem.get());
>
> osg::ref_ptr<osgParticle::PointPlacer> mPlacer = new
> osgParticle::PointPlacer();
> mPlacer->setCenter(osg::Vec3(0.0,0.0,0.0));
> m_Emitter->setPlacer(mPlacer.get());
>
> m_Program = new osgParticle::ModularProgram;
> m_Program->setParticleSystem(m_ParticleSystem.get());
>
>
> Root->addChild(m_Program.get());
> }
>
>
>
>
>
>
> Then in the Update function for each frame I want to change the life time,
> random rate counter and initial speed. The problem is that I can change the
> random rate counter and initial speed but I can´t change the life time
> because the particle disappear (whatever the value I set). Below you can find
> my code snipet
>
>
>
> Code:
>
>
> void Update(float speed, float count, float life)
> {
>
> osg::ref_ptr<osgParticle::RadialShooter> pShooter =
> (osgParticle::RadialShooter *)_pEmitter->getShooter();
> pShooter->setInitialSpeedRange(speed*0.5,speed*0.9);
>
> osg::ref_ptr<osgParticle::RandomRateCounter> pRate =
> static_cast<osgParticle::RandomRateCounter *> (_pEmitter->getCounter());
> pRate->setRateRange(count*1.0,count*1.5);
>
>
> //osgParticle::Particle myParticle =
> _pParticleSystem->getDefaultParticleTemplate(); //This don't work if there
> are several particle systems
>
> //if I remove the following instruction, the system works properly but
> without the lifetime functionality.
> m_myParticle->setLifeTime(life);
> }
>
>
>
>
>
> Thanks in advance for your help
>
> cheers,
>
> Iban
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=12605#12605
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> osg-users@lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
--
Maxim Gammer
/***************************************************************************
BaseParticleObject.cpp - îïèñàíèå êëàññà èñòî÷íèêà ÷àñòèö
-------------------
begin : 24 àïðåëÿ 2009
copyright : (C) 2009 by Ãàììåð Ìàêñèì Äìèòðèåâè÷ (maximum2000)
email : gammer...@yandex.ru
***************************************************************************/
#include "./BaseParticleObject.h"
//êîíñòðóêòîð
BaseParticleObject::BaseParticleObject(std::string Name,
osg::ref_ptr<osg::Group> _root)
{
//Çàïîìèíàåì ññûëêó íà root
root = _root;
//çàäàåì èìÿ
setName (Name);
//Particle
ptemplate = new osgParticle::Particle();
ptemplate->setLifeTime(3); // 3 seconds of life
ptemplate->setSizeRange(osgParticle::rangef(0.75f, 3.0f));
ptemplate->setAlphaRange(osgParticle::rangef(0.0f, 1.5f));
ptemplate->setColorRange(osgParticle::rangev4(
osg::Vec4(1, 0.5f, 0.3f, 1.5f),
osg::Vec4(0, 0.0f, 0.0f, 0.0f)));
// these are physical properties of the particle
ptemplate->setRadius(0.05f); // 5 cm wide particles
ptemplate->setMass(0.05f); // 50 g heavy
//ParticleSystem
ps = new osgParticle::ParticleSystem;
ps->setDefaultAttributes("", false, false);
// assign the particle template to the system.
ps->setDefaultParticleTemplate(*ptemplate);
//emitter
emitter = new osgParticle::ModularEmitter;
//Ïî çàìå÷àíèÿì Àññåëü...
//Óãëû âûëåòå ÷àñòèö
//RadialShooter
shooter = new osgParticle::RadialShooter();
emitter->setShooter (shooter.get());
float k =64.0f;
float k2 =4.0f;
shooter->setPhiRange (-0.05f*k,0.05f*k);
shooter->setThetaRange (-0.05f*k2, 0.05f*k2);
//SectorPlacer
placer = new osgParticle::SectorPlacer();
placer->setRadiusRange (0.1f,0.2f);
emitter->setPlacer(placer.get());
emitter->setParticleSystem(ps.get());
rrc = static_cast<osgParticle::RandomRateCounter *>(emitter->getCounter());
rrc->setRateRange(20, 30); // generate 20 to 30 particles per second
osg::ref_ptr <osg::Geode> geode = new osg::Geode;
geode->addDrawable(ps.get());
//Â ýòîé ìàòðèöå ýìèòòåð (äîëæåí áûòü ñòðîãî â Root)
otherMatrix = new osg::MatrixTransform();
otherMatrix->setName (Name + "_emitter");
otherMatrix->addChild (geode);
psu = new osgParticle::ParticleSystemUpdater;
psu->addParticleSystem(ps.get());
root->addChild (otherMatrix);
addChild(psu.get());
addChild(emitter.get());
//ñîçäàííûå îáåðòêè (äëÿ âîçìîæíîñòè ðàáîòàòü ñ òåêñòîì êîìàíäàìè òèïà
RSetPosition..)
root->addChild(this);
}
//äåñòðóêòîð
BaseParticleObject::~BaseParticleObject()
{
std::cout << "Destructor BaseParticleObject: " << getName() <<
std::endl;
delete ptemplate;
//otherMatrix
}
//1. âðåìÿ æèçíè ÷àñòèöû
bool BaseParticleObject::ParticleSetLifeTime(float value)
{
ptemplate->setLifeTime((double)value);
ps->setDefaultParticleTemplate(*ptemplate);
return true;
}
//2. ðàçáðîñ ðàçìåðîâ
bool BaseParticleObject::ParticleSetSizeRange(float minValue, float maxValue)
{
ptemplate->setSizeRange(osgParticle::rangef(minValue, maxValue));
ps->setDefaultParticleTemplate(*ptemplate);
return true;
}
//3. ðàçáðîñ ïðîçðà÷íîñòè
bool BaseParticleObject::ParticleSetAlphaRange(float minValue, float maxValue)
{
ptemplate->setAlphaRange(osgParticle::rangef(minValue, maxValue));
ps->setDefaultParticleTemplate(*ptemplate);
return true;
}
//4. ðàçáðîñ öâåòà
bool BaseParticleObject::ParticleSetColorRange(float x1, float r1, float g1,
float b1, float x2, float r2, float g2, float b2)
{
ptemplate->setColorRange(osgParticle::rangev4(
osg::Vec4(x1, r1, g1, b1),
osg::Vec4(x2, r2, g2, b2)));
ps->setDefaultParticleTemplate(*ptemplate);
return true;
}
//5. ïåðâîíà÷àëüíûé ðàäèóñ
bool BaseParticleObject::ParticleSetRadius(float value)
{
ptemplate->setRadius(value);
ps->setDefaultParticleTemplate(*ptemplate);
return true;
}
//6. ìàññà
bool BaseParticleObject::ParticleSetMass(float value)
{
ptemplate->setMass(value);
ps->setDefaultParticleTemplate(*ptemplate);
return true;
}
//7. óãîë ïîâîðîòà ïðîñêîñòåé
bool BaseParticleObject::ParticleSetAngle (float x, float y, float z)
{
ptemplate->setAngle (osg::Vec3(x,y,z));
ps->setDefaultParticleTemplate(*ptemplate);
return true;
}
//8. òåêñòðóðà, emissive? , îñâåùàþòñÿ èëè íåò, òåêñòóðíûé þíèò=0
bool BaseParticleObject::ParticleSetDefaultAttributes(std::string filename ,
bool Emissive, bool Lighting)
{
ps->setDefaultAttributes(filename, Emissive, Lighting);
return true;
}
//9. Ðàçáðîñ êîëè÷åñòâà ãåíåðèðóåìûõ ÷àñòèö â ñåêóíäó
bool BaseParticleObject::ParticleSetRateRange(float minValue, float maxValue)
{
rrc->setRateRange(minValue, maxValue);
return true;
}
//10. Óêàçàíèå óãëîâ ðàçáðîñà ïî äâóì ïëîñêàñòÿì (çàìå÷àíèå Àññåëü)
bool BaseParticleObject::ParticleSetEmitterAngle(float alpha1,float alpha2
,float beta1 ,float beta2)
{
shooter->setPhiRange (alpha1,alpha2);
shooter->setThetaRange (beta1, beta2);
return true;
}
//11. Óêàçàíèå ðàäèóñà ñåãìåíòà, èç êîòîðîãî ïîÿâëÿþòñÿ ÷àñòèöû (çàìå÷àíèå
Àññåëü)
bool BaseParticleObject::ParticleSetEmitterRadius(float r1,float r2)
{
placer->setRadiusRange (r1,r2);
return true;
}
#ifndef BASEPARTICLEOBJECTCLASS
#define BASEPARTICLEOBJECTCLASS
#include <osg/Geode>
#include <osg/MatrixTransform>
#include <osgParticle/Particle>
#include <osgParticle/ParticleSystem>
#include <osgParticle/ParticleSystemUpdater>
#include <osgParticle/ModularEmitter>
#include <osgParticle/ModularProgram>
#include <osgParticle/RandomRateCounter>
#include <osgParticle/SectorPlacer>
#include <osgParticle/RadialShooter>
#include <osgParticle/AccelOperator>
#include <osgParticle/FluidFrictionOperator>
#include <iostream>
class BaseParticleObject: public osg::MatrixTransform
{
public:
//êîíñòðóêòîð
BaseParticleObject(std::string Name, osg::ref_ptr<osg::Group> _root);
//äåñòðóêòîð
~BaseParticleObject();
//1. âðåìÿ æèçíè ÷àñòèöû
bool ParticleSetLifeTime(float value);
//1. ðàçáðîñ ðàçìåðîâ
bool ParticleSetSizeRange(float minValue, float maxValue);
//3. ðàçáðîñ ïðîçðà÷íîñòè
bool ParticleSetAlphaRange(float minValue, float maxValue);
//4. ðàçáðîñ öâåòà
bool ParticleSetColorRange(float x1, float r1, float g1, float b1,
float x2, float r2, float g2, float b2);
//5. ïåðâîíà÷àëüíûé ðàäèóñ
bool ParticleSetRadius(float value);
//6. ìàññà
bool ParticleSetMass(float value);
//7. óãîë ïîâîðîòà ïðîñêîñòåé
bool ParticleSetAngle (float x, float y, float z);
//8. òåêñòðóðà, emissive? , îñâåùàþòñÿ èëè íåò, òåêñòóðíûé þíèò=0
bool ParticleSetDefaultAttributes( std::string filename , bool
Emissive, bool Lighting);
//9. Ðàçáðîñ êîëè÷åñòâà ãåíåðèðóåìûõ ÷àñòèö â ñåêóíäó
bool ParticleSetRateRange(float minValue, float maxValue);
//10. Óêàçàíèå óãëîâ ðàçáðîñà ïî äâóì ïëîñêàñòÿì (çàìå÷àíèå Àññåëü)
bool ParticleSetEmitterAngle(float alpha1,float alpha2 ,float beta1
,float beta2);
//11. Óêàçàíèå ðàäèóñà ñåãìåíòà, èç êîòîðîãî ïîÿâëÿþòñÿ ÷àñòèöû
(çàìå÷àíèå Àññåëü)
bool ParticleSetEmitterRadius(float r1,float r2);
private:
osgParticle::Particle* ptemplate;
osg::ref_ptr<osgParticle::ParticleSystem> ps;
osg::ref_ptr<osgParticle::ModularEmitter> emitter;
osg::ref_ptr<osgParticle::RandomRateCounter> rrc;
osg::ref_ptr<osgParticle::ParticleSystemUpdater> psu;
osg::ref_ptr<osgParticle::RadialShooter> shooter;
//èëè èëè
osg::ref_ptr<osgParticle::SectorPlacer> placer;
//ñîçäàííûå îáåðòêè (äëÿ âîçìîæíîñòè ðàáîòàòü ñ òåêñòîì êîìàíäàìè òèïà
RSetPosition..)
//ýòà ìàòðèöà êîòîðîé ìîæíî óïðàâëÿòü (RsetRotate, position ...)
//this
//Â ýòîé ìàòðèöå ýìèòòåð (äîëæåí áûòü ñòðîãî â Root)
osg::ref_ptr<osg::MatrixTransform> otherMatrix;
//ññûëêà íà root
osg::ref_ptr<osg::Group> root;
};
#endif
//see
//http://faculty.nps.edu/jasullivan/osgTutorials/osgParticleHelper.htm
//http://faculty.nps.edu/jasullivan/osgTutorials/Download/psHelper.cpp
//Author: jasulli...@nps.edu
#ifndef PARTICLEHELPER
#define PARTICLEHELPER
class psGeodeTransform : public osg::MatrixTransform
{
public:
class psGeodeTransformCallback : public osg::NodeCallback
{
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
if ( psGeodeTransform* ps = dynamic_cast<psGeodeTransform*>( node ) )
{
osg::NodePath& fullNodePath = nv->getNodePath();
fullNodePath.pop_back();
osg::Matrix localCoordMat = osg::computeLocalToWorld( fullNodePath
);
osg::Matrix inverseOfAccum = osg::Matrix::inverse( localCoordMat );
ps->setMatrix( inverseOfAccum );
}
traverse(node, nv);
}
};
psGeodeTransform() {setUpdateCallback( new psGeodeTransformCallback() );}
};
class findGeodeVisitor : public osg::NodeVisitor
{
public:
findGeodeVisitor() : osg::NodeVisitor(TRAVERSE_ALL_CHILDREN)
{
foundGeode = NULL;
}
virtual void apply(osg::Node &searchNode)
{
if (osg::Geode* g = dynamic_cast<osg::Geode*> (&searchNode) )
foundGeode = g;
else
traverse(searchNode);
}
osg::ref_ptr<osg::Geode> getGeode() {return foundGeode.get();}
protected:
osg::ref_ptr<osg::Geode> foundGeode;
};
class particleSystemHelper : public osg::Group
{
public:
particleSystemHelper(osg::Group* psGroup) : osg::Group(*psGroup)
{
osg::ref_ptr<findGeodeVisitor> fg = new findGeodeVisitor();
accept(*fg.get());
osg::ref_ptr<osg::Geode> psGeode = fg->getGeode().get();
psGeodeXForm = new psGeodeTransform();
psGeodeXForm->addChild (psGeode.get());
replaceChild(psGeode.get(),psGeodeXForm.get());
}
void addEffect(osg::Group* psGroup)
{
this->addChild(psGroup);
osg::ref_ptr<findGeodeVisitor> fg = new findGeodeVisitor();
psGroup->accept(*fg.get());
osg::ref_ptr<osg::Geode> psGeode = fg->getGeode().get();
psGeodeXForm->addChild(psGeode.get());
psGroup->removeChild( psGroup->getChildIndex(psGeode.get()) );
}
protected:
osg::ref_ptr<psGeodeTransform> psGeodeXForm;
};
#endif
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org