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-04 01:49:28 +08:00
|
|
|
|
|
|
|
#ifndef OSG_CullingSet
|
|
|
|
#define OSG_CullingSet 1
|
|
|
|
|
|
|
|
#include <osg/Polytope>
|
2002-06-12 21:54:14 +08:00
|
|
|
#include <osg/ShadowVolumeOccluder>
|
2005-10-13 02:42:36 +08:00
|
|
|
#include <osg/Viewport>
|
2002-06-04 01:49:28 +08:00
|
|
|
|
2005-09-28 21:33:58 +08:00
|
|
|
#include <math.h>
|
|
|
|
|
2005-03-12 04:29:21 +08:00
|
|
|
|
2002-06-04 01:49:28 +08:00
|
|
|
namespace osg {
|
|
|
|
|
2005-03-12 04:29:21 +08:00
|
|
|
#define COMPILE_WITH_SHADOW_OCCLUSION_CULLING
|
|
|
|
|
2004-08-31 22:49:33 +08:00
|
|
|
/** A CullingSet class which contains a frustum and a list of occluders. */
|
2005-04-12 01:14:17 +08:00
|
|
|
class OSG_EXPORT CullingSet : public Referenced
|
2002-06-04 01:49:28 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
2005-09-08 00:18:43 +08:00
|
|
|
|
|
|
|
typedef std::pair< osg::ref_ptr<osg::StateSet>, osg::Polytope > StateFrustumPair;
|
2012-03-22 01:36:20 +08:00
|
|
|
typedef std::vector< StateFrustumPair > StateFrustumList;
|
|
|
|
|
2002-06-04 01:49:28 +08:00
|
|
|
CullingSet();
|
2003-09-26 05:54:33 +08:00
|
|
|
|
|
|
|
CullingSet(const CullingSet& cs):
|
|
|
|
Referenced(),
|
|
|
|
_mask(cs._mask),
|
|
|
|
_frustum(cs._frustum),
|
2005-09-08 00:18:43 +08:00
|
|
|
_stateFrustumList(cs._stateFrustumList),
|
2003-09-26 05:54:33 +08:00
|
|
|
_occluderList(cs._occluderList),
|
|
|
|
_pixelSizeVector(cs._pixelSizeVector),
|
|
|
|
_smallFeatureCullingPixelSize(cs._smallFeatureCullingPixelSize)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-06-04 23:21:24 +08:00
|
|
|
CullingSet(const CullingSet& cs,const Matrix& matrix, const Vec4& pixelSizeVector):
|
2002-06-04 01:49:28 +08:00
|
|
|
_mask(cs._mask),
|
2002-06-10 19:21:21 +08:00
|
|
|
_frustum(cs._frustum),
|
2005-09-08 00:18:43 +08:00
|
|
|
_stateFrustumList(cs._stateFrustumList),
|
2002-06-04 01:49:28 +08:00
|
|
|
_occluderList(cs._occluderList),
|
2002-06-04 23:21:24 +08:00
|
|
|
_pixelSizeVector(pixelSizeVector),
|
|
|
|
_smallFeatureCullingPixelSize(cs._smallFeatureCullingPixelSize)
|
2002-06-04 01:49:28 +08:00
|
|
|
{
|
2002-06-10 19:21:21 +08:00
|
|
|
_frustum.transformProvidingInverse(matrix);
|
2002-06-04 01:49:28 +08:00
|
|
|
for(OccluderList::iterator itr=_occluderList.begin();
|
|
|
|
itr!=_occluderList.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
itr->transformProvidingInverse(matrix);
|
|
|
|
}
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-09-28 18:15:10 +08:00
|
|
|
CullingSet& operator = (const CullingSet& cs)
|
|
|
|
{
|
|
|
|
if (this==&cs) return *this;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-09-28 18:15:10 +08:00
|
|
|
_mask = cs._mask;
|
|
|
|
_frustum = cs._frustum;
|
2005-09-08 00:18:43 +08:00
|
|
|
_stateFrustumList = cs._stateFrustumList;
|
2003-09-28 18:15:10 +08:00
|
|
|
_occluderList = cs._occluderList;
|
|
|
|
_pixelSizeVector = cs._pixelSizeVector;
|
|
|
|
_smallFeatureCullingPixelSize = cs._smallFeatureCullingPixelSize;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-09-28 18:15:10 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-26 05:54:33 +08:00
|
|
|
inline void set(const CullingSet& cs)
|
|
|
|
{
|
|
|
|
_mask = cs._mask;
|
|
|
|
_frustum = cs._frustum;
|
2005-09-08 00:18:43 +08:00
|
|
|
_stateFrustumList = cs._stateFrustumList;
|
2003-09-26 05:54:33 +08:00
|
|
|
_occluderList = cs._occluderList;
|
|
|
|
_pixelSizeVector = cs._pixelSizeVector;
|
|
|
|
_smallFeatureCullingPixelSize = cs._smallFeatureCullingPixelSize;
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-09-26 05:54:33 +08:00
|
|
|
inline void set(const CullingSet& cs,const Matrix& matrix, const Vec4& pixelSizeVector)
|
|
|
|
{
|
|
|
|
_mask = cs._mask;
|
2005-09-08 00:18:43 +08:00
|
|
|
_stateFrustumList = cs._stateFrustumList;
|
2003-09-26 05:54:33 +08:00
|
|
|
_occluderList = cs._occluderList;
|
|
|
|
_pixelSizeVector = pixelSizeVector;
|
|
|
|
_smallFeatureCullingPixelSize = cs._smallFeatureCullingPixelSize;
|
|
|
|
|
2003-12-04 05:45:32 +08:00
|
|
|
//_frustum = cs._frustum;
|
|
|
|
//_frustum.transformProvidingInverse(matrix);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-12-04 05:45:32 +08:00
|
|
|
_frustum.setAndTransformProvidingInverse(cs._frustum,matrix);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-10-26 17:22:13 +08:00
|
|
|
for(StateFrustumList::iterator sitr=_stateFrustumList.begin();
|
|
|
|
sitr!=_stateFrustumList.end();
|
|
|
|
++sitr)
|
2005-09-08 00:18:43 +08:00
|
|
|
{
|
2005-10-26 17:22:13 +08:00
|
|
|
sitr->second.transformProvidingInverse(matrix);
|
2005-09-08 00:18:43 +08:00
|
|
|
}
|
|
|
|
|
2005-10-26 17:22:13 +08:00
|
|
|
for(OccluderList::iterator oitr=_occluderList.begin();
|
|
|
|
oitr!=_occluderList.end();
|
|
|
|
++oitr)
|
2003-09-26 05:54:33 +08:00
|
|
|
{
|
2005-10-26 17:22:13 +08:00
|
|
|
oitr->transformProvidingInverse(matrix);
|
2003-09-26 05:54:33 +08:00
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-09-26 05:54:33 +08:00
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
typedef std::vector<ShadowVolumeOccluder> OccluderList;
|
2002-06-04 01:49:28 +08:00
|
|
|
|
2010-03-05 20:55:08 +08:00
|
|
|
typedef int Mask;
|
2002-06-04 01:49:28 +08:00
|
|
|
|
|
|
|
enum MaskValues
|
|
|
|
{
|
2003-08-20 20:50:54 +08:00
|
|
|
NO_CULLING = 0x0,
|
|
|
|
VIEW_FRUSTUM_SIDES_CULLING = 0x1,
|
|
|
|
NEAR_PLANE_CULLING = 0x2,
|
|
|
|
FAR_PLANE_CULLING = 0x4,
|
|
|
|
VIEW_FRUSTUM_CULLING = VIEW_FRUSTUM_SIDES_CULLING|
|
|
|
|
NEAR_PLANE_CULLING|
|
|
|
|
FAR_PLANE_CULLING,
|
|
|
|
SMALL_FEATURE_CULLING = 0x8,
|
|
|
|
SHADOW_OCCLUSION_CULLING = 0x10,
|
|
|
|
DEFAULT_CULLING = VIEW_FRUSTUM_SIDES_CULLING|
|
|
|
|
SMALL_FEATURE_CULLING|
|
|
|
|
SHADOW_OCCLUSION_CULLING,
|
|
|
|
ENABLE_ALL_CULLING = VIEW_FRUSTUM_CULLING|
|
|
|
|
SMALL_FEATURE_CULLING|
|
|
|
|
SHADOW_OCCLUSION_CULLING
|
2002-06-04 01:49:28 +08:00
|
|
|
};
|
2010-03-05 20:55:08 +08:00
|
|
|
|
2002-06-04 01:49:28 +08:00
|
|
|
void setCullingMask(Mask mask) { _mask = mask; }
|
2004-12-17 09:06:33 +08:00
|
|
|
Mask getCullingMask() const { return _mask; }
|
2002-06-04 01:49:28 +08:00
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
void setFrustum(Polytope& cv) { _frustum = cv; }
|
2002-06-04 01:49:28 +08:00
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
Polytope& getFrustum() { return _frustum; }
|
2012-03-22 01:36:20 +08:00
|
|
|
const Polytope& getFrustum() const { return _frustum; }
|
|
|
|
|
2005-09-08 00:18:43 +08:00
|
|
|
void addStateFrustum(StateSet* stateset, Polytope& polytope) { _stateFrustumList.push_back(StateFrustumPair(stateset,polytope)); }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-09-08 00:18:43 +08:00
|
|
|
void getStateFrustumList(StateFrustumList& sfl) { _stateFrustumList = sfl; }
|
|
|
|
StateFrustumList& getStateFrustumList() { return _stateFrustumList; }
|
2002-06-04 01:49:28 +08:00
|
|
|
|
2005-09-08 00:18:43 +08:00
|
|
|
void addOccluder(ShadowVolumeOccluder& cv) { _occluderList.push_back(cv); }
|
2002-06-04 23:21:24 +08:00
|
|
|
|
|
|
|
void setPixelSizeVector(const Vec4& v) { _pixelSizeVector = v; }
|
|
|
|
|
|
|
|
Vec4& getPixelSizeVector() { return _pixelSizeVector; }
|
|
|
|
const Vec4& getPixelSizeVector() const { return _pixelSizeVector; }
|
|
|
|
|
2011-09-13 17:59:21 +08:00
|
|
|
/** Threshold at which small features are culled.
|
|
|
|
\param value Boulding volume size in screen space. Default is 2.0. */
|
2002-06-04 23:21:24 +08:00
|
|
|
void setSmallFeatureCullingPixelSize(float value) { _smallFeatureCullingPixelSize=value; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-04 23:21:24 +08:00
|
|
|
float& getSmallFeatureCullingPixelSize() { return _smallFeatureCullingPixelSize; }
|
|
|
|
|
|
|
|
float getSmallFeatureCullingPixelSize() const { return _smallFeatureCullingPixelSize; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
|
|
|
|
2002-06-04 23:21:24 +08:00
|
|
|
/** Compute the pixel of an object at position v, with specified radius.*/
|
|
|
|
float pixelSize(const Vec3& v,float radius) const { return radius/(v*_pixelSizeVector); }
|
|
|
|
|
2004-08-31 22:49:33 +08:00
|
|
|
/** Compute the pixel of a bounding sphere.*/
|
2002-06-04 23:21:24 +08:00
|
|
|
float pixelSize(const BoundingSphere& bs) const { return bs.radius()/(bs.center()*_pixelSizeVector); }
|
|
|
|
|
2005-09-28 21:33:58 +08:00
|
|
|
/** Compute the pixel of an object at position v, with specified radius. fabs()ed to always be positive. */
|
|
|
|
float clampedPixelSize(const Vec3& v,float radius) const { return fabs(pixelSize(v, radius)); }
|
|
|
|
|
|
|
|
/** Compute the pixel of a bounding sphere. fabs()ed to always be positive. */
|
|
|
|
float clampedPixelSize(const BoundingSphere& bs) const { return fabs(pixelSize(bs)); }
|
|
|
|
|
2002-06-09 03:58:05 +08:00
|
|
|
|
2002-06-12 17:22:30 +08:00
|
|
|
inline bool isCulled(const std::vector<Vec3>& vertices)
|
|
|
|
{
|
|
|
|
if (_mask&VIEW_FRUSTUM_CULLING)
|
|
|
|
{
|
|
|
|
// is it outside the view frustum...
|
|
|
|
if (!_frustum.contains(vertices)) return true;
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-12 17:22:30 +08:00
|
|
|
if (_mask&SMALL_FEATURE_CULLING)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_mask&SHADOW_OCCLUSION_CULLING)
|
|
|
|
{
|
|
|
|
// is it in one of the shadow occluder volumes.
|
|
|
|
if (!_occluderList.empty())
|
|
|
|
{
|
|
|
|
for(OccluderList::iterator itr=_occluderList.begin();
|
|
|
|
itr!=_occluderList.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
if (itr->contains(vertices)) return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-12 17:22:30 +08:00
|
|
|
return false;
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-04 01:49:28 +08:00
|
|
|
inline bool isCulled(const BoundingBox& bb)
|
|
|
|
{
|
|
|
|
if (_mask&VIEW_FRUSTUM_CULLING)
|
|
|
|
{
|
|
|
|
// is it outside the view frustum...
|
2002-06-10 19:21:21 +08:00
|
|
|
if (!_frustum.contains(bb)) return true;
|
2002-06-04 01:49:28 +08:00
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-04 01:49:28 +08:00
|
|
|
if (_mask&SMALL_FEATURE_CULLING)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_mask&SHADOW_OCCLUSION_CULLING)
|
|
|
|
{
|
|
|
|
// is it in one of the shadow occluder volumes.
|
|
|
|
if (!_occluderList.empty())
|
|
|
|
{
|
|
|
|
for(OccluderList::iterator itr=_occluderList.begin();
|
|
|
|
itr!=_occluderList.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
if (itr->contains(bb)) return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-04 01:49:28 +08:00
|
|
|
return false;
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-04 01:49:28 +08:00
|
|
|
inline bool isCulled(const BoundingSphere& bs)
|
|
|
|
{
|
|
|
|
if (_mask&VIEW_FRUSTUM_CULLING)
|
|
|
|
{
|
|
|
|
// is it outside the view frustum...
|
2002-06-10 19:21:21 +08:00
|
|
|
if (!_frustum.contains(bs)) return true;
|
2002-06-04 01:49:28 +08:00
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-04 01:49:28 +08:00
|
|
|
if (_mask&SMALL_FEATURE_CULLING)
|
|
|
|
{
|
2002-06-04 23:21:24 +08:00
|
|
|
if (((bs.center()*_pixelSizeVector)*_smallFeatureCullingPixelSize)>bs.radius()) return true;
|
2002-06-04 01:49:28 +08:00
|
|
|
}
|
2005-03-12 04:29:21 +08:00
|
|
|
#ifdef COMPILE_WITH_SHADOW_OCCLUSION_CULLING
|
2002-06-04 01:49:28 +08:00
|
|
|
if (_mask&SHADOW_OCCLUSION_CULLING)
|
|
|
|
{
|
|
|
|
// is it in one of the shadow occluder volumes.
|
|
|
|
if (!_occluderList.empty())
|
|
|
|
{
|
|
|
|
for(OccluderList::iterator itr=_occluderList.begin();
|
|
|
|
itr!=_occluderList.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
if (itr->contains(bs)) return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
#endif
|
2002-06-04 01:49:28 +08:00
|
|
|
return false;
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2014-05-15 00:01:40 +08:00
|
|
|
#ifdef OSG_USE_BOUND
|
2014-05-14 18:19:43 +08:00
|
|
|
inline bool isCulled(const Bound& bound)
|
|
|
|
{
|
|
|
|
if (bound.bb) return isCulled(*bound.bb);
|
|
|
|
else return isCulled(*bound.bs);
|
|
|
|
}
|
2014-05-15 00:01:40 +08:00
|
|
|
#endif
|
2002-06-04 01:49:28 +08:00
|
|
|
inline void pushCurrentMask()
|
|
|
|
{
|
2002-06-10 19:21:21 +08:00
|
|
|
_frustum.pushCurrentMask();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-09-08 00:18:43 +08:00
|
|
|
if (!_stateFrustumList.empty())
|
|
|
|
{
|
|
|
|
for(StateFrustumList::iterator itr=_stateFrustumList.begin();
|
|
|
|
itr!=_stateFrustumList.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
itr->second.pushCurrentMask();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-12 04:29:21 +08:00
|
|
|
#ifdef COMPILE_WITH_SHADOW_OCCLUSION_CULLING
|
2002-06-04 01:49:28 +08:00
|
|
|
if (!_occluderList.empty())
|
|
|
|
{
|
|
|
|
for(OccluderList::iterator itr=_occluderList.begin();
|
|
|
|
itr!=_occluderList.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
itr->pushCurrentMask();
|
|
|
|
}
|
|
|
|
}
|
2005-03-12 04:29:21 +08:00
|
|
|
#endif
|
2002-06-04 01:49:28 +08:00
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-04 01:49:28 +08:00
|
|
|
inline void popCurrentMask()
|
|
|
|
{
|
2002-06-10 19:21:21 +08:00
|
|
|
_frustum.popCurrentMask();
|
2002-06-04 01:49:28 +08:00
|
|
|
|
2005-09-08 00:18:43 +08:00
|
|
|
if (!_stateFrustumList.empty())
|
|
|
|
{
|
|
|
|
for(StateFrustumList::iterator itr=_stateFrustumList.begin();
|
|
|
|
itr!=_stateFrustumList.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
itr->second.popCurrentMask();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-12 04:29:21 +08:00
|
|
|
#ifdef COMPILE_WITH_SHADOW_OCCLUSION_CULLING
|
2002-06-04 01:49:28 +08:00
|
|
|
if (!_occluderList.empty())
|
|
|
|
{
|
|
|
|
for(OccluderList::iterator itr=_occluderList.begin();
|
|
|
|
itr!=_occluderList.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
itr->popCurrentMask();
|
|
|
|
}
|
|
|
|
}
|
2005-03-12 04:29:21 +08:00
|
|
|
#endif
|
2002-06-04 01:49:28 +08:00
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2013-01-28 22:30:21 +08:00
|
|
|
inline void resetCullingMask()
|
|
|
|
{
|
|
|
|
_frustum.setResultMask(_frustum.getCurrentMask());
|
|
|
|
}
|
|
|
|
|
2002-06-17 17:10:26 +08:00
|
|
|
void disableAndPushOccludersCurrentMask(NodePath& nodePath);
|
|
|
|
|
|
|
|
void popOccludersCurrentMask(NodePath& nodePath);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-10-13 02:42:36 +08:00
|
|
|
static osg::Vec4 computePixelSizeVector(const Viewport& W, const Matrix& P, const Matrix& M);
|
|
|
|
|
2003-09-26 05:54:33 +08:00
|
|
|
virtual ~CullingSet();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-09-26 05:54:33 +08:00
|
|
|
|
2002-06-04 01:49:28 +08:00
|
|
|
protected:
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-01-10 17:25:42 +08:00
|
|
|
|
2005-09-08 00:18:43 +08:00
|
|
|
Mask _mask;
|
|
|
|
Polytope _frustum;
|
2012-03-22 01:36:20 +08:00
|
|
|
StateFrustumList _stateFrustumList;
|
|
|
|
OccluderList _occluderList;
|
2005-09-08 00:18:43 +08:00
|
|
|
Vec4 _pixelSizeVector;
|
|
|
|
float _smallFeatureCullingPixelSize;
|
2002-06-04 01:49:28 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2005-11-18 01:44:48 +08:00
|
|
|
} // end of namespace
|
2002-06-04 01:49:28 +08:00
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
#endif
|