2012-03-22 01:36:20 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2003-01-22 00:45:36 +08:00
|
|
|
*
|
2012-03-22 01:36:20 +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
|
2003-01-22 00:45:36 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2012-03-22 01:36:20 +08:00
|
|
|
*
|
2003-01-22 00:45:36 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-03-22 01:36:20 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2003-01-22 00:45:36 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2002-06-10 19:21:21 +08:00
|
|
|
|
|
|
|
#ifndef OSG_CULLSTACK
|
|
|
|
#define OSG_CULLSTACK 1
|
|
|
|
|
|
|
|
#include <osg/CullingSet>
|
2004-04-30 23:35:31 +08:00
|
|
|
#include <osg/CullSettings>
|
2002-06-10 19:21:21 +08:00
|
|
|
#include <osg/Viewport>
|
|
|
|
#include <osg/fast_back_stack>
|
2007-02-08 00:32:14 +08:00
|
|
|
#include <osg/Transform>
|
2002-06-10 19:21:21 +08:00
|
|
|
|
|
|
|
namespace osg {
|
|
|
|
|
|
|
|
/** A CullStack class which accumulates the current project, modelview matrices
|
|
|
|
and the CullingSet. */
|
2005-04-12 01:14:17 +08:00
|
|
|
class OSG_EXPORT CullStack : public osg::CullSettings
|
2002-06-10 19:21:21 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
2012-03-22 01:36:20 +08:00
|
|
|
|
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
CullStack();
|
2007-08-11 01:44:54 +08:00
|
|
|
CullStack(const CullStack& cs);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
~CullStack();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
typedef std::vector<ShadowVolumeOccluder> OccluderList;
|
|
|
|
|
|
|
|
void reset();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2009-01-28 23:15:19 +08:00
|
|
|
void pushCullingSet();
|
|
|
|
void popCullingSet();
|
|
|
|
|
2002-06-14 00:21:00 +08:00
|
|
|
void setOccluderList(const ShadowVolumeOccluderList& svol) { _occluderList = svol; }
|
|
|
|
ShadowVolumeOccluderList& getOccluderList() { return _occluderList; }
|
|
|
|
const ShadowVolumeOccluderList& getOccluderList() const { return _occluderList; }
|
2002-06-10 19:21:21 +08:00
|
|
|
|
|
|
|
void pushViewport(osg::Viewport* viewport);
|
|
|
|
void popViewport();
|
|
|
|
|
2003-01-10 17:25:42 +08:00
|
|
|
void pushProjectionMatrix(osg::RefMatrix* matrix);
|
2002-06-10 19:21:21 +08:00
|
|
|
void popProjectionMatrix();
|
|
|
|
|
2007-02-08 00:32:14 +08:00
|
|
|
void pushModelViewMatrix(osg::RefMatrix* matrix, Transform::ReferenceFrame referenceFrame);
|
2002-06-10 19:21:21 +08:00
|
|
|
void popModelViewMatrix();
|
|
|
|
|
2002-06-15 20:14:42 +08:00
|
|
|
inline float getFrustumVolume() { if (_frustumVolume<0.0f) computeFrustumVolume(); return _frustumVolume; }
|
|
|
|
|
|
|
|
|
2005-04-04 21:22:24 +08:00
|
|
|
/** Compute the pixel size of an object at position v, with specified radius.*/
|
2002-06-10 19:21:21 +08:00
|
|
|
float pixelSize(const Vec3& v,float radius) const
|
|
|
|
{
|
2003-09-26 05:54:33 +08:00
|
|
|
return getCurrentCullingSet().pixelSize(v,radius);
|
2002-06-10 19:21:21 +08:00
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-04-04 21:22:24 +08:00
|
|
|
/** Compute the pixel size of the bounding sphere.*/
|
2002-06-10 19:21:21 +08:00
|
|
|
float pixelSize(const BoundingSphere& bs) const
|
|
|
|
{
|
|
|
|
return pixelSize(bs.center(),bs.radius());
|
|
|
|
}
|
|
|
|
|
2005-09-28 21:33:58 +08:00
|
|
|
/** Compute the pixel size of an object at position v, with specified radius. fabs()ed to always be positive. */
|
|
|
|
float clampedPixelSize(const Vec3& v,float radius) const
|
|
|
|
{
|
|
|
|
return getCurrentCullingSet().clampedPixelSize(v,radius);
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-09-28 21:33:58 +08:00
|
|
|
/** Compute the pixel size of the bounding sphere. fabs()ed to always be positive. */
|
|
|
|
float clampedPixelSize(const BoundingSphere& bs) const
|
|
|
|
{
|
|
|
|
return clampedPixelSize(bs.center(),bs.radius());
|
|
|
|
}
|
|
|
|
|
2002-06-17 17:10:26 +08:00
|
|
|
inline void disableAndPushOccludersCurrentMask(NodePath& nodePath)
|
2002-06-10 19:21:21 +08:00
|
|
|
{
|
2003-09-26 05:54:33 +08:00
|
|
|
getCurrentCullingSet().disableAndPushOccludersCurrentMask(nodePath);
|
2002-06-17 17:10:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void popOccludersCurrentMask(NodePath& nodePath)
|
|
|
|
{
|
2003-09-26 05:54:33 +08:00
|
|
|
getCurrentCullingSet().popOccludersCurrentMask(nodePath);
|
2002-06-10 19:21:21 +08:00
|
|
|
}
|
|
|
|
|
2002-06-12 17:22:30 +08:00
|
|
|
inline bool isCulled(const std::vector<Vec3>& vertices)
|
|
|
|
{
|
2003-09-26 05:54:33 +08:00
|
|
|
return getCurrentCullingSet().isCulled(vertices);
|
2002-06-12 17:22:30 +08:00
|
|
|
}
|
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
inline bool isCulled(const BoundingBox& bb)
|
|
|
|
{
|
2003-09-26 05:54:33 +08:00
|
|
|
return bb.valid() && getCurrentCullingSet().isCulled(bb);
|
2002-06-10 19:21:21 +08:00
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
inline bool isCulled(const BoundingSphere& bs)
|
|
|
|
{
|
2003-09-26 05:54:33 +08:00
|
|
|
return getCurrentCullingSet().isCulled(bs);
|
2002-06-10 19:21:21 +08:00
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
inline bool isCulled(const osg::Node& node)
|
|
|
|
{
|
2013-01-28 22:30:21 +08:00
|
|
|
if (node.isCullingActive())
|
|
|
|
{
|
|
|
|
return getCurrentCullingSet().isCulled(node.getBound());
|
|
|
|
}
|
2016-07-02 21:45:10 +08:00
|
|
|
else
|
2013-01-28 22:30:21 +08:00
|
|
|
{
|
|
|
|
getCurrentCullingSet().resetCullingMask();
|
|
|
|
return false;
|
|
|
|
}
|
2002-06-10 19:21:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void pushCurrentMask()
|
|
|
|
{
|
2003-09-26 05:54:33 +08:00
|
|
|
getCurrentCullingSet().pushCurrentMask();
|
2002-06-10 19:21:21 +08:00
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
inline void popCurrentMask()
|
|
|
|
{
|
2003-09-26 05:54:33 +08:00
|
|
|
getCurrentCullingSet().popCurrentMask();
|
2002-06-10 19:21:21 +08:00
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
|
|
|
|
2003-09-26 19:20:43 +08:00
|
|
|
typedef std::vector< CullingSet > CullingStack;
|
2002-06-14 00:21:00 +08:00
|
|
|
|
2003-09-26 05:54:33 +08:00
|
|
|
inline CullingStack& getClipSpaceCullingStack() { return _clipspaceCullingStack; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-09-26 05:54:33 +08:00
|
|
|
inline CullingStack& getProjectionCullingStack() { return _projectionCullingStack; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-09-26 05:54:33 +08:00
|
|
|
inline CullingStack& getModelViewCullingStack() { return _modelviewCullingStack; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-09-26 19:20:43 +08:00
|
|
|
inline CullingSet& getCurrentCullingSet() { return *_back_modelviewCullingStack; }
|
|
|
|
inline const CullingSet& getCurrentCullingSet() const { return *_back_modelviewCullingStack; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
inline osg::Viewport* getViewport();
|
2017-04-26 18:50:35 +08:00
|
|
|
inline const osg::Viewport* getViewport() const;
|
|
|
|
|
2007-02-21 21:48:01 +08:00
|
|
|
inline osg::RefMatrix* getModelViewMatrix();
|
2017-04-26 18:50:35 +08:00
|
|
|
inline const osg::RefMatrix* getModelViewMatrix() const;
|
|
|
|
|
2007-02-21 21:48:01 +08:00
|
|
|
inline osg::RefMatrix* getProjectionMatrix();
|
2017-04-26 18:50:35 +08:00
|
|
|
inline const osg::RefMatrix* getProjectionMatrix() const;
|
|
|
|
|
2016-07-02 21:45:10 +08:00
|
|
|
inline osg::Matrix getWindowMatrix() const;
|
2007-02-21 21:48:01 +08:00
|
|
|
inline const osg::RefMatrix* getMVPW();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2006-12-16 01:27:18 +08:00
|
|
|
inline const osg::Vec3& getReferenceViewPoint() const { return _referenceViewPoints.back(); }
|
|
|
|
inline void pushReferenceViewPoint(const osg::Vec3& viewPoint) { _referenceViewPoints.push_back(viewPoint); }
|
|
|
|
inline void popReferenceViewPoint() { _referenceViewPoints.pop_back(); }
|
2002-06-10 19:21:21 +08:00
|
|
|
|
|
|
|
inline const osg::Vec3& getEyeLocal() const { return _eyePointStack.back(); }
|
|
|
|
|
2006-12-16 01:27:18 +08:00
|
|
|
inline const osg::Vec3& getViewPointLocal() const { return _viewPointStack.back(); }
|
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
inline const osg::Vec3 getUpLocal() const
|
|
|
|
{
|
|
|
|
const osg::Matrix& matrix = *_modelviewStack.back();
|
|
|
|
return osg::Vec3(matrix(0,1),matrix(1,1),matrix(2,1));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const osg::Vec3 getLookVectorLocal() const
|
|
|
|
{
|
|
|
|
const osg::Matrix& matrix = *_modelviewStack.back();
|
|
|
|
return osg::Vec3(-matrix(0,2),-matrix(1,2),-matrix(2,2));
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2017-01-17 18:52:26 +08:00
|
|
|
typedef fast_back_stack< ref_ptr<RefMatrix> > MatrixStack;
|
|
|
|
|
|
|
|
MatrixStack& getProjectionStack() { return _projectionStack; }
|
|
|
|
const MatrixStack& getProjectionStack() const { return _projectionStack; }
|
|
|
|
|
|
|
|
MatrixStack& getModelViewStack() { return _modelviewStack; }
|
|
|
|
const MatrixStack& getModelViewStack() const { return _modelviewStack; }
|
|
|
|
|
|
|
|
MatrixStack& getMVPWStack() { return _MVPW_Stack; }
|
|
|
|
const MatrixStack& getMVPWStack() const { return _MVPW_Stack; }
|
2006-12-16 01:27:18 +08:00
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
protected:
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-14 00:21:00 +08:00
|
|
|
// base set of shadow volume occluder to use in culling.
|
|
|
|
ShadowVolumeOccluderList _occluderList;
|
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
|
|
|
|
MatrixStack _projectionStack;
|
|
|
|
|
|
|
|
MatrixStack _modelviewStack;
|
|
|
|
MatrixStack _MVPW_Stack;
|
|
|
|
|
|
|
|
typedef fast_back_stack<ref_ptr<Viewport> > ViewportStack;
|
|
|
|
ViewportStack _viewportStack;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
typedef fast_back_stack<Vec3> EyePointStack;
|
2006-12-16 01:27:18 +08:00
|
|
|
EyePointStack _referenceViewPoints;
|
2002-06-10 19:21:21 +08:00
|
|
|
EyePointStack _eyePointStack;
|
2006-12-16 01:27:18 +08:00
|
|
|
EyePointStack _viewPointStack;
|
2002-06-10 19:21:21 +08:00
|
|
|
|
|
|
|
CullingStack _clipspaceCullingStack;
|
|
|
|
CullingStack _projectionCullingStack;
|
2003-09-26 19:20:43 +08:00
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
CullingStack _modelviewCullingStack;
|
2003-09-26 19:20:43 +08:00
|
|
|
unsigned int _index_modelviewCullingStack;
|
|
|
|
CullingSet* _back_modelviewCullingStack;
|
2002-06-10 19:21:21 +08:00
|
|
|
|
2002-06-15 20:14:42 +08:00
|
|
|
void computeFrustumVolume();
|
|
|
|
float _frustumVolume;
|
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
unsigned int _bbCornerNear;
|
|
|
|
unsigned int _bbCornerFar;
|
|
|
|
|
2003-01-10 17:25:42 +08:00
|
|
|
ref_ptr<osg::RefMatrix> _identity;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-01-10 17:25:42 +08:00
|
|
|
typedef std::vector< osg::ref_ptr<osg::RefMatrix> > MatrixList;
|
2005-11-18 01:44:48 +08:00
|
|
|
MatrixList _reuseMatrixList;
|
|
|
|
unsigned int _currentReuseMatrixIndex;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-11-18 01:44:48 +08:00
|
|
|
inline osg::RefMatrix* createOrReuseMatrix(const osg::Matrix& value);
|
2002-06-10 19:21:21 +08:00
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
inline osg::Viewport* CullStack::getViewport()
|
|
|
|
{
|
2017-04-26 18:50:35 +08:00
|
|
|
return _viewportStack.empty() ? 0 : _viewportStack.back().get();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const osg::Viewport* CullStack::getViewport() const
|
|
|
|
{
|
|
|
|
return _viewportStack.empty() ? 0 : _viewportStack.back().get();
|
2002-06-10 19:21:21 +08:00
|
|
|
}
|
|
|
|
|
2007-02-21 21:48:01 +08:00
|
|
|
inline osg::RefMatrix* CullStack::getModelViewMatrix()
|
2002-06-10 19:21:21 +08:00
|
|
|
{
|
2017-04-26 18:50:35 +08:00
|
|
|
return _modelviewStack.empty() ? _identity.get() : _modelviewStack.back().get();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const osg::RefMatrix* CullStack::getModelViewMatrix() const
|
|
|
|
{
|
|
|
|
return _modelviewStack.empty() ? _identity.get() : _modelviewStack.back().get();
|
2002-06-10 19:21:21 +08:00
|
|
|
}
|
|
|
|
|
2007-02-21 21:48:01 +08:00
|
|
|
inline osg::RefMatrix* CullStack::getProjectionMatrix()
|
2002-06-10 19:21:21 +08:00
|
|
|
{
|
2017-04-26 18:50:35 +08:00
|
|
|
return _projectionStack.empty() ? _identity.get() : _projectionStack.back().get();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const osg::RefMatrix* CullStack::getProjectionMatrix() const
|
|
|
|
{
|
|
|
|
return _projectionStack.empty() ? _identity.get() : _projectionStack.back().get();
|
2002-06-10 19:21:21 +08:00
|
|
|
}
|
|
|
|
|
2016-07-02 21:45:10 +08:00
|
|
|
inline osg::Matrix CullStack::getWindowMatrix() const
|
2002-06-10 19:21:21 +08:00
|
|
|
{
|
|
|
|
if (!_viewportStack.empty())
|
|
|
|
{
|
|
|
|
osg::Viewport* viewport = _viewportStack.back().get();
|
|
|
|
return viewport->computeWindowMatrix();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-01-10 17:25:42 +08:00
|
|
|
return *_identity;
|
2002-06-10 19:21:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-21 21:48:01 +08:00
|
|
|
inline const osg::RefMatrix* CullStack::getMVPW()
|
2002-06-10 19:21:21 +08:00
|
|
|
{
|
|
|
|
if (!_MVPW_Stack.empty())
|
|
|
|
{
|
|
|
|
if (!_MVPW_Stack.back())
|
|
|
|
{
|
2007-02-21 21:48:01 +08:00
|
|
|
_MVPW_Stack.back() = createOrReuseMatrix(*getModelViewMatrix());
|
|
|
|
(*_MVPW_Stack.back()) *= *(getProjectionMatrix());
|
2002-06-10 19:21:21 +08:00
|
|
|
(*_MVPW_Stack.back()) *= getWindowMatrix();
|
|
|
|
}
|
2007-02-21 21:48:01 +08:00
|
|
|
return _MVPW_Stack.back().get();
|
2002-06-10 19:21:21 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-02-21 21:48:01 +08:00
|
|
|
return _identity.get();
|
2002-06-10 19:21:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-10 17:25:42 +08:00
|
|
|
inline RefMatrix* CullStack::createOrReuseMatrix(const osg::Matrix& value)
|
2002-06-10 19:21:21 +08:00
|
|
|
{
|
|
|
|
// skip of any already reused matrix.
|
2012-03-22 01:36:20 +08:00
|
|
|
while (_currentReuseMatrixIndex<_reuseMatrixList.size() &&
|
2002-06-10 19:21:21 +08:00
|
|
|
_reuseMatrixList[_currentReuseMatrixIndex]->referenceCount()>1)
|
|
|
|
{
|
|
|
|
++_currentReuseMatrixIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if still within list, element must be singularly referenced
|
|
|
|
// there return it to be reused.
|
|
|
|
if (_currentReuseMatrixIndex<_reuseMatrixList.size())
|
|
|
|
{
|
2003-01-10 17:25:42 +08:00
|
|
|
RefMatrix* matrix = _reuseMatrixList[_currentReuseMatrixIndex++].get();
|
2002-06-10 19:21:21 +08:00
|
|
|
matrix->set(value);
|
|
|
|
return matrix;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise need to create new matrix.
|
2003-01-10 17:25:42 +08:00
|
|
|
osg::RefMatrix* matrix = new RefMatrix(value);
|
2002-06-10 19:21:21 +08:00
|
|
|
_reuseMatrixList.push_back(matrix);
|
|
|
|
++_currentReuseMatrixIndex;
|
|
|
|
return matrix;
|
|
|
|
}
|
|
|
|
|
2005-11-18 01:44:48 +08:00
|
|
|
} // end of namespace
|
2002-06-10 19:21:21 +08:00
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
#endif
|