From 49a5ef9ee11b9336413867b9e7355c745589c230 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 24 Apr 2006 06:36:26 +0000 Subject: [PATCH] Added support for wind. --- .../osgprecipitation/osgprecipitation.cpp | 39 +++++++++++++------ include/osgParticle/PrecipitationEffect | 3 +- src/osgParticle/PrecipitationEffect.cpp | 31 ++++++++++++--- 3 files changed, 54 insertions(+), 19 deletions(-) diff --git a/examples/osgprecipitation/osgprecipitation.cpp b/examples/osgprecipitation/osgprecipitation.cpp index bfeaa723e..e318d2176 100644 --- a/examples/osgprecipitation/osgprecipitation.cpp +++ b/examples/osgprecipitation/osgprecipitation.cpp @@ -15,8 +15,29 @@ #include #include +#include +#include #include +class MyGustCallback : public osg::NodeCallback +{ + + public: + + MyGustCallback() {} + + virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) + { + osgParticle::PrecipitationEffect* pe = dynamic_cast(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 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(); diff --git a/include/osgParticle/PrecipitationEffect b/include/osgParticle/PrecipitationEffect index d04c7893f..a875c8b5b 100644 --- a/include/osgParticle/PrecipitationEffect +++ b/include/osgParticle/PrecipitationEffect @@ -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; diff --git a/src/osgParticle/PrecipitationEffect.cpp b/src/osgParticle/PrecipitationEffect.cpp index e3394aa73..22e5f7fd2 100644 --- a/src/osgParticle/PrecipitationEffect.cpp +++ b/src/osgParticle/PrecipitationEffect.cpp @@ -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(copy._parameters.get())); update(); } void PrecipitationEffect::compileGLObjects(osg::State& state) const { - osg::notify(osg::NOTICE)<<"PrecipitationEffect::compileGLObjects()"<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(&nv); @@ -204,6 +221,7 @@ void PrecipitationEffect::traverse(osg::NodeVisitor& nv) if (nv.getVisitorType() != osg::NodeVisitor::CULL_VISITOR) { + osg::notify(osg::NOTICE)<<"!CUll"<(&nv); if (!cv) { + osg::notify(osg::NOTICE)<<"Cull cast failed"<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);