PrecipitationEffect 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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_PRECIPITATIONEFFECT
  14. #define OSGPARTICLE_PRECIPITATIONEFFECT
  15. #include <osg/Group>
  16. #include <osg/BoundingBox>
  17. #include <osg/Fog>
  18. #include <osg/Geometry>
  19. #include <osgUtil/CullVisitor>
  20. #include <osgParticle/Export>
  21. namespace osgParticle
  22. {
  23. class OSGPARTICLE_EXPORT PrecipitationEffect : public osg::Node
  24. {
  25. public:
  26. PrecipitationEffect();
  27. PrecipitationEffect(const PrecipitationEffect& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
  28. virtual const char* libraryName() const { return "osgParticle"; }
  29. virtual const char* className() const { return "PrecipitationEffect"; }
  30. virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const PrecipitationEffect*>(obj) != 0; }
  31. virtual void accept(osg::NodeVisitor& nv) { if (nv.validNodeMask(*this)) { nv.pushOntoNodePath(this); nv.apply(*this); nv.popFromNodePath(); } }
  32. virtual void resizeGLObjectBuffers(unsigned int maxSize);
  33. virtual void releaseGLObjects(osg::State* state = 0) const;
  34. virtual void traverse(osg::NodeVisitor& nv);
  35. /** Set all the parameters to create an rain effect of specified intensity.*/
  36. void rain(float intensity);
  37. /** Set all the parameters to create an snow effect of specified intensity.*/
  38. void snow(float intensity);
  39. void setMaximumParticleDensity(float density) { if (_maximumParticleDensity==density) return; _maximumParticleDensity = density; _dirty = true;}
  40. float getMaximumParticleDensity() const { return _maximumParticleDensity; }
  41. void setWind(const osg::Vec3& wind) { _wind = wind; }
  42. const osg::Vec3& getWind() const { return _wind; }
  43. void setPosition(const osg::Vec3& position) { _origin = position; }
  44. const osg::Vec3& getPosition() const { return _origin; }
  45. void setCellSize(const osg::Vec3& cellSize) { if (_cellSize==cellSize) return; _cellSize = cellSize; _dirty = true; }
  46. const osg::Vec3& getCellSize() const { return _cellSize; }
  47. void setParticleSpeed(float particleSpeed) { if (_particleSpeed==particleSpeed) return; _particleSpeed = particleSpeed; _dirty = true; }
  48. float getParticleSpeed() const { return _particleSpeed; }
  49. void setParticleSize(float particleSize) { if (_particleSize==particleSize) return; _particleSize = particleSize; _dirty = true;}
  50. float getParticleSize() const { return _particleSize; }
  51. void setParticleColor(const osg::Vec4& color) { if (_particleColor==color) return; _particleColor = color; _dirty = true;}
  52. const osg::Vec4& getParticleColor() const { return _particleColor; }
  53. void setNearTransition(float nearTransition) { _nearTransition = nearTransition; }
  54. float getNearTransition() const { return _nearTransition; }
  55. void setFarTransition(float farTransition) { _farTransition = farTransition; }
  56. float getFarTransition() const { return _farTransition; }
  57. void setUseFarLineSegments(bool useFarLineSegments) { _useFarLineSegments = useFarLineSegments; }
  58. bool getUseFarLineSegments() const { return _useFarLineSegments; }
  59. void setFog(osg::Fog* fog) { _fog = fog; }
  60. osg::Fog* getFog() { return _fog.get(); }
  61. const osg::Fog* getFog() const { return _fog.get(); }
  62. osg::Geometry* getQuadGeometry() { return _quadGeometry.get(); }
  63. osg::StateSet* getQuadStateSet() { return _quadStateSet.get(); }
  64. osg::Geometry* getLineGeometry() { return _lineGeometry.get(); }
  65. osg::StateSet* getLineStateSet() { return _lineStateSet.get(); }
  66. osg::Geometry* getPointGeometry() { return _pointGeometry.get(); }
  67. osg::StateSet* getPointStateSet() { return _pointStateSet.get(); }
  68. /** Internal drawable used to render batches of cells.*/
  69. class OSGPARTICLE_EXPORT PrecipitationDrawable : public osg::Drawable
  70. {
  71. public:
  72. PrecipitationDrawable();
  73. PrecipitationDrawable(const PrecipitationDrawable& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
  74. META_Object(osgParticle, PrecipitationDrawable);
  75. virtual bool supports(const osg::PrimitiveFunctor&) const { return false; }
  76. virtual void accept(osg::PrimitiveFunctor&) const {}
  77. virtual bool supports(const osg::PrimitiveIndexFunctor&) const { return false; }
  78. virtual void accept(osg::PrimitiveIndexFunctor&) const {}
  79. void setRequiresPreviousMatrix(bool flag) { _requiresPreviousMatrix = flag; }
  80. bool getRequiresPreviousMatrix() const { return _requiresPreviousMatrix; }
  81. void setGeometry(osg::Geometry* geom) { _geometry = geom; }
  82. osg::Geometry* getGeometry() { return _geometry.get(); }
  83. const osg::Geometry* getGeometry() const { return _geometry.get(); }
  84. void setDrawType(GLenum type) { _drawType = type; }
  85. GLenum getDrawType() const { return _drawType; }
  86. void setNumberOfVertices(unsigned int numVertices) { _numberOfVertices = numVertices; }
  87. unsigned int getNumberOfVertices() const { return _numberOfVertices; }
  88. virtual void resizeGLObjectBuffers(unsigned int maxSize);
  89. virtual void releaseGLObjects(osg::State* state) const;
  90. virtual void drawImplementation(osg::RenderInfo& renderInfo) const;
  91. struct Cell
  92. {
  93. Cell(int in_i, int in_j, int in_k):
  94. i(in_i), j(in_j), k(in_k) {}
  95. inline bool operator < (const Cell& rhs) const
  96. {
  97. if (i<rhs.i) return true;
  98. if (i>rhs.i) return false;
  99. if (j<rhs.j) return true;
  100. if (j>rhs.j) return false;
  101. if (k<rhs.k) return true;
  102. if (k>rhs.k) return false;
  103. return false;
  104. }
  105. int i;
  106. int j;
  107. int k;
  108. };
  109. struct DepthMatrixStartTime
  110. {
  111. inline bool operator < (const DepthMatrixStartTime& rhs) const
  112. {
  113. return depth < rhs.depth;
  114. }
  115. float depth;
  116. float startTime;
  117. osg::Matrix modelview;
  118. };
  119. typedef std::map< Cell, DepthMatrixStartTime > CellMatrixMap;
  120. struct LessFunctor
  121. {
  122. inline bool operator () (const CellMatrixMap::value_type* lhs,const CellMatrixMap::value_type* rhs) const
  123. {
  124. return (*lhs).second<(*rhs).second;
  125. }
  126. };
  127. CellMatrixMap& getCurrentCellMatrixMap() { return _currentCellMatrixMap; }
  128. CellMatrixMap& getPreviousCellMatrixMap() { return _previousCellMatrixMap; }
  129. inline void newFrame()
  130. {
  131. _previousCellMatrixMap.swap(_currentCellMatrixMap);
  132. _currentCellMatrixMap.clear();
  133. }
  134. protected:
  135. virtual ~PrecipitationDrawable();
  136. bool _requiresPreviousMatrix;
  137. osg::ref_ptr<osg::Geometry> _geometry;
  138. mutable CellMatrixMap _currentCellMatrixMap;
  139. mutable CellMatrixMap _previousCellMatrixMap;
  140. GLenum _drawType;
  141. unsigned int _numberOfVertices;
  142. };
  143. protected:
  144. virtual ~PrecipitationEffect() {}
  145. void compileGLObjects(osg::RenderInfo& renderInfo) const;
  146. void update();
  147. void createGeometry(unsigned int numParticles,
  148. osg::Geometry* quad_geometry,
  149. osg::Geometry* line_geometry,
  150. osg::Geometry* point_geometry);
  151. void setUpGeometries(unsigned int numParticles);
  152. struct PrecipitationDrawableSet
  153. {
  154. osg::ref_ptr<PrecipitationDrawable> _quadPrecipitationDrawable;
  155. osg::ref_ptr<PrecipitationDrawable> _linePrecipitationDrawable;
  156. osg::ref_ptr<PrecipitationDrawable> _pointPrecipitationDrawable;
  157. };
  158. void cull(PrecipitationDrawableSet& pds, osgUtil::CullVisitor* cv) const;
  159. bool build(const osg::Vec3 eyeLocal, int i, int j, int k, float startTime, PrecipitationDrawableSet& pds, osg::Polytope& frustum, osgUtil::CullVisitor* cv) const;
  160. // parameters
  161. bool _dirty;
  162. osg::Vec3 _wind;
  163. float _particleSpeed;
  164. float _particleSize;
  165. osg::Vec4 _particleColor;
  166. float _maximumParticleDensity;
  167. osg::Vec3 _cellSize;
  168. float _nearTransition;
  169. float _farTransition;
  170. bool _useFarLineSegments;
  171. osg::ref_ptr<osg::Fog> _fog;
  172. osg::ref_ptr<osg::Uniform> _inversePeriodUniform;
  173. osg::ref_ptr<osg::Uniform> _particleSizeUniform;
  174. osg::ref_ptr<osg::Uniform> _particleColorUniform;
  175. typedef std::pair< osg::NodeVisitor*, osg::NodePath > ViewIdentifier;
  176. typedef std::map< ViewIdentifier, PrecipitationDrawableSet > ViewDrawableMap;
  177. OpenThreads::Mutex _mutex;
  178. ViewDrawableMap _viewDrawableMap;
  179. osg::ref_ptr<osg::Geometry> _quadGeometry;
  180. osg::ref_ptr<osg::StateSet> _quadStateSet;
  181. osg::ref_ptr<osg::Geometry> _lineGeometry;
  182. osg::ref_ptr<osg::StateSet> _lineStateSet;
  183. osg::ref_ptr<osg::Geometry> _pointGeometry;
  184. osg::ref_ptr<osg::StateSet> _pointStateSet;
  185. // cache variables.
  186. float _period;
  187. osg::Vec3 _origin;
  188. osg::Vec3 _du;
  189. osg::Vec3 _dv;
  190. osg::Vec3 _dw;
  191. osg::Vec3 _inverse_du;
  192. osg::Vec3 _inverse_dv;
  193. osg::Vec3 _inverse_dw;
  194. double _previousFrameTime;
  195. };
  196. }
  197. #endif