1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
14#ifndef OSGPARTICLE_PRECIPITATIONEFFECT
15#define OSGPARTICLE_PRECIPITATIONEFFECT
18#include <osg/BoundingBox>
20#include <osg/Geometry>
22#include <osgUtil/CullVisitor>
24#include <osgParticle/Export>
28 class OSGPARTICLE_EXPORT PrecipitationEffect : public osg::Node
32 PrecipitationEffect();
33 PrecipitationEffect(const PrecipitationEffect& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
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(); } }
40 virtual void resizeGLObjectBuffers(unsigned int maxSize);
41 virtual void releaseGLObjects(osg::State* state = 0) const;
43 virtual void traverse(osg::NodeVisitor& nv);
45 /** Set all the parameters to create an rain effect of specified intensity.*/
46 void rain(float intensity);
48 /** Set all the parameters to create an snow effect of specified intensity.*/
49 void snow(float intensity);
51 void setMaximumParticleDensity(float density) { if (_maximumParticleDensity==density) return; _maximumParticleDensity = density; _dirty = true;}
52 float getMaximumParticleDensity() const { return _maximumParticleDensity; }
54 void setWind(const osg::Vec3& wind) { _wind = wind; }
55 const osg::Vec3& getWind() const { return _wind; }
57 void setPosition(const osg::Vec3& position) { _origin = position; }
58 const osg::Vec3& getPosition() const { return _origin; }
60 void setCellSize(const osg::Vec3& cellSize) { if (_cellSize==cellSize) return; _cellSize = cellSize; _dirty = true; }
61 const osg::Vec3& getCellSize() const { return _cellSize; }
63 void setParticleSpeed(float particleSpeed) { if (_particleSpeed==particleSpeed) return; _particleSpeed = particleSpeed; _dirty = true; }
64 float getParticleSpeed() const { return _particleSpeed; }
66 void setParticleSize(float particleSize) { if (_particleSize==particleSize) return; _particleSize = particleSize; _dirty = true;}
67 float getParticleSize() const { return _particleSize; }
69 void setParticleColor(const osg::Vec4& color) { if (_particleColor==color) return; _particleColor = color; _dirty = true;}
70 const osg::Vec4& getParticleColor() const { return _particleColor; }
72 void setNearTransition(float nearTransition) { _nearTransition = nearTransition; }
73 float getNearTransition() const { return _nearTransition; }
75 void setFarTransition(float farTransition) { _farTransition = farTransition; }
76 float getFarTransition() const { return _farTransition; }
78 void setUseFarLineSegments(bool useFarLineSegments) { _useFarLineSegments = useFarLineSegments; }
79 bool getUseFarLineSegments() const { return _useFarLineSegments; }
81 void setFog(osg::Fog* fog) { _fog = fog; }
82 osg::Fog* getFog() { return _fog.get(); }
83 const osg::Fog* getFog() const { return _fog.get(); }
85 osg::Geometry* getQuadGeometry() { return _quadGeometry.get(); }
86 osg::StateSet* getQuadStateSet() { return _quadStateSet.get(); }
88 osg::Geometry* getLineGeometry() { return _lineGeometry.get(); }
89 osg::StateSet* getLineStateSet() { return _lineStateSet.get(); }
91 osg::Geometry* getPointGeometry() { return _pointGeometry.get(); }
92 osg::StateSet* getPointStateSet() { return _pointStateSet.get(); }
94 /** Internal drawable used to render batches of cells.*/
95 class OSGPARTICLE_EXPORT PrecipitationDrawable : public osg::Drawable
99 PrecipitationDrawable();
100 PrecipitationDrawable(const PrecipitationDrawable& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
102 META_Object(osgParticle, PrecipitationDrawable);
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 {}
109 void setRequiresPreviousMatrix(bool flag) { _requiresPreviousMatrix = flag; }
110 bool getRequiresPreviousMatrix() const { return _requiresPreviousMatrix; }
112 void setGeometry(osg::Geometry* geom) { _geometry = geom; }
113 osg::Geometry* getGeometry() { return _geometry.get(); }
114 const osg::Geometry* getGeometry() const { return _geometry.get(); }
116 void setDrawType(GLenum type) { _drawType = type; }
117 GLenum getDrawType() const { return _drawType; }
119 void setNumberOfVertices(unsigned int numVertices) { _numberOfVertices = numVertices; }
120 unsigned int getNumberOfVertices() const { return _numberOfVertices; }
122 virtual void resizeGLObjectBuffers(unsigned int maxSize);
123 virtual void releaseGLObjects(osg::State* state) const;
125 virtual void drawImplementation(osg::RenderInfo& renderInfo) const;
129 Cell(int in_i, int in_j, int in_k):
130 i(in_i), j(in_j), k(in_k) {}
132 inline bool operator < (const Cell& rhs) const
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;
148 struct DepthMatrixStartTime
150 inline bool operator < (const DepthMatrixStartTime& rhs) const
152 return depth < rhs.depth;
157 osg::Matrix modelview;
160 typedef std::map< Cell, DepthMatrixStartTime > CellMatrixMap;
164 inline bool operator () (const CellMatrixMap::value_type* lhs,const CellMatrixMap::value_type* rhs) const
166 return (*lhs).second<(*rhs).second;
171 CellMatrixMap& getCurrentCellMatrixMap() { return _currentCellMatrixMap; }
172 CellMatrixMap& getPreviousCellMatrixMap() { return _previousCellMatrixMap; }
174 inline void newFrame()
176 _previousCellMatrixMap.swap(_currentCellMatrixMap);
177 _currentCellMatrixMap.clear();
182 virtual ~PrecipitationDrawable();
184 bool _requiresPreviousMatrix;
186 osg::ref_ptr<osg::Geometry> _geometry;
188 mutable CellMatrixMap _currentCellMatrixMap;
189 mutable CellMatrixMap _previousCellMatrixMap;
192 unsigned int _numberOfVertices;
198 virtual ~PrecipitationEffect() {}
200 void compileGLObjects(osg::RenderInfo& renderInfo) const;
204 void createGeometry(unsigned int numParticles,
205 osg::Geometry* quad_geometry,
206 osg::Geometry* line_geometry,
207 osg::Geometry* point_geometry);
209 void setUpGeometries(unsigned int numParticles);
212 struct PrecipitationDrawableSet
214 osg::ref_ptr<PrecipitationDrawable> _quadPrecipitationDrawable;
215 osg::ref_ptr<PrecipitationDrawable> _linePrecipitationDrawable;
216 osg::ref_ptr<PrecipitationDrawable> _pointPrecipitationDrawable;
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;
225 float _particleSpeed;
227 osg::Vec4 _particleColor;
228 float _maximumParticleDensity;
230 float _nearTransition;
231 float _farTransition;
232 bool _useFarLineSegments;
233 osg::ref_ptr<osg::Fog> _fog;
235 osg::ref_ptr<osg::Uniform> _inversePeriodUniform;
236 osg::ref_ptr<osg::Uniform> _particleSizeUniform;
237 osg::ref_ptr<osg::Uniform> _particleColorUniform;
239 typedef std::pair< osg::NodeVisitor*, osg::NodePath > ViewIdentifier;
240 typedef std::map< ViewIdentifier, PrecipitationDrawableSet > ViewDrawableMap;
242 OpenThreads::Mutex _mutex;
243 ViewDrawableMap _viewDrawableMap;
245 osg::ref_ptr<osg::Geometry> _quadGeometry;
246 osg::ref_ptr<osg::StateSet> _quadStateSet;
248 osg::ref_ptr<osg::Geometry> _lineGeometry;
249 osg::ref_ptr<osg::StateSet> _lineStateSet;
251 osg::ref_ptr<osg::Geometry> _pointGeometry;
252 osg::ref_ptr<osg::StateSet> _pointStateSet;
260 osg::Vec3 _inverse_du;
261 osg::Vec3 _inverse_dv;
262 osg::Vec3 _inverse_dw;
264 double _previousFrameTime;