OpenSceneGraph 3.6.5
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
32{
34 {
35 return (lhs->_depth < rhs->_depth);
36 }
37};
38
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
50
51#ifdef OSGUTIL_RENDERBACKEND_USE_REF_PTR
53#else
55#endif
56
57 int _depth;
60
61 mutable float _averageDistance;
62 mutable float _minimumDistance;
63
65
67
71 _depth(0),
75 _dynamic(false)
76 {
77 }
78
79 StateGraph(StateGraph* parent,const osg::StateSet* stateset):
80 _parent(parent),
81 _stateset(stateset),
82 _depth(0),
86 _dynamic(false)
87 {
88 if (_parent) _depth = _parent->_depth + 1;
89
90 if (_parent && _parent->_dynamic) _dynamic = true;
92 }
93
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
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
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
169 void reset();
170
173 void clean();
174
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
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
352 StateGraph(const StateGraph&) : osg::Object() {}
354 StateGraph& operator = (const StateGraph&) { return *this; }
355
356};
357
358}
359
360#endif
361
The core osg library provides the basic scene graph classes such as Nodes, State and Drawables,...
Definition AlphaFunc:19
The osgUtil library provides general purpose utility classes such as update, cull and draw traverses,...
Definition NodeVisitor:25
Copy Op(erator) used to control whether shallow or deep copy is used during copy construction and clo...
Definition CopyOp:41
Base class/standard interface for objects which require IO support, cloning and reference counting.
Definition Object:61
@ DYNAMIC
Definition Object:218
DataVariance getDataVariance() const
Get the data variance of this object.
Definition Object:231
Smart pointer for handling referenced counted objects.
Definition ref_ptr:32
T * get() const
Definition ref_ptr:117
Base class for providing reference counted objects.
Definition Referenced:44
Encapsulates the current applied OpenGL modes, attributes and vertex arrays settings,...
Definition State:80
void popStateSet()
Pop stateset off state stack.
void pushStateSet(const StateSet *dstate)
Push stateset onto state stack.
Stores a set of modes and attributes which represent a set of OpenGL state.
Definition StateSet:46
Container class for all data required for rendering of drawables.
Definition RenderLeaf:34
StateGraph * _parent
Definition RenderLeaf:92
bool _dynamic
Definition RenderLeaf:104
Definition StateGraph:32
bool operator()(const osg::ref_ptr< RenderLeaf > &lhs, const osg::ref_ptr< RenderLeaf > &rhs)
Definition StateGraph:33
StateGraph - contained in a renderBin, defines the scene to be drawn.
Definition StateGraph:42
virtual const char * className() const
return the name of the object's class type.
Definition StateGraph:102
osg::ref_ptr< osg::Referenced > _userData
Definition StateGraph:64
float _minimumDistance
Definition StateGraph:62
bool _dynamic
Definition StateGraph:66
void clean()
Recursively clean the StateGraph of all its drawables, lights and depths.
StateGraph * find_or_insert(const osg::StateSet *stateset)
Definition StateGraph:215
ChildList _children
Definition StateGraph:58
virtual bool isSameKindAs(const osg::Object *obj) const
Definition StateGraph:100
StateGraph(StateGraph *parent, const osg::StateSet *stateset)
Definition StateGraph:79
osg::Referenced * getUserData()
Get user data.
Definition StateGraph:105
void releaseGLObjects(osg::State *state=0) const
If State is non-zero, this function releases any associated OpenGL objects for the specified graphics...
Definition StateGraph:196
LeafList _leaves
Definition StateGraph:59
float getAverageDistance() const
Definition StateGraph:128
void setStateSet(const osg::StateSet *stateset)
Definition StateGraph:108
bool leaves_empty() const
Definition StateGraph:122
virtual StateGraph * cloneStateGraph() const
Definition StateGraph:98
static int numToPop(StateGraph *sg_curr)
Definition StateGraph:336
virtual osg::Object * cloneType() const
Clone the type of an object, with Object* return type.
Definition StateGraph:97
StateGraph * _parent
Definition StateGraph:49
void reset()
Reset the internal contents of a StateGraph, including deleting all children.
static void moveToRootStateGraph(osg::State &state, StateGraph *sg_curr)
Definition StateGraph:325
StateGraph()
Definition StateGraph:68
void addLeaf(RenderLeaf *leaf)
add a render leaf.
Definition StateGraph:229
void prune()
Recursively prune the StateGraph of empty children.
void resizeGLObjectBuffers(unsigned int maxSize)
Resize any per context GLObject buffers to specified size.
Definition StateGraph:179
void setUserData(osg::Referenced *obj)
Set user data, data must be subclassed from Referenced to allow automatic memory handling.
Definition StateGraph:104
std::map< const osg::StateSet *, osg::ref_ptr< StateGraph > > ChildList
Definition StateGraph:46
const osg::StateSet * getStateSet() const
Definition StateGraph:113
float getMinimumDistance() const
Definition StateGraph:145
virtual const char * libraryName() const
return the name of the object's library.
Definition StateGraph:101
float _averageDistance
Definition StateGraph:61
~StateGraph()
Definition StateGraph:94
static void moveStateGraph(osg::State &state, StateGraph *sg_curr, StateGraph *sg_new)
Definition StateGraph:241
bool empty() const
return true if all of drawables, lights and children are empty.
Definition StateGraph:117
int _depth
Definition StateGraph:57
void sortFrontToBack()
Definition StateGraph:163
const osg::StateSet * _stateset
Definition StateGraph:54
std::vector< osg::ref_ptr< RenderLeaf > > LeafList
Definition StateGraph:47
virtual osg::Object * clone(const osg::CopyOp &) const
Clone an object, with Object* return type.
Definition StateGraph:99
const osg::Referenced * getUserData() const
Get const user data.
Definition StateGraph:106
#define NULL
Definition Export:55
#define OSGUTIL_EXPORT
Definition Export:40

osg logo
Generated at Sun Jul 20 2025 00:00:00 for the OpenSceneGraph by doxygen 1.14.0.