root/OpenSceneGraph/trunk/include/osgShadow/DebugShadowMap @ 9555

Revision 9555, 7.1 kB (checked in by robert, 5 years ago)

Converted viewer camera references to use observer_ptr<> rather than ref_ptr<> or C pointers to avoid issues with circular references/dangling pointers.

RevLine 
[8985]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 debuging resources used by derived xxxShadowMap classes.
32As designed by its base ViewDepndentShadowTechnique, DebugShadowMap serves mainly as container of
33DebugShadowMap::ViewData objects. Most of the debuging support work is done by these objects.
34DebugShadowMap technique only initializes them in initViewDependentData method.
35
36Debuging 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( ViewDependentShadow, DebugShadowMap );
67
68        /** Turn on/off debuging hud & rendering of debug volumes in main view */
69        void setDebugDraw( bool draw ) { _doDebugDraw = draw; }
70
71        /** Tell if debuging hud & rendering of debug volumes is active */
72        bool getDebugDraw( void )      { return _doDebugDraw; }
73
74    protected:
75        /** Classic protected OSG destructor */
76        virtual ~DebugShadowMap();
77
[9540]78        // forward declare, interface and implementation provided in DebugShadowMap.cpp
79        class DrawableDrawWithDepthShadowComparisonOffCallback;
80
[8985]81        osg::Vec2s  _hudSize;
82        osg::Vec2s  _hudOrigin;
83        osg::Vec2s  _viewportSize;
84        osg::Vec2s  _viewportOrigin;
85        osg::Vec2s  _orthoSize;
86        osg::Vec2s  _orthoOrigin;
87
88        bool        _doDebugDraw;
89
90        osg::ref_ptr< osg::Shader > _depthColorFragmentShader;
91
[9113]92        struct OSGSHADOW_EXPORT ViewData: public BaseClass::ViewData
[8985]93        {
94            /**
95            Texture used as ShadowMap - initialized by derived classes.
96            But it has to be defined now since DebugShadowMap::ViewData methods use it
97            */
98            osg::ref_ptr< osg::Texture2D > _texture;
99            /**
100            Camera used to render ShadowMap - initialized by derived classes.
101            But it has to be defined now since DebugShadowMap::ViewData methods use it
102            */
103            osg::ref_ptr< osg::Camera >    _camera;
104
[8989]105            osg::Matrixd                   _viewProjection;
[9555]106            osg::observer_ptr<osg::Camera> _viewCamera;
[8985]107
108            // Debug hud variables
109
110            /** Coloring Shader used to present shadow depth map contents */
111            osg::ref_ptr< osg::Shader >    _depthColorFragmentShader;
112
113            struct PolytopeGeometry {
114
115                ConvexPolyhedron                 _polytope;
116                osg::ref_ptr< osg::Geometry >    _geometry[2];
117                osg::Vec4                        _colorOutline;
118                osg::Vec4                        _colorInside;
119            };
120
121            typedef std::map< std::string, PolytopeGeometry > PolytopeGeometryMap;
122
[8988]123            osg::Vec2s                                  _hudSize;
124            osg::Vec2s                                  _hudOrigin;
[8985]125            osg::Vec2s                                _viewportSize;
126            osg::Vec2s                                _viewportOrigin;
127            osg::Vec2s                                _orthoSize;
128            osg::Vec2s                                _orthoOrigin;
129
130            bool                                     *_doDebugDrawPtr;
131
[8988]132            PolytopeGeometryMap                          _polytopeGeometryMap;
[8985]133            osg::ref_ptr< osg::Geode >                _geode[2];
134            osg::ref_ptr< osg::MatrixTransform >      _transform[2];
135
136            std::map< std::string, osg::Matrix >      _matrixMap;
137            std::map< std::string, osg::Polytope >    _polytopeMap;
138            std::map< std::string, osg::BoundingBox > _boundingBoxMap;
139
[8988]140            osg::ref_ptr<osg::Camera>                 _cameraDebugHUD;           
[8985]141
[8988]142            bool getDebugDraw()    { return *_doDebugDrawPtr; }
[8985]143
144            virtual void init( ThisClass * st, osgUtil::CullVisitor * cv );
145
146            virtual void cull( );
147
148            virtual void createDebugHUD( void );
149
150            virtual void cullDebugGeometry( );
151
152            virtual void updateDebugGeometry( const osg::Camera * screenCam,
153                const osg::Camera * shadowCam );
154
155            void setDebugPolytope( const char * name,
156                const ConvexPolyhedron & polytope = *(ConvexPolyhedron*)( NULL ),
157                osg::Vec4 colorOutline = osg::Vec4(0,0,0,0),
158                osg::Vec4 colorInside = osg::Vec4(0,0,0,0) );
159
160            bool DebugBoundingBox( const osg::BoundingBox & bb, const char * name = "" );
161            bool DebugPolytope( const osg::Polytope & p, const char * name = "" );
162            bool DebugMatrix( const osg::Matrix & m, const char * name = "" );
163
164            static osg::Vec3d computeShadowTexelToPixelError
165                ( const osg::Matrix & mvpwView,
166                  const osg::Matrix & mvpwShadow,
167                  const osg::Vec3d & vWorld,
168                  const osg::Vec3d & vDelta = osg::Vec3d( 0.01,0.01,0.01 ) );
169
170            static void displayShadowTexelToPixelErrors
171                ( const osg::Camera * viewCam,
172                  const osg::Camera * shadowCam,
173                  const ConvexPolyhedron * hull );
174        };
175
176        META_ViewDependentShadowTechniqueData( ThisClass, ViewData )
177};
178
179} // namespace osgShadow
180
181#endif
Note: See TracBrowser for help on using the browser.