OpenSceneGraph/include/osgParticle/PrecipitationEffect

244 lines
9.1 KiB
Plaintext
Raw Normal View History

2006-04-22 03:39:05 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSGPARTICLE_PRECIPITATIONEFFECT
#define OSGPARTICLE_PRECIPITATIONEFFECT
#include <osg/Group>
#include <osg/BoundingBox>
#include <osg/Fog>
#include <osg/Geometry>
2006-04-22 03:39:05 +08:00
#include <osgUtil/CullVisitor>
#include <osgParticle/Export>
namespace osgParticle
{
class OSGPARTICLE_EXPORT PrecipitationEffect : public osg::Group
{
public:
PrecipitationEffect();
PrecipitationEffect(const PrecipitationEffect& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
virtual const char* libraryName() const { return "osgParticle"; }
virtual const char* className() const { return "PrecipitationEffect"; }
virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const PrecipitationEffect*>(obj) != 0; }
virtual void accept(osg::NodeVisitor& nv) { if (nv.validNodeMask(*this)) { nv.pushOntoNodePath(this); nv.apply(*this); nv.popFromNodePath(); } }
virtual void traverse(osg::NodeVisitor& nv);
/** Set all the parameters to create an rain effect of specified intensity.*/
void rain(float intensity);
2006-04-25 05:48:23 +08:00
/** Set all the parameters to create an snow effect of specified intensity.*/
void snow(float intensity);
void setIntensity(float intensity) { _intensity = intensity; }
float getIntensity() const { return _intensity; }
void setMaximumParticleDensity(float density) { _maximumParticleDensity = density; }
float setMaximumParticleDensity() const { return _maximumParticleDensity; }
2006-04-22 03:39:05 +08:00
void setWind(const osg::Vec3& wind) { _wind = wind; }
const osg::Vec3& getWind() const { return _wind; }
2006-04-22 03:39:05 +08:00
void setPosition(const osg::Vec3& position) { _origin = position; }
const osg::Vec3& getPosition() const { return _origin; }
2006-04-22 03:39:05 +08:00
void setCellSize(const osg::Vec3& cellSize) { _cellSize = cellSize; }
const osg::Vec3& getCellSize() const { return _cellSize; }
void setParticleSpeed(float particleSpeed) { _particleSpeed = particleSpeed; }
float getParticleSpeed() const { return _particleSpeed; }
void setParticleSize(float particleSize) { _particleSize = particleSize; }
float getParticleSize() const { return _particleSize; }
void setParticleColor(const osg::Vec4& color) { _particleColor = color; }
const osg::Vec4& getParticleColor() const { return _particleColor; }
void setNearTransition(float nearTransition) { _nearTransition = nearTransition; }
float getNearTransition() const { return _nearTransition; }
void setFarTransition(float farTransition) { _farTransition = farTransition; }
float getFarTransition() const { return _farTransition; }
void setFog(osg::Fog* fog) { _fog = fog; }
osg::Fog* getFog() { return _fog.get(); }
const osg::Fog* getFog() const { return _fog.get(); }
2006-04-22 03:39:05 +08:00
void compileGLObjects(osg::State& state) const;
void update();
protected:
virtual ~PrecipitationEffect() {}
void createGeometry(unsigned int numParticles,
osg::Geometry* quad_geometry,
osg::Geometry* line_geometry,
osg::Geometry* point_geometry);
void setUpGeometries(unsigned int numParticles);
class OSGPARTICLE_EXPORT PrecipitationDrawable : public osg::Drawable
2006-04-22 03:39:05 +08:00
{
public:
PrecipitationDrawable();
PrecipitationDrawable(const PrecipitationDrawable& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
META_Object(osgParticle, PrecipitationDrawable);
virtual bool supports(const osg::PrimitiveFunctor&) const { return false; }
virtual void accept(osg::PrimitiveFunctor&) const {}
virtual bool supports(const osg::PrimitiveIndexFunctor&) const { return false; }
virtual void accept(osg::PrimitiveIndexFunctor&) const {}
void setRequiresPreviousMatrix(bool flag) { _requiresPreviousMatrix = flag; }
bool getRequiresPreviousMatrix() const { return _requiresPreviousMatrix; }
void setGeometry(osg::Geometry* geom) { _geometry = geom; }
osg::Geometry* getGeometry() { return _geometry.get(); }
const osg::Geometry* getGeometry() const { return _geometry.get(); }
virtual void drawImplementation(osg::State& state) const;
struct Cell
{
Cell(int in_i, int in_j, int in_k):
i(in_i), j(in_j), k(in_k) {}
inline bool operator < (const Cell& rhs) const
{
if (i<rhs.i) return true;
if (i>rhs.i) return false;
if (j<rhs.j) return true;
if (j>rhs.j) return false;
if (k<rhs.k) return true;
if (k>rhs.k) return false;
return false;
}
int i;
int j;
int k;
};
struct DepthMatrixStartTime
{
inline bool operator < (const DepthMatrixStartTime& rhs) const
{
return depth < rhs.depth;
}
float depth;
float startTime;
osg::Matrix modelview;
};
typedef std::map< Cell, DepthMatrixStartTime > CellMatrixMap;
2006-04-25 05:48:23 +08:00
struct LessFunctor
{
inline bool operator () (const CellMatrixMap::value_type* lhs,const CellMatrixMap::value_type* rhs) const
{
return (*lhs).second<(*rhs).second;
}
};
CellMatrixMap& getCurrentCellMatrixMap() { return _currentCellMatrixMap; }
CellMatrixMap& getPreviousCellMatrixMap() { return _previousCellMatrixMap; }
inline void newFrame()
2006-04-22 03:39:05 +08:00
{
_previousCellMatrixMap.swap(_currentCellMatrixMap);
_currentCellMatrixMap.clear();
2006-04-22 03:39:05 +08:00
}
protected:
virtual ~PrecipitationDrawable() {}
bool _requiresPreviousMatrix;
osg::ref_ptr<osg::Geometry> _geometry;
mutable CellMatrixMap _currentCellMatrixMap;
mutable CellMatrixMap _previousCellMatrixMap;
};
struct PrecipitationDrawableSet
{
2006-04-22 03:39:05 +08:00
osg::ref_ptr<PrecipitationDrawable> _quadPrecipitationDrawable;
osg::ref_ptr<PrecipitationDrawable> _linePrecipitationDrawable;
osg::ref_ptr<PrecipitationDrawable> _pointPrecipitationDrawable;
};
void cull(PrecipitationDrawableSet& pds, osgUtil::CullVisitor* cv) const;
bool build(const osg::Vec3 eyeLocal, int i, int j, int k, float startTime, PrecipitationDrawableSet& pds, osg::Polytope& frustum, osgUtil::CullVisitor* cv) const;
2006-04-22 03:39:05 +08:00
// parameters
osg::BoundingBox _boundingBox;
float _intensity;
osg::Vec3 _wind;
float _particleSpeed;
float _particleSize;
osg::Vec4 _particleColor;
float _maximumParticleDensity;
osg::Vec3 _cellSize;
float _nearTransition;
float _farTransition;
bool _useFarLineSegments;
osg::ref_ptr<osg::Fog> _fog;
2006-04-22 03:39:05 +08:00
typedef std::pair< osg::NodeVisitor*, osg::NodePath > ViewIdenitifier;
typedef std::map< ViewIdenitifier, PrecipitationDrawableSet > ViewDrawableMap;
OpenThreads::Mutex _mutex;
ViewDrawableMap _viewDrawableMap;
osg::ref_ptr<osg::StateSet> _precipitationStateSet;
osg::ref_ptr<osg::Geometry> _quadGeometry;
osg::ref_ptr<osg::StateSet> _quadStateSet;
osg::ref_ptr<osg::Geometry> _lineGeometry;
osg::ref_ptr<osg::StateSet> _lineStateSet;
osg::ref_ptr<osg::Geometry> _pointGeometry;
osg::ref_ptr<osg::StateSet> _pointStateSet;
// cache variables.
float _period;
osg::Vec3 _origin;
osg::Vec3 _du;
osg::Vec3 _dv;
osg::Vec3 _dw;
osg::Vec3 _inverse_du;
osg::Vec3 _inverse_dv;
osg::Vec3 _inverse_dw;
2006-04-22 03:39:05 +08:00
};
}
#endif