| 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 | { |
|---|
| 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 traverse(osg::NodeVisitor& nv); |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | /** Set all the parameters to create an rain effect of specified intensity.*/ |
|---|
| 44 | void rain(float intensity); |
|---|
| 45 | |
|---|
| 46 | /** Set all the parameters to create an snow effect of specified intensity.*/ |
|---|
| 47 | void snow(float intensity); |
|---|
| 48 | |
|---|
| 49 | void setMaximumParticleDensity(float density) { if (_maximumParticleDensity==density) return; _maximumParticleDensity = density; _dirty = true;} |
|---|
| 50 | float setMaximumParticleDensity() 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 | |
|---|
| 92 | /** Internal drawable used to render batches of cells.*/ |
|---|
| 93 | class OSGPARTICLE_EXPORT PrecipitationDrawable : public osg::Drawable |
|---|
| 94 | { |
|---|
| 95 | public: |
|---|
| 96 | |
|---|
| 97 | PrecipitationDrawable(); |
|---|
| 98 | PrecipitationDrawable(const PrecipitationDrawable& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY); |
|---|
| 99 | |
|---|
| 100 | META_Object(osgParticle, PrecipitationDrawable); |
|---|
| 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 | |
|---|
| 143 | struct DepthMatrixStartTime |
|---|
| 144 | { |
|---|
| 145 | inline bool operator < (const DepthMatrixStartTime& rhs) const |
|---|
| 146 | { |
|---|
| 147 | return depth < rhs.depth; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | float depth; |
|---|
| 151 | float startTime; |
|---|
| 152 | osg::Matrix modelview; |
|---|
| 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 | |
|---|
| 177 | virtual ~PrecipitationDrawable() {} |
|---|
| 178 | |
|---|
| 179 | bool _requiresPreviousMatrix; |
|---|
| 180 | |
|---|
| 181 | osg::ref_ptr<osg::Geometry> _geometry; |
|---|
| 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 | |
|---|
| 207 | struct PrecipitationDrawableSet |
|---|
| 208 | { |
|---|
| 209 | osg::ref_ptr<PrecipitationDrawable> _quadPrecipitationDrawable; |
|---|
| 210 | osg::ref_ptr<PrecipitationDrawable> _linePrecipitationDrawable; |
|---|
| 211 | osg::ref_ptr<PrecipitationDrawable> _pointPrecipitationDrawable; |
|---|
| 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; |
|---|
| 219 | osg::Vec3 _wind; |
|---|
| 220 | float _particleSpeed; |
|---|
| 221 | float _particleSize; |
|---|
| 222 | osg::Vec4 _particleColor; |
|---|
| 223 | float _maximumParticleDensity; |
|---|
| 224 | osg::Vec3 _cellSize; |
|---|
| 225 | float _nearTransition; |
|---|
| 226 | float _farTransition; |
|---|
| 227 | bool _useFarLineSegments; |
|---|
| 228 | osg::ref_ptr<osg::Fog> _fog; |
|---|
| 229 | |
|---|
| 230 | osg::ref_ptr<osg::Uniform> _inversePeriodUniform; |
|---|
| 231 | osg::ref_ptr<osg::Uniform> _particleSizeUniform; |
|---|
| 232 | osg::ref_ptr<osg::Uniform> _particleColorUniform; |
|---|
| 233 | |
|---|
| 234 | typedef std::pair< osg::NodeVisitor*, osg::NodePath > ViewIdentifier; |
|---|
| 235 | typedef std::map< ViewIdentifier, PrecipitationDrawableSet > ViewDrawableMap; |
|---|
| 236 | |
|---|
| 237 | OpenThreads::Mutex _mutex; |
|---|
| 238 | ViewDrawableMap _viewDrawableMap; |
|---|
| 239 | |
|---|
| 240 | osg::ref_ptr<osg::Geometry> _quadGeometry; |
|---|
| 241 | osg::ref_ptr<osg::StateSet> _quadStateSet; |
|---|
| 242 | |
|---|
| 243 | osg::ref_ptr<osg::Geometry> _lineGeometry; |
|---|
| 244 | osg::ref_ptr<osg::StateSet> _lineStateSet; |
|---|
| 245 | |
|---|
| 246 | osg::ref_ptr<osg::Geometry> _pointGeometry; |
|---|
| 247 | osg::ref_ptr<osg::StateSet> _pointStateSet; |
|---|
| 248 | |
|---|
| 249 | // cache variables. |
|---|
| 250 | float _period; |
|---|
| 251 | osg::Vec3 _origin; |
|---|
| 252 | osg::Vec3 _du; |
|---|
| 253 | osg::Vec3 _dv; |
|---|
| 254 | osg::Vec3 _dw; |
|---|
| 255 | osg::Vec3 _inverse_du; |
|---|
| 256 | osg::Vec3 _inverse_dv; |
|---|
| 257 | osg::Vec3 _inverse_dw; |
|---|
| 258 | |
|---|
| 259 | double _previousFrameTime; |
|---|
| 260 | |
|---|
| 261 | }; |
|---|
| 262 | |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | #endif |
|---|