2006-07-18 23:21:48 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2003-01-22 00:45:36 +08:00
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2001-10-04 23:12:57 +08:00
|
|
|
|
2005-10-13 20:51:00 +08:00
|
|
|
#ifndef OSGUTIL_STATEGRAPH
|
|
|
|
#define OSGUTIL_STATEGRAPH 1
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
#include <osg/Matrix>
|
|
|
|
#include <osg/Drawable>
|
|
|
|
#include <osg/StateSet>
|
|
|
|
#include <osg/State>
|
|
|
|
#include <osg/Light>
|
|
|
|
|
|
|
|
#include <osgUtil/RenderLeaf>
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
2002-05-28 18:24:43 +08:00
|
|
|
#include <algorithm>
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
namespace osgUtil {
|
|
|
|
|
2007-01-28 17:30:11 +08:00
|
|
|
struct LessDepthSortFunctor
|
2002-05-28 18:24:43 +08:00
|
|
|
{
|
2002-09-02 20:31:35 +08:00
|
|
|
bool operator() (const osg::ref_ptr<RenderLeaf>& lhs,const osg::ref_ptr<RenderLeaf>& rhs)
|
2002-05-28 18:24:43 +08:00
|
|
|
{
|
2007-01-28 17:30:11 +08:00
|
|
|
return (lhs->_depth < rhs->_depth);
|
2002-05-28 18:24:43 +08:00
|
|
|
}
|
|
|
|
};
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2005-10-13 20:51:00 +08:00
|
|
|
/** StateGraph - contained in a renderBin, defines the scene to be drawn.
|
2004-09-27 02:39:34 +08:00
|
|
|
*/
|
2005-10-13 20:51:00 +08:00
|
|
|
class OSGUTIL_EXPORT StateGraph : public osg::Referenced
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
2005-10-13 20:51:00 +08:00
|
|
|
typedef std::map< const osg::StateSet*, osg::ref_ptr<StateGraph> > ChildList;
|
2001-09-20 05:19:47 +08:00
|
|
|
typedef std::vector< osg::ref_ptr<RenderLeaf> > LeafList;
|
|
|
|
|
2007-04-16 20:18:56 +08:00
|
|
|
StateGraph* _parent;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2007-09-04 17:00:38 +08:00
|
|
|
#ifdef OSGUTIL_RENDERBACKEND_USE_REF_PTR
|
|
|
|
osg::ref_ptr<const osg::StateSet> _stateset;
|
|
|
|
#else
|
|
|
|
const osg::StateSet* _stateset;
|
|
|
|
#endif
|
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
int _depth;
|
|
|
|
ChildList _children;
|
|
|
|
LeafList _leaves;
|
2002-05-18 16:39:42 +08:00
|
|
|
|
2002-05-28 18:24:43 +08:00
|
|
|
mutable float _averageDistance;
|
2002-09-18 22:57:01 +08:00
|
|
|
mutable float _minimumDistance;
|
2002-05-28 18:24:43 +08:00
|
|
|
|
2002-05-18 16:39:42 +08:00
|
|
|
osg::ref_ptr<osg::Referenced> _userData;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2007-01-30 06:44:29 +08:00
|
|
|
bool _dynamic;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2005-10-13 20:51:00 +08:00
|
|
|
StateGraph():
|
2006-05-02 23:52:46 +08:00
|
|
|
osg::Referenced(false),
|
2007-09-07 23:03:56 +08:00
|
|
|
_parent(NULL),
|
|
|
|
_stateset(NULL),
|
2002-05-28 18:24:43 +08:00
|
|
|
_depth(0),
|
2002-07-21 07:54:55 +08:00
|
|
|
_averageDistance(0),
|
2002-09-18 22:57:01 +08:00
|
|
|
_minimumDistance(0),
|
2007-01-30 06:44:29 +08:00
|
|
|
_userData(NULL),
|
|
|
|
_dynamic(false)
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2005-10-13 20:51:00 +08:00
|
|
|
StateGraph(StateGraph* parent,const osg::StateSet* stateset):
|
2006-05-02 23:52:46 +08:00
|
|
|
osg::Referenced(false),
|
2007-09-07 23:03:56 +08:00
|
|
|
_parent(parent),
|
2002-07-21 07:54:55 +08:00
|
|
|
_stateset(stateset),
|
|
|
|
_depth(0),
|
|
|
|
_averageDistance(0),
|
2002-09-18 22:57:01 +08:00
|
|
|
_minimumDistance(0),
|
2007-01-30 06:44:29 +08:00
|
|
|
_userData(NULL),
|
|
|
|
_dynamic(false)
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
|
|
|
if (_parent) _depth = _parent->_depth + 1;
|
2007-01-30 06:44:29 +08:00
|
|
|
|
|
|
|
if (_parent && _parent->_dynamic) _dynamic = true;
|
|
|
|
else _dynamic = stateset->getDataVariance()==osg::Object::DYNAMIC;
|
2001-09-20 05:19:47 +08:00
|
|
|
}
|
|
|
|
|
2005-10-13 20:51:00 +08:00
|
|
|
~StateGraph() {}
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2005-10-13 20:51:00 +08:00
|
|
|
StateGraph* cloneType() const { return new StateGraph; }
|
2002-02-19 07:01:09 +08:00
|
|
|
|
2002-05-18 16:39:42 +08:00
|
|
|
void setUserData(osg::Referenced* obj) { _userData = obj; }
|
|
|
|
osg::Referenced* getUserData() { return _userData.get(); }
|
|
|
|
const osg::Referenced* getUserData() const { return _userData.get(); }
|
2011-08-09 14:54:44 +08:00
|
|
|
|
|
|
|
void setStateSet(const osg::StateSet* stateset) { _stateset = stateset; }
|
|
|
|
|
2007-09-01 00:05:24 +08:00
|
|
|
#ifdef OSGUTIL_RENDERBACKEND_USE_REF_PTR
|
|
|
|
const osg::StateSet* getStateSet() const { return _stateset.get(); }
|
|
|
|
#else
|
2007-09-04 17:00:38 +08:00
|
|
|
const osg::StateSet* getStateSet() const { return _stateset; }
|
2007-09-01 00:05:24 +08:00
|
|
|
#endif
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2001-09-28 20:36:40 +08:00
|
|
|
/** return true if all of drawables, lights and children are empty.*/
|
2002-09-02 20:31:35 +08:00
|
|
|
inline bool empty() const
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
|
|
|
return _leaves.empty() && _children.empty();
|
|
|
|
}
|
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
inline bool leaves_empty() const
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
|
|
|
return _leaves.empty();
|
|
|
|
}
|
|
|
|
|
2002-05-28 18:24:43 +08:00
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
inline float getAverageDistance() const
|
2002-05-28 18:24:43 +08:00
|
|
|
{
|
|
|
|
if (_averageDistance==FLT_MAX && !_leaves.empty())
|
|
|
|
{
|
|
|
|
_averageDistance = 0.0f;
|
|
|
|
for(LeafList::const_iterator itr=_leaves.begin();
|
|
|
|
itr!=_leaves.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
_averageDistance += (*itr)->_depth;
|
|
|
|
}
|
|
|
|
_averageDistance /= (float)_leaves.size();
|
|
|
|
|
|
|
|
}
|
|
|
|
return _averageDistance;
|
|
|
|
}
|
|
|
|
|
2002-09-18 22:57:01 +08:00
|
|
|
inline float getMinimumDistance() const
|
|
|
|
{
|
|
|
|
if (_minimumDistance==FLT_MAX && !_leaves.empty())
|
|
|
|
{
|
|
|
|
LeafList::const_iterator itr=_leaves.begin();
|
|
|
|
_minimumDistance = (*itr)->_depth;
|
|
|
|
++itr;
|
|
|
|
for(;
|
|
|
|
itr!=_leaves.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
if ((*itr)->_depth<_minimumDistance) _minimumDistance=(*itr)->_depth;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return _minimumDistance;
|
|
|
|
}
|
|
|
|
|
2002-05-28 18:24:43 +08:00
|
|
|
inline void sortFrontToBack()
|
|
|
|
{
|
2007-01-28 17:30:11 +08:00
|
|
|
std::sort(_leaves.begin(),_leaves.end(),LessDepthSortFunctor());
|
2002-05-28 18:24:43 +08:00
|
|
|
}
|
|
|
|
|
2005-10-13 20:51:00 +08:00
|
|
|
/** Reset the internal contents of a StateGraph, including deleting all children.*/
|
2001-09-20 05:19:47 +08:00
|
|
|
void reset();
|
|
|
|
|
2005-10-13 20:51:00 +08:00
|
|
|
/** Recursively clean the StateGraph of all its drawables, lights and depths.
|
2001-09-20 05:19:47 +08:00
|
|
|
* Leaves children intact, and ready to be populated again.*/
|
|
|
|
void clean();
|
|
|
|
|
2005-10-13 20:51:00 +08:00
|
|
|
/** Recursively prune the StateGraph of empty children.*/
|
2001-09-20 05:19:47 +08:00
|
|
|
void prune();
|
|
|
|
|
|
|
|
|
2005-10-13 20:51:00 +08:00
|
|
|
inline StateGraph* find_or_insert(const osg::StateSet* stateset)
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
|
|
|
// search for the appropriate state group, return it if found.
|
|
|
|
ChildList::iterator itr = _children.find(stateset);
|
|
|
|
if (itr!=_children.end()) return itr->second.get();
|
|
|
|
|
2001-09-28 20:36:40 +08:00
|
|
|
// create a state group and insert it into the children list
|
2001-09-20 05:19:47 +08:00
|
|
|
// then return the state group.
|
2005-10-13 20:51:00 +08:00
|
|
|
StateGraph* sg = new StateGraph(this,stateset);
|
2001-09-20 05:19:47 +08:00
|
|
|
_children[stateset] = sg;
|
|
|
|
return sg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** add a render leaf.*/
|
|
|
|
inline void addLeaf(RenderLeaf* leaf)
|
|
|
|
{
|
|
|
|
if (leaf)
|
|
|
|
{
|
2002-05-28 18:24:43 +08:00
|
|
|
_averageDistance = FLT_MAX; // signify dirty.
|
2002-09-18 22:57:01 +08:00
|
|
|
_minimumDistance = FLT_MAX; // signify dirty.
|
2001-09-20 05:19:47 +08:00
|
|
|
_leaves.push_back(leaf);
|
|
|
|
leaf->_parent = this;
|
2007-01-30 06:44:29 +08:00
|
|
|
if (_dynamic) leaf->_dynamic = true;
|
2001-09-20 05:19:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-13 20:51:00 +08:00
|
|
|
static inline void moveStateGraph(osg::State& state,StateGraph* sg_curr,StateGraph* sg_new)
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
|
|
|
if (sg_new==sg_curr || sg_new==NULL) return;
|
|
|
|
|
|
|
|
if (sg_curr==NULL)
|
|
|
|
{
|
|
|
|
|
|
|
|
// use return path to trace back steps to sg_new.
|
2005-10-13 20:51:00 +08:00
|
|
|
std::vector<StateGraph*> return_path;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
// need to pop back root render graph.
|
|
|
|
do
|
|
|
|
{
|
|
|
|
return_path.push_back(sg_new);
|
|
|
|
sg_new = sg_new->_parent;
|
|
|
|
} while (sg_new);
|
|
|
|
|
2005-10-13 20:51:00 +08:00
|
|
|
for(std::vector<StateGraph*>::reverse_iterator itr=return_path.rbegin();
|
2001-09-20 05:19:47 +08:00
|
|
|
itr!=return_path.rend();
|
|
|
|
++itr)
|
|
|
|
{
|
2005-10-13 20:51:00 +08:00
|
|
|
StateGraph* rg = (*itr);
|
2007-09-01 00:05:24 +08:00
|
|
|
if (rg->getStateSet()) state.pushStateSet(rg->getStateSet());
|
2001-09-20 05:19:47 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// first handle the typical case which is two state groups
|
|
|
|
// are neighbours.
|
|
|
|
if (sg_curr->_parent==sg_new->_parent)
|
|
|
|
{
|
|
|
|
|
|
|
|
// state has changed so need to pop old state.
|
2007-09-01 00:05:24 +08:00
|
|
|
if (sg_curr->getStateSet()) state.popStateSet();
|
2001-09-20 05:19:47 +08:00
|
|
|
// and push new state.
|
2007-09-01 00:05:24 +08:00
|
|
|
if (sg_new->getStateSet()) state.pushStateSet(sg_new->getStateSet());
|
2001-09-20 05:19:47 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-28 20:36:40 +08:00
|
|
|
// need to pop back up to the same depth as the new state group.
|
2001-09-20 05:19:47 +08:00
|
|
|
while (sg_curr->_depth>sg_new->_depth)
|
|
|
|
{
|
2007-09-01 00:05:24 +08:00
|
|
|
if (sg_curr->getStateSet()) state.popStateSet();
|
2001-09-20 05:19:47 +08:00
|
|
|
sg_curr = sg_curr->_parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
// use return path to trace back steps to sg_new.
|
2005-10-13 20:51:00 +08:00
|
|
|
std::vector<StateGraph*> return_path;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2001-09-28 20:36:40 +08:00
|
|
|
// need to pop back up to the same depth as the curr state group.
|
2001-09-20 05:19:47 +08:00
|
|
|
while (sg_new->_depth>sg_curr->_depth)
|
|
|
|
{
|
|
|
|
return_path.push_back(sg_new);
|
|
|
|
sg_new = sg_new->_parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now pop back up both parent paths until they agree.
|
2002-10-23 15:46:11 +08:00
|
|
|
|
|
|
|
// DRT - 10/22/02
|
2005-10-13 20:51:00 +08:00
|
|
|
// should be this to conform with above case where two StateGraph
|
2002-10-23 15:46:11 +08:00
|
|
|
// nodes have the same parent
|
|
|
|
while (sg_curr != sg_new)
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
2007-09-01 00:05:24 +08:00
|
|
|
if (sg_curr->getStateSet()) state.popStateSet();
|
2001-09-20 05:19:47 +08:00
|
|
|
sg_curr = sg_curr->_parent;
|
|
|
|
|
|
|
|
return_path.push_back(sg_new);
|
|
|
|
sg_new = sg_new->_parent;
|
|
|
|
}
|
|
|
|
|
2005-10-13 20:51:00 +08:00
|
|
|
for(std::vector<StateGraph*>::reverse_iterator itr=return_path.rbegin();
|
2001-09-20 05:19:47 +08:00
|
|
|
itr!=return_path.rend();
|
|
|
|
++itr)
|
|
|
|
{
|
2005-10-13 20:51:00 +08:00
|
|
|
StateGraph* rg = (*itr);
|
2007-09-01 00:05:24 +08:00
|
|
|
if (rg->getStateSet()) state.pushStateSet(rg->getStateSet());
|
2001-09-20 05:19:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-10-13 20:51:00 +08:00
|
|
|
inline static void moveToRootStateGraph(osg::State& state,StateGraph* sg_curr)
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
|
|
|
// need to pop back all statesets and matrices.
|
|
|
|
while (sg_curr)
|
|
|
|
{
|
2007-09-01 00:05:24 +08:00
|
|
|
if (sg_curr->getStateSet()) state.popStateSet();
|
2001-09-20 05:19:47 +08:00
|
|
|
sg_curr = sg_curr->_parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-04-25 18:32:28 +08:00
|
|
|
inline static int numToPop(StateGraph* sg_curr)
|
2007-04-16 20:18:56 +08:00
|
|
|
{
|
|
|
|
int numToPop = 0;
|
|
|
|
// need to pop back all statesets and matrices.
|
|
|
|
while (sg_curr)
|
|
|
|
{
|
2007-09-01 00:05:24 +08:00
|
|
|
if (sg_curr->getStateSet()) ++numToPop;
|
2007-04-16 20:18:56 +08:00
|
|
|
sg_curr = sg_curr->_parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
return numToPop;
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
private:
|
|
|
|
|
|
|
|
/// disallow copy construction.
|
2005-10-13 20:51:00 +08:00
|
|
|
StateGraph(const StateGraph&):osg::Referenced() {}
|
2001-09-20 05:19:47 +08:00
|
|
|
/// disallow copy operator.
|
2005-10-13 20:51:00 +08:00
|
|
|
StateGraph& operator = (const StateGraph&) { return *this; }
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2002-02-03 20:33:41 +08:00
|
|
|
}
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|