openscenegraph
ShadowMap
Go to the documentation of this file.
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 OSGSHADOW_SHADOWEMAP
15#define OSGSHADOW_SHADOWEMAP 1
16
17#include <osg/Camera>
18#include <osg/Material>
19#include <osg/MatrixTransform>
20#include <osg/LightSource>
21
22#include <osgShadow/ShadowTechnique>
23
24namespace osgShadow {
25
26/** ShadowedTexture provides an implementation of shadow textures.*/
27class OSGSHADOW_EXPORT ShadowMap : public ShadowTechnique
28{
29 public :
30 ShadowMap();
31
32 ShadowMap(const ShadowMap& es, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
33
34 META_Object(osgShadow, ShadowMap);
35
36 /** Set the texture unit that the shadow texture will be applied on.*/
37 void setTextureUnit(unsigned int unit);
38
39 /** Get the texture unit that the shadow texture will be applied on.*/
40 unsigned int getTextureUnit() const { return _shadowTextureUnit; }
41
42 /** set the polygon offset used initially */
43 void setPolygonOffset(const osg::Vec2& polyOffset);
44
45 /** get the used polygon offset */
46 const osg::Vec2& getPolygonOffset() const { return _polyOffset; }
47
48 /** Set the values for the ambient bias the shader will use.*/
49 void setAmbientBias(const osg::Vec2& ambientBias );
50
51 /** Get the values that are used for the ambient bias in the shader.*/
52 const osg::Vec2& getAmbientBias() const { return _ambientBias; }
53
54 /** set the size in pixels x / y for the shadow texture.*/
55 void setTextureSize(const osg::Vec2s& textureSize);
56
57 /** Get the values that are used for the ambient bias in the shader.*/
58 const osg::Vec2s& getTextureSize() const { return _textureSize; }
59
60 /** Set the Light that will cast shadows */
61 void setLight(osg::Light* light);
62 void setLight(osg::LightSource* ls);
63
64 typedef std::vector< osg::ref_ptr<osg::Uniform> > UniformList;
65
66 typedef std::vector< osg::ref_ptr<osg::Shader> > ShaderList;
67
68 /** Add a shader to internal list, will be used instead of the default ones */
69 inline void addShader(osg::Shader* shader) { _shaderList.push_back(shader); }
70
71 template<class T> void addShader( const osg::ref_ptr<T>& shader ) { addShader(shader.get()); }
72
73 /** Reset internal shader list */
74 inline void clearShaderList() { _shaderList.clear(); }
75
76 /** initialize the ShadowedScene and local cached data structures.*/
77 virtual void init();
78
79 /** run the update traversal of the ShadowedScene and update any loca chached data structures.*/
80 virtual void update(osg::NodeVisitor& nv);
81
82 /** run the cull traversal of the ShadowedScene and set up the rendering for this ShadowTechnique.*/
83 virtual void cull(osgUtil::CullVisitor& cv);
84
85 /** Clean scene graph from any shadow technique specific nodes, state and drawables.*/
86 virtual void cleanSceneGraph();
87
88
89 /** Resize any per context GLObject buffers to specified size. */
90 virtual void resizeGLObjectBuffers(unsigned int maxSize);
91
92 /** If State is non-zero, this function releases any associated OpenGL objects for
93 * the specified graphics context. Otherwise, releases OpenGL objects
94 * for all graphics contexts. */
95 virtual void releaseGLObjects(osg::State* = 0) const;
96
97
98 // debug methods
99
100 osg::ref_ptr<osg::Camera> makeDebugHUD();
101
102 protected:
103 virtual ~ShadowMap(void) {};
104
105 /** Create the managed Uniforms */
106 virtual void createUniforms();
107
108 virtual void createShaders();
109
110 // forward declare, interface and implementation provided in ShadowMap.cpp
111 class DrawableDrawWithDepthShadowComparisonOffCallback;
112
113 osg::ref_ptr<osg::Camera> _camera;
114 osg::ref_ptr<osg::TexGen> _texgen;
115 osg::ref_ptr<osg::Texture2D> _texture;
116 osg::ref_ptr<osg::StateSet> _stateset;
117 osg::ref_ptr<osg::Program> _program;
118 osg::ref_ptr<osg::Light> _light;
119
120 osg::ref_ptr<osg::LightSource> _ls;
121
122 osg::ref_ptr<osg::Uniform> _ambientBiasUniform;
123 UniformList _uniformList;
124 ShaderList _shaderList;
125 unsigned int _baseTextureUnit;
126 unsigned int _shadowTextureUnit;
127 osg::Vec2 _polyOffset;
128 osg::Vec2 _ambientBias;
129 osg::Vec2s _textureSize;
130
131 };
132
133}
134
135#endif