StandardShadowMap 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #ifndef OSGSHADOW_STANDARDSHADOWMAP
  17. #define OSGSHADOW_STANDARDSHADOWMAP 1
  18. #include <osgShadow/DebugShadowMap>
  19. namespace osgShadow {
  20. class OSGSHADOW_EXPORT StandardShadowMap : public DebugShadowMap
  21. {
  22. public :
  23. /** Convenient typedef used in definition of ViewData struct and methods */
  24. typedef StandardShadowMap ThisClass;
  25. /** Convenient typedef used in definition of ViewData struct and methods */
  26. typedef DebugShadowMap BaseClass;
  27. /** Classic OSG constructor */
  28. StandardShadowMap();
  29. /** Classic OSG cloning constructor */
  30. StandardShadowMap(const StandardShadowMap& ssm,
  31. const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  32. /** Declaration of standard OSG object methods */
  33. META_Object( osgShadow, StandardShadowMap );
  34. void setBaseTextureUnit( unsigned int unit )
  35. { _baseTextureUnit = unit; dirty(); }
  36. unsigned int getBaseTextureUnit( void ) const
  37. { return _baseTextureUnit; }
  38. void setShadowTextureUnit( unsigned int unit )
  39. { _shadowTextureUnit = unit; dirty(); }
  40. unsigned int getShadowTextureUnit( void ) const
  41. { return _shadowTextureUnit; }
  42. // Texture Indices are changed by search and replace on shader source
  43. // Carefully order these calls when changing both base and shadow indices
  44. // In worst case when intend to swap indices
  45. // one will have to call these methods more than once
  46. // with one extra pass to change index to unused value to avoid
  47. // unwanted superfluous replace:
  48. //
  49. // Example: Imagine we want to swap base(0) and shadow(1) indices:
  50. // We have to do an extra step to make sure both do not end up as 1
  51. //
  52. // // initially change base to something else than 1
  53. // setBaseTextureCoordIndex( 100 );
  54. // // now search and replace all gl_TexCord[1] to gl_TexCord[0]
  55. // setShadowTextureCoordIndex( 0 );
  56. // // finally change base from 100 to 0
  57. // setBaseTextureCoordIndex( 1 );
  58. void setBaseTextureCoordIndex( unsigned int index )
  59. { updateTextureCoordIndices( _baseTextureCoordIndex, index );
  60. _baseTextureCoordIndex = index; }
  61. unsigned int getBaseTextureCoordIndex( void ) const
  62. { return _baseTextureCoordIndex; }
  63. // Texture Indices are changed by search and replace on shader source
  64. // Look at the comment above setBaseTextureCoordIndex
  65. void setShadowTextureCoordIndex( unsigned int index )
  66. { updateTextureCoordIndices( _shadowTextureCoordIndex, index );
  67. _shadowTextureCoordIndex = index; }
  68. unsigned int getShadowTextureCoordIndex( void ) const
  69. { return _shadowTextureCoordIndex; }
  70. void setTextureSize( const osg::Vec2s& textureSize )
  71. { _textureSize = textureSize; dirty(); }
  72. const osg::Vec2s& getTextureSize() const
  73. { return _textureSize; }
  74. void setLight( osg::Light* light )
  75. { _light = light; }
  76. osg::Light* getLight( void )
  77. { return _light.get(); }
  78. const osg::Light* getLight( void ) const
  79. { return _light.get(); }
  80. osg::Shader * getShadowVertexShader()
  81. { return _shadowVertexShader.get(); }
  82. osg::Shader * getShadowFragmentShader()
  83. { return _shadowFragmentShader.get(); }
  84. osg::Shader * getMainVertexShader( )
  85. { return _mainVertexShader.get(); }
  86. osg::Shader * getMainFragmentShader( )
  87. { return _mainFragmentShader.get(); }
  88. void setShadowVertexShader( osg::Shader * shader )
  89. { _shadowVertexShader = shader; }
  90. void setShadowFragmentShader( osg::Shader * shader )
  91. { _shadowFragmentShader = shader; }
  92. void setMainVertexShader( osg::Shader * shader )
  93. { _mainVertexShader = shader; }
  94. void setMainFragmentShader( osg::Shader * shader )
  95. { _mainFragmentShader = shader; }
  96. /** Resize any per context GLObject buffers to specified size. */
  97. virtual void resizeGLObjectBuffers(unsigned int maxSize);
  98. /** If State is non-zero, this function releases any associated OpenGL objects for
  99. * the specified graphics context. Otherwise, releases OpenGL objects
  100. * for all graphics contexts. */
  101. virtual void releaseGLObjects(osg::State* = 0) const;
  102. protected:
  103. /** Classic protected OSG destructor */
  104. virtual ~StandardShadowMap(void);
  105. virtual void updateTextureCoordIndices
  106. ( unsigned int baseTexCoordIndex, unsigned int shadowTexCoordIndex );
  107. virtual void searchAndReplaceShaderSource
  108. ( osg::Shader*, std::string fromString, std::string toString );
  109. osg::ref_ptr< osg::Shader > _mainVertexShader;
  110. osg::ref_ptr< osg::Shader > _mainFragmentShader;
  111. osg::ref_ptr< osg::Shader > _shadowVertexShader;
  112. osg::ref_ptr< osg::Shader > _shadowFragmentShader;
  113. osg::ref_ptr< osg::Light > _light;
  114. float _polygonOffsetFactor;
  115. float _polygonOffsetUnits;
  116. osg::Vec2s _textureSize;
  117. unsigned int _baseTextureUnit;
  118. unsigned int _shadowTextureUnit;
  119. unsigned int _baseTextureCoordIndex;
  120. unsigned int _shadowTextureCoordIndex;
  121. struct OSGSHADOW_EXPORT ViewData: public BaseClass::ViewData
  122. {
  123. osg::ref_ptr< osg::Light > * _lightPtr;
  124. unsigned int * _baseTextureUnitPtr;
  125. unsigned int * _shadowTextureUnitPtr;
  126. // ShadowMap texture is defined by base DebugShadowMap
  127. // osg::ref_ptr<osg::Texture2D> _texture;
  128. // ShadowMap camera is defined by base DebugShadowMap
  129. // osg::ref_ptr<osg::Camera> _camera;
  130. osg::ref_ptr<osg::TexGen> _texgen;
  131. osg::ref_ptr<osg::StateSet> _stateset;
  132. virtual void init( ThisClass * st, osgUtil::CullVisitor * cv );
  133. virtual void cull( );
  134. virtual void aimShadowCastingCamera(
  135. const osg::BoundingSphere &bounds,
  136. const osg::Light *light,
  137. const osg::Vec4 &worldLightPos,
  138. const osg::Vec3 &worldLightDir,
  139. const osg::Vec3 &worldLightUp = osg::Vec3(0,1,0) );
  140. virtual void cullShadowReceivingScene( );
  141. virtual void cullShadowCastingScene( );
  142. virtual void addShadowReceivingTexGen( );
  143. virtual const osg::Light* selectLight( osg::Vec4 &viewLightPos,
  144. osg::Vec3 &viewLightDir );
  145. virtual void aimShadowCastingCamera( const osg::Light *light,
  146. const osg::Vec4 &worldLightPos,
  147. const osg::Vec3 &worldLightDir,
  148. const osg::Vec3 &worldLightUp
  149. = osg::Vec3(0,1,0) );
  150. virtual void resizeGLObjectBuffers(unsigned int maxSize);
  151. virtual void releaseGLObjects(osg::State* = 0) const;
  152. };
  153. friend struct ViewData;
  154. META_ViewDependentShadowTechniqueData( ThisClass, ThisClass::ViewData )
  155. };
  156. } // namespace osgShadow
  157. #endif