openscenegraph
StateGraph
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
14#ifndef OSGUTIL_STATEGRAPH
15#define OSGUTIL_STATEGRAPH 1
16
17#include <osg/Matrix>
18#include <osg/Drawable>
19#include <osg/StateSet>
20#include <osg/State>
21#include <osg/Light>
22
23#include <osgUtil/RenderLeaf>
24
25#include <set>
26#include <vector>
27#include <algorithm>
28
29namespace osgUtil {
30
31struct LessDepthSortFunctor
32{
33 bool operator() (const osg::ref_ptr<RenderLeaf>& lhs,const osg::ref_ptr<RenderLeaf>& rhs)
34 {
35 return (lhs->_depth < rhs->_depth);
36 }
37};
38
39/** StateGraph - contained in a renderBin, defines the scene to be drawn.
40 */
41class OSGUTIL_EXPORT StateGraph : public osg::Object
42{
43 public:
44
45
46 typedef std::map< const osg::StateSet*, osg::ref_ptr<StateGraph> > ChildList;
47 typedef std::vector< osg::ref_ptr<RenderLeaf> > LeafList;
48
49 StateGraph* _parent;
50
51#ifdef OSGUTIL_RENDERBACKEND_USE_REF_PTR
52 osg::ref_ptr<const osg::StateSet> _stateset;
53#else
54 const osg::StateSet* _stateset;
55#endif
56
57 int _depth;
58 ChildList _children;
59 LeafList _leaves;
60
61 mutable float _averageDistance;
62 mutable float _minimumDistance;
63
64 osg::ref_ptr<osg::Referenced> _userData;
65
66 bool _dynamic;
67
68 StateGraph():
69 _parent(NULL),
70 _stateset(NULL),
71 _depth(0),
72 _averageDistance(0),
73 _minimumDistance(0),
74 _userData(NULL),
75 _dynamic(false)
76 {
77 }
78
79 StateGraph(StateGraph* parent,const osg::StateSet* stateset):
80 _parent(parent),
81 _stateset(stateset),
82 _depth(0),
83 _averageDistance(0),
84 _minimumDistance(0),
85 _userData(NULL),
86 _dynamic(false)
87 {
88 if (_parent) _depth = _parent->_depth + 1;
89
90 if (_parent && _parent->_dynamic) _dynamic = true;
91 else _dynamic = stateset->getDataVariance()==osg::Object::DYNAMIC;
92 }
93
94 ~StateGraph() {}
95
96
97 virtual osg::Object* cloneType() const { return new StateGraph(); }
98 virtual StateGraph* cloneStateGraph() const { return new StateGraph(); }
99 virtual osg::Object* clone(const osg::CopyOp&) const { return new StateGraph(); }
100 virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const StateGraph*>(obj)!=0L; }
101 virtual const char* libraryName() const { return "osgUtil"; }
102 virtual const char* className() const { return "StateGraph"; }
103
104 void setUserData(osg::Referenced* obj) { _userData = obj; }
105 osg::Referenced* getUserData() { return _userData.get(); }
106 const osg::Referenced* getUserData() const { return _userData.get(); }
107
108 void setStateSet(const osg::StateSet* stateset) { _stateset = stateset; }
109
110#ifdef OSGUTIL_RENDERBACKEND_USE_REF_PTR
111 const osg::StateSet* getStateSet() const { return _stateset.get(); }
112#else
113 const osg::StateSet* getStateSet() const { return _stateset; }
114#endif
115
116 /** return true if all of drawables, lights and children are empty.*/
117 inline bool empty() const
118 {
119 return _leaves.empty() && _children.empty();
120 }
121
122 inline bool leaves_empty() const
123 {
124 return _leaves.empty();
125 }
126
127
128 inline float getAverageDistance() const
129 {
130 if (_averageDistance==FLT_MAX && !_leaves.empty())
131 {
132 _averageDistance = 0.0f;
133 for(LeafList::const_iterator itr=_leaves.begin();
134 itr!=_leaves.end();
135 ++itr)
136 {
137 _averageDistance += (*itr)->_depth;
138 }
139 _averageDistance /= (float)_leaves.size();
140
141 }
142 return _averageDistance;
143 }
144
145 inline float getMinimumDistance() const
146 {
147 if (_minimumDistance==FLT_MAX && !_leaves.empty())
148 {
149 LeafList::const_iterator itr=_leaves.begin();
150 _minimumDistance = (*itr)->_depth;
151 ++itr;
152 for(;
153 itr!=_leaves.end();
154 ++itr)
155 {
156 if ((*itr)->_depth<_minimumDistance) _minimumDistance=(*itr)->_depth;
157 }
158
159 }
160 return _minimumDistance;
161 }
162
163 inline void sortFrontToBack()
164 {
165 std::sort(_leaves.begin(),_leaves.end(),LessDepthSortFunctor());
166 }
167
168 /** Reset the internal contents of a StateGraph, including deleting all children.*/
169 void reset();
170
171 /** Recursively clean the StateGraph of all its drawables, lights and depths.
172 * Leaves children intact, and ready to be populated again.*/
173 void clean();
174
175 /** Recursively prune the StateGraph of empty children.*/
176 void prune();
177
178
179 void resizeGLObjectBuffers(unsigned int maxSize)
180 {
181 for(ChildList::iterator itr = _children.begin();
182 itr != _children.end();
183 ++itr)
184 {
185 (itr->second)->resizeGLObjectBuffers(maxSize);
186 }
187
188 for(LeafList::iterator itr = _leaves.begin();
189 itr != _leaves.end();
190 ++itr)
191 {
192 (*itr)->resizeGLObjectBuffers(maxSize);
193 }
194 }
195
196 void releaseGLObjects(osg::State* state=0) const
197 {
198 if (_stateset) _stateset->releaseGLObjects(state);
199
200 for(ChildList::const_iterator itr = _children.begin();
201 itr != _children.end();
202 ++itr)
203 {
204 (itr->second)->releaseGLObjects(state);
205 }
206
207 for(LeafList::const_iterator itr = _leaves.begin();
208 itr != _leaves.end();
209 ++itr)
210 {
211 (*itr)->releaseGLObjects(state);
212 }
213 }
214
215 inline StateGraph* find_or_insert(const osg::StateSet* stateset)
216 {
217 // search for the appropriate state group, return it if found.
218 ChildList::iterator itr = _children.find(stateset);
219 if (itr!=_children.end()) return itr->second.get();
220
221 // create a state group and insert it into the children list
222 // then return the state group.
223 StateGraph* sg = new StateGraph(this,stateset);
224 _children[stateset] = sg;
225 return sg;
226 }
227
228 /** add a render leaf.*/
229 inline void addLeaf(RenderLeaf* leaf)
230 {
231 if (leaf)
232 {
233 _averageDistance = FLT_MAX; // signify dirty.
234 _minimumDistance = FLT_MAX; // signify dirty.
235 _leaves.push_back(leaf);
236 leaf->_parent = this;
237 if (_dynamic) leaf->_dynamic = true;
238 }
239 }
240
241 static inline void moveStateGraph(osg::State& state,StateGraph* sg_curr,StateGraph* sg_new)
242 {
243 if (sg_new==sg_curr || sg_new==NULL) return;
244
245 if (sg_curr==NULL)
246 {
247
248 // use return path to trace back steps to sg_new.
249 std::vector<StateGraph*> return_path;
250 return_path.reserve(sg_new->_depth+1);
251
252 // need to pop back root render graph.
253 do
254 {
255 return_path.push_back(sg_new);
256 sg_new = sg_new->_parent;
257 } while (sg_new);
258
259 for(std::vector<StateGraph*>::reverse_iterator itr=return_path.rbegin();
260 itr!=return_path.rend();
261 ++itr)
262 {
263 StateGraph* rg = (*itr);
264 if (rg->getStateSet()) state.pushStateSet(rg->getStateSet());
265 }
266 return;
267 }
268
269
270 // first handle the typical case which is two state groups
271 // are neighbours.
272 if (sg_curr->_parent==sg_new->_parent)
273 {
274
275 // state has changed so need to pop old state.
276 if (sg_curr->getStateSet()) state.popStateSet();
277 // and push new state.
278 if (sg_new->getStateSet()) state.pushStateSet(sg_new->getStateSet());
279 return;
280 }
281
282
283 // need to pop back up to the same depth as the new state group.
284 while (sg_curr->_depth>sg_new->_depth)
285 {
286 if (sg_curr->getStateSet()) state.popStateSet();
287 sg_curr = sg_curr->_parent;
288 }
289
290 // use return path to trace back steps to sg_new.
291 std::vector<StateGraph*> return_path;
292 return_path.reserve(sg_new->_depth+1);
293
294 // need to pop back up to the same depth as the curr state group.
295 while (sg_new->_depth>sg_curr->_depth)
296 {
297 return_path.push_back(sg_new);
298 sg_new = sg_new->_parent;
299 }
300
301 // now pop back up both parent paths until they agree.
302
303 // DRT - 10/22/02
304 // should be this to conform with above case where two StateGraph
305 // nodes have the same parent
306 while (sg_curr != sg_new)
307 {
308 if (sg_curr->getStateSet()) state.popStateSet();
309 sg_curr = sg_curr->_parent;
310
311 return_path.push_back(sg_new);
312 sg_new = sg_new->_parent;
313 }
314
315 for(std::vector<StateGraph*>::reverse_iterator itr=return_path.rbegin();
316 itr!=return_path.rend();
317 ++itr)
318 {
319 StateGraph* rg = (*itr);
320 if (rg->getStateSet()) state.pushStateSet(rg->getStateSet());
321 }
322
323 }
324
325 inline static void moveToRootStateGraph(osg::State& state,StateGraph* sg_curr)
326 {
327 // need to pop back all statesets and matrices.
328 while (sg_curr)
329 {
330 if (sg_curr->getStateSet()) state.popStateSet();
331 sg_curr = sg_curr->_parent;
332 }
333
334 }
335
336 inline static int numToPop(StateGraph* sg_curr)
337 {
338 int numToPop = 0;
339 // need to pop back all statesets and matrices.
340 while (sg_curr)
341 {
342 if (sg_curr->getStateSet()) ++numToPop;
343 sg_curr = sg_curr->_parent;
344 }
345
346 return numToPop;
347 }
348
349 private:
350
351 /// disallow copy construction.
352 StateGraph(const StateGraph&) : osg::Object() {}
353 /// disallow copy operator.
354 StateGraph& operator = (const StateGraph&) { return *this; }
355
356};
357
358}
359
360#endif
361