OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PrecipitationEffect.h
Go to the documentation of this file.
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 
14 #ifndef OSGPARTICLE_PRECIPITATIONEFFECT
15 #define OSGPARTICLE_PRECIPITATIONEFFECT
16 
17 #include <osg/Group>
18 #include <osg/BoundingBox>
19 #include <osg/Fog>
20 #include <osg/Geometry>
21 
22 #include <osgUtil/CullVisitor>
23 
24 #include <osgParticle/Export>
25 
26 namespace osgParticle
27 {
29  {
30  public:
31 
34 
35  virtual const char* libraryName() const { return "osgParticle"; }
36  virtual const char* className() const { return "PrecipitationEffect"; }
37  virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const PrecipitationEffect*>(obj) != 0; }
38  virtual void accept(osg::NodeVisitor& nv) { if (nv.validNodeMask(*this)) { nv.pushOntoNodePath(this); nv.apply(*this); nv.popFromNodePath(); } }
39 
40  virtual void traverse(osg::NodeVisitor& nv);
41 
42 
44  void rain(float intensity);
45 
47  void snow(float intensity);
48 
49  void setMaximumParticleDensity(float density) { if (_maximumParticleDensity==density) return; _maximumParticleDensity = density; _dirty = true;}
50  float getMaximumParticleDensity() const { return _maximumParticleDensity; }
51 
52  void setWind(const osg::Vec3& wind) { _wind = wind; }
53  const osg::Vec3& getWind() const { return _wind; }
54 
55  void setPosition(const osg::Vec3& position) { _origin = position; }
56  const osg::Vec3& getPosition() const { return _origin; }
57 
58  void setCellSize(const osg::Vec3& cellSize) { if (_cellSize==cellSize) return; _cellSize = cellSize; _dirty = true; }
59  const osg::Vec3& getCellSize() const { return _cellSize; }
60 
61  void setParticleSpeed(float particleSpeed) { if (_particleSpeed==particleSpeed) return; _particleSpeed = particleSpeed; _dirty = true; }
62  float getParticleSpeed() const { return _particleSpeed; }
63 
64  void setParticleSize(float particleSize) { if (_particleSize==particleSize) return; _particleSize = particleSize; _dirty = true;}
65  float getParticleSize() const { return _particleSize; }
66 
67  void setParticleColor(const osg::Vec4& color) { if (_particleColor==color) return; _particleColor = color; _dirty = true;}
68  const osg::Vec4& getParticleColor() const { return _particleColor; }
69 
70  void setNearTransition(float nearTransition) { _nearTransition = nearTransition; }
71  float getNearTransition() const { return _nearTransition; }
72 
73  void setFarTransition(float farTransition) { _farTransition = farTransition; }
74  float getFarTransition() const { return _farTransition; }
75 
76  void setUseFarLineSegments(bool useFarLineSegments) { _useFarLineSegments = useFarLineSegments; }
77  bool getUseFarLineSegments() const { return _useFarLineSegments; }
78 
79  void setFog(osg::Fog* fog) { _fog = fog; }
80  osg::Fog* getFog() { return _fog.get(); }
81  const osg::Fog* getFog() const { return _fog.get(); }
82 
83  osg::Geometry* getQuadGeometry() { return _quadGeometry.get(); }
84  osg::StateSet* getQuadStateSet() { return _quadStateSet.get(); }
85 
86  osg::Geometry* getLineGeometry() { return _lineGeometry.get(); }
87  osg::StateSet* getLineStateSet() { return _lineStateSet.get(); }
88 
89  osg::Geometry* getPointGeometry() { return _pointGeometry.get(); }
90  osg::StateSet* getPointStateSet() { return _pointStateSet.get(); }
91 
94  {
95  public:
96 
99 
101 
102  virtual bool supports(const osg::PrimitiveFunctor&) const { return false; }
103  virtual void accept(osg::PrimitiveFunctor&) const {}
104  virtual bool supports(const osg::PrimitiveIndexFunctor&) const { return false; }
105  virtual void accept(osg::PrimitiveIndexFunctor&) const {}
106 
107  void setRequiresPreviousMatrix(bool flag) { _requiresPreviousMatrix = flag; }
108  bool getRequiresPreviousMatrix() const { return _requiresPreviousMatrix; }
109 
110  void setGeometry(osg::Geometry* geom) { _geometry = geom; }
111  osg::Geometry* getGeometry() { return _geometry.get(); }
112  const osg::Geometry* getGeometry() const { return _geometry.get(); }
113 
114  void setDrawType(GLenum type) { _drawType = type; }
115  GLenum getDrawType() const { return _drawType; }
116 
117  void setNumberOfVertices(unsigned int numVertices) { _numberOfVertices = numVertices; }
118  unsigned int getNumberOfVertices() const { return _numberOfVertices; }
119 
120  virtual void drawImplementation(osg::RenderInfo& renderInfo) const;
121 
122  struct Cell
123  {
124  Cell(int in_i, int in_j, int in_k):
125  i(in_i), j(in_j), k(in_k) {}
126 
127  inline bool operator < (const Cell& rhs) const
128  {
129  if (i<rhs.i) return true;
130  if (i>rhs.i) return false;
131  if (j<rhs.j) return true;
132  if (j>rhs.j) return false;
133  if (k<rhs.k) return true;
134  if (k>rhs.k) return false;
135  return false;
136  }
137 
138  int i;
139  int j;
140  int k;
141  };
142 
144  {
145  inline bool operator < (const DepthMatrixStartTime& rhs) const
146  {
147  return depth < rhs.depth;
148  }
149 
150  float depth;
151  float startTime;
153  };
154 
155  typedef std::map< Cell, DepthMatrixStartTime > CellMatrixMap;
156 
157  struct LessFunctor
158  {
159  inline bool operator () (const CellMatrixMap::value_type* lhs,const CellMatrixMap::value_type* rhs) const
160  {
161  return (*lhs).second<(*rhs).second;
162  }
163  };
164 
165 
166  CellMatrixMap& getCurrentCellMatrixMap() { return _currentCellMatrixMap; }
167  CellMatrixMap& getPreviousCellMatrixMap() { return _previousCellMatrixMap; }
168 
169  inline void newFrame()
170  {
171  _previousCellMatrixMap.swap(_currentCellMatrixMap);
172  _currentCellMatrixMap.clear();
173  }
174 
175  protected:
176 
178 
180 
182 
183  mutable CellMatrixMap _currentCellMatrixMap;
184  mutable CellMatrixMap _previousCellMatrixMap;
185 
186  GLenum _drawType;
187  unsigned int _numberOfVertices;
188 
189  };
190 
191  protected:
192 
193  virtual ~PrecipitationEffect() {}
194 
195  void compileGLObjects(osg::RenderInfo& renderInfo) const;
196 
197  void update();
198 
199  void createGeometry(unsigned int numParticles,
200  osg::Geometry* quad_geometry,
201  osg::Geometry* line_geometry,
202  osg::Geometry* point_geometry);
203 
204  void setUpGeometries(unsigned int numParticles);
205 
206 
208  {
212  };
213 
214  void cull(PrecipitationDrawableSet& pds, osgUtil::CullVisitor* cv) const;
215  bool build(const osg::Vec3 eyeLocal, int i, int j, int k, float startTime, PrecipitationDrawableSet& pds, osg::Polytope& frustum, osgUtil::CullVisitor* cv) const;
216 
217  // parameters
218  bool _dirty;
229 
233 
234  typedef std::pair< osg::NodeVisitor*, osg::NodePath > ViewIdentifier;
235  typedef std::map< ViewIdentifier, PrecipitationDrawableSet > ViewDrawableMap;
236 
238  ViewDrawableMap _viewDrawableMap;
239 
242 
245 
248 
249  // cache variables.
250  float _period;
258 
260 
261  };
262 
263 }
264 
265 #endif
virtual const char * className() const
osg::ref_ptr< osg::StateSet > _lineStateSet
This class provides an object-oriented thread mutex interface.
Definition: Mutex.h:31
void setWind(const osg::Vec3 &wind)
const osg::Vec4 & getParticleColor() const
GLint GLenum GLsizei GLsizei GLsizei depth
Definition: GLU.h:71
osg::ref_ptr< osg::StateSet > _quadStateSet
void popFromNodePath()
Definition: NodeVisitor.h:204
osg::ref_ptr< osg::Geometry > _lineGeometry
bool validNodeMask(const osg::Node &node) const
Definition: NodeVisitor.h:166
const osg::Fog * getFog() const
virtual bool isSameKindAs(const osg::Object *obj) const
void setUseFarLineSegments(bool useFarLineSegments)
osg::ref_ptr< osg::Geometry > _pointGeometry
virtual bool supports(const osg::PrimitiveFunctor &) const
void setParticleSize(float particleSize)
#define OSGPARTICLE_EXPORT
Definition: Export.h:40
virtual void accept(osg::PrimitiveFunctor &) const
osg::ref_ptr< osg::Uniform > _particleSizeUniform
osg::ref_ptr< osg::Uniform > _particleColorUniform
void setMaximumParticleDensity(float density)
virtual const char * libraryName() const
#define META_Object(library, name)
Definition: Object.h:42
virtual void apply(Drawable &drawable)
void setPosition(const osg::Vec3 &position)
osg::ref_ptr< osg::Geometry > _quadGeometry
void setCellSize(const osg::Vec3 &cellSize)
Definition: Fog.h:52
osg::ref_ptr< osg::StateSet > _pointStateSet
void setParticleSpeed(float particleSpeed)
const osg::Vec3 & getPosition() const
std::pair< osg::NodeVisitor *, osg::NodePath > ViewIdentifier
void pushOntoNodePath(Node *node)
Definition: NodeVisitor.h:198
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum GLenum type
Definition: GLU.h:71
const osg::Vec3 & getCellSize() const
const osg::Vec3 & getWind() const
virtual void accept(osg::PrimitiveIndexFunctor &) const
std::map< Cell, DepthMatrixStartTime > CellMatrixMap
void setNearTransition(float nearTransition)
Definition: Node.h:71
std::map< ViewIdentifier, PrecipitationDrawableSet > ViewDrawableMap
osg::ref_ptr< PrecipitationDrawable > _linePrecipitationDrawable
osg::ref_ptr< PrecipitationDrawable > _pointPrecipitationDrawable
virtual bool supports(const osg::PrimitiveIndexFunctor &) const
void setFarTransition(float farTransition)
void setParticleColor(const osg::Vec4 &color)
virtual void accept(osg::NodeVisitor &nv)
osg::ref_ptr< osg::Uniform > _inversePeriodUniform
osg::ref_ptr< PrecipitationDrawable > _quadPrecipitationDrawable