openscenegraph
ViewDependentShadowMap
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2011 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 OSGSHADOW_VIEWDEPENDENTSHADOWMAP
15#define OSGSHADOW_VIEWDEPENDENTSHADOWMAP 1
16
17#include <osg/Camera>
18#include <osg/Material>
19#include <osg/MatrixTransform>
20#include <osg/LightSource>
21#include <osg/PolygonOffset>
22
23#include <osgShadow/ShadowTechnique>
24
25namespace osgShadow {
26
27/** ViewDependentShadowMap provides an base implementation of view dependent shadow mapping techniques.*/
28class OSGSHADOW_EXPORT ViewDependentShadowMap : public ShadowTechnique
29{
30 public :
31 ViewDependentShadowMap();
32
33 ViewDependentShadowMap(const ViewDependentShadowMap& vdsm, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
34
35 META_Object(osgShadow, ViewDependentShadowMap);
36
37 /** initialize the ShadowedScene and local cached data structures.*/
38 virtual void init();
39
40 /** run the update traversal of the ShadowedScene and update any loca chached data structures.*/
41 virtual void update(osg::NodeVisitor& nv);
42
43 /** run the cull traversal of the ShadowedScene and set up the rendering for this ShadowTechnique.*/
44 virtual void cull(osgUtil::CullVisitor& cv);
45
46 /** Resize any per context GLObject buffers to specified size. */
47 virtual void resizeGLObjectBuffers(unsigned int maxSize);
48
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;
53
54 /** Clean scene graph from any shadow technique specific nodes, state and drawables.*/
55 virtual void cleanSceneGraph();
56
57
58 struct OSGSHADOW_EXPORT Frustum
59 {
60 Frustum(osgUtil::CullVisitor* cv, double minZNear, double maxZFar);
61
62 osg::Matrixd projectionMatrix;
63 osg::Matrixd modelViewMatrix;
64
65 typedef std::vector<osg::Vec3d> Vertices;
66 Vertices corners;
67
68 typedef std::vector<unsigned int> Indices;
69 typedef std::vector<Indices> Faces;
70 Faces faces;
71
72 typedef std::vector<Indices> Edges;
73 Edges edges;
74
75 osg::Vec3d eye;
76 osg::Vec3d centerNearPlane;
77 osg::Vec3d centerFarPlane;
78 osg::Vec3d center;
79 osg::Vec3d frustumCenterLine;
80 };
81
82 // forward declare
83 class ViewDependentData;
84
85 struct OSGSHADOW_EXPORT LightData : public osg::Referenced
86 {
87 LightData(ViewDependentData* vdd);
88
89 virtual void setLightData(osg::RefMatrix* lm, const osg::Light* l, const osg::Matrixd& modelViewMatrix);
90
91 ViewDependentData* _viewDependentData;
92
93 osg::ref_ptr<osg::RefMatrix> lightMatrix;
94 osg::ref_ptr<const osg::Light> light;
95
96 osg::Vec4d lightPos;
97 osg::Vec3d lightPos3;
98 osg::Vec3d lightDir;
99 bool directionalLight;
100
101 typedef std::vector<unsigned int> ActiveTextureUnits;
102 ActiveTextureUnits textureUnits;
103 };
104
105 typedef std::list< osg::ref_ptr<LightData> > LightDataList;
106
107 struct OSGSHADOW_EXPORT ShadowData : public osg::Referenced
108 {
109 ShadowData(ViewDependentData* vdd);
110
111 virtual void releaseGLObjects(osg::State* = 0) const;
112
113 ViewDependentData* _viewDependentData;
114
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;
119 };
120
121 typedef std::list< osg::ref_ptr<ShadowData> > ShadowDataList;
122
123
124 class OSGSHADOW_EXPORT ViewDependentData : public osg::Referenced
125 {
126 public:
127 ViewDependentData(ViewDependentShadowMap* vdsm);
128
129 const ViewDependentShadowMap* getViewDependentShadowMap() const { return _viewDependentShadowMap; }
130
131 LightDataList& getLightDataList() { return _lightDataList; }
132
133 ShadowDataList& getShadowDataList() { return _shadowDataList; }
134
135 osg::StateSet* getStateSet() { return _stateset.get(); }
136
137 virtual void releaseGLObjects(osg::State* = 0) const;
138
139 protected:
140 virtual ~ViewDependentData() {}
141
142 ViewDependentShadowMap* _viewDependentShadowMap;
143
144 osg::ref_ptr<osg::StateSet> _stateset;
145
146 LightDataList _lightDataList;
147 ShadowDataList _shadowDataList;
148 };
149
150 virtual ViewDependentData* createViewDependentData(osgUtil::CullVisitor* cv);
151
152 ViewDependentData* getViewDependentData(osgUtil::CullVisitor* cv);
153
154
155
156 virtual void createShaders();
157
158 virtual bool selectActiveLights(osgUtil::CullVisitor* cv, ViewDependentData* vdd) const;
159
160 virtual osg::Polytope computeLightViewFrustumPolytope(Frustum& frustum, LightData& positionedLight);
161
162 virtual bool computeShadowCameraSettings(Frustum& frustum, LightData& positionedLight, osg::Matrixd& projectionMatrix, osg::Matrixd& viewMatrix);
163
164 virtual bool adjustPerspectiveShadowMapCameraSettings(osgUtil::RenderStage* renderStage, Frustum& frustum, LightData& positionedLight, osg::Camera* camera);
165
166 virtual bool assignTexGenSettings(osgUtil::CullVisitor* cv, osg::Camera* camera, unsigned int textureUnit, osg::TexGen* texgen);
167
168 virtual void cullShadowReceivingScene(osgUtil::CullVisitor* cv) const;
169
170 virtual void cullShadowCastingScene(osgUtil::CullVisitor* cv, osg::Camera* camera) const;
171
172 virtual osg::StateSet* selectStateSetForRenderingShadow(ViewDependentData& vdd) const;
173
174protected:
175 virtual ~ViewDependentShadowMap();
176
177 typedef std::map< osgUtil::CullVisitor*, osg::ref_ptr<ViewDependentData> > ViewDependentDataMap;
178 mutable OpenThreads::Mutex _viewDependentDataMapMutex;
179 ViewDependentDataMap _viewDependentDataMap;
180
181 osg::ref_ptr<osg::StateSet> _shadowRecievingPlaceholderStateSet;
182
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;
187
188 typedef std::vector< osg::ref_ptr<osg::Uniform> > Uniforms;
189 mutable OpenThreads::Mutex _accessUniformsAndProgramMutex;
190 Uniforms _uniforms;
191 osg::ref_ptr<osg::Program> _program;
192};
193
194}
195
196#endif