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_VIEWDEPENDENTSHADOWTECHINIQUE
18#define OSGSHADOW_VIEWDEPENDENTSHADOWTECHINIQUE 1
20#include <osg/Identifier>
21#include <osgShadow/ShadowTechnique>
22#include <osgShadow/Export>
27 META_ViewDependentShadowTechniqueData macro defines initViewDependentData
28 method used by derived shadow techniques to initialize their specific
29 ViewData objects. initViewDependentData will be called from
30 ViewDependentShadowTechnique base class to init derived class
32#define META_ViewDependentShadowTechniqueData( ShadowTechnique, TechniqueData )\
33virtual ViewDependentShadowTechnique::ViewData * initViewDependentData \
34( osgUtil::CullVisitor *cv, ViewDependentShadowTechnique::ViewData * vd ) \
36 TechniqueData* td = dynamic_cast<TechniqueData*>( vd ); \
37 if ( !td ) td = new TechniqueData; \
38 td->init( this, cv ); \
43 ViewDependentShadowTechnique is a base class for all
44 View Dependent Shadow techniques. It defines fundamental object structure
45 and methods to manage separate shadow resources for each view of the scene.
46 By view we understand osg::View or SceneView instance and their associated
47 Camera. Typical osg application has one or more such views. View Dependent
48 Shadow techniques manage shadow generation for them.
50 View Dependent Shadow techniques are used to optimize shadow algorithms for
51 part of the scene seen on the view. If rest of the scene is behind view
52 frustum, there is no sense in computing shadows for it. Since in practice we
53 often use 3d databases extending far beyond current camera frustum View
54 Dependent Shadow approach may produce much better shadows.
56 The other goal is to provide framework for thread safe rendering of
57 the shadows. It allows to use shadows with different OSG threading models.
59 Conceptually ViewDependentShadowTechnique is similar to osgSim::OverlayNode.
60 Its a container class for number of ViewData (or ViewData derived) objects
61 doing actual shadow work for each of the scene views.
63 But ViewDependentShadowTechnique is intended as foundation layer for all
64 derived classes so in some way it extends osgSim::OverlayNode approach a bit.
68 ViewDependendentShadowTechnique is derived from osgShadow::ShadowTechnique
69 and as such overrides virtual methods of osgShadow::ShadowTechnique.
70 But most of the shadow dirty work is done by ViewData objects,
71 ViewDependendentShadowTechnique::cull is the only osgShadow::ShadowTechnique
72 method where ViewDependendentShadowTechnique does something significant:
74 What ViewDependentShadowTechnique::cull( CullVisitor & cv ) does ?
75 It identifies View. CullVisitor ptr is used as View identificator.
76 In practice we do not check and interpret what are actual Views and SceneViews
77 set up by application. We focus on Camera and CullVisitors as a identificators
78 of views. We can safely do this because each such view will have associated
79 unique CullVisitor used to cull the scene in every frame.
81 Based on CullVisitor ptr passed to cull method, associated Technique::ViewData
82 object is created (if necessary) and then seleced. Then control is passed to
83 this ViewData object. So, each view has its associated unique ViewData
84 (or derived) object performing dirty work of shadow resources management and
85 shadow generation for the view.
87 To support creation of classes derived from ViewDependentShadowTechnique it
88 was necessary to provide mechanism to override ViewData and allow for
89 initialization of new derived ViewData objects. Creation and initialization
90 is performed when ViewDependendentShadowTechnique::cull gets called with
91 CullVistor ptr which does not yet have associated ViewData object. When it
92 happens, virtual initViewDependentData method is called to give
93 derived techniques a chance to allocate and iniitalize its specific
94 resources as new ViewData derived instance. In practice initViewDependentData
95 in derived techniques should look the same as in base class so as a convenience
96 it was defined as META_ViewDependentShadowTechnique macro. Derived techniques
97 use this macro to override initViewDependentData method for their usage.
99 After ViewData derived object is construted and selected, control is passed
100 to this object by call to virtual ViewData::cull method. The rest of work
101 is the done by this object. ViewDependentShadowTechnique::ViewData is intended
102 as a base class so it does nothing. In practice the rest of dirty work will
103 do new ViewData classes implemented in derived techniques.
105class OSGSHADOW_EXPORT ViewDependentShadowTechnique
106 : public osgShadow::ShadowTechnique
110 osgShadow::ShadowTechnique equivalent methods for view dependent techniques
113 /** Classic OSG constructor */
114 ViewDependentShadowTechnique( void );
116 /** Classic OSG cloning constructor */
117 ViewDependentShadowTechnique(
118 const ViewDependentShadowTechnique& vdst,
119 const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY );
122 /** Declaration of standard OSG object methods */
123 META_Object( osgShadow, ViewDependentShadowTechnique );
125 /** Dirty view data bits and force update of view data resources */
126 virtual void dirty();
128 /** Initialize the ShadowedScene and some data structures.*/
131 /** Run the update traversal of the ShadowedScene and update any local cached data structures.*/
132 virtual void update(osg::NodeVisitor& nv);
134 /** Run the cull traversal of the ShadowedScene and set up the rendering for this ShadowTechnique.*/
135 virtual void cull(osgUtil::CullVisitor& cv);
137 /** Clean scene graph from any shadow technique specific nodes, state and drawables.*/
138 virtual void cleanSceneGraph();
140 /** Traverse shadow scene graph.*/
141 virtual void traverse(osg::NodeVisitor& nv);
144 /** Resize any per context GLObject buffers to specified size. */
145 virtual void resizeGLObjectBuffers(unsigned int maxSize);
147 /** If State is non-zero, this function releases any associated OpenGL objects for
148 * the specified graphics context. Otherwise, releases OpenGL objects
149 * for all graphics contexts. */
150 virtual void releaseGLObjects(osg::State* = 0) const;
153 /** Classic protected OSG destructor */
154 ~ViewDependentShadowTechnique( void );
157 Base container class for view dependent shadow resources.
158 Techniques based on ViewDependentShadowTechnique will usually define
159 similar struct and derive it from ViewData to contain their specufic resources.
161 struct OSGSHADOW_EXPORT ViewData: public osg::Referenced
163 virtual const char* className() const { return "ViewData"; }
166 Method called upon ViewData instance to initialize internal variables
169 ( ViewDependentShadowTechnique *st, osgUtil::CullVisitor *cv );
172 Method called by ViewDependentShadowTechnique to allow ViewData
173 do the hard work computing shadows for its associated view
178 Dirty is called by parent ViewDependentShadowTechnique to force
179 update of resources after some of them were modified in parent technique
181 virtual void dirty( bool flag );
184 Simple constructor zeroing all variables.
186 ViewData(): _dirty( true ), _cv( NULL ), _st( NULL ) { };
189 Mutex used to guard _dirty flag from override in case when parent technique calls
190 dirty() simultaneously with ViewData while it is updating resources inside init method.
192 OpenThreads::Mutex _mutex;
195 Dirty flag tells this instance to update its resources
200 View's CullVisitor associated with this ViewData instance
202 osg::observer_ptr< osgUtil::CullVisitor > _cv;
205 Parent ViewDependentShadowTechnique
207 osg::observer_ptr< ViewDependentShadowTechnique > _st;
209 virtual void resizeGLObjectBuffers(unsigned int /*maxSize*/) {}
211 /** If State is non-zero, this function releases any associated OpenGL objects for
212 * the specified graphics context. Otherwise, releases OpenGL objects
213 * for all graphics contexts. */
214 virtual void releaseGLObjects(osg::State* = 0) const {}
218 Map of view dependent data per view cull visitor (CVs are used as indices)
219 ViewDependentShadowTechnique uses this map to find VieData for each cull vitior
222 typedef std::map< osg::ref_ptr< osg::Identifier >,
223 osg::ref_ptr< ViewData > > ViewDataMap;
225 ViewDataMap _viewDataMap;
229 Mutex used to serialize accesses to ViewDataMap
231 OpenThreads::Mutex _viewDataMapMutex;
233 /** Return view dependent data for the cull visitor */
234 virtual ViewDependentShadowTechnique::ViewData * getViewDependentData( osgUtil::CullVisitor * cv );
236 /** Define view dependent data for the cull visitor */
237 virtual void setViewDependentData( osgUtil::CullVisitor * cv, ViewDependentShadowTechnique::ViewData * data );
240 Declare standard initViewDependentData method.
242 META_ViewDependentShadowTechniqueData( ViewDependentShadowTechnique, ViewData )
246} // namespace osgShadow