ShadowMap 5.0 KB

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