Added support for wind.

This commit is contained in:
Robert Osfield 2006-04-24 06:36:26 +00:00
parent e8fa0433b3
commit 49a5ef9ee1
3 changed files with 54 additions and 19 deletions

View File

@ -15,8 +15,29 @@
#include <osgUtil/CullVisitor>
#include <osgProducer/Viewer>
#include <osg/MatrixTransform>
#include <osgUtil/TransformCallback>
#include <osgParticle/PrecipitationEffect>
class MyGustCallback : public osg::NodeCallback
{
public:
MyGustCallback() {}
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
osgParticle::PrecipitationEffect* pe = dynamic_cast<osgParticle::PrecipitationEffect*>(node);
float value = sin(nv->getFrameStamp()->getReferenceTime());
if (value<-0.5) pe->getParameters()->wind.set(5.0,0.0,0.0);
else pe->getParameters()->wind.set(1.0,0.0,0.0);
traverse(node, nv);
}
};
#if 0
osg::Node* createModel(osg::Node* loadedModel, osgParticle::PrecipitationParameters& parameters)
{
@ -119,8 +140,10 @@ int main( int argc, char **argv )
while (arguments.read("--particleColor", parameters.particleColour.r(), parameters.particleColour.g(), parameters.particleColour.b(), parameters.particleColour.a())) {}
while (arguments.read("--particleColour", parameters.particleColour.r(), parameters.particleColour.g(), parameters.particleColour.b(), parameters.particleColour.a())) {}
osg::Vec3 particleVelocity;
while (arguments.read("--particleVelocity", particleVelocity.x(), particleVelocity.y(), particleVelocity.z() )) parameters.particleVelocity = particleVelocity;
osg::Vec3 wind;
while (arguments.read("--wind", wind.x(), wind.y(), wind.z())) parameters.wind = wind;
while (arguments.read("--particleVelocity", parameters.particleVelocity)) {}
while (arguments.read("--nearTransition", parameters.nearTransition )) {}
while (arguments.read("--farTransition", parameters.farTransition )) {}
@ -180,8 +203,8 @@ int main( int argc, char **argv )
return 1;
}
#if 1
// precipitationEffect->setUpdateCallback(new MyGustCallback);
osg::ref_ptr<osg::Group> group = new osg::Group;
group->addChild(precipitationEffect.get());
group->addChild(loadedModel.get());
@ -189,14 +212,6 @@ int main( int argc, char **argv )
// set the scene to render
viewer.setSceneData(group.get());
#else
loadedModel = createModel(loadedModel.get(), parameters);
// set the scene to render
viewer.setSceneData(loadedModel.get());
#endif
// create the windows and run the threads.
viewer.realize();

View File

@ -34,8 +34,9 @@ namespace osgParticle
void snow(float intensity);
osg::Vec3 wind;
osg::BoundingBox boundingBox;
osg::Vec3 particleVelocity;
float particleVelocity;
float particleSize;
osg::Vec4 particleColour;
float particleDensity;

View File

@ -110,7 +110,8 @@ PrecipitationParameters::PrecipitationParameters()
void PrecipitationParameters::rain(float intensity)
{
particleVelocity = osg::Vec3(0.0,0.0,-2.0) + osg::Vec3(0.0,0.0,-5.0)*intensity;
wind.set(0.0f,0.0f,0.0f);
particleVelocity = -2.0f + -5.0f*intensity;
particleSize = 0.01 + 0.02*intensity;
particleColour = osg::Vec4(0.6, 0.6, 0.6, 1.0) - osg::Vec4(0.1, 0.1, 0.1, 1.0)* intensity;
particleDensity = intensity * 8.5f;
@ -129,8 +130,9 @@ void PrecipitationParameters::rain(float intensity)
void PrecipitationParameters::snow(float intensity)
{
particleVelocity = osg::Vec3(0.0,0.0,-0.75) + osg::Vec3(0.0,0.0,-0.25)*intensity;
particleSize = 0.02 + 0.03*intensity;
wind.set(0.0f,0.0f,0.0f);
particleVelocity = -0.75f - 0.25f*intensity;
particleSize = 0.02f + 0.03f*intensity;
particleColour = osg::Vec4(0.85f, 0.85f, 0.85f, 1.0f) - osg::Vec4(0.1f, 0.1f, 0.1f, 1.0f)* intensity;
particleDensity = intensity * 8.2f;
cellSizeX = 5.0f / (0.25f+intensity);
@ -149,6 +151,7 @@ void PrecipitationParameters::snow(float intensity)
PrecipitationEffect::PrecipitationEffect()
{
setNumChildrenRequiringUpdateTraversal(1);
setParameters(new PrecipitationParameters);
update();
}
@ -156,14 +159,13 @@ PrecipitationEffect::PrecipitationEffect()
PrecipitationEffect::PrecipitationEffect(const PrecipitationEffect& copy, const osg::CopyOp& copyop):
osg::Group(copy,copyop)
{
setNumChildrenRequiringUpdateTraversal(getNumChildrenRequiringUpdateTraversal()+1);
setParameters(const_cast<PrecipitationParameters*>(copy._parameters.get()));
update();
}
void PrecipitationEffect::compileGLObjects(osg::State& state) const
{
osg::notify(osg::NOTICE)<<"PrecipitationEffect::compileGLObjects()"<<this<<std::endl;
if (_quadGeometry.valid())
{
_quadGeometry->compileGLObjects(state);
@ -186,6 +188,21 @@ void PrecipitationEffect::compileGLObjects(osg::State& state) const
void PrecipitationEffect::traverse(osg::NodeVisitor& nv)
{
if (nv.getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR)
{
if (nv.getFrameStamp())
{
double currentTime = nv.getFrameStamp()->getReferenceTime();
static double previousTime = currentTime;
double delta = currentTime - previousTime;
_origin += _parameters->wind * delta;
previousTime = currentTime;
}
Group::traverse(nv);
return;
}
if (nv.getVisitorType() == osg::NodeVisitor::NODE_VISITOR)
{
osgUtil::GLObjectsVisitor* globjVisitor = dynamic_cast<osgUtil::GLObjectsVisitor*>(&nv);
@ -204,6 +221,7 @@ void PrecipitationEffect::traverse(osg::NodeVisitor& nv)
if (nv.getVisitorType() != osg::NodeVisitor::CULL_VISITOR)
{
osg::notify(osg::NOTICE)<<"!CUll"<<std::endl;
Group::traverse(nv);
return;
}
@ -211,6 +229,7 @@ void PrecipitationEffect::traverse(osg::NodeVisitor& nv)
osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(&nv);
if (!cv)
{
osg::notify(osg::NOTICE)<<"Cull cast failed"<<std::endl;
Group::traverse(nv);
return;
}
@ -326,7 +345,7 @@ void PrecipitationEffect::update()
float cellVolume = length_u*length_v*length_w;
// time taken to get from start to the end of cycle
_period = fabsf(_parameters->cellSizeZ / _parameters->particleVelocity.z());
_period = fabsf(_parameters->cellSizeZ / _parameters->particleVelocity);
_du.set(length_u, 0.0f, 0.0f);
_dv.set(0.0f, length_v, 0.0f);