openscenegraph
PrecipitationEffect
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
26namespace osgParticle
27{
28 class OSGPARTICLE_EXPORT PrecipitationEffect : public osg::Node
29 {
30 public:
31
32 PrecipitationEffect();
33 PrecipitationEffect(const PrecipitationEffect& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
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 resizeGLObjectBuffers(unsigned int maxSize);
41 virtual void releaseGLObjects(osg::State* state = 0) const;
42
43 virtual void traverse(osg::NodeVisitor& nv);
44
45 /** Set all the parameters to create an rain effect of specified intensity.*/
46 void rain(float intensity);
47
48 /** Set all the parameters to create an snow effect of specified intensity.*/
49 void snow(float intensity);
50
51 void setMaximumParticleDensity(float density) { if (_maximumParticleDensity==density) return; _maximumParticleDensity = density; _dirty = true;}
52 float getMaximumParticleDensity() const { return _maximumParticleDensity; }
53
54 void setWind(const osg::Vec3& wind) { _wind = wind; }
55 const osg::Vec3& getWind() const { return _wind; }
56
57 void setPosition(const osg::Vec3& position) { _origin = position; }
58 const osg::Vec3& getPosition() const { return _origin; }
59
60 void setCellSize(const osg::Vec3& cellSize) { if (_cellSize==cellSize) return; _cellSize = cellSize; _dirty = true; }
61 const osg::Vec3& getCellSize() const { return _cellSize; }
62
63 void setParticleSpeed(float particleSpeed) { if (_particleSpeed==particleSpeed) return; _particleSpeed = particleSpeed; _dirty = true; }
64 float getParticleSpeed() const { return _particleSpeed; }
65
66 void setParticleSize(float particleSize) { if (_particleSize==particleSize) return; _particleSize = particleSize; _dirty = true;}
67 float getParticleSize() const { return _particleSize; }
68
69 void setParticleColor(const osg::Vec4& color) { if (_particleColor==color) return; _particleColor = color; _dirty = true;}
70 const osg::Vec4& getParticleColor() const { return _particleColor; }
71
72 void setNearTransition(float nearTransition) { _nearTransition = nearTransition; }
73 float getNearTransition() const { return _nearTransition; }
74
75 void setFarTransition(float farTransition) { _farTransition = farTransition; }
76 float getFarTransition() const { return _farTransition; }
77
78 void setUseFarLineSegments(bool useFarLineSegments) { _useFarLineSegments = useFarLineSegments; }
79 bool getUseFarLineSegments() const { return _useFarLineSegments; }
80
81 void setFog(osg::Fog* fog) { _fog = fog; }
82 osg::Fog* getFog() { return _fog.get(); }
83 const osg::Fog* getFog() const { return _fog.get(); }
84
85 osg::Geometry* getQuadGeometry() { return _quadGeometry.get(); }
86 osg::StateSet* getQuadStateSet() { return _quadStateSet.get(); }
87
88 osg::Geometry* getLineGeometry() { return _lineGeometry.get(); }
89 osg::StateSet* getLineStateSet() { return _lineStateSet.get(); }
90
91 osg::Geometry* getPointGeometry() { return _pointGeometry.get(); }
92 osg::StateSet* getPointStateSet() { return _pointStateSet.get(); }
93
94 /** Internal drawable used to render batches of cells.*/
95 class OSGPARTICLE_EXPORT PrecipitationDrawable : public osg::Drawable
96 {
97 public:
98
99 PrecipitationDrawable();
100 PrecipitationDrawable(const PrecipitationDrawable& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
101
102 META_Object(osgParticle, PrecipitationDrawable);
103
104 virtual bool supports(const osg::PrimitiveFunctor&) const { return false; }
105 virtual void accept(osg::PrimitiveFunctor&) const {}
106 virtual bool supports(const osg::PrimitiveIndexFunctor&) const { return false; }
107 virtual void accept(osg::PrimitiveIndexFunctor&) const {}
108
109 void setRequiresPreviousMatrix(bool flag) { _requiresPreviousMatrix = flag; }
110 bool getRequiresPreviousMatrix() const { return _requiresPreviousMatrix; }
111
112 void setGeometry(osg::Geometry* geom) { _geometry = geom; }
113 osg::Geometry* getGeometry() { return _geometry.get(); }
114 const osg::Geometry* getGeometry() const { return _geometry.get(); }
115
116 void setDrawType(GLenum type) { _drawType = type; }
117 GLenum getDrawType() const { return _drawType; }
118
119 void setNumberOfVertices(unsigned int numVertices) { _numberOfVertices = numVertices; }
120 unsigned int getNumberOfVertices() const { return _numberOfVertices; }
121
122 virtual void resizeGLObjectBuffers(unsigned int maxSize);
123 virtual void releaseGLObjects(osg::State* state) const;
124
125 virtual void drawImplementation(osg::RenderInfo& renderInfo) const;
126
127 struct Cell
128 {
129 Cell(int in_i, int in_j, int in_k):
130 i(in_i), j(in_j), k(in_k) {}
131
132 inline bool operator < (const Cell& rhs) const
133 {
134 if (i<rhs.i) return true;
135 if (i>rhs.i) return false;
136 if (j<rhs.j) return true;
137 if (j>rhs.j) return false;
138 if (k<rhs.k) return true;
139 if (k>rhs.k) return false;
140 return false;
141 }
142
143 int i;
144 int j;
145 int k;
146 };
147
148 struct DepthMatrixStartTime
149 {
150 inline bool operator < (const DepthMatrixStartTime& rhs) const
151 {
152 return depth < rhs.depth;
153 }
154
155 float depth;
156 float startTime;
157 osg::Matrix modelview;
158 };
159
160 typedef std::map< Cell, DepthMatrixStartTime > CellMatrixMap;
161
162 struct LessFunctor
163 {
164 inline bool operator () (const CellMatrixMap::value_type* lhs,const CellMatrixMap::value_type* rhs) const
165 {
166 return (*lhs).second<(*rhs).second;
167 }
168 };
169
170
171 CellMatrixMap& getCurrentCellMatrixMap() { return _currentCellMatrixMap; }
172 CellMatrixMap& getPreviousCellMatrixMap() { return _previousCellMatrixMap; }
173
174 inline void newFrame()
175 {
176 _previousCellMatrixMap.swap(_currentCellMatrixMap);
177 _currentCellMatrixMap.clear();
178 }
179
180 protected:
181
182 virtual ~PrecipitationDrawable();
183
184 bool _requiresPreviousMatrix;
185
186 osg::ref_ptr<osg::Geometry> _geometry;
187
188 mutable CellMatrixMap _currentCellMatrixMap;
189 mutable CellMatrixMap _previousCellMatrixMap;
190
191 GLenum _drawType;
192 unsigned int _numberOfVertices;
193
194 };
195
196 protected:
197
198 virtual ~PrecipitationEffect() {}
199
200 void compileGLObjects(osg::RenderInfo& renderInfo) const;
201
202 void update();
203
204 void createGeometry(unsigned int numParticles,
205 osg::Geometry* quad_geometry,
206 osg::Geometry* line_geometry,
207 osg::Geometry* point_geometry);
208
209 void setUpGeometries(unsigned int numParticles);
210
211
212 struct PrecipitationDrawableSet
213 {
214 osg::ref_ptr<PrecipitationDrawable> _quadPrecipitationDrawable;
215 osg::ref_ptr<PrecipitationDrawable> _linePrecipitationDrawable;
216 osg::ref_ptr<PrecipitationDrawable> _pointPrecipitationDrawable;
217 };
218
219 void cull(PrecipitationDrawableSet& pds, osgUtil::CullVisitor* cv) const;
220 bool build(const osg::Vec3 eyeLocal, int i, int j, int k, float startTime, PrecipitationDrawableSet& pds, osg::Polytope& frustum, osgUtil::CullVisitor* cv) const;
221
222 // parameters
223 bool _dirty;
224 osg::Vec3 _wind;
225 float _particleSpeed;
226 float _particleSize;
227 osg::Vec4 _particleColor;
228 float _maximumParticleDensity;
229 osg::Vec3 _cellSize;
230 float _nearTransition;
231 float _farTransition;
232 bool _useFarLineSegments;
233 osg::ref_ptr<osg::Fog> _fog;
234
235 osg::ref_ptr<osg::Uniform> _inversePeriodUniform;
236 osg::ref_ptr<osg::Uniform> _particleSizeUniform;
237 osg::ref_ptr<osg::Uniform> _particleColorUniform;
238
239 typedef std::pair< osg::NodeVisitor*, osg::NodePath > ViewIdentifier;
240 typedef std::map< ViewIdentifier, PrecipitationDrawableSet > ViewDrawableMap;
241
242 OpenThreads::Mutex _mutex;
243 ViewDrawableMap _viewDrawableMap;
244
245 osg::ref_ptr<osg::Geometry> _quadGeometry;
246 osg::ref_ptr<osg::StateSet> _quadStateSet;
247
248 osg::ref_ptr<osg::Geometry> _lineGeometry;
249 osg::ref_ptr<osg::StateSet> _lineStateSet;
250
251 osg::ref_ptr<osg::Geometry> _pointGeometry;
252 osg::ref_ptr<osg::StateSet> _pointStateSet;
253
254 // cache variables.
255 float _period;
256 osg::Vec3 _origin;
257 osg::Vec3 _du;
258 osg::Vec3 _dv;
259 osg::Vec3 _dw;
260 osg::Vec3 _inverse_du;
261 osg::Vec3 _inverse_dv;
262 osg::Vec3 _inverse_dw;
263
264 double _previousFrameTime;
265
266 };
267
268}
269
270#endif