1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
15#define OSG_CULLSTACK 1
17#include <osg/CullingSet>
18#include <osg/CullSettings>
19#include <osg/Viewport>
20#include <osg/fast_back_stack>
21#include <osg/Transform>
25/** A CullStack class which accumulates the current project, modelview matrices
27class OSG_EXPORT CullStack : public osg::CullSettings
34 CullStack(const CullStack& cs);
38 typedef std::vector<ShadowVolumeOccluder> OccluderList;
42 void pushCullingSet();
45 void setOccluderList(const ShadowVolumeOccluderList& svol) { _occluderList = svol; }
46 ShadowVolumeOccluderList& getOccluderList() { return _occluderList; }
47 const ShadowVolumeOccluderList& getOccluderList() const { return _occluderList; }
49 void pushViewport(osg::Viewport* viewport);
52 void pushProjectionMatrix(osg::RefMatrix* matrix);
53 void popProjectionMatrix();
55 void pushModelViewMatrix(osg::RefMatrix* matrix, Transform::ReferenceFrame referenceFrame);
56 void popModelViewMatrix();
58 inline float getFrustumVolume() { if (_frustumVolume<0.0f) computeFrustumVolume(); return _frustumVolume; }
61 /** Compute the pixel size of an object at position v, with specified radius.*/
62 float pixelSize(const Vec3& v,float radius) const
64 return getCurrentCullingSet().pixelSize(v,radius);
67 /** Compute the pixel size of the bounding sphere.*/
68 float pixelSize(const BoundingSphere& bs) const
70 return pixelSize(bs.center(),bs.radius());
73 /** Compute the pixel size of an object at position v, with specified radius. fabs()ed to always be positive. */
74 float clampedPixelSize(const Vec3& v,float radius) const
76 return getCurrentCullingSet().clampedPixelSize(v,radius);
79 /** Compute the pixel size of the bounding sphere. fabs()ed to always be positive. */
80 float clampedPixelSize(const BoundingSphere& bs) const
82 return clampedPixelSize(bs.center(),bs.radius());
85 inline void disableAndPushOccludersCurrentMask(NodePath& nodePath)
87 getCurrentCullingSet().disableAndPushOccludersCurrentMask(nodePath);
90 inline void popOccludersCurrentMask(NodePath& nodePath)
92 getCurrentCullingSet().popOccludersCurrentMask(nodePath);
95 inline bool isCulled(const std::vector<Vec3>& vertices)
97 return getCurrentCullingSet().isCulled(vertices);
100 inline bool isCulled(const BoundingBox& bb)
102 return bb.valid() && getCurrentCullingSet().isCulled(bb);
105 inline bool isCulled(const BoundingSphere& bs)
107 return getCurrentCullingSet().isCulled(bs);
110 inline bool isCulled(const osg::Node& node)
112 if (node.isCullingActive())
114 return getCurrentCullingSet().isCulled(node.getBound());
118 getCurrentCullingSet().resetCullingMask();
123 inline void pushCurrentMask()
125 getCurrentCullingSet().pushCurrentMask();
128 inline void popCurrentMask()
130 getCurrentCullingSet().popCurrentMask();
134 typedef std::vector< CullingSet > CullingStack;
136 inline CullingStack& getClipSpaceCullingStack() { return _clipspaceCullingStack; }
138 inline CullingStack& getProjectionCullingStack() { return _projectionCullingStack; }
140 inline CullingStack& getModelViewCullingStack() { return _modelviewCullingStack; }
142 inline CullingSet& getCurrentCullingSet() { return *_back_modelviewCullingStack; }
143 inline const CullingSet& getCurrentCullingSet() const { return *_back_modelviewCullingStack; }
145 inline osg::Viewport* getViewport();
146 inline const osg::Viewport* getViewport() const;
148 inline osg::RefMatrix* getModelViewMatrix();
149 inline const osg::RefMatrix* getModelViewMatrix() const;
151 inline osg::RefMatrix* getProjectionMatrix();
152 inline const osg::RefMatrix* getProjectionMatrix() const;
154 inline osg::Matrix getWindowMatrix() const;
155 inline const osg::RefMatrix* getMVPW();
157 inline const osg::Vec3& getReferenceViewPoint() const { return _referenceViewPoints.back(); }
158 inline void pushReferenceViewPoint(const osg::Vec3& viewPoint) { _referenceViewPoints.push_back(viewPoint); }
159 inline void popReferenceViewPoint() { _referenceViewPoints.pop_back(); }
161 inline const osg::Vec3& getEyeLocal() const { return _eyePointStack.back(); }
163 inline const osg::Vec3& getViewPointLocal() const { return _viewPointStack.back(); }
165 inline const osg::Vec3 getUpLocal() const
167 const osg::Matrix& matrix = *_modelviewStack.back();
168 return osg::Vec3(matrix(0,1),matrix(1,1),matrix(2,1));
171 inline const osg::Vec3 getLookVectorLocal() const
173 const osg::Matrix& matrix = *_modelviewStack.back();
174 return osg::Vec3(-matrix(0,2),-matrix(1,2),-matrix(2,2));
177 typedef fast_back_stack< ref_ptr<RefMatrix> > MatrixStack;
179 MatrixStack& getProjectionStack() { return _projectionStack; }
180 const MatrixStack& getProjectionStack() const { return _projectionStack; }
182 MatrixStack& getModelViewStack() { return _modelviewStack; }
183 const MatrixStack& getModelViewStack() const { return _modelviewStack; }
185 MatrixStack& getMVPWStack() { return _MVPW_Stack; }
186 const MatrixStack& getMVPWStack() const { return _MVPW_Stack; }
190 // base set of shadow volume occluder to use in culling.
191 ShadowVolumeOccluderList _occluderList;
194 MatrixStack _projectionStack;
196 MatrixStack _modelviewStack;
197 MatrixStack _MVPW_Stack;
199 typedef fast_back_stack<ref_ptr<Viewport> > ViewportStack;
200 ViewportStack _viewportStack;
202 typedef fast_back_stack<Vec3> EyePointStack;
203 EyePointStack _referenceViewPoints;
204 EyePointStack _eyePointStack;
205 EyePointStack _viewPointStack;
207 CullingStack _clipspaceCullingStack;
208 CullingStack _projectionCullingStack;
210 CullingStack _modelviewCullingStack;
211 unsigned int _index_modelviewCullingStack;
212 CullingSet* _back_modelviewCullingStack;
214 void computeFrustumVolume();
215 float _frustumVolume;
217 unsigned int _bbCornerNear;
218 unsigned int _bbCornerFar;
220 ref_ptr<osg::RefMatrix> _identity;
222 typedef std::vector< osg::ref_ptr<osg::RefMatrix> > MatrixList;
223 MatrixList _reuseMatrixList;
224 unsigned int _currentReuseMatrixIndex;
226 inline osg::RefMatrix* createOrReuseMatrix(const osg::Matrix& value);
231inline osg::Viewport* CullStack::getViewport()
233 return _viewportStack.empty() ? 0 : _viewportStack.back().get();
236inline const osg::Viewport* CullStack::getViewport() const
238 return _viewportStack.empty() ? 0 : _viewportStack.back().get();
241inline osg::RefMatrix* CullStack::getModelViewMatrix()
243 return _modelviewStack.empty() ? _identity.get() : _modelviewStack.back().get();
246inline const osg::RefMatrix* CullStack::getModelViewMatrix() const
248 return _modelviewStack.empty() ? _identity.get() : _modelviewStack.back().get();
251inline osg::RefMatrix* CullStack::getProjectionMatrix()
253 return _projectionStack.empty() ? _identity.get() : _projectionStack.back().get();
256inline const osg::RefMatrix* CullStack::getProjectionMatrix() const
258 return _projectionStack.empty() ? _identity.get() : _projectionStack.back().get();
261inline osg::Matrix CullStack::getWindowMatrix() const
263 if (!_viewportStack.empty())
265 osg::Viewport* viewport = _viewportStack.back().get();
266 return viewport->computeWindowMatrix();
274inline const osg::RefMatrix* CullStack::getMVPW()
276 if (!_MVPW_Stack.empty())
278 if (!_MVPW_Stack.back())
280 _MVPW_Stack.back() = createOrReuseMatrix(*getModelViewMatrix());
281 (*_MVPW_Stack.back()) *= *(getProjectionMatrix());
282 (*_MVPW_Stack.back()) *= getWindowMatrix();
284 return _MVPW_Stack.back().get();
288 return _identity.get();
292inline RefMatrix* CullStack::createOrReuseMatrix(const osg::Matrix& value)
294 // skip of any already reused matrix.
295 while (_currentReuseMatrixIndex<_reuseMatrixList.size() &&
296 _reuseMatrixList[_currentReuseMatrixIndex]->referenceCount()>1)
298 ++_currentReuseMatrixIndex;
301 // if still within list, element must be singularly referenced
302 // there return it to be reused.
303 if (_currentReuseMatrixIndex<_reuseMatrixList.size())
305 RefMatrix* matrix = _reuseMatrixList[_currentReuseMatrixIndex++].get();
310 // otherwise need to create new matrix.
311 osg::RefMatrix* matrix = new RefMatrix(value);
312 _reuseMatrixList.push_back(matrix);
313 ++_currentReuseMatrixIndex;