OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
StandardShadowMap.h
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  * ViewDependentShadow codes Copyright (C) 2008 Wojciech Lewandowski
14  * Thanks to to my company http://www.ai.com.pl for allowing me free this work.
15 */
16 
17 #ifndef OSGSHADOW_STANDARDSHADOWMAP
18 #define OSGSHADOW_STANDARDSHADOWMAP 1
19 
20 #include <osgShadow/DebugShadowMap>
21 
22 namespace osgShadow {
23 
25 {
26  public :
31 
34 
38 
41 
42  void setBaseTextureUnit( unsigned int unit )
43  { _baseTextureUnit = unit; dirty(); }
44 
45  unsigned int getBaseTextureUnit( void ) const
46  { return _baseTextureUnit; }
47 
48  void setShadowTextureUnit( unsigned int unit )
49  { _shadowTextureUnit = unit; dirty(); }
50 
51  unsigned int getShadowTextureUnit( void ) const
52  { return _shadowTextureUnit; }
53 
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:
60  //
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
63  //
64  // // initialy 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 );
70 
71  void setBaseTextureCoordIndex( unsigned int index )
72  { updateTextureCoordIndices( _baseTextureCoordIndex, index );
73  _baseTextureCoordIndex = index; }
74 
75  unsigned int getBaseTextureCoordIndex( void ) const
76  { return _baseTextureCoordIndex; }
77 
78  // Texture Indices are changed by search and replace on shader source
79  // Look at the comment above setBaseTextureCoordIndex
80 
81  void setShadowTextureCoordIndex( unsigned int index )
82  { updateTextureCoordIndices( _shadowTextureCoordIndex, index );
83  _shadowTextureCoordIndex = index; }
84 
85  unsigned int getShadowTextureCoordIndex( void ) const
86  { return _shadowTextureCoordIndex; }
87 
88  void setTextureSize( const osg::Vec2s& textureSize )
89  { _textureSize = textureSize; dirty(); }
90 
91  const osg::Vec2s& getTextureSize() const
92  { return _textureSize; }
93 
94  void setLight( osg::Light* light )
95  { _light = light; }
96 
98  { return _light.get(); }
99 
100  const osg::Light* getLight( void ) const
101  { return _light.get(); }
102 
104  { return _shadowVertexShader.get(); }
105 
107  { return _shadowFragmentShader.get(); }
108 
110  { return _mainVertexShader.get(); }
111 
113  { return _mainFragmentShader.get(); }
114 
116  { _shadowVertexShader = shader; }
117 
119  { _shadowFragmentShader = shader; }
120 
122  { _mainVertexShader = shader; }
123 
125  { _mainFragmentShader = shader; }
126 
127  protected:
129  virtual ~StandardShadowMap(void);
130 
131  virtual void updateTextureCoordIndices
132  ( unsigned int baseTexCoordIndex, unsigned int shadowTexCoordIndex );
133 
134  virtual void searchAndReplaceShaderSource
135  ( osg::Shader*, std::string fromString, std::string toString );
136 
141 
146  unsigned int _baseTextureUnit;
147  unsigned int _shadowTextureUnit;
150 
152  {
154  unsigned int * _baseTextureUnitPtr;
155  unsigned int * _shadowTextureUnitPtr;
156 
157  // ShadowMap texture is defined by base DebugShadowMap
158  // osg::ref_ptr<osg::Texture2D> _texture;
159 
160  // ShadowMap camera is defined by base DebugShadowMap
161  // osg::ref_ptr<osg::Camera> _camera;
162 
165 
166  virtual void init( ThisClass * st, osgUtil::CullVisitor * cv );
167 
168  virtual void cull( );
169 
170  virtual void aimShadowCastingCamera(
171  const osg::BoundingSphere &bounds,
172  const osg::Light *light,
173  const osg::Vec4 &worldLightPos,
174  const osg::Vec3 &worldLightDir,
175  const osg::Vec3 &worldLightUp = osg::Vec3(0,1,0) );
176 
177  virtual void cullShadowReceivingScene( );
178 
179  virtual void cullShadowCastingScene( );
180 
181  virtual void addShadowReceivingTexGen( );
182 
183  virtual const osg::Light* selectLight( osg::Vec4 &viewLightPos,
184  osg::Vec3 &viewLightDir );
185 
186  virtual void aimShadowCastingCamera( const osg::Light *light,
187  const osg::Vec4 &worldLightPos,
188  const osg::Vec3 &worldLightDir,
189  const osg::Vec3 &worldLightUp
190  = osg::Vec3(0,1,0) );
191  };
192 
193  friend struct ViewData;
194 
196 };
197 
198 } // namespace osgShadow
199 
200 #endif
unsigned int getShadowTextureUnit(void) const
osg::ref_ptr< osg::Shader > _shadowVertexShader
osg::ref_ptr< osg::Shader > _mainFragmentShader
osg::ref_ptr< osg::Light > _light
unsigned int getBaseTextureUnit(void) const
osg::ref_ptr< osg::Light > * _lightPtr
#define META_ViewDependentShadowTechniqueData(ShadowTechnique, TechniqueData)
void setShadowFragmentShader(osg::Shader *shader)
void setBaseTextureCoordIndex(unsigned int index)
osg::ref_ptr< osg::TexGen > _texgen
void setMainFragmentShader(osg::Shader *shader)
#define OSGSHADOW_EXPORT
Definition: Export.h:39
#define META_Object(library, name)
Definition: Object.h:42
unsigned int getBaseTextureCoordIndex(void) const
osg::ref_ptr< osg::Shader > _shadowFragmentShader
void setMainVertexShader(osg::Shader *shader)
osg::Shader * getShadowFragmentShader()
void setShadowVertexShader(osg::Shader *shader)
const osg::Vec2s & getTextureSize() const
void setTextureSize(const osg::Vec2s &textureSize)
unsigned int getShadowTextureCoordIndex(void) const
osg::ref_ptr< osg::StateSet > _stateset
void setBaseTextureUnit(unsigned int unit)
void setShadowTextureUnit(unsigned int unit)
const osg::Light * getLight(void) const
osg::ref_ptr< osg::Shader > _mainVertexShader
void setShadowTextureCoordIndex(unsigned int index)
void setLight(osg::Light *light)