Added support for a sort callback in RenderBin.

This commit is contained in:
Robert Osfield 2002-08-03 16:49:42 +00:00
parent 067c10b49c
commit 1ba901cf64
2 changed files with 125 additions and 14 deletions

View File

@ -55,7 +55,30 @@ class OSGUTIL_EXPORT RenderBin : public osg::Object
void sort();
enum SortMode
{
SORT_BY_STATE,
SORT_FRONT_TO_BACK,
SORT_BACK_TO_FONT
};
void setSortMode(SortMode mode) { _sortMode = mode; }
SortMode getSortMode() const { return _sortMode; }
virtual void sort_local();
virtual void sort_local_by_state();
virtual void sort_local_front_to_back();
virtual void sort_local_back_to_front();
struct SortCallback : public osg::Referenced
{
virtual void sort(RenderBin* bin) {};
};
void setSortLocalCallback(SortCallback* sortCallback) { _sortLocalCallback = sortCallback; }
SortCallback* getSortLocalCallback() { return _sortLocalCallback.get(); }
const SortCallback* getSortLocalCallback() const { return _sortLocalCallback.get(); }
virtual void draw(osg::State& state,RenderLeaf*& previous);
@ -69,14 +92,21 @@ class OSGUTIL_EXPORT RenderBin : public osg::Object
public:
void copyLeavesFromRenderGraphListToRenderLeafList();
int _binNum;
RenderBin* _parent;
RenderStage* _stage;
RenderBinList _bins;
RenderGraphList _renderGraphList;
RenderLeafList _renderLeafList;
int _binNum;
RenderBin* _parent;
RenderStage* _stage;
RenderBinList _bins;
RenderGraphList _renderGraphList;
RenderLeafList _renderLeafList;
SortMode _sortMode;
osg::ref_ptr<SortCallback> _sortLocalCallback;
typedef std::map< std::string, osg::ref_ptr<RenderBin> > RenderBinPrototypeList;
static RenderBinPrototypeList s_renderBinPrototypeList;

View File

@ -78,24 +78,105 @@ void RenderBin::sort()
{
itr->second->sort();
}
sort_local();
if (_sortLocalCallback.valid()) _sortLocalCallback->sort(this);
else sort_local();
}
struct StateSortFunctor
void RenderBin::sort_local()
{
const bool operator() (const RenderGraph* lhs,const RenderGraph* rhs)
switch(_sortMode)
{
case(SORT_BY_STATE):
sort_local_by_state();
break;
case(SORT_FRONT_TO_BACK):
sort_local_front_to_back();
break;
case(SORT_BACK_TO_FONT):
sort_local_back_to_front();
break;
default:
break;
}
}
struct SortByStateFunctor
{
const bool operator() (const RenderGraph* lhs,const RenderGraph* rhs) const
{
return (*(lhs->_stateset)<*(rhs->_stateset));
}
};
void RenderBin::sort_local()
void RenderBin::sort_local_by_state()
{
// actually we'll do nothing right now, as fine grained sorting by state
// appears to cost more to do than it saves in draw. The contents of
// the RenderGraph leaves is already coasrse grained sorted, this
// sorting is as a function of the cull traversal.
}
struct FrontToBackSortFunctor
{
bool operator() (const RenderLeaf* lhs,const RenderLeaf* rhs) const
{
return (lhs->_depth<rhs->_depth);
}
};
void RenderBin::sort_local_front_to_back()
{
copyLeavesFromRenderGraphListToRenderLeafList();
// now sort the list into acending depth order.
// std::sort(_renderGraphList.begin(),_renderGraphList.end(),StateSortFunctor());
std::sort(_renderLeafList.begin(),_renderLeafList.end(),FrontToBackSortFunctor());
}
struct BackToFrontSortFunctor
{
const bool operator() (const RenderLeaf* lhs,const RenderLeaf* rhs) const
{
return (rhs->_depth<lhs->_depth);
}
};
void RenderBin::sort_local_back_to_front()
{
copyLeavesFromRenderGraphListToRenderLeafList();
// now sort the list into acending depth order.
std::sort(_renderLeafList.begin(),_renderLeafList.end(),BackToFrontSortFunctor());
}
void RenderBin::copyLeavesFromRenderGraphListToRenderLeafList()
{
_renderLeafList.clear();
int totalsize=0;
RenderGraphList::iterator itr;
for(itr=_renderGraphList.begin();
itr!=_renderGraphList.end();
++itr)
{
totalsize += (*itr)->_leaves.size();
}
_renderLeafList.reserve(totalsize);
// first copy all the leaves from the render graphs into the leaf list.
for(itr=_renderGraphList.begin();
itr!=_renderGraphList.end();
++itr)
{
for(RenderGraph::LeafList::iterator dw_itr = (*itr)->_leaves.begin();
dw_itr != (*itr)->_leaves.end();
++dw_itr)
{
_renderLeafList.push_back(dw_itr->get());
}
}
}
RenderBin* RenderBin::find_or_insert(int binNum,const std::string& binName)