MinimalDrawBoundsShadowMap 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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_MINIMALDRAWBOUNDSSHADOWMAP
  17. #define OSGSHADOW_MINIMALDRAWBOUNDSSHADOWMAP 1
  18. #include <osgShadow/MinimalShadowMap>
  19. namespace osgShadow {
  20. class OSGSHADOW_EXPORT MinimalDrawBoundsShadowMap
  21. : public MinimalShadowMap
  22. {
  23. public :
  24. /** Convenient typedef used in definition of ViewData struct and methods */
  25. typedef MinimalDrawBoundsShadowMap ThisClass;
  26. /** Convenient typedef used in definition of ViewData struct and methods */
  27. typedef MinimalShadowMap BaseClass;
  28. /** Classic OSG constructor */
  29. MinimalDrawBoundsShadowMap();
  30. /** Classic OSG cloning constructor */
  31. MinimalDrawBoundsShadowMap(
  32. const MinimalDrawBoundsShadowMap& mdbsm,
  33. const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  34. /** Declaration of standard OSG object methods */
  35. META_Object( osgShadow, MinimalDrawBoundsShadowMap );
  36. protected:
  37. /** Classic protected OSG destructor */
  38. virtual ~MinimalDrawBoundsShadowMap(void);
  39. struct OSGSHADOW_EXPORT ViewData: public BaseClass::ViewData
  40. {
  41. osg::ref_ptr< osg::RefMatrix > _projection;
  42. osg::Vec2s _boundAnalysisSize;
  43. osg::ref_ptr< osg::Image > _boundAnalysisImage;
  44. osg::ref_ptr< osg::Texture2D > _boundAnalysisTexture;
  45. osg::ref_ptr< osg::Camera > _boundAnalysisCamera;
  46. osg::observer_ptr< osg::Camera > _mainCamera;
  47. void setShadowCameraProjectionMatrixPtr( osg::RefMatrix * projection )
  48. { _projection = projection; }
  49. osg::RefMatrix * getShadowCameraProjectionMatrixPtr( void )
  50. { return _projection.get(); }
  51. virtual void init( ThisClass * st, osgUtil::CullVisitor * cv );
  52. virtual void cullShadowReceivingScene( );
  53. virtual void createDebugHUD( );
  54. virtual void recordShadowMapParams( );
  55. virtual void cullBoundAnalysisScene( );
  56. static osg::BoundingBox scanImage( const osg::Image * image, osg::Matrix m );
  57. virtual void performBoundAnalysis( const osg::Camera& camera );
  58. ViewData( void ): _boundAnalysisSize( 64, 64 ) {}
  59. virtual void resizeGLObjectBuffers(unsigned int maxSize);
  60. virtual void releaseGLObjects(osg::State* = 0) const;
  61. };
  62. friend struct ViewData;
  63. META_ViewDependentShadowTechniqueData( ThisClass, ThisClass::ViewData );
  64. struct CameraPostDrawCallback : public osg::Camera::DrawCallback {
  65. CameraPostDrawCallback( ViewData * vd ): _vd( vd )
  66. {
  67. }
  68. virtual void operator ()( const osg::Camera& camera ) const
  69. {
  70. if( _vd.valid() )
  71. _vd->performBoundAnalysis( camera );
  72. }
  73. osg::observer_ptr< ViewData > _vd;
  74. };
  75. struct CameraCullCallback: public osg::Callback {
  76. CameraCullCallback(ViewData * vd, osg::Callback * nc): _vd(vd), _nc(nc)
  77. {
  78. }
  79. virtual bool run(osg::Object* object, osg::Object* data)
  80. {
  81. osgUtil::CullVisitor *cv = dynamic_cast< osgUtil::CullVisitor *>( data );
  82. if( _nc.valid() )
  83. _nc->run(object, data);
  84. else
  85. traverse(object, data);
  86. if( cv )
  87. _vd->recordShadowMapParams( );
  88. return true;
  89. }
  90. protected:
  91. osg::observer_ptr< ViewData > _vd;
  92. osg::ref_ptr< osg::Callback > _nc;
  93. };
  94. };
  95. } // namespace osgShadow
  96. #endif