1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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.
13 * ViewDependentShadow codes Copyright (C) 2008 Wojciech Lewandowski
14 * Thanks to to my company http://www.ai.com.pl for allowing me free this work.
17#ifndef OSGSHADOW_STANDARDSHADOWMAP
18#define OSGSHADOW_STANDARDSHADOWMAP 1
20#include <osgShadow/DebugShadowMap>
24class OSGSHADOW_EXPORT StandardShadowMap : public DebugShadowMap
27 /** Convenient typedef used in definition of ViewData struct and methods */
28 typedef StandardShadowMap ThisClass;
29 /** Convenient typedef used in definition of ViewData struct and methods */
30 typedef DebugShadowMap BaseClass;
32 /** Classic OSG constructor */
35 /** Classic OSG cloning constructor */
36 StandardShadowMap(const StandardShadowMap& ssm,
37 const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
39 /** Declaration of standard OSG object methods */
40 META_Object( osgShadow, StandardShadowMap );
42 void setBaseTextureUnit( unsigned int unit )
43 { _baseTextureUnit = unit; dirty(); }
45 unsigned int getBaseTextureUnit( void ) const
46 { return _baseTextureUnit; }
48 void setShadowTextureUnit( unsigned int unit )
49 { _shadowTextureUnit = unit; dirty(); }
51 unsigned int getShadowTextureUnit( void ) const
52 { return _shadowTextureUnit; }
54 // Texture Indices are changed by search and replace on shader source
55 // Carefully order these calls when changing both base and shadow indices
56 // In worst case when intend to swap indices
57 // one will have to call these methods more than once
58 // with one extra pass to change index to unused value to avoid
59 // unwanted superfluous replace:
61 // Example: Imagine we want to swap base(0) and shadow(1) indices:
62 // We have to do an extra step to make sure both do not end up as 1
64 // // initially change base to something else than 1
65 // setBaseTextureCoordIndex( 100 );
66 // // now search and replace all gl_TexCord[1] to gl_TexCord[0]
67 // setShadowTextureCoordIndex( 0 );
68 // // finally change base from 100 to 0
69 // setBaseTextureCoordIndex( 1 );
71 void setBaseTextureCoordIndex( unsigned int index )
72 { updateTextureCoordIndices( _baseTextureCoordIndex, index );
73 _baseTextureCoordIndex = index; }
75 unsigned int getBaseTextureCoordIndex( void ) const
76 { return _baseTextureCoordIndex; }
78 // Texture Indices are changed by search and replace on shader source
79 // Look at the comment above setBaseTextureCoordIndex
81 void setShadowTextureCoordIndex( unsigned int index )
82 { updateTextureCoordIndices( _shadowTextureCoordIndex, index );
83 _shadowTextureCoordIndex = index; }
85 unsigned int getShadowTextureCoordIndex( void ) const
86 { return _shadowTextureCoordIndex; }
88 void setTextureSize( const osg::Vec2s& textureSize )
89 { _textureSize = textureSize; dirty(); }
91 const osg::Vec2s& getTextureSize() const
92 { return _textureSize; }
94 void setLight( osg::Light* light )
97 osg::Light* getLight( void )
98 { return _light.get(); }
100 const osg::Light* getLight( void ) const
101 { return _light.get(); }
103 osg::Shader * getShadowVertexShader()
104 { return _shadowVertexShader.get(); }
106 osg::Shader * getShadowFragmentShader()
107 { return _shadowFragmentShader.get(); }
109 osg::Shader * getMainVertexShader( )
110 { return _mainVertexShader.get(); }
112 osg::Shader * getMainFragmentShader( )
113 { return _mainFragmentShader.get(); }
115 void setShadowVertexShader( osg::Shader * shader )
116 { _shadowVertexShader = shader; }
118 void setShadowFragmentShader( osg::Shader * shader )
119 { _shadowFragmentShader = shader; }
121 void setMainVertexShader( osg::Shader * shader )
122 { _mainVertexShader = shader; }
124 void setMainFragmentShader( osg::Shader * shader )
125 { _mainFragmentShader = shader; }
128 /** Resize any per context GLObject buffers to specified size. */
129 virtual void resizeGLObjectBuffers(unsigned int maxSize);
131 /** If State is non-zero, this function releases any associated OpenGL objects for
132 * the specified graphics context. Otherwise, releases OpenGL objects
133 * for all graphics contexts. */
134 virtual void releaseGLObjects(osg::State* = 0) const;
137 /** Classic protected OSG destructor */
138 virtual ~StandardShadowMap(void);
140 virtual void updateTextureCoordIndices
141 ( unsigned int baseTexCoordIndex, unsigned int shadowTexCoordIndex );
143 virtual void searchAndReplaceShaderSource
144 ( osg::Shader*, std::string fromString, std::string toString );
146 osg::ref_ptr< osg::Shader > _mainVertexShader;
147 osg::ref_ptr< osg::Shader > _mainFragmentShader;
148 osg::ref_ptr< osg::Shader > _shadowVertexShader;
149 osg::ref_ptr< osg::Shader > _shadowFragmentShader;
151 osg::ref_ptr< osg::Light > _light;
152 float _polygonOffsetFactor;
153 float _polygonOffsetUnits;
154 osg::Vec2s _textureSize;
155 unsigned int _baseTextureUnit;
156 unsigned int _shadowTextureUnit;
157 unsigned int _baseTextureCoordIndex;
158 unsigned int _shadowTextureCoordIndex;
160 struct OSGSHADOW_EXPORT ViewData: public BaseClass::ViewData
162 osg::ref_ptr< osg::Light > * _lightPtr;
163 unsigned int * _baseTextureUnitPtr;
164 unsigned int * _shadowTextureUnitPtr;
166 // ShadowMap texture is defined by base DebugShadowMap
167 // osg::ref_ptr<osg::Texture2D> _texture;
169 // ShadowMap camera is defined by base DebugShadowMap
170 // osg::ref_ptr<osg::Camera> _camera;
172 osg::ref_ptr<osg::TexGen> _texgen;
173 osg::ref_ptr<osg::StateSet> _stateset;
175 virtual void init( ThisClass * st, osgUtil::CullVisitor * cv );
177 virtual void cull( );
179 virtual void aimShadowCastingCamera(
180 const osg::BoundingSphere &bounds,
181 const osg::Light *light,
182 const osg::Vec4 &worldLightPos,
183 const osg::Vec3 &worldLightDir,
184 const osg::Vec3 &worldLightUp = osg::Vec3(0,1,0) );
186 virtual void cullShadowReceivingScene( );
188 virtual void cullShadowCastingScene( );
190 virtual void addShadowReceivingTexGen( );
192 virtual const osg::Light* selectLight( osg::Vec4 &viewLightPos,
193 osg::Vec3 &viewLightDir );
195 virtual void aimShadowCastingCamera( const osg::Light *light,
196 const osg::Vec4 &worldLightPos,
197 const osg::Vec3 &worldLightDir,
198 const osg::Vec3 &worldLightUp
199 = osg::Vec3(0,1,0) );
201 virtual void resizeGLObjectBuffers(unsigned int maxSize);
202 virtual void releaseGLObjects(osg::State* = 0) const;
205 friend struct ViewData;
207 META_ViewDependentShadowTechniqueData( ThisClass, ThisClass::ViewData )
210} // namespace osgShadow