FluidProgram 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSGPARTICLE_FLUIDPROGRAM
  14. #define OSGPARTICLE_FLUIDPROGRAM 1
  15. #include <osgParticle/Export>
  16. #include <osgParticle/Program>
  17. #include <osg/CopyOp>
  18. #include <osg/Object>
  19. #include <osg/Node>
  20. #include <osg/NodeVisitor>
  21. namespace osgParticle
  22. {
  23. /** A program class for performing operations on particles using a sequence of <I>operators</I>.
  24. To use a <CODE>FluidProgram</CODE> you have to create some <CODE>Operator</CODE> objects and
  25. add them to the program.
  26. All operators will be applied to each particle in the same order they've been added to the program.
  27. */
  28. class OSGPARTICLE_EXPORT FluidProgram: public Program {
  29. public:
  30. FluidProgram();
  31. FluidProgram(const FluidProgram& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
  32. META_Node(osgParticle,FluidProgram);
  33. /// Set the viscosity of the fluid.
  34. inline void setFluidViscosity(float v)
  35. {
  36. _viscosity = v;
  37. _viscosityCoefficient = 6 * osg::PI * _viscosity;
  38. }
  39. /// Get the viscosity of the fluid.
  40. inline float getFluidViscosity() const { return _viscosity; }
  41. /// Set the density of the fluid.
  42. inline void setFluidDensity(float d)
  43. {
  44. _density = d;
  45. _densityCoefficient = 0.2f * osg::PI * _density;
  46. }
  47. /// Get the density of the fluid.
  48. inline float getFluidDensity() const { return _density; }
  49. /// Set the wind vector.
  50. inline void setWind(const osg::Vec3& wind) { _wind = wind; }
  51. /// Get the wind vector.
  52. inline const osg::Vec3& getWind() const { return _wind; }
  53. /// Set the acceleration vector.
  54. inline void setAcceleration(const osg::Vec3& v) { _acceleration = v; }
  55. /// Get the acceleration vector.
  56. inline const osg::Vec3& getAcceleration() const { return _acceleration; }
  57. /** Set the acceleration vector to the gravity on earth (0, 0, -9.81).
  58. The acceleration will be multiplied by the <CODE>scale</CODE> parameter.
  59. */
  60. inline void setToGravity(float scale = 1.0f) { _acceleration.set(0, 0, -9.81f*scale); }
  61. /// Set the fluid parameters as for air (20°C temperature).
  62. inline void setFluidToAir()
  63. {
  64. setToGravity(1.0f);
  65. setFluidDensity(1.2929f);
  66. setFluidViscosity(1.8e-5f);
  67. }
  68. /// Set the fluid parameters as for pure water (20°C temperature).
  69. inline void setFluidToWater()
  70. {
  71. setToGravity(1.0f);
  72. setFluidDensity(1.0f);
  73. setFluidViscosity(1.002e-3f);
  74. }
  75. protected:
  76. virtual ~FluidProgram() {}
  77. FluidProgram& operator=(const FluidProgram&) { return *this; }
  78. virtual void execute(double dt);
  79. osg::Vec3 _acceleration;
  80. float _viscosity;
  81. float _density;
  82. osg::Vec3 _wind;
  83. float _viscosityCoefficient;
  84. float _densityCoefficient;
  85. };
  86. }
  87. #endif