1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
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.
17#ifndef OSGSHADOW_DEBUGSHADOWMAP
18#define OSGSHADOW_DEBUGSHADOWMAP 1
20#include <osgShadow/ViewDependentShadowTechnique>
21#include <osgShadow/ConvexPolyhedron>
22#include <osg/MatrixTransform>
24#include <osg/Geometry>
31Class used as a layer for debugging resources used by derived xxxShadowMap classes.
32As designed by its base ViewDepndentShadowTechnique, DebugShadowMap serves mainly as container of
33DebugShadowMap::ViewData objects. Most of the debugging support work is done by these objects.
34DebugShadowMap technique only initializes them in initViewDependentData method.
36Debugging outputs present:
37 Shadow maps (pseudo colored to improve readability)
38 Shadow and related volumes (represented as convex polyhedra)
41class OSGSHADOW_EXPORT DebugShadowMap : public ViewDependentShadowTechnique
46 All classes stemming from ViewDependentShadowTechnique follow the same pattern.
48 They are always based on some underlying level base Technique and they always
49 derive their ViewData from ViewData structure defined in underlying base Technique.
51 I use some typedefs to make these inheritance patterns easier to declare/define.
54 /** Convenient typedef used in definition of ViewData struct and methods */
55 typedef DebugShadowMap ThisClass;
56 /** Convenient typedef used in definition of ViewData struct and methods */
57 typedef ViewDependentShadowTechnique BaseClass;
59 /** Classic OSG constructor */
62 /** Classic OSG cloning constructor */
63 DebugShadowMap(const DebugShadowMap& dsm, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
65 /** Declaration of standard OSG object methods */
66 META_Object( osgShadow, DebugShadowMap );
68 /** Turn on/off debugging hud & rendering of debug volumes in main view */
69 void setDebugDraw( bool draw ) { _doDebugDraw = draw; }
71 /** Tell if debugging hud & rendering of debug volumes is active */
72 bool getDebugDraw( void ) const { return _doDebugDraw; }
74 /** Get the file name of debugging dump */
75 std::string getDebugDump( void ) const { return _debugDump; }
77 /** Set the file name of debugging dump */
78 void setDebugDump( const std::string & debugDumpFile ) { _debugDump = debugDumpFile; }
81 /** Resize any per context GLObject buffers to specified size. */
82 virtual void resizeGLObjectBuffers(unsigned int maxSize);
84 /** If State is non-zero, this function releases any associated OpenGL objects for
85 * the specified graphics context. Otherwise, releases OpenGL objects
86 * for all graphics contexts. */
87 virtual void releaseGLObjects(osg::State* = 0) const;
90 /** Classic protected OSG destructor */
91 virtual ~DebugShadowMap();
93 // forward declare, interface and implementation provided in DebugShadowMap.cpp
94 class DrawableDrawWithDepthShadowComparisonOffCallback;
97 osg::Vec2s _hudOrigin;
98 osg::Vec2s _viewportSize;
99 osg::Vec2s _viewportOrigin;
100 osg::Vec2s _orthoSize;
101 osg::Vec2s _orthoOrigin;
104 std::string _debugDump;
106 osg::ref_ptr< osg::Shader > _depthColorFragmentShader;
108 struct OSGSHADOW_EXPORT ViewData: public BaseClass::ViewData
111 Texture used as ShadowMap - initialized by derived classes.
112 But it has to be defined now since DebugShadowMap::ViewData methods use it
114 osg::ref_ptr< osg::Texture > _texture;
116 Camera used to render ShadowMap - initialized by derived classes.
117 But it has to be defined now since DebugShadowMap::ViewData methods use it
119 osg::ref_ptr< osg::Camera > _camera;
121 osg::Matrixd _viewProjection;
122 osg::observer_ptr<osg::Camera> _viewCamera;
124 // Debug hud variables
126 /** Coloring Shader used to present shadow depth map contents */
127 osg::ref_ptr< osg::Shader > _depthColorFragmentShader;
129 struct PolytopeGeometry {
131 ConvexPolyhedron _polytope;
132 osg::ref_ptr< osg::Geometry > _geometry[2];
133 osg::Vec4 _colorOutline;
134 osg::Vec4 _colorInside;
137 typedef std::map< std::string, PolytopeGeometry > PolytopeGeometryMap;
140 osg::Vec2s _hudOrigin;
141 osg::Vec2s _viewportSize;
142 osg::Vec2s _viewportOrigin;
143 osg::Vec2s _orthoSize;
144 osg::Vec2s _orthoOrigin;
146 bool *_doDebugDrawPtr;
147 std::string *_debugDumpPtr;
149 PolytopeGeometryMap _polytopeGeometryMap;
150 osg::ref_ptr< osg::Geode > _geode[2];
151 osg::ref_ptr< osg::MatrixTransform > _transform[2];
153 std::map< std::string, osg::Matrix > _matrixMap;
154 std::map< std::string, osg::Polytope > _polytopeMap;
155 std::map< std::string, osg::BoundingBox > _boundingBoxMap;
157 osg::ref_ptr<osg::Camera> _cameraDebugHUD;
159 bool getDebugDraw() { return *_doDebugDrawPtr; }
160 std::string * getDebugDump() { return _debugDumpPtr; }
162 virtual void init( ThisClass * st, osgUtil::CullVisitor * cv );
164 virtual void cull( );
166 virtual void createDebugHUD( void );
168 virtual void cullDebugGeometry( );
170 virtual void updateDebugGeometry( const osg::Camera * screenCam,
171 const osg::Camera * shadowCam );
173 void setDebugPolytope( const char * name,
174 const ConvexPolyhedron & polytope = *(ConvexPolyhedron*)( NULL ),
175 osg::Vec4 colorOutline = osg::Vec4(0,0,0,0),
176 osg::Vec4 colorInside = osg::Vec4(0,0,0,0) );
178 bool DebugBoundingBox( const osg::BoundingBox & bb, const char * name = "" );
179 bool DebugPolytope( const osg::Polytope & p, const char * name = "" );
180 bool DebugMatrix( const osg::Matrix & m, const char * name = "" );
182 static osg::Vec3d computeShadowTexelToPixelError
183 ( const osg::Matrix & mvpwView,
184 const osg::Matrix & mvpwShadow,
185 const osg::Vec3d & vWorld,
186 const osg::Vec3d & vDelta = osg::Vec3d( 0.01,0.01,0.01 ) );
188 static void displayShadowTexelToPixelErrors
189 ( const osg::Camera * viewCam,
190 const osg::Camera * shadowCam,
191 const ConvexPolyhedron * hull );
193 void dump( const std::string & filename );
195 virtual void resizeGLObjectBuffers(unsigned int maxSize);
196 virtual void releaseGLObjects(osg::State* = 0) const;
201 META_ViewDependentShadowTechniqueData( ThisClass, ViewData )
204} // namespace osgShadow