2013-05-28 22:40:36 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2011 Robert Osfield
|
2011-08-07 23:51:54 +08:00
|
|
|
*
|
|
|
|
* 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 OSGSHADOW_VIEWDEPENDENTSHADOWMAP
|
|
|
|
#define OSGSHADOW_VIEWDEPENDENTSHADOWMAP 1
|
|
|
|
|
|
|
|
#include <osg/Camera>
|
|
|
|
#include <osg/Material>
|
|
|
|
#include <osg/MatrixTransform>
|
|
|
|
#include <osg/LightSource>
|
2011-08-12 22:53:40 +08:00
|
|
|
#include <osg/PolygonOffset>
|
2011-08-07 23:51:54 +08:00
|
|
|
|
|
|
|
#include <osgShadow/ShadowTechnique>
|
|
|
|
|
|
|
|
namespace osgShadow {
|
|
|
|
|
|
|
|
/** ViewDependentShadowMap provides an base implementation of view dependent shadow mapping techniques.*/
|
|
|
|
class OSGSHADOW_EXPORT ViewDependentShadowMap : public ShadowTechnique
|
|
|
|
{
|
|
|
|
public :
|
|
|
|
ViewDependentShadowMap();
|
|
|
|
|
|
|
|
ViewDependentShadowMap(const ViewDependentShadowMap& vdsm, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
|
|
|
|
|
|
|
|
META_Object(osgShadow, ViewDependentShadowMap);
|
|
|
|
|
|
|
|
/** initialize the ShadowedScene and local cached data structures.*/
|
|
|
|
virtual void init();
|
|
|
|
|
|
|
|
/** run the update traversal of the ShadowedScene and update any loca chached data structures.*/
|
|
|
|
virtual void update(osg::NodeVisitor& nv);
|
|
|
|
|
|
|
|
/** run the cull traversal of the ShadowedScene and set up the rendering for this ShadowTechnique.*/
|
|
|
|
virtual void cull(osgUtil::CullVisitor& cv);
|
|
|
|
|
2011-08-26 18:52:25 +08:00
|
|
|
/** Resize any per context GLObject buffers to specified size. */
|
|
|
|
virtual void resizeGLObjectBuffers(unsigned int maxSize);
|
|
|
|
|
|
|
|
/** If State is non-zero, this function releases any associated OpenGL objects for
|
|
|
|
* the specified graphics context. Otherwise, releases OpenGL objects
|
|
|
|
* for all graphics contexts. */
|
|
|
|
virtual void releaseGLObjects(osg::State* = 0) const;
|
|
|
|
|
2011-08-07 23:51:54 +08:00
|
|
|
/** Clean scene graph from any shadow technique specific nodes, state and drawables.*/
|
|
|
|
virtual void cleanSceneGraph();
|
|
|
|
|
|
|
|
|
2011-08-09 01:00:55 +08:00
|
|
|
struct OSGSHADOW_EXPORT Frustum
|
|
|
|
{
|
2011-08-26 01:21:38 +08:00
|
|
|
Frustum(osgUtil::CullVisitor* cv, double minZNear, double maxZFar);
|
2011-08-09 01:00:55 +08:00
|
|
|
|
|
|
|
osg::Matrixd projectionMatrix;
|
|
|
|
osg::Matrixd modelViewMatrix;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2011-08-09 01:00:55 +08:00
|
|
|
typedef std::vector<osg::Vec3d> Vertices;
|
|
|
|
Vertices corners;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2011-08-09 01:00:55 +08:00
|
|
|
typedef std::vector<unsigned int> Indices;
|
|
|
|
typedef std::vector<Indices> Faces;
|
|
|
|
Faces faces;
|
|
|
|
|
|
|
|
typedef std::vector<Indices> Edges;
|
|
|
|
Edges edges;
|
|
|
|
|
2011-08-18 00:09:17 +08:00
|
|
|
osg::Vec3d eye;
|
2011-08-09 01:00:55 +08:00
|
|
|
osg::Vec3d centerNearPlane;
|
|
|
|
osg::Vec3d centerFarPlane;
|
|
|
|
osg::Vec3d center;
|
|
|
|
osg::Vec3d frustumCenterLine;
|
|
|
|
};
|
2011-08-07 23:51:54 +08:00
|
|
|
|
2011-08-09 23:57:37 +08:00
|
|
|
// forward declare
|
|
|
|
class ViewDependentData;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2011-08-09 21:39:57 +08:00
|
|
|
struct OSGSHADOW_EXPORT LightData : public osg::Referenced
|
2011-08-09 01:00:55 +08:00
|
|
|
{
|
2011-08-09 23:57:37 +08:00
|
|
|
LightData(ViewDependentData* vdd);
|
2011-08-09 21:39:57 +08:00
|
|
|
|
|
|
|
virtual void setLightData(osg::RefMatrix* lm, const osg::Light* l, const osg::Matrixd& modelViewMatrix);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2011-08-09 23:57:37 +08:00
|
|
|
ViewDependentData* _viewDependentData;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2011-08-09 01:00:55 +08:00
|
|
|
osg::ref_ptr<osg::RefMatrix> lightMatrix;
|
|
|
|
osg::ref_ptr<const osg::Light> light;
|
|
|
|
|
|
|
|
osg::Vec4d lightPos;
|
|
|
|
osg::Vec3d lightPos3;
|
|
|
|
osg::Vec3d lightDir;
|
|
|
|
bool directionalLight;
|
|
|
|
|
|
|
|
typedef std::vector<unsigned int> ActiveTextureUnits;
|
|
|
|
ActiveTextureUnits textureUnits;
|
|
|
|
};
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2011-08-09 21:39:57 +08:00
|
|
|
typedef std::list< osg::ref_ptr<LightData> > LightDataList;
|
|
|
|
|
|
|
|
struct OSGSHADOW_EXPORT ShadowData : public osg::Referenced
|
|
|
|
{
|
2011-08-09 23:57:37 +08:00
|
|
|
ShadowData(ViewDependentData* vdd);
|
2011-08-09 21:39:57 +08:00
|
|
|
|
2011-08-26 18:52:25 +08:00
|
|
|
virtual void releaseGLObjects(osg::State* = 0) const;
|
|
|
|
|
2011-08-09 23:57:37 +08:00
|
|
|
ViewDependentData* _viewDependentData;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2011-08-09 21:39:57 +08:00
|
|
|
unsigned int _textureUnit;
|
|
|
|
osg::ref_ptr<osg::Texture2D> _texture;
|
|
|
|
osg::ref_ptr<osg::TexGen> _texgen;
|
|
|
|
osg::ref_ptr<osg::Camera> _camera;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::list< osg::ref_ptr<ShadowData> > ShadowDataList;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2011-08-09 21:39:57 +08:00
|
|
|
|
|
|
|
class OSGSHADOW_EXPORT ViewDependentData : public osg::Referenced
|
|
|
|
{
|
|
|
|
public:
|
2011-08-09 23:57:37 +08:00
|
|
|
ViewDependentData(ViewDependentShadowMap* vdsm);
|
|
|
|
|
|
|
|
const ViewDependentShadowMap* getViewDependentShadowMap() const { return _viewDependentShadowMap; }
|
2011-08-09 21:39:57 +08:00
|
|
|
|
|
|
|
LightDataList& getLightDataList() { return _lightDataList; }
|
|
|
|
|
|
|
|
ShadowDataList& getShadowDataList() { return _shadowDataList; }
|
|
|
|
|
|
|
|
osg::StateSet* getStateSet() { return _stateset.get(); }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2011-08-26 18:52:25 +08:00
|
|
|
virtual void releaseGLObjects(osg::State* = 0) const;
|
|
|
|
|
2011-08-09 21:39:57 +08:00
|
|
|
protected:
|
|
|
|
virtual ~ViewDependentData() {}
|
|
|
|
|
2011-08-09 23:57:37 +08:00
|
|
|
ViewDependentShadowMap* _viewDependentShadowMap;
|
|
|
|
|
2011-08-09 21:39:57 +08:00
|
|
|
osg::ref_ptr<osg::StateSet> _stateset;
|
|
|
|
|
2011-08-09 23:57:37 +08:00
|
|
|
LightDataList _lightDataList;
|
|
|
|
ShadowDataList _shadowDataList;
|
2011-08-09 21:39:57 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
virtual ViewDependentData* createViewDependentData(osgUtil::CullVisitor* cv);
|
|
|
|
|
|
|
|
ViewDependentData* getViewDependentData(osgUtil::CullVisitor* cv);
|
|
|
|
|
|
|
|
|
2011-08-29 17:59:47 +08:00
|
|
|
|
2011-08-26 18:52:25 +08:00
|
|
|
virtual void createShaders();
|
2011-08-25 03:38:58 +08:00
|
|
|
|
|
|
|
virtual bool selectActiveLights(osgUtil::CullVisitor* cv, ViewDependentData* vdd) const;
|
|
|
|
|
|
|
|
virtual osg::Polytope computeLightViewFrustumPolytope(Frustum& frustum, LightData& positionedLight);
|
|
|
|
|
2011-11-23 05:55:30 +08:00
|
|
|
virtual bool computeShadowCameraSettings(Frustum& frustum, LightData& positionedLight, osg::Matrixd& projectionMatrix, osg::Matrixd& viewMatrix);
|
2011-08-07 23:51:54 +08:00
|
|
|
|
2011-08-19 20:42:51 +08:00
|
|
|
virtual bool adjustPerspectiveShadowMapCameraSettings(osgUtil::RenderStage* renderStage, Frustum& frustum, LightData& positionedLight, osg::Camera* camera);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2011-08-07 23:51:54 +08:00
|
|
|
virtual bool assignTexGenSettings(osgUtil::CullVisitor* cv, osg::Camera* camera, unsigned int textureUnit, osg::TexGen* texgen);
|
|
|
|
|
2011-08-09 14:54:44 +08:00
|
|
|
virtual void cullShadowReceivingScene(osgUtil::CullVisitor* cv) const;
|
2011-08-07 23:51:54 +08:00
|
|
|
|
|
|
|
virtual void cullShadowCastingScene(osgUtil::CullVisitor* cv, osg::Camera* camera) const;
|
|
|
|
|
2011-08-09 21:39:57 +08:00
|
|
|
virtual osg::StateSet* selectStateSetForRenderingShadow(ViewDependentData& vdd) const;
|
2011-08-07 23:51:54 +08:00
|
|
|
|
2011-08-09 23:57:37 +08:00
|
|
|
|
2011-08-07 23:51:54 +08:00
|
|
|
protected:
|
|
|
|
virtual ~ViewDependentShadowMap();
|
|
|
|
|
|
|
|
typedef std::map< osgUtil::CullVisitor*, osg::ref_ptr<ViewDependentData> > ViewDependentDataMap;
|
2011-08-26 18:52:25 +08:00
|
|
|
mutable OpenThreads::Mutex _viewDependentDataMapMutex;
|
|
|
|
ViewDependentDataMap _viewDependentDataMap;
|
2011-08-09 14:54:44 +08:00
|
|
|
|
2011-08-12 22:53:40 +08:00
|
|
|
osg::ref_ptr<osg::StateSet> _shadowRecievingPlaceholderStateSet;
|
2011-08-09 23:57:37 +08:00
|
|
|
|
2011-08-12 22:53:40 +08:00
|
|
|
osg::ref_ptr<osg::StateSet> _shadowCastingStateSet;
|
|
|
|
osg::ref_ptr<osg::PolygonOffset> _polygonOffset;
|
2011-08-29 18:26:05 +08:00
|
|
|
osg::ref_ptr<osg::Texture2D> _fallbackBaseTexture;
|
|
|
|
osg::ref_ptr<osg::Texture2D> _fallbackShadowMapTexture;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2011-08-12 22:53:40 +08:00
|
|
|
typedef std::vector< osg::ref_ptr<osg::Uniform> > Uniforms;
|
2015-04-13 18:43:56 +08:00
|
|
|
mutable OpenThreads::Mutex _accessUniformsAndProgramMutex;
|
2011-08-12 22:53:40 +08:00
|
|
|
Uniforms _uniforms;
|
|
|
|
osg::ref_ptr<osg::Program> _program;
|
2011-08-07 23:51:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|