openscenegraph
DebugShadowMap
Go to the documentation of this file.
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
17#ifndef OSGSHADOW_DEBUGSHADOWMAP
18#define OSGSHADOW_DEBUGSHADOWMAP 1
19
20#include <osgShadow/ViewDependentShadowTechnique>
21#include <osgShadow/ConvexPolyhedron>
22#include <osg/MatrixTransform>
23#include <osg/Geode>
24#include <osg/Geometry>
25#include <string>
26#include <map>
27
28namespace osgShadow {
29
30/**
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.
35
36Debugging outputs present:
37 Shadow maps (pseudo colored to improve readability)
38 Shadow and related volumes (represented as convex polyhedra)
39*/
40
41class OSGSHADOW_EXPORT DebugShadowMap : public ViewDependentShadowTechnique
42{
43 public :
44
45 /*
46 All classes stemming from ViewDependentShadowTechnique follow the same pattern.
47
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.
50
51 I use some typedefs to make these inheritance patterns easier to declare/define.
52 */
53
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;
58
59 /** Classic OSG constructor */
60 DebugShadowMap();
61
62 /** Classic OSG cloning constructor */
63 DebugShadowMap(const DebugShadowMap& dsm, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
64
65 /** Declaration of standard OSG object methods */
66 META_Object( osgShadow, DebugShadowMap );
67
68 /** Turn on/off debugging hud & rendering of debug volumes in main view */
69 void setDebugDraw( bool draw ) { _doDebugDraw = draw; }
70
71 /** Tell if debugging hud & rendering of debug volumes is active */
72 bool getDebugDraw( void ) const { return _doDebugDraw; }
73
74 /** Get the file name of debugging dump */
75 std::string getDebugDump( void ) const { return _debugDump; }
76
77 /** Set the file name of debugging dump */
78 void setDebugDump( const std::string & debugDumpFile ) { _debugDump = debugDumpFile; }
79
80
81 /** Resize any per context GLObject buffers to specified size. */
82 virtual void resizeGLObjectBuffers(unsigned int maxSize);
83
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;
88
89protected:
90 /** Classic protected OSG destructor */
91 virtual ~DebugShadowMap();
92
93 // forward declare, interface and implementation provided in DebugShadowMap.cpp
94 class DrawableDrawWithDepthShadowComparisonOffCallback;
95
96 osg::Vec2s _hudSize;
97 osg::Vec2s _hudOrigin;
98 osg::Vec2s _viewportSize;
99 osg::Vec2s _viewportOrigin;
100 osg::Vec2s _orthoSize;
101 osg::Vec2s _orthoOrigin;
102
103 bool _doDebugDraw;
104 std::string _debugDump;
105
106 osg::ref_ptr< osg::Shader > _depthColorFragmentShader;
107
108 struct OSGSHADOW_EXPORT ViewData: public BaseClass::ViewData
109 {
110 /**
111 Texture used as ShadowMap - initialized by derived classes.
112 But it has to be defined now since DebugShadowMap::ViewData methods use it
113 */
114 osg::ref_ptr< osg::Texture > _texture;
115 /**
116 Camera used to render ShadowMap - initialized by derived classes.
117 But it has to be defined now since DebugShadowMap::ViewData methods use it
118 */
119 osg::ref_ptr< osg::Camera > _camera;
120
121 osg::Matrixd _viewProjection;
122 osg::observer_ptr<osg::Camera> _viewCamera;
123
124 // Debug hud variables
125
126 /** Coloring Shader used to present shadow depth map contents */
127 osg::ref_ptr< osg::Shader > _depthColorFragmentShader;
128
129 struct PolytopeGeometry {
130
131 ConvexPolyhedron _polytope;
132 osg::ref_ptr< osg::Geometry > _geometry[2];
133 osg::Vec4 _colorOutline;
134 osg::Vec4 _colorInside;
135 };
136
137 typedef std::map< std::string, PolytopeGeometry > PolytopeGeometryMap;
138
139 osg::Vec2s _hudSize;
140 osg::Vec2s _hudOrigin;
141 osg::Vec2s _viewportSize;
142 osg::Vec2s _viewportOrigin;
143 osg::Vec2s _orthoSize;
144 osg::Vec2s _orthoOrigin;
145
146 bool *_doDebugDrawPtr;
147 std::string *_debugDumpPtr;
148
149 PolytopeGeometryMap _polytopeGeometryMap;
150 osg::ref_ptr< osg::Geode > _geode[2];
151 osg::ref_ptr< osg::MatrixTransform > _transform[2];
152
153 std::map< std::string, osg::Matrix > _matrixMap;
154 std::map< std::string, osg::Polytope > _polytopeMap;
155 std::map< std::string, osg::BoundingBox > _boundingBoxMap;
156
157 osg::ref_ptr<osg::Camera> _cameraDebugHUD;
158
159 bool getDebugDraw() { return *_doDebugDrawPtr; }
160 std::string * getDebugDump() { return _debugDumpPtr; }
161
162 virtual void init( ThisClass * st, osgUtil::CullVisitor * cv );
163
164 virtual void cull( );
165
166 virtual void createDebugHUD( void );
167
168 virtual void cullDebugGeometry( );
169
170 virtual void updateDebugGeometry( const osg::Camera * screenCam,
171 const osg::Camera * shadowCam );
172
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) );
177
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 = "" );
181
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 ) );
187
188 static void displayShadowTexelToPixelErrors
189 ( const osg::Camera * viewCam,
190 const osg::Camera * shadowCam,
191 const ConvexPolyhedron * hull );
192
193 void dump( const std::string & filename );
194
195 virtual void resizeGLObjectBuffers(unsigned int maxSize);
196 virtual void releaseGLObjects(osg::State* = 0) const;
197
198
199 };
200
201 META_ViewDependentShadowTechniqueData( ThisClass, ViewData )
202};
203
204} // namespace osgShadow
205
206#endif