ViewDependentShadowMap 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2011 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_VIEWDEPENDENTSHADOWMAP
  14. #define OSGSHADOW_VIEWDEPENDENTSHADOWMAP 1
  15. #include <osg/Camera>
  16. #include <osg/Material>
  17. #include <osg/MatrixTransform>
  18. #include <osg/LightSource>
  19. #include <osg/PolygonOffset>
  20. #include <osgShadow/ShadowTechnique>
  21. namespace osgShadow {
  22. /** ViewDependentShadowMap provides an base implementation of view dependent shadow mapping techniques.*/
  23. class OSGSHADOW_EXPORT ViewDependentShadowMap : public ShadowTechnique
  24. {
  25. public :
  26. ViewDependentShadowMap();
  27. ViewDependentShadowMap(const ViewDependentShadowMap& vdsm, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  28. META_Object(osgShadow, ViewDependentShadowMap);
  29. /** initialize the ShadowedScene and local cached data structures.*/
  30. virtual void init();
  31. /** run the update traversal of the ShadowedScene and update any loca chached data structures.*/
  32. virtual void update(osg::NodeVisitor& nv);
  33. /** run the cull traversal of the ShadowedScene and set up the rendering for this ShadowTechnique.*/
  34. virtual void cull(osgUtil::CullVisitor& cv);
  35. /** Resize any per context GLObject buffers to specified size. */
  36. virtual void resizeGLObjectBuffers(unsigned int maxSize);
  37. /** If State is non-zero, this function releases any associated OpenGL objects for
  38. * the specified graphics context. Otherwise, releases OpenGL objects
  39. * for all graphics contexts. */
  40. virtual void releaseGLObjects(osg::State* = 0) const;
  41. /** Clean scene graph from any shadow technique specific nodes, state and drawables.*/
  42. virtual void cleanSceneGraph();
  43. struct OSGSHADOW_EXPORT Frustum
  44. {
  45. Frustum(osgUtil::CullVisitor* cv, double minZNear, double maxZFar);
  46. osg::Matrixd projectionMatrix;
  47. osg::Matrixd modelViewMatrix;
  48. typedef std::vector<osg::Vec3d> Vertices;
  49. Vertices corners;
  50. typedef std::vector<unsigned int> Indices;
  51. typedef std::vector<Indices> Faces;
  52. Faces faces;
  53. typedef std::vector<Indices> Edges;
  54. Edges edges;
  55. osg::Vec3d eye;
  56. osg::Vec3d centerNearPlane;
  57. osg::Vec3d centerFarPlane;
  58. osg::Vec3d center;
  59. osg::Vec3d frustumCenterLine;
  60. };
  61. // forward declare
  62. class ViewDependentData;
  63. struct OSGSHADOW_EXPORT LightData : public osg::Referenced
  64. {
  65. LightData(ViewDependentData* vdd);
  66. virtual void setLightData(osg::RefMatrix* lm, const osg::Light* l, const osg::Matrixd& modelViewMatrix);
  67. ViewDependentData* _viewDependentData;
  68. osg::ref_ptr<osg::RefMatrix> lightMatrix;
  69. osg::ref_ptr<const osg::Light> light;
  70. osg::Vec4d lightPos;
  71. osg::Vec3d lightPos3;
  72. osg::Vec3d lightDir;
  73. bool directionalLight;
  74. typedef std::vector<unsigned int> ActiveTextureUnits;
  75. ActiveTextureUnits textureUnits;
  76. };
  77. typedef std::list< osg::ref_ptr<LightData> > LightDataList;
  78. struct OSGSHADOW_EXPORT ShadowData : public osg::Referenced
  79. {
  80. ShadowData(ViewDependentData* vdd);
  81. virtual void releaseGLObjects(osg::State* = 0) const;
  82. ViewDependentData* _viewDependentData;
  83. unsigned int _textureUnit;
  84. osg::ref_ptr<osg::Texture2D> _texture;
  85. osg::ref_ptr<osg::TexGen> _texgen;
  86. osg::ref_ptr<osg::Camera> _camera;
  87. };
  88. typedef std::list< osg::ref_ptr<ShadowData> > ShadowDataList;
  89. class OSGSHADOW_EXPORT ViewDependentData : public osg::Referenced
  90. {
  91. public:
  92. ViewDependentData(ViewDependentShadowMap* vdsm);
  93. const ViewDependentShadowMap* getViewDependentShadowMap() const { return _viewDependentShadowMap; }
  94. LightDataList& getLightDataList() { return _lightDataList; }
  95. ShadowDataList& getShadowDataList() { return _shadowDataList; }
  96. osg::StateSet* getStateSet() { return _stateset.get(); }
  97. virtual void releaseGLObjects(osg::State* = 0) const;
  98. protected:
  99. virtual ~ViewDependentData() {}
  100. ViewDependentShadowMap* _viewDependentShadowMap;
  101. osg::ref_ptr<osg::StateSet> _stateset;
  102. LightDataList _lightDataList;
  103. ShadowDataList _shadowDataList;
  104. };
  105. virtual ViewDependentData* createViewDependentData(osgUtil::CullVisitor* cv);
  106. ViewDependentData* getViewDependentData(osgUtil::CullVisitor* cv);
  107. virtual void createShaders();
  108. virtual bool selectActiveLights(osgUtil::CullVisitor* cv, ViewDependentData* vdd) const;
  109. virtual osg::Polytope computeLightViewFrustumPolytope(Frustum& frustum, LightData& positionedLight);
  110. virtual bool computeShadowCameraSettings(Frustum& frustum, LightData& positionedLight, osg::Matrixd& projectionMatrix, osg::Matrixd& viewMatrix);
  111. virtual bool adjustPerspectiveShadowMapCameraSettings(osgUtil::RenderStage* renderStage, Frustum& frustum, LightData& positionedLight, osg::Camera* camera);
  112. virtual bool assignTexGenSettings(osgUtil::CullVisitor* cv, osg::Camera* camera, unsigned int textureUnit, osg::TexGen* texgen);
  113. virtual void cullShadowReceivingScene(osgUtil::CullVisitor* cv) const;
  114. virtual void cullShadowCastingScene(osgUtil::CullVisitor* cv, osg::Camera* camera) const;
  115. virtual osg::StateSet* selectStateSetForRenderingShadow(ViewDependentData& vdd) const;
  116. protected:
  117. virtual ~ViewDependentShadowMap();
  118. typedef std::map< osgUtil::CullVisitor*, osg::ref_ptr<ViewDependentData> > ViewDependentDataMap;
  119. mutable OpenThreads::Mutex _viewDependentDataMapMutex;
  120. ViewDependentDataMap _viewDependentDataMap;
  121. osg::ref_ptr<osg::StateSet> _shadowRecievingPlaceholderStateSet;
  122. osg::ref_ptr<osg::StateSet> _shadowCastingStateSet;
  123. osg::ref_ptr<osg::PolygonOffset> _polygonOffset;
  124. osg::ref_ptr<osg::Texture2D> _fallbackBaseTexture;
  125. osg::ref_ptr<osg::Texture2D> _fallbackShadowMapTexture;
  126. typedef std::vector< osg::ref_ptr<osg::Uniform> > Uniforms;
  127. mutable OpenThreads::Mutex _accessUniformsAndProgramMutex;
  128. Uniforms _uniforms;
  129. osg::ref_ptr<osg::Program> _program;
  130. };
  131. }
  132. #endif