MinimalShadowMap 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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_MINIMALSHADOWMAP
  17. #define OSGSHADOW_MINIMALSHADOWMAP 1
  18. #include <osgShadow/StandardShadowMap>
  19. namespace osgShadow {
  20. class OSGSHADOW_EXPORT MinimalShadowMap : public StandardShadowMap
  21. {
  22. public :
  23. /** Convenient typedef used in definition of ViewData struct and methods */
  24. typedef MinimalShadowMap ThisClass;
  25. /** Convenient typedef used in definition of ViewData struct and methods */
  26. typedef StandardShadowMap BaseClass;
  27. /** Classic OSG constructor */
  28. MinimalShadowMap();
  29. /** Classic OSG cloning constructor */
  30. MinimalShadowMap(
  31. const MinimalShadowMap& msm,
  32. const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  33. /** Declaration of standard OSG object methods */
  34. META_Object( osgShadow, MinimalShadowMap );
  35. void setModellingSpaceToWorldTransform( const osg::Matrix & modellingSpaceToWorld )
  36. { _modellingSpaceToWorld = modellingSpaceToWorld; }
  37. const osg::Matrix & getModellingSpaceToWorldTransform( void ) const
  38. { return _modellingSpaceToWorld; }
  39. float getMaxFarPlane( ) const
  40. { return _maxFarPlane; }
  41. void setMaxFarPlane( float maxFarPlane )
  42. { _maxFarPlane = maxFarPlane; }
  43. float getMinLightMargin( ) const
  44. { return _minLightMargin; }
  45. void setMinLightMargin( float minLightMargin )
  46. { _minLightMargin = minLightMargin; }
  47. enum ShadowReceivingCoarseBoundAccuracy {
  48. EMPTY_BOX,
  49. BOUNDING_SPHERE,
  50. BOUNDING_BOX,
  51. DEFAULT_ACCURACY = BOUNDING_BOX
  52. };
  53. void setShadowReceivingCoarseBoundAccuracy
  54. ( ShadowReceivingCoarseBoundAccuracy accuracy )
  55. { _shadowReceivingCoarseBoundAccuracy = accuracy; }
  56. ShadowReceivingCoarseBoundAccuracy
  57. getShadowReceivingCoarseBoundAccuracy() const
  58. { return _shadowReceivingCoarseBoundAccuracy; }
  59. protected:
  60. /** Classic protected OSG destructor */
  61. virtual ~MinimalShadowMap(void);
  62. protected:
  63. // Matrix modellingSpaceToWorld and its inverse
  64. // are used to define Modelling Space where shadowed scene drawables
  65. // have minimal (smallest possible extent) bounding boxes.
  66. // Computing visible shadow range in this space
  67. // allows for optimal use of ShadowMap resolution.
  68. // By default it is set to identity ie computations are in world space.
  69. // But it should be set to ElipsoidModel::localToWorld
  70. // when scene objects are put on earth ellipsoid surface.
  71. // Other scenarios are also possible for example when models are
  72. // built in XZY space which would require identity matrix with swapped columns
  73. osg::Matrix _modellingSpaceToWorld;
  74. float _maxFarPlane;
  75. float _minLightMargin;
  76. ShadowReceivingCoarseBoundAccuracy _shadowReceivingCoarseBoundAccuracy;
  77. struct OSGSHADOW_EXPORT ViewData: public BaseClass::ViewData
  78. {
  79. osg::Matrix *_modellingSpaceToWorldPtr;
  80. float *_maxFarPlanePtr;
  81. float *_minLightMarginPtr;
  82. int _frameShadowCastingCameraPasses;
  83. ConvexPolyhedron _sceneReceivingShadowPolytope;
  84. std::vector< osg::Vec3d > _sceneReceivingShadowPolytopePoints;
  85. osg::Matrixd _clampedProjection;
  86. virtual void init( ThisClass * st, osgUtil::CullVisitor * cv );
  87. virtual osg::BoundingBox computeShadowReceivingCoarseBounds( );
  88. virtual void cullShadowReceivingScene( );
  89. virtual void aimShadowCastingCamera(
  90. const osg::BoundingSphere &bounds,
  91. const osg::Light *light,
  92. const osg::Vec4 &worldLightPos,
  93. const osg::Vec3 &worldLightDir,
  94. const osg::Vec3 &worldLightUp = osg::Vec3(0,1,0) );
  95. virtual void aimShadowCastingCamera( const osg::Light *light,
  96. const osg::Vec4 &worldLightPos,
  97. const osg::Vec3 &worldLightDir,
  98. const osg::Vec3 &worldLightUp
  99. = osg::Vec3(0,1,0) );
  100. virtual void frameShadowCastingCamera
  101. ( const osg::Camera* cameraMain, osg::Camera* cameraShadow, int pass = 1 );
  102. void cutScenePolytope( const osg::Matrix & matrix,
  103. const osg::Matrix & inverse,
  104. const osg::BoundingBox &bb =
  105. osg::BoundingBox(-1,-1,-1,1,1,1) );
  106. osg::BoundingBox computeScenePolytopeBounds();
  107. osg::BoundingBox computeScenePolytopeBounds(const osg::Matrix& m);
  108. // Utility methods for adjusting projection matrices
  109. // Modify projection matrix so that some output subrange
  110. // is remapped to whole clip space (-1..1,-1..1,-1..1).
  111. // Bit mask can be used to limit remaping to selected bounds only.
  112. static void trimProjection
  113. ( osg::Matrixd & projection, osg::BoundingBox subrange,
  114. unsigned int trimMask = (1|2|4|8|16|32)
  115. /*1=left|2=right|4=bottom|8=top|16=near|32=far*/);
  116. static void clampProjection
  117. ( osg::Matrixd & projection, float n = 0, float f = FLT_MAX );
  118. static void extendProjection
  119. ( osg::Matrixd & projection, osg::Viewport * viewport, const osg::Vec2& margin );
  120. };
  121. META_ViewDependentShadowTechniqueData( ThisClass, ThisClass::ViewData )
  122. };
  123. } // namespace osgShadow
  124. #endif