diff --git a/VisualStudio/osg/osg.dsp b/VisualStudio/osg/osg.dsp index 286f1c6c2..ab5739055 100755 --- a/VisualStudio/osg/osg.dsp +++ b/VisualStudio/osg/osg.dsp @@ -581,6 +581,10 @@ SOURCE=..\..\Include\Osg\Quat # End Source File # Begin Source File +SOURCE=..\..\include\osg\fast_back_stack +# End Source File +# Begin Source File + SOURCE=..\..\include\osg\ref_ptr # End Source File # Begin Source File diff --git a/include/osg/fast_back_stack b/include/osg/fast_back_stack new file mode 100644 index 000000000..47472767e --- /dev/null +++ b/include/osg/fast_back_stack @@ -0,0 +1,67 @@ +//C++ header - Open Scene Graph - Copyright (C) 1998-2001 Robert Osfield +//Distributed under the terms of the GNU Library General Public License (LGPL) +//as published by the Free Software Foundation. + +#ifndef OSG_FAST_BACK_STACK +#define OSG_FAST_BACK_STACK 1 + +namespace osg { + +/** Simple stack implementation that keeps the back() cached locally for fast access + * rather than at the back of the vector which is the traditional stack implementation. + * A conventional std::vector<> stores the rest of the stack. The fast_back_stack + * although contains a stl container it only implments the back push_back(),pop_back() + * and back() methods so is not as general purpose as stl stack implementation. + * The focus of the fast_back_stack is purly to maximize the speed at which the + * back can be accessed.*/ + +template +class fast_back_stack +{ + public: + + inline fast_back_stack():_value(),_stack(),_size(0) {} + + inline fast_back_stack(const T& value):_value(value),_stack(),_size(1) {} + + inline void clear() { _stack.clear(); _size = 0; } + + inline bool empty() const { return _size==0; } + + inline unsigned int size() const { return _size; } + + inline T& back() { return _value; } + + inline const T& back() const { return _value; } + + inline void push_back(const T& value) + { + if (_size>0) + { + _stack.push_back(_value); + } + _value = value; + ++_size; + } + + inline void pop_back() + { + if (_size>0) + { + if (!_stack.empty()) + { + _value = _stack.back(); + _stack.pop_back(); + } + --_size; + } // else error condition. + } + + T _value; + std::vector _stack; + unsigned int _size; +}; + +} + +#endif diff --git a/include/osgUtil/CullVisitor b/include/osgUtil/CullVisitor index d144e5e04..69cf9bfee 100644 --- a/include/osgUtil/CullVisitor +++ b/include/osgUtil/CullVisitor @@ -16,6 +16,8 @@ #include #include +#include + #include #include @@ -329,43 +331,48 @@ class OSGUTIL_EXPORT CullVisitor : public osg::NodeVisitor void popClippingVolume(); - typedef std::vector ClippingVolumeStack; - typedef std::vector > MatrixStack; +// typedef std::vector ClippingVolumeStack; +// typedef std::vector > MatrixStack; - MatrixStack _projectionStack; - MatrixStack _PW_Stack; - ClippingVolumeStack _projectionClippingVolumeStack; + typedef osg::fast_back_stack ClippingVolumeStack; + typedef osg::fast_back_stack< osg::ref_ptr > MatrixStack; - MatrixStack _modelviewStack; - MatrixStack _MVPW_Stack; - ClippingVolumeStack _modelviewClippingVolumeStack; + MatrixStack _projectionStack; + MatrixStack _PW_Stack; + ClippingVolumeStack _projectionClippingVolumeStack; + + MatrixStack _modelviewStack; + MatrixStack _MVPW_Stack; + ClippingVolumeStack _modelviewClippingVolumeStack; - bool _windowToModelFactorDirty; - float _windowToModelFactor; + bool _windowToModelFactorDirty; + float _windowToModelFactor; - typedef std::vector > ViewportStack; - ViewportStack _viewportStack; +// typedef std::vector > ViewportStack; + typedef osg::fast_back_stack > ViewportStack; + ViewportStack _viewportStack; - typedef std::vector EyePointStack; - EyePointStack _eyePointStack; +// typedef std::vector EyePointStack; + typedef osg::fast_back_stack EyePointStack; + EyePointStack _eyePointStack; - typedef std::vector CullingModeStack; - CullingModeStack _cullingModeStack; + typedef osg::fast_back_stack CullingModeStack; + CullingModeStack _cullingModeStack; - unsigned int _bbCornerNear; - unsigned int _bbCornerFar; + unsigned int _bbCornerNear; + unsigned int _bbCornerFar; - osg::Matrix _identity; + osg::Matrix _identity; - osg::ref_ptr _rootRenderGraph; - RenderGraph* _currentRenderGraph; + osg::ref_ptr _rootRenderGraph; + RenderGraph* _currentRenderGraph; - osg::ref_ptr _rootRenderStage; - RenderBin* _currentRenderBin; + osg::ref_ptr _rootRenderStage; + RenderBin* _currentRenderBin; - float _LODBias; - float _smallFeatureCullingPixelSize; + float _LODBias; + float _smallFeatureCullingPixelSize; ComputeNearFarMode _computeNearFar; diff --git a/include/osgUtil/DepthSortedBin b/include/osgUtil/DepthSortedBin index 7e282d7dd..fe1e93633 100644 --- a/include/osgUtil/DepthSortedBin +++ b/include/osgUtil/DepthSortedBin @@ -42,7 +42,7 @@ class OSGUTIL_EXPORT DepthSortedBin : public RenderBin virtual ~DepthSortedBin(); DrawOrder _drawOrder; - RenderLeafList _renderLeafList; + //RenderLeafList _renderLeafList; }; } diff --git a/include/osgUtil/IntersectVisitor b/include/osgUtil/IntersectVisitor index 7e9ea2de4..3bd3cf5d3 100644 --- a/include/osgUtil/IntersectVisitor +++ b/include/osgUtil/IntersectVisitor @@ -64,9 +64,7 @@ class OSGUTIL_EXPORT Hit }; -/** Basic visitor for ray based collisions of a scene. - Note, still in development, current version has not - practical functionality!*/ +/** Basic visitor for ray based collisions of a scene.*/ class OSGUTIL_EXPORT IntersectVisitor : public osg::NodeVisitor { public: diff --git a/include/osgUtil/RenderBin b/include/osgUtil/RenderBin index 0218518d8..0d698756e 100644 --- a/include/osgUtil/RenderBin +++ b/include/osgUtil/RenderBin @@ -72,6 +72,7 @@ class OSGUTIL_EXPORT RenderBin : public osg::Object RenderStage* _stage; RenderBinList _bins; RenderGraphList _renderGraphList; + RenderLeafList _renderLeafList; typedef std::map< std::string, osg::ref_ptr > RenderBinPrototypeList; static RenderBinPrototypeList s_renderBinPrototypeList; diff --git a/include/osgUtil/RenderGraph b/include/osgUtil/RenderGraph index 1a272e559..f82b34a45 100644 --- a/include/osgUtil/RenderGraph +++ b/include/osgUtil/RenderGraph @@ -15,9 +15,17 @@ #include #include +#include namespace osgUtil { +struct LeafDepthSortFunctor +{ + const bool operator() (const osg::ref_ptr& lhs,const osg::ref_ptr& rhs) + { + return (lhs->_depth>rhs->_depth); + } +}; class OSGUTIL_EXPORT RenderGraph : public osg::Referenced { @@ -34,13 +42,16 @@ class OSGUTIL_EXPORT RenderGraph : public osg::Referenced ChildList _children; LeafList _leaves; + mutable float _averageDistance; + osg::ref_ptr _userData; RenderGraph(): _parent(NULL), _stateset(NULL), - _depth(0) + _depth(0), + _averageDistance(0) { } @@ -72,6 +83,29 @@ class OSGUTIL_EXPORT RenderGraph : public osg::Referenced return _leaves.empty(); } + + inline const float getAverageDistance() const + { + 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; + } + + inline void sortFrontToBack() + { + std::sort(_leaves.begin(),_leaves.end(),LeafDepthSortFunctor()); + } + /** reset the internal contents of a RenderGraph, including deleting all children.*/ void reset(); @@ -101,6 +135,7 @@ class OSGUTIL_EXPORT RenderGraph : public osg::Referenced { if (leaf) { + _averageDistance = FLT_MAX; // signify dirty. _leaves.push_back(leaf); leaf->_parent = this; } diff --git a/src/osgUtil/CullVisitor.cpp b/src/osgUtil/CullVisitor.cpp index ad09efd78..9b4f25588 100644 --- a/src/osgUtil/CullVisitor.cpp +++ b/src/osgUtil/CullVisitor.cpp @@ -135,8 +135,8 @@ void CullVisitor::reset() _eyePointStack.clear(); // remove all accept the first element of the stack. - _cullingModeStack.erase(_cullingModeStack.begin()+1,_cullingModeStack.end()); - + //_cullingModeStack.erase(_cullingModeStack.begin()+1,_cullingModeStack.end()); + _cullingModeStack.clear(); // reset the calculated near far planes. _computeNearFar = COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES;