From ae5aefac0e3d771fa17a11215c1c2558660b1d90 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 13 May 2004 22:09:23 +0000 Subject: [PATCH] Added the beginings of new osgParticle Explosion, Fire and SmokeEffects. Added support for generation explosion, fire and smoke effects in osgsimulation --- VisualStudio/VisualStudio.dsw | 3 + VisualStudio/osgParticle/osgParticle.dsp | 32 ++ examples/osgsimulation/GNUmakefile | 2 +- examples/osgsimulation/GNUmakefile.inst | 2 +- examples/osgsimulation/osgsimulation.cpp | 108 ++--- include/osgParticle/ExplosionEffect | 41 ++ include/osgParticle/FireEffect | 41 ++ include/osgParticle/ParticleEffect | 60 +++ include/osgParticle/RandomRateCounter | 1 - include/osgParticle/SmokeEffect | 41 ++ src/osgParticle/ExplosionEffect.cpp | 134 ++++++ src/osgParticle/FireEffect.cpp | 131 ++++++ src/osgParticle/GNUmakefile | 4 + src/osgParticle/ParticleEffect.cpp | 24 ++ src/osgParticle/SmokeEffect.cpp | 130 ++++++ src/osgPlugins/dds/ReaderWriterDDS.cpp | 513 ++++++++++++++++++----- 16 files changed, 1091 insertions(+), 176 deletions(-) create mode 100644 include/osgParticle/ExplosionEffect create mode 100644 include/osgParticle/FireEffect create mode 100644 include/osgParticle/ParticleEffect create mode 100644 include/osgParticle/SmokeEffect create mode 100644 src/osgParticle/ExplosionEffect.cpp create mode 100644 src/osgParticle/FireEffect.cpp create mode 100644 src/osgParticle/ParticleEffect.cpp create mode 100644 src/osgParticle/SmokeEffect.cpp diff --git a/VisualStudio/VisualStudio.dsw b/VisualStudio/VisualStudio.dsw index 486af2fb5..5dba48b13 100644 --- a/VisualStudio/VisualStudio.dsw +++ b/VisualStudio/VisualStudio.dsw @@ -852,6 +852,9 @@ Package=<4> Project_Dep_Name Core osgSim End Project Dependency Begin Project Dependency + Project_Dep_Name Core osgParticle + End Project Dependency + Begin Project Dependency Project_Dep_Name Core osgProducer End Project Dependency Begin Project Dependency diff --git a/VisualStudio/osgParticle/osgParticle.dsp b/VisualStudio/osgParticle/osgParticle.dsp index 5feec650d..fc648cb61 100644 --- a/VisualStudio/osgParticle/osgParticle.dsp +++ b/VisualStudio/osgParticle/osgParticle.dsp @@ -99,6 +99,22 @@ SOURCE=..\..\src\osgParticle\Emitter.cpp # End Source File # Begin Source File +SOURCE=..\..\src\osgParticle\ExplosionEffect.cpp +# End Source File +# Begin Source File + +SOURCE=..\..\src\osgParticle\FireEffect.cpp +# End Source File +# Begin Source File + +SOURCE=..\..\src\osgParticle\SmokeEffect.cpp +# End Source File +# Begin Source File + +SOURCE=..\..\src\osgParticle\ParticleEffect.cpp +# End Source File +# Begin Source File + SOURCE=..\..\src\osgParticle\FluidFrictionOperator.cpp # End Source File # Begin Source File @@ -167,6 +183,22 @@ SOURCE=..\..\include\osgParticle\Export # End Source File # Begin Source File +SOURCE=..\..\src\osgParticle\ExplosionEffect +# End Source File +# Begin Source File + +SOURCE=..\..\src\osgParticle\FireEffect +# End Source File +# Begin Source File + +SOURCE=..\..\src\osgParticle\SmokeEffect +# End Source File +# Begin Source File + +SOURCE=..\..\src\osgParticle\ParticleEffect +# End Source File +# Begin Source File + SOURCE=..\..\include\osgParticle\FluidFrictionOperator # End Source File # Begin Source File diff --git a/examples/osgsimulation/GNUmakefile b/examples/osgsimulation/GNUmakefile index bea0845a3..0f459affe 100644 --- a/examples/osgsimulation/GNUmakefile +++ b/examples/osgsimulation/GNUmakefile @@ -4,7 +4,7 @@ include $(TOPDIR)/Make/makedefs CXXFILES =\ osgsimulation.cpp\ -LIBS += -losgProducer -lProducer -losgSim -losgText -losgGA -losgDB -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS) +LIBS += -losgProducer -lProducer -losgParticle -losgSim -losgText -losgGA -losgDB -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS) INSTFILES = \ $(CXXFILES)\ diff --git a/examples/osgsimulation/GNUmakefile.inst b/examples/osgsimulation/GNUmakefile.inst index 3ed50c4bf..4d5b8b012 100644 --- a/examples/osgsimulation/GNUmakefile.inst +++ b/examples/osgsimulation/GNUmakefile.inst @@ -4,7 +4,7 @@ include $(TOPDIR)/Make/makedefs CXXFILES =\ osgsimulation.cpp\ -LIBS += -losgProducer -lProducer -losgDB -losgSim -losgText -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS) +LIBS += -losgProducer -lProducer -losgDB -losgParticle -losgSim -losgText -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS) EXEC = osgsimulation diff --git a/examples/osgsimulation/osgsimulation.cpp b/examples/osgsimulation/osgsimulation.cpp index 3d9802e3e..543aeb2a3 100644 --- a/examples/osgsimulation/osgsimulation.cpp +++ b/examples/osgsimulation/osgsimulation.cpp @@ -1,51 +1,54 @@ -/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield - * - * This application is open source and may be redistributed and/or modified - * freely and without restriction, both in commericial and non commericial applications, - * as long as this copyright notice is maintained. - * - * This application is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -*/ - -#include -#include #include +#include +#include -#include +#include +#include +#include +#include -using namespace osgSim; -osg::Node* createSphereSegment() +////////////////////////////////////////////////////////////////////////////// +// MAIN SCENE GRAPH BUILDING FUNCTION +////////////////////////////////////////////////////////////////////////////// + +void build_world(osg::Group *root) { - osgSim::SphereSegment* ss = new osgSim::SphereSegment(osg::Vec3(0.0f,0.0f,0.0f), 1.0f, - osg::Vec3(0.0f,1.0f,0.0f), - osg::DegreesToRadians(360.0f), - osg::DegreesToRadians(45.0f), - 40); - ss->setAllColors(osg::Vec4(1.0f,1.0f,1.0f,0.5f)); - return ss; -} + osgParticle::ExplosionEffect* explosion = new osgParticle::ExplosionEffect; + osgParticle::SmokeEffect* smoke = new osgParticle::SmokeEffect; + osgParticle::FireEffect* fire = new osgParticle::FireEffect; + + root->addChild(explosion); + root->addChild(smoke); + root->addChild(fire); + + osgParticle::ParticleSystemUpdater *psu = new osgParticle::ParticleSystemUpdater; + + psu->addParticleSystem(explosion->getParticleSystem()); + psu->addParticleSystem(smoke->getParticleSystem()); + psu->addParticleSystem(fire->getParticleSystem()); + + // add the updater node to the scene graph + root->addChild(psu); -osg::Node* createModel() -{ - return createSphereSegment(); } -int main( int argc, char **argv ) -{ +////////////////////////////////////////////////////////////////////////////// +// main() +////////////////////////////////////////////////////////////////////////////// + +int main(int argc, char **argv) +{ // use an ArgumentParser object to manage the program arguments. osg::ArgumentParser arguments(&argc,argv); // set up the usage document, in case we need to print out how to use this program. - arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); - arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard OpenSceneGraph example which loads and visualises 3d models."); - arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); + arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of particle systems."); + arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] image_file_left_eye image_file_right_eye"); arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); @@ -75,40 +78,12 @@ int main( int argc, char **argv ) return 1; } - if (arguments.argc()<=1) - { - arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION); - return 1; - } - -// osg::Timer timer; -// osg::Timer_t start_tick = timer.tick(); -// -// // read the scene from the list of file specified commandline args. -// osg::ref_ptr loadedModel = osgDB::readNodeFiles(arguments); -// -// // if no model has been successfully loaded report failure. -// if (!loadedModel) -// { -// std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl; -// return 1; -// } -// -// osg::Timer_t end_tick = timer.tick(); -// -// std::cout << "Time to load = "< loadedModel = createModel(); - - - // optimize the scene graph, remove rendundent nodes and state etc. - osgUtil::Optimizer optimizer; - optimizer.optimize(loadedModel.get()); - - - // set the scene to render - viewer.setSceneData(loadedModel.get()); - + osg::Group *root = new osg::Group; + build_world(root); + + // add a viewport to the viewer and attach the scene graph. + viewer.setSceneData(root); + // create the windows and run the threads. viewer.realize(); @@ -131,4 +106,3 @@ int main( int argc, char **argv ) return 0; } - diff --git a/include/osgParticle/ExplosionEffect b/include/osgParticle/ExplosionEffect new file mode 100644 index 000000000..39ca0234d --- /dev/null +++ b/include/osgParticle/ExplosionEffect @@ -0,0 +1,41 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#ifndef OSGPARTICLE_EXPLOSIONEFFECT +#define OSGPARTICLE_EXPLOSIONEFFECT + +#include + +namespace osgParticle +{ + + class OSGPARTICLE_EXPORT ExplosionEffect : public ParticleEffect + { + public: + + ExplosionEffect(); + + ExplosionEffect(const ExplosionEffect& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY); + + META_Node(osgParticle,ExplosionEffect); + + virtual void setDefaults(); + + virtual void buildEffect(); + + protected: + + }; +} + +#endif diff --git a/include/osgParticle/FireEffect b/include/osgParticle/FireEffect new file mode 100644 index 000000000..0ed8047ba --- /dev/null +++ b/include/osgParticle/FireEffect @@ -0,0 +1,41 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#ifndef OSGPARTICLE_FireEffect +#define OSGPARTICLE_FireEffect + +#include + +namespace osgParticle +{ + + class OSGPARTICLE_EXPORT FireEffect : public ParticleEffect + { + public: + + FireEffect(); + + FireEffect(const FireEffect& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY); + + META_Node(osgParticle,FireEffect); + + virtual void setDefaults(); + + virtual void buildEffect(); + + protected: + + }; +} + +#endif diff --git a/include/osgParticle/ParticleEffect b/include/osgParticle/ParticleEffect new file mode 100644 index 000000000..186f43380 --- /dev/null +++ b/include/osgParticle/ParticleEffect @@ -0,0 +1,60 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#ifndef OSGPARTICLE_PARTICLEEFFECT +#define OSGPARTICLE_PARTICLEEFFECT + +#include +#include + +namespace osgParticle +{ + + class OSGPARTICLE_EXPORT ParticleEffect : public osg::Group + { + public: + + ParticleEffect() {} + + ParticleEffect(const ParticleEffect& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY); + + virtual const char *libraryName() const { return "osgParticle"; } + virtual const char *className() const { return "ParticleEffect"; } + virtual bool isSameKindAs(const osg::Object *obj) const { return dynamic_cast(obj) != 0; } + virtual void accept(osg::NodeVisitor& nv) { if (nv.validNodeMask(*this)) { nv.pushOntoNodePath(this); nv.apply(*this); nv.popFromNodePath(); } } + + void setEmitter(Emitter* emitter); + Emitter* getEmitter() { return _emitter.get(); } + const Emitter* getEmitter() const { return _emitter.get(); } + + void setProgram(Program* program); + Program* getProgram() { return _program.get(); } + const Program* getProgram() const { return _program.get(); } + + void setParticleSystem(ParticleSystem* ps); + ParticleSystem* getParticleSystem() { return _particleSystem.get(); } + const ParticleSystem* getParticleSystem() const { return _particleSystem.get(); } + + virtual void buildEffect() = 0; + + protected: + + osg::ref_ptr _emitter; + osg::ref_ptr _program; + osg::ref_ptr _particleSystem; + + }; + +} + +#endif diff --git a/include/osgParticle/RandomRateCounter b/include/osgParticle/RandomRateCounter index ede9a0e35..9084e6713 100644 --- a/include/osgParticle/RandomRateCounter +++ b/include/osgParticle/RandomRateCounter @@ -36,7 +36,6 @@ namespace osgParticle protected: virtual ~RandomRateCounter() {} - private: mutable float np_; }; diff --git a/include/osgParticle/SmokeEffect b/include/osgParticle/SmokeEffect new file mode 100644 index 000000000..0cefd7206 --- /dev/null +++ b/include/osgParticle/SmokeEffect @@ -0,0 +1,41 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#ifndef OSGPARTICLE_SmokeEffect +#define OSGPARTICLE_SmokeEffect + +#include + +namespace osgParticle +{ + + class OSGPARTICLE_EXPORT SmokeEffect : public ParticleEffect + { + public: + + SmokeEffect(); + + SmokeEffect(const SmokeEffect& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY); + + META_Node(osgParticle,SmokeEffect); + + virtual void setDefaults(); + + virtual void buildEffect(); + + protected: + + }; +} + +#endif diff --git a/src/osgParticle/ExplosionEffect.cpp b/src/osgParticle/ExplosionEffect.cpp new file mode 100644 index 000000000..a1c69a441 --- /dev/null +++ b/src/osgParticle/ExplosionEffect.cpp @@ -0,0 +1,134 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +using namespace osgParticle; + +ExplosionEffect::ExplosionEffect() +{ + setDefaults(); +} + +ExplosionEffect::ExplosionEffect(const ExplosionEffect& copy, const osg::CopyOp& copyop): + ParticleEffect(copy,copyop) +{ +} + +void ExplosionEffect::setDefaults() +{ + osgParticle::ParticleSystem *ps = new osgParticle::ParticleSystem; + ps->setDefaultAttributes("Images/particle.rgb", false, false); + + _particleSystem = ps; + + // set up the emitter + { + osgParticle::ModularEmitter *emitter = new osgParticle::ModularEmitter; + emitter->setParticleSystem(ps); + + osgParticle::Particle ptemplate; + + ptemplate.setLifeTime(3); // 3 seconds of life + + // the following ranges set the envelope of the respective + // graphical properties in time. + ptemplate.setSizeRange(osgParticle::rangef(0.75f, 3.0f)); + ptemplate.setAlphaRange(osgParticle::rangef(0.0f, 1.0f)); + ptemplate.setColorRange(osgParticle::rangev4( + osg::Vec4(1, 1.0f, 0.0f, 1.0f), + osg::Vec4(0.5, 0.5f, 0.0f, 0.0f))); + + // these are physical properties of the particle + ptemplate.setRadius(0.1f); // 5 cm wide particles + ptemplate.setMass(1.0f); // 1kg heavy + + // assign the particle template to the system. + ps->setDefaultParticleTemplate(ptemplate); + + //osgParticle::LimitedDurationRandomRateCounter* counter = new osgParticle::LimitedDurationRandomRateCounter; + osgParticle::RandomRateCounter* counter = new osgParticle::RandomRateCounter; + counter->setRateRange(2000,2000); // generate 1000 particles per second + emitter->setCounter(counter); + + osgParticle::SectorPlacer* placer = new osgParticle::SectorPlacer; + placer->setCenter(osg::Vec3(0.0,0.0,0.0)); + placer->setRadiusRange(0.0f,1.0f); + emitter->setPlacer(placer); + + osgParticle::RadialShooter* shooter = new osgParticle::RadialShooter; + shooter->setThetaRange(0.0f, osg::PI_2); + shooter->setInitialSpeedRange(5.0f,30.0f); + emitter->setShooter(shooter); + + emitter->setStartTime(0.0f); + emitter->setResetTime(5.0f); + emitter->setLifeTime(0.1f); + emitter->setEndless(false); + + _emitter = emitter; + } + + + // set up the program + { + osgParticle::ModularProgram *program = new osgParticle::ModularProgram; + program->setParticleSystem(ps); + + // create an operator that simulates the gravity acceleration. + osgParticle::AccelOperator *op1 = new osgParticle::AccelOperator; + op1->setToGravity(); + program->addOperator(op1); + + // let's add a fluid operator to simulate air friction. + osgParticle::FluidFrictionOperator *op3 = new osgParticle::FluidFrictionOperator; + op3->setFluidToAir(); + program->addOperator(op3); + + _program = program; + } + + + buildEffect(); +} + +void ExplosionEffect::buildEffect() +{ + // clear the children. + removeChild(0,getNumChildren()); + + if (!_emitter || !_particleSystem || !_program) return; + + // add the emitter + addChild(_emitter.get()); + + // add the program to update the particles + addChild(_program.get()); + + osg::Geode *geode = new osg::Geode; + geode->addDrawable(_particleSystem.get()); + + // add the geode to the scene graph + addChild(geode); +} diff --git a/src/osgParticle/FireEffect.cpp b/src/osgParticle/FireEffect.cpp new file mode 100644 index 000000000..c99cf3994 --- /dev/null +++ b/src/osgParticle/FireEffect.cpp @@ -0,0 +1,131 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +using namespace osgParticle; + +FireEffect::FireEffect() +{ + setDefaults(); +} + +FireEffect::FireEffect(const FireEffect& copy, const osg::CopyOp& copyop): + ParticleEffect(copy,copyop) +{ +} + +void FireEffect::setDefaults() +{ + osgParticle::ParticleSystem *ps = new osgParticle::ParticleSystem; + ps->setDefaultAttributes("Images/smoke.rgb", true, false); + + _particleSystem = ps; + + // set up the emitter + { + osgParticle::ModularEmitter *emitter = new osgParticle::ModularEmitter; + emitter->setParticleSystem(ps); + + + osgParticle::Particle ptemplate; + + ptemplate.setLifeTime(2); // 3 seconds of life + + // the following ranges set the envelope of the respective + // graphical properties in time. + ptemplate.setSizeRange(osgParticle::rangef(0.75f, 3.0f)); + ptemplate.setAlphaRange(osgParticle::rangef(0.0f, 1.0f)); + ptemplate.setColorRange(osgParticle::rangev4( + osg::Vec4(1, 1.0f, 0.2f, 1.0f), + osg::Vec4(1, 0.0f, 0.f, 0.0f))); + + // these are physical properties of the particle + ptemplate.setRadius(0.05f); // 5 cm wide particles + ptemplate.setMass(0.01f); // 10g heavy + + // assign the particle template to the system. + ps->setDefaultParticleTemplate(ptemplate); + + osgParticle::RandomRateCounter* counter = new osgParticle::RandomRateCounter; + counter->setRateRange(1,10); // generate 1000 particles per second + emitter->setCounter(counter); + + osgParticle::SectorPlacer* placer = new osgParticle::SectorPlacer; + placer->setCenter(osg::Vec3(0.0,0.0,0.0)); + placer->setRadiusRange(0.0f,0.25f); + emitter->setPlacer(placer); + + osgParticle::RadialShooter* shooter = new osgParticle::RadialShooter; + shooter->setThetaRange(0.0f, osg::PI_4); + shooter->setInitialSpeedRange(0.0f,0.0f); + emitter->setShooter(shooter); + + emitter->setStartTime(0.0f); + + _emitter = emitter; + } + + + // set up the program + { + osgParticle::ModularProgram *program = new osgParticle::ModularProgram; + program->setParticleSystem(ps); + + // create an operator that simulates the gravity acceleration. + osgParticle::AccelOperator *op1 = new osgParticle::AccelOperator; + op1->setToGravity(-0.2); + program->addOperator(op1); + + // let's add a fluid operator to simulate air friction. + osgParticle::FluidFrictionOperator *op3 = new osgParticle::FluidFrictionOperator; + op3->setFluidToAir(); + program->addOperator(op3); + + // add the program to the scene graph + _program = program; + } + + + buildEffect(); +} + +void FireEffect::buildEffect() +{ + // clear the children. + removeChild(0,getNumChildren()); + + if (!_emitter || !_particleSystem || !_program) return; + + // add the emitter + addChild(_emitter.get()); + + // add the program to update the particles + addChild(_program.get()); + + osg::Geode *geode = new osg::Geode; + geode->addDrawable(_particleSystem.get()); + + // add the geode to the scene graph + addChild(geode); +} diff --git a/src/osgParticle/GNUmakefile b/src/osgParticle/GNUmakefile index 754035595..1913c51b5 100644 --- a/src/osgParticle/GNUmakefile +++ b/src/osgParticle/GNUmakefile @@ -3,11 +3,15 @@ include $(TOPDIR)/Make/makedefs CXXFILES =\ Emitter.cpp\ + ExplosionEffect.cpp\ + SmokeEffect.cpp\ + FireEffect.cpp\ FluidFrictionOperator.cpp\ ModularEmitter.cpp\ ModularProgram.cpp\ MultiSegmentPlacer.cpp\ Particle.cpp\ + ParticleEffect.cpp\ ParticleProcessor.cpp\ ParticleSystem.cpp\ ParticleSystemUpdater.cpp\ diff --git a/src/osgParticle/ParticleEffect.cpp b/src/osgParticle/ParticleEffect.cpp new file mode 100644 index 000000000..770f23641 --- /dev/null +++ b/src/osgParticle/ParticleEffect.cpp @@ -0,0 +1,24 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#include + +using namespace osgParticle; + +ParticleEffect::ParticleEffect(const ParticleEffect& copy, const osg::CopyOp& copyop): + osg::Group(copy,copyop)/*, + _emitter(), + _program(), + _particleSystem(copy._particleSystem.valid()?copy._particleSystem->clone():0)*/ +{ +} diff --git a/src/osgParticle/SmokeEffect.cpp b/src/osgParticle/SmokeEffect.cpp new file mode 100644 index 000000000..d0d60976a --- /dev/null +++ b/src/osgParticle/SmokeEffect.cpp @@ -0,0 +1,130 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +using namespace osgParticle; + +SmokeEffect::SmokeEffect() +{ + setDefaults(); +} + +SmokeEffect::SmokeEffect(const SmokeEffect& copy, const osg::CopyOp& copyop): + ParticleEffect(copy,copyop) +{ +} + +void SmokeEffect::setDefaults() +{ + + osgParticle::ParticleSystem *ps = new osgParticle::ParticleSystem; + ps->setDefaultAttributes("Images/smoke.rgb", false, false); + + _particleSystem = ps; + + // set up the emitter + { + osgParticle::ModularEmitter *emitter = new osgParticle::ModularEmitter; + emitter->setParticleSystem(ps); + + + osgParticle::Particle ptemplate; + + ptemplate.setLifeTime(10); // 3 seconds of life + + // the following ranges set the envelope of the respective + // graphical properties in time. + ptemplate.setSizeRange(osgParticle::rangef(0.75f, 3.0f)); + ptemplate.setAlphaRange(osgParticle::rangef(0.0f, 1.0f)); + ptemplate.setColorRange(osgParticle::rangev4( + osg::Vec4(1, 1.0f, 1.0f, 1.0f), + osg::Vec4(1, 1.0f, 1.f, 0.0f))); + + // these are physical properties of the particle + ptemplate.setRadius(0.05f); // 5 cm wide particles + ptemplate.setMass(0.01f); // 10g heavy + + // assign the particle template to the system. + ps->setDefaultParticleTemplate(ptemplate); + + osgParticle::RandomRateCounter* counter = new osgParticle::RandomRateCounter; + counter->setRateRange(1,2); // generate 1000 particles per second + emitter->setCounter(counter); + + osgParticle::SectorPlacer* placer = new osgParticle::SectorPlacer; + placer->setCenter(osg::Vec3(0.0,0.0,0.0)); + placer->setRadiusRange(0.0f,0.5f); + emitter->setPlacer(placer); + + osgParticle::RadialShooter* shooter = new osgParticle::RadialShooter; + shooter->setThetaRange(0.0f, osg::PI_4); + shooter->setInitialSpeedRange(0.0f,0.0f); + emitter->setShooter(shooter); + + emitter->setStartTime(0.0f); + + _emitter = emitter; + } + + + // set up the program + { + osgParticle::ModularProgram *program = new osgParticle::ModularProgram; + program->setParticleSystem(ps); + + // create an operator that simulates the gravity acceleration. + osgParticle::AccelOperator *op1 = new osgParticle::AccelOperator; + op1->setToGravity(-0.1); + program->addOperator(op1); + + // let's add a fluid operator to simulate air friction. + osgParticle::FluidFrictionOperator *op3 = new osgParticle::FluidFrictionOperator; + op3->setFluidToAir(); + program->addOperator(op3); + + _program = program; + } + + buildEffect(); +} + +void SmokeEffect::buildEffect() +{ + // clear the children. + removeChild(0,getNumChildren()); + + if (!_emitter || !_particleSystem || !_program) return; + + // add the emitter + addChild(_emitter.get()); + + // add the program to update the particles + addChild(_program.get()); + + osg::Geode *geode = new osg::Geode; + geode->addDrawable(_particleSystem.get()); + + // add the geode to the scene graph + addChild(geode); +} diff --git a/src/osgPlugins/dds/ReaderWriterDDS.cpp b/src/osgPlugins/dds/ReaderWriterDDS.cpp index 73065c8f3..cbecbf82e 100644 --- a/src/osgPlugins/dds/ReaderWriterDDS.cpp +++ b/src/osgPlugins/dds/ReaderWriterDDS.cpp @@ -1,19 +1,21 @@ /********************************************************************** - * - * FILE: ReaderWriterdds.cpp - * - * DESCRIPTION: Class for reading a DDS file into an osg::Image. - * - * Example on reading a DDS file code can be found at: - * http://developer.nvidia.com/docs/IO/1327/ATT/ - * ARB_texture_compression.pdf - * Author: Sébastien Dominé, NVIDIA Corporation - * - * CREATED BY: Rune Schmidt Jensen, rune@schmidt-jensen.com - * - * HISTORY: Created 31.03.2003 - * - **********************************************************************/ +* +* FILE: ReaderWriterdds.cpp +* +* DESCRIPTION: Class for reading a DDS file into an osg::Image. +* +* Example on reading a DDS file code can be found at: +* http://developer.nvidia.com/docs/IO/1327/ATT/ +* ARB_texture_compression.pdf +* Author: Sebastien Domine, NVIDIA Corporation +* +* CREATED BY: Rune Schmidt Jensen, rsj@uni-dk +* +* HISTORY: Created 31.03.2003 +* Modified 13.05.2004 +* by George Tarantilis, gtaranti@nps.navy.mil +* +**********************************************************************/ #include #include @@ -24,6 +26,7 @@ #include + // NOTICE ON WIN32: // typedef DWORD unsigned long; // sizeof(DWORD) = 4 @@ -33,7 +36,6 @@ typedef struct _DDCOLORKEY unsigned long dwColorSpaceHighValue; } DDCOLORKEY; - typedef struct _DDPIXELFORMAT { unsigned long dwSize; @@ -87,49 +89,81 @@ typedef struct _DDSURFACEDESC2 unsigned long dwSize; unsigned long dwFlags; unsigned long dwHeight; - unsigned long dwWidth; - union + unsigned long dwWidth; + union { long lPitch; unsigned long dwLinearSize; }; - unsigned long dwBackBufferCount; + union + { + unsigned long dwBackBufferCount; + unsigned long dwDepth; + }; union { unsigned long dwMipMapCount; unsigned long dwRefreshRate; }; unsigned long dwAlphaBitDepth; - unsigned long dwReserved; - unsigned long* lpSurface; - DDCOLORKEY ddckCKDestOverlay; - DDCOLORKEY ddckCKDestBlt; - DDCOLORKEY ddckCKSrcOverlay; - DDCOLORKEY ddckCKSrcBlt; - DDPIXELFORMAT ddpfPixelFormat; - DDSCAPS2 ddsCaps; - unsigned long dwTextureStage; + unsigned long dwReserved; + unsigned long* lpSurface; + DDCOLORKEY ddckCKDestOverlay; + DDCOLORKEY ddckCKDestBlt; + DDCOLORKEY ddckCKSrcOverlay; + DDCOLORKEY ddckCKSrcBlt; + DDPIXELFORMAT ddpfPixelFormat; + DDSCAPS2 ddsCaps; + unsigned long dwTextureStage; } DDSURFACEDESC2; +// +// DDSURFACEDESC2 flags that mark the validity of the struct data +// +#define DDSD_CAPS 0x00000001l // default +#define DDSD_HEIGHT 0x00000002l // default +#define DDSD_WIDTH 0x00000004l // default +#define DDSD_PIXELFORMAT 0x00001000l // default +#define DDSD_PITCH 0x00000008l // For uncompressed formats +#define DDSD_MIPMAPCOUNT 0x00020000l +#define DDSD_LINEARSIZE 0x00080000l // For compressed formats +#define DDSD_DEPTH 0x00800000l // Volume Textures + +// +// DDPIXELFORMAT flags +// #define DDPF_ALPHAPIXELS 0x00000001l -#define DDPF_FOURCC 0x00000004l -#define DDPF_RGB 0x00000040l +#define DDPF_FOURCC 0x00000004l // Compressed formats +#define DDPF_RGB 0x00000040l // Uncompressed formats +#define DDPF_ALPHA 0x00000002l +#define DDPF_COMPRESSED 0x00000080l +#define DDPF_LUMINANCE 0x00020000l + +// +// DDSCAPS flags +// +#define DDSCAPS_TEXTURE 0x00001000l // default +#define DDSCAPS_COMPLEX 0x00000008l +#define DDSCAPS_MIPMAP 0x00400000l +#define DDSCAPS2_VOLUME 0x00200000l + #ifndef MAKEFOURCC - #define MAKEFOURCC(ch0, ch1, ch2, ch3) \ - ((unsigned long)(char)(ch0) | ((unsigned long)(char)(ch1) << 8) | \ - ((unsigned long)(char)(ch2) << 16) | ((unsigned long)(char)(ch3) << 24 )) +#define MAKEFOURCC(ch0, ch1, ch2, ch3) \ + ((unsigned long)(char)(ch0) | ((unsigned long)(char)(ch1) << 8) | \ + ((unsigned long)(char)(ch2) << 16) | ((unsigned long)(char)(ch3) << 24 )) #endif //defined(MAKEFOURCC) /* - * FOURCC codes for DX compressed-texture pixel formats - */ +* FOURCC codes for DX compressed-texture pixel formats +*/ #define FOURCC_DXT1 (MAKEFOURCC('D','X','T','1')) #define FOURCC_DXT2 (MAKEFOURCC('D','X','T','2')) #define FOURCC_DXT3 (MAKEFOURCC('D','X','T','3')) #define FOURCC_DXT4 (MAKEFOURCC('D','X','T','4')) #define FOURCC_DXT5 (MAKEFOURCC('D','X','T','5')) + osg::Image* ReadDDSFile(const char *filename) { @@ -153,7 +187,7 @@ osg::Image* ReadDDSFile(const char *filename) // Get the surface desc. fread(&ddsd, sizeof(ddsd), 1, fp); - // Read image data. + // Size of 2d images - 3d images don't set dwLinearSize unsigned int size = ddsd.dwMipMapCount > 1 ? ddsd.dwLinearSize * (ddsd.ddpfPixelFormat.dwFourCC==FOURCC_DXT1 ? 2: 4) : ddsd.dwLinearSize; if(size <= 0) @@ -161,86 +195,109 @@ osg::Image* ReadDDSFile(const char *filename) osg::notify(osg::WARN)<<"Warning:: dwLinearSize is not defined in dds file, image not loaded."< osgImage = new osg::Image(); osgImage->setFileName(filename); - unsigned char* imageData = new unsigned char [size]; - fread(imageData, 1, size, fp); - // Close the file. - fclose(fp); + //Check valid structure sizes + if(ddsd.dwSize != 124 && ddsd.ddpfPixelFormat.dwSize != 32) + { + fclose(fp); + return NULL; + } + + bool is3dImage = false; + int depth = 1; + + // Check for volume image + if( ddsd.dwDepth > 0 && (ddsd.dwFlags & DDSD_DEPTH)) + { + is3dImage = true; + depth = ddsd.dwDepth; + } // Retreive image properties. int s = ddsd.dwWidth; int t = ddsd.dwHeight; - int r = 1; // we're not a 3d texture... + int r = depth; unsigned int dataType = GL_UNSIGNED_BYTE; unsigned int pixelFormat = 0; unsigned int internalFormat = 0; // Uncompressed formats will usually use DDPF_RGB to indicate an RGB format, // while compressed formats will use DDPF_FOURCC with a four-character code. + + bool usingAlpha = ddsd.ddpfPixelFormat.dwFlags & DDPF_ALPHAPIXELS; // Uncompressed formats. if(ddsd.ddpfPixelFormat.dwFlags & DDPF_RGB) { - bool usingAlpha = ddsd.ddpfPixelFormat.dwFlags & DDPF_ALPHAPIXELS; switch(ddsd.ddpfPixelFormat.dwRGBBitCount) { - case 32: - internalFormat = usingAlpha ? 4 : 3; - pixelFormat = usingAlpha ? GL_RGBA : GL_RGB; - break; - case 24: - case 16: - default: - osg::notify(osg::WARN)<<"Warning:: unhandled pixel format in dds file, image not loaded."<getDataType(); + unsigned int pixelFormat = img->getPixelFormat(); + unsigned int internalFormat = img->getInternalTextureFormat(); + unsigned int components = osg::Image::computeNumComponents(pixelFormat); + unsigned int pixelSize = osg::Image::computePixelSizeInBits(pixelFormat, dataType); + unsigned int imageSize = img->getImageSizeInBytes() / components; + bool is3dImage = false; + + int s = ddsd.dwWidth = img->s(); + int t = ddsd.dwHeight = img->t(); + int r = img->r(); + + if(r > 1) /* check for 3d image */ + { + is3dImage = true; + ddsd.dwDepth = r; + SD_flags |= DDSD_DEPTH; + CAPS_flags |= DDSCAPS_COMPLEX; + CAPS2_flags |= DDSCAPS2_VOLUME; + } + + // Determine format - set flags and ddsd, ddpf properties + switch (pixelFormat) + { + //Uncompressed + case GL_RGBA: + { + ddpf.dwRBitMask = 0x00ff0000; //1st change + ddpf.dwGBitMask = 0x0000ff00; + ddpf.dwBBitMask = 0x000000ff; + ddpf.dwRGBAlphaBitMask = 0xff000000; + PF_flags |= (DDPF_ALPHAPIXELS | DDPF_RGB); + ddpf.dwRGBBitCount = pixelSize; + ddsd.lPitch = img->getRowSizeInBytes(); + SD_flags |= DDSD_PITCH; + } + break; + case GL_BGRA: + { + ddpf.dwBBitMask = 0x00ff0000; //2nd change + ddpf.dwGBitMask = 0x0000ff00; + ddpf.dwRBitMask = 0x000000ff; + ddpf.dwRGBAlphaBitMask = 0xff000000; + PF_flags |= (DDPF_ALPHAPIXELS | DDPF_RGB); + ddpf.dwRGBBitCount = pixelSize; + ddsd.lPitch = img->getRowSizeInBytes(); + SD_flags |= DDSD_PITCH; + } + break; + case GL_LUMINANCE_ALPHA: + { + ddpf.dwRBitMask = 0x00ff0000; + ddpf.dwRGBAlphaBitMask = 0x000000ff; + PF_flags |= (DDPF_ALPHAPIXELS | DDPF_LUMINANCE); //3rd change + ddpf.dwRGBBitCount = pixelSize; + ddsd.lPitch = img->getRowSizeInBytes(); + SD_flags |= DDSD_PITCH; + } + break; + case GL_RGB: + { + ddpf.dwRBitMask = 0x00ff0000; + ddpf.dwGBitMask = 0x0000ff00; + ddpf.dwBBitMask = 0x000000ff; + PF_flags |= DDPF_RGB; + ddpf.dwRGBBitCount = pixelSize; + ddsd.lPitch = img->getRowSizeInBytes(); + SD_flags |= DDSD_PITCH; + + } + break; + case GL_LUMINANCE: + { + ddpf.dwRBitMask = 0x00ff0000; + PF_flags |= DDPF_LUMINANCE; + ddpf.dwRGBBitCount = pixelSize; + ddsd.lPitch = img->getRowSizeInBytes(); + SD_flags |= DDSD_PITCH; + } + break; + + //Compressed + case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: + { + ddpf.dwFourCC = FOURCC_DXT1; + PF_flags |= (DDPF_ALPHAPIXELS | DDPF_FOURCC); + ddsd.dwLinearSize = imageSize; + SD_flags |= DDSD_LINEARSIZE; + } + break; + case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT: + { + ddpf.dwFourCC = FOURCC_DXT3; + PF_flags |= (DDPF_ALPHAPIXELS | DDPF_FOURCC); + ddsd.dwLinearSize = imageSize; + SD_flags |= DDSD_LINEARSIZE; + } + break; + case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT: + { + ddpf.dwFourCC = FOURCC_DXT5; + PF_flags |= (DDPF_ALPHAPIXELS | DDPF_FOURCC); + ddsd.dwLinearSize = imageSize; + SD_flags |= DDSD_LINEARSIZE; + } + break; + case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: + { + ddpf.dwFourCC = FOURCC_DXT1; + PF_flags |= DDPF_FOURCC; /* No alpha here */ + ddsd.dwLinearSize = imageSize; + SD_flags |= DDSD_LINEARSIZE; + } + break; + default: + osg::notify(osg::WARN)<<"Warning:: unhandled pixel format in image, file cannot be written."<isMipmap() && !is3dImage) + { + SD_flags |= DDSD_MIPMAPCOUNT; + CAPS_flags |= DDSCAPS_COMPLEX | DDSCAPS_MIPMAP; + ddsd.dwMipMapCount = img->getNumMipmapLevels(); + } + + + // Assign flags and structure members of ddsd + ddsd.dwFlags = SD_flags; + ddpf.dwFlags = PF_flags; + ddsCaps.dwCaps = CAPS_flags; + ddsCaps.dwCaps2 = CAPS2_flags; + + ddsd.ddpfPixelFormat = ddpf; + ddsd.ddsCaps = ddsCaps; + + + // Write DDS file + unsigned char *header = new unsigned char[128]; + char *ddsName = "DDS "; + memcpy(header, ddsName, 4); + memcpy(header+4, &ddsd, sizeof(ddsd)); + + fwrite(header, sizeof(ddsd)+4, 1, fp); /* write file header */ + + int isize = img->getTotalSizeInBytesIncludingMipmaps(); + unsigned char *buffer = new unsigned char[isize]; + unsigned char *dataPtr = buffer; + + if(!is3dImage) + { + // Copy main image data // + memcpy(dataPtr, img->data(), imageSize); + + // Handle mipmaps if any + if(img->isMipmap()) + { + dataPtr += imageSize; + unsigned char *mmdPtr, *next_mmdPtr; + int offset; + unsigned int mipmaps = img->getNumMipmapLevels(); + unsigned int blockSize = (pixelFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) ? 8 : 16; + + mmdPtr = img->getMipmapData(1); /* get the first mipmap address */ + + for(unsigned int mID = 1 ; mID < mipmaps; mID++) + { + // the last mipmap is 1x1 so we assign size equal to the blockSize + next_mmdPtr = (mID == mipmaps-1) ? (mmdPtr+blockSize) : img->getMipmapData(mID+1); + offset = next_mmdPtr - mmdPtr; + + memcpy(dataPtr, mmdPtr, offset); + + mmdPtr += offset; + dataPtr += offset; + } + } + } + else /* 3d image */ + { + for(int i = 0; i < r; ++i) + { + memcpy(dataPtr, img->data(0, 0, i), imageSize*components); //4th change + dataPtr += imageSize*components; + } + } + + // Check for correct saving + if (fwrite(buffer, isize , 1, fp) != 1) + { + fclose(fp); + return false; + } + + // close file pointer + fclose(fp); + + // If we get that far the file was saved properly // + return true; +} + + class ReaderWriterDDS : public osgDB::ReaderWriter { - public: - virtual const char* className() - { - return "DDS Image Reader"; - } +public: + virtual const char* className() + { + return "DDS Image Reader/Writer"; + } - virtual bool acceptsExtension(const std::string& extension) - { - return osgDB::equalCaseInsensitive(extension,"dds"); - } + virtual bool acceptsExtension(const std::string& extension) + { + return osgDB::equalCaseInsensitive(extension,"dds"); + } - virtual ReadResult readImage(const std::string& file, const osgDB::ReaderWriter::Options*) - { - std::string ext = osgDB::getLowerCaseFileExtension(file); - if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; + virtual ReadResult readImage(const std::string& file, const osgDB::ReaderWriter::Options*) + { + std::string ext = osgDB::getLowerCaseFileExtension(file); + if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; - std::string fileName = osgDB::findDataFile( file ); - if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; + std::string fileName = osgDB::findDataFile( file ); + if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; + + osg::Image* osgImage = ReadDDSFile(fileName.c_str()); + if (osgImage==NULL) return ReadResult::FILE_NOT_HANDLED; + return osgImage; - osg::Image* osgImage = ReadDDSFile(fileName.c_str()); - if (osgImage==NULL) return ReadResult::FILE_NOT_HANDLED; - return osgImage; + } - } + virtual WriteResult writeImage(const osg::Image &img,const std::string& file, const osgDB::ReaderWriter::Options*) + { + std::string ext = osgDB::getFileExtension(file); + if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED; + + bool success = WriteDDSFile(&img, file.c_str()); + + if(success) + return WriteResult::FILE_SAVED; + else + return WriteResult::ERROR_IN_WRITING_FILE; + + } }; // now register with Registry to instantiate the above // reader/writer. -osgDB::RegisterReaderWriterProxy g_readerWriter_DDS_Proxy; +osgDB::RegisterReaderWriterProxy g_readerWriter_DDS_Proxy; \ No newline at end of file