1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2011 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 OSGSHADOW_VIEWDEPENDENTSHADOWMAP
15#define OSGSHADOW_VIEWDEPENDENTSHADOWMAP 1
18#include <osg/Material>
19#include <osg/MatrixTransform>
20#include <osg/LightSource>
21#include <osg/PolygonOffset>
23#include <osgShadow/ShadowTechnique>
27/** ViewDependentShadowMap provides an base implementation of view dependent shadow mapping techniques.*/
28class OSGSHADOW_EXPORT ViewDependentShadowMap : public ShadowTechnique
31 ViewDependentShadowMap();
33 ViewDependentShadowMap(const ViewDependentShadowMap& vdsm, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
35 META_Object(osgShadow, ViewDependentShadowMap);
37 /** initialize the ShadowedScene and local cached data structures.*/
40 /** run the update traversal of the ShadowedScene and update any loca chached data structures.*/
41 virtual void update(osg::NodeVisitor& nv);
43 /** run the cull traversal of the ShadowedScene and set up the rendering for this ShadowTechnique.*/
44 virtual void cull(osgUtil::CullVisitor& cv);
46 /** Resize any per context GLObject buffers to specified size. */
47 virtual void resizeGLObjectBuffers(unsigned int maxSize);
49 /** If State is non-zero, this function releases any associated OpenGL objects for
50 * the specified graphics context. Otherwise, releases OpenGL objects
51 * for all graphics contexts. */
52 virtual void releaseGLObjects(osg::State* = 0) const;
54 /** Clean scene graph from any shadow technique specific nodes, state and drawables.*/
55 virtual void cleanSceneGraph();
58 struct OSGSHADOW_EXPORT Frustum
60 Frustum(osgUtil::CullVisitor* cv, double minZNear, double maxZFar);
62 osg::Matrixd projectionMatrix;
63 osg::Matrixd modelViewMatrix;
65 typedef std::vector<osg::Vec3d> Vertices;
68 typedef std::vector<unsigned int> Indices;
69 typedef std::vector<Indices> Faces;
72 typedef std::vector<Indices> Edges;
76 osg::Vec3d centerNearPlane;
77 osg::Vec3d centerFarPlane;
79 osg::Vec3d frustumCenterLine;
83 class ViewDependentData;
85 struct OSGSHADOW_EXPORT LightData : public osg::Referenced
87 LightData(ViewDependentData* vdd);
89 virtual void setLightData(osg::RefMatrix* lm, const osg::Light* l, const osg::Matrixd& modelViewMatrix);
91 ViewDependentData* _viewDependentData;
93 osg::ref_ptr<osg::RefMatrix> lightMatrix;
94 osg::ref_ptr<const osg::Light> light;
99 bool directionalLight;
101 typedef std::vector<unsigned int> ActiveTextureUnits;
102 ActiveTextureUnits textureUnits;
105 typedef std::list< osg::ref_ptr<LightData> > LightDataList;
107 struct OSGSHADOW_EXPORT ShadowData : public osg::Referenced
109 ShadowData(ViewDependentData* vdd);
111 virtual void releaseGLObjects(osg::State* = 0) const;
113 ViewDependentData* _viewDependentData;
115 unsigned int _textureUnit;
116 osg::ref_ptr<osg::Texture2D> _texture;
117 osg::ref_ptr<osg::TexGen> _texgen;
118 osg::ref_ptr<osg::Camera> _camera;
121 typedef std::list< osg::ref_ptr<ShadowData> > ShadowDataList;
124 class OSGSHADOW_EXPORT ViewDependentData : public osg::Referenced
127 ViewDependentData(ViewDependentShadowMap* vdsm);
129 const ViewDependentShadowMap* getViewDependentShadowMap() const { return _viewDependentShadowMap; }
131 LightDataList& getLightDataList() { return _lightDataList; }
133 ShadowDataList& getShadowDataList() { return _shadowDataList; }
135 osg::StateSet* getStateSet() { return _stateset.get(); }
137 virtual void releaseGLObjects(osg::State* = 0) const;
140 virtual ~ViewDependentData() {}
142 ViewDependentShadowMap* _viewDependentShadowMap;
144 osg::ref_ptr<osg::StateSet> _stateset;
146 LightDataList _lightDataList;
147 ShadowDataList _shadowDataList;
150 virtual ViewDependentData* createViewDependentData(osgUtil::CullVisitor* cv);
152 ViewDependentData* getViewDependentData(osgUtil::CullVisitor* cv);
156 virtual void createShaders();
158 virtual bool selectActiveLights(osgUtil::CullVisitor* cv, ViewDependentData* vdd) const;
160 virtual osg::Polytope computeLightViewFrustumPolytope(Frustum& frustum, LightData& positionedLight);
162 virtual bool computeShadowCameraSettings(Frustum& frustum, LightData& positionedLight, osg::Matrixd& projectionMatrix, osg::Matrixd& viewMatrix);
164 virtual bool adjustPerspectiveShadowMapCameraSettings(osgUtil::RenderStage* renderStage, Frustum& frustum, LightData& positionedLight, osg::Camera* camera);
166 virtual bool assignTexGenSettings(osgUtil::CullVisitor* cv, osg::Camera* camera, unsigned int textureUnit, osg::TexGen* texgen);
168 virtual void cullShadowReceivingScene(osgUtil::CullVisitor* cv) const;
170 virtual void cullShadowCastingScene(osgUtil::CullVisitor* cv, osg::Camera* camera) const;
172 virtual osg::StateSet* selectStateSetForRenderingShadow(ViewDependentData& vdd) const;
175 virtual ~ViewDependentShadowMap();
177 typedef std::map< osgUtil::CullVisitor*, osg::ref_ptr<ViewDependentData> > ViewDependentDataMap;
178 mutable OpenThreads::Mutex _viewDependentDataMapMutex;
179 ViewDependentDataMap _viewDependentDataMap;
181 osg::ref_ptr<osg::StateSet> _shadowRecievingPlaceholderStateSet;
183 osg::ref_ptr<osg::StateSet> _shadowCastingStateSet;
184 osg::ref_ptr<osg::PolygonOffset> _polygonOffset;
185 osg::ref_ptr<osg::Texture2D> _fallbackBaseTexture;
186 osg::ref_ptr<osg::Texture2D> _fallbackShadowMapTexture;
188 typedef std::vector< osg::ref_ptr<osg::Uniform> > Uniforms;
189 mutable OpenThreads::Mutex _accessUniformsAndProgramMutex;
191 osg::ref_ptr<osg::Program> _program;