CullingSet
Go to the documentation of this file.
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* 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
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSG_CullingSet
#define OSG_CullingSet 1
#include <osg/Polytope>
#include <osg/ShadowVolumeOccluder>
#include <osg/Viewport>
#include <math.h>
namespace osg {
#define COMPILE_WITH_SHADOW_OCCLUSION_CULLING
class OSG_EXPORT CullingSet : public Referenced
{
public:
typedef std::pair< osg::ref_ptr<osg::StateSet>, osg::Polytope > StateFrustumPair;
typedef std::vector< StateFrustumPair > StateFrustumList;
CullingSet();
CullingSet(const CullingSet& cs):
Referenced(),
_mask(cs._mask),
_frustum(cs._frustum),
_stateFrustumList(cs._stateFrustumList),
_occluderList(cs._occluderList),
_pixelSizeVector(cs._pixelSizeVector),
_smallFeatureCullingPixelSize(cs._smallFeatureCullingPixelSize)
{
}
CullingSet(const CullingSet& cs,const Matrix& matrix, const Vec4& pixelSizeVector):
_mask(cs._mask),
_frustum(cs._frustum),
_stateFrustumList(cs._stateFrustumList),
_occluderList(cs._occluderList),
_pixelSizeVector(pixelSizeVector),
_smallFeatureCullingPixelSize(cs._smallFeatureCullingPixelSize)
{
_frustum.transformProvidingInverse(matrix);
for(OccluderList::iterator itr=_occluderList.begin();
itr!=_occluderList.end();
++itr)
{
itr->transformProvidingInverse(matrix);
}
}
CullingSet& operator = (const CullingSet& cs)
{
if (this==&cs) return *this;
_mask = cs._mask;
_frustum = cs._frustum;
_stateFrustumList = cs._stateFrustumList;
_occluderList = cs._occluderList;
_pixelSizeVector = cs._pixelSizeVector;
_smallFeatureCullingPixelSize = cs._smallFeatureCullingPixelSize;
return *this;
}
inline void set(const CullingSet& cs)
{
_mask = cs._mask;
_frustum = cs._frustum;
_stateFrustumList = cs._stateFrustumList;
_occluderList = cs._occluderList;
_pixelSizeVector = cs._pixelSizeVector;
_smallFeatureCullingPixelSize = cs._smallFeatureCullingPixelSize;
}
inline void set(const CullingSet& cs,const Matrix& matrix, const Vec4& pixelSizeVector)
{
_mask = cs._mask;
_stateFrustumList = cs._stateFrustumList;
_occluderList = cs._occluderList;
_pixelSizeVector = pixelSizeVector;
_smallFeatureCullingPixelSize = cs._smallFeatureCullingPixelSize;
//_frustum = cs._frustum;
//_frustum.transformProvidingInverse(matrix);
_frustum.setAndTransformProvidingInverse(cs._frustum,matrix);
for(StateFrustumList::iterator sitr=_stateFrustumList.begin();
sitr!=_stateFrustumList.end();
++sitr)
{
sitr->second.transformProvidingInverse(matrix);
}
for(OccluderList::iterator oitr=_occluderList.begin();
oitr!=_occluderList.end();
++oitr)
{
oitr->transformProvidingInverse(matrix);
}
}
typedef std::vector<ShadowVolumeOccluder> OccluderList;
typedef int Mask;
enum MaskValues
{
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
};
void setCullingMask(Mask mask) { _mask = mask; }
Mask getCullingMask() const { return _mask; }
void setFrustum(Polytope& cv) { _frustum = cv; }
Polytope& getFrustum() { return _frustum; }
const Polytope& getFrustum() const { return _frustum; }
void addStateFrustum(StateSet* stateset, Polytope& polytope) { _stateFrustumList.push_back(StateFrustumPair(stateset,polytope)); }
void getStateFrustumList(StateFrustumList& sfl) { _stateFrustumList = sfl; }
StateFrustumList& getStateFrustumList() { return _stateFrustumList; }
void addOccluder(ShadowVolumeOccluder& cv) { _occluderList.push_back(cv); }
void setPixelSizeVector(const Vec4& v) { _pixelSizeVector = v; }
Vec4& getPixelSizeVector() { return _pixelSizeVector; }
const Vec4& getPixelSizeVector() const { return _pixelSizeVector; }
void setSmallFeatureCullingPixelSize(float value) { _smallFeatureCullingPixelSize=value; }
float& getSmallFeatureCullingPixelSize() { return _smallFeatureCullingPixelSize; }
float getSmallFeatureCullingPixelSize() const { return _smallFeatureCullingPixelSize; }
float pixelSize(const Vec3& v,float radius) const { return radius/(v*_pixelSizeVector); }
float pixelSize(const BoundingSphere& bs) const { return bs.radius()/(bs.center()*_pixelSizeVector); }
float clampedPixelSize(const Vec3& v,float radius) const { return fabs(pixelSize(v, radius)); }
float clampedPixelSize(const BoundingSphere& bs) const { return fabs(pixelSize(bs)); }
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;
}
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;
}
}
}
return false;
}
inline bool isCulled(const BoundingBox& bb)
{
if (_mask&VIEW_FRUSTUM_CULLING)
{
// is it outside the view frustum...
if (!_frustum.contains(bb)) return true;
}
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;
}
}
}
return false;
}
inline bool isCulled(const BoundingSphere& bs)
{
if (_mask&VIEW_FRUSTUM_CULLING)
{
// is it outside the view frustum...
if (!_frustum.contains(bs)) return true;
}
if (_mask&SMALL_FEATURE_CULLING)
{
if (((bs.center()*_pixelSizeVector)*_smallFeatureCullingPixelSize)>bs.radius()) return true;
}
#ifdef COMPILE_WITH_SHADOW_OCCLUSION_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(bs)) return true;
}
}
}
#endif
return false;
}
inline void pushCurrentMask()
{
_frustum.pushCurrentMask();
if (!_stateFrustumList.empty())
{
for(StateFrustumList::iterator itr=_stateFrustumList.begin();
itr!=_stateFrustumList.end();
++itr)
{
itr->second.pushCurrentMask();
}
}
#ifdef COMPILE_WITH_SHADOW_OCCLUSION_CULLING
if (!_occluderList.empty())
{
for(OccluderList::iterator itr=_occluderList.begin();
itr!=_occluderList.end();
++itr)
{
itr->pushCurrentMask();
}
}
#endif
}
inline void popCurrentMask()
{
_frustum.popCurrentMask();
if (!_stateFrustumList.empty())
{
for(StateFrustumList::iterator itr=_stateFrustumList.begin();
itr!=_stateFrustumList.end();
++itr)
{
itr->second.popCurrentMask();
}
}
#ifdef COMPILE_WITH_SHADOW_OCCLUSION_CULLING
if (!_occluderList.empty())
{
for(OccluderList::iterator itr=_occluderList.begin();
itr!=_occluderList.end();
++itr)
{
itr->popCurrentMask();
}
}
#endif
}
inline void resetCullingMask()
{
_frustum.setResultMask(_frustum.getCurrentMask());
}
void disableAndPushOccludersCurrentMask(NodePath& nodePath);
void popOccludersCurrentMask(NodePath& nodePath);
static osg::Vec4 computePixelSizeVector(const Viewport& W, const Matrix& P, const Matrix& M);
virtual ~CullingSet();
protected:
Mask _mask;
Polytope _frustum;
StateFrustumList _stateFrustumList;
OccluderList _occluderList;
Vec4 _pixelSizeVector;
float _smallFeatureCullingPixelSize;
};
} // end of namespace
#endif
Polytope
ShadowVolumeOccluder
Viewport
osg::BoundingSphereImpl::center
vec_type & center()
Definition BoundingSphere:83
osg::BoundingSphereImpl::radius
value_type & radius()
Definition BoundingSphere:89
osg::CullingSet::popOccludersCurrentMask
void popOccludersCurrentMask(NodePath &nodePath)
osg::CullingSet::MaskValues
MaskValues
Definition CullingSet:126
osg::CullingSet::VIEW_FRUSTUM_SIDES_CULLING
@ VIEW_FRUSTUM_SIDES_CULLING
Definition CullingSet:128
osg::CullingSet::SMALL_FEATURE_CULLING
@ SMALL_FEATURE_CULLING
Definition CullingSet:134
osg::CullingSet::ENABLE_ALL_CULLING
@ ENABLE_ALL_CULLING
Definition CullingSet:139
osg::CullingSet::DEFAULT_CULLING
@ DEFAULT_CULLING
Definition CullingSet:136
osg::CullingSet::NEAR_PLANE_CULLING
@ NEAR_PLANE_CULLING
Definition CullingSet:129
osg::CullingSet::SHADOW_OCCLUSION_CULLING
@ SHADOW_OCCLUSION_CULLING
Definition CullingSet:135
osg::CullingSet::VIEW_FRUSTUM_CULLING
@ VIEW_FRUSTUM_CULLING
Definition CullingSet:131
osg::CullingSet::FAR_PLANE_CULLING
@ FAR_PLANE_CULLING
Definition CullingSet:130
osg::CullingSet::NO_CULLING
@ NO_CULLING
Definition CullingSet:127
osg::CullingSet::getFrustum
Polytope & getFrustum()
Definition CullingSet:149
osg::CullingSet::isCulled
bool isCulled(const std::vector< Vec3 > &vertices)
Definition CullingSet:186
osg::CullingSet::getPixelSizeVector
Vec4 & getPixelSizeVector()
Definition CullingSet:161
osg::CullingSet::getStateFrustumList
void getStateFrustumList(StateFrustumList &sfl)
Definition CullingSet:154
osg::CullingSet::_pixelSizeVector
Vec4 _pixelSizeVector
Definition CullingSet:350
osg::CullingSet::OccluderList
std::vector< ShadowVolumeOccluder > OccluderList
Definition CullingSet:121
osg::CullingSet::popCurrentMask
void popCurrentMask()
Definition CullingSet:302
osg::CullingSet::getFrustum
const Polytope & getFrustum() const
Definition CullingSet:150
osg::CullingSet::resetCullingMask
void resetCullingMask()
Definition CullingSet:329
osg::CullingSet::_occluderList
OccluderList _occluderList
Definition CullingSet:349
osg::CullingSet::getStateFrustumList
StateFrustumList & getStateFrustumList()
Definition CullingSet:155
osg::CullingSet::setSmallFeatureCullingPixelSize
void setSmallFeatureCullingPixelSize(float value)
Definition CullingSet:166
osg::CullingSet::Mask
int Mask
Definition CullingSet:123
osg::CullingSet::set
void set(const CullingSet &cs)
Definition CullingSet:82
osg::CullingSet::StateFrustumPair
std::pair< osg::ref_ptr< osg::StateSet >, osg::Polytope > StateFrustumPair
Definition CullingSet:34
osg::CullingSet::addStateFrustum
void addStateFrustum(StateSet *stateset, Polytope &polytope)
Definition CullingSet:152
osg::CullingSet::getSmallFeatureCullingPixelSize
float getSmallFeatureCullingPixelSize() const
Definition CullingSet:170
osg::CullingSet::clampedPixelSize
float clampedPixelSize(const Vec3 &v, float radius) const
Definition CullingSet:180
osg::CullingSet::CullingSet
CullingSet(const CullingSet &cs, const Matrix &matrix, const Vec4 &pixelSizeVector)
Definition CullingSet:50
osg::CullingSet::pixelSize
float pixelSize(const Vec3 &v, float radius) const
Definition CullingSet:174
osg::CullingSet::_mask
Mask _mask
Definition CullingSet:346
osg::CullingSet::addOccluder
void addOccluder(ShadowVolumeOccluder &cv)
Definition CullingSet:157
osg::CullingSet::setFrustum
void setFrustum(Polytope &cv)
Definition CullingSet:147
osg::CullingSet::getPixelSizeVector
const Vec4 & getPixelSizeVector() const
Definition CullingSet:162
osg::CullingSet::StateFrustumList
std::vector< StateFrustumPair > StateFrustumList
Definition CullingSet:35
osg::CullingSet::~CullingSet
virtual ~CullingSet()
osg::CullingSet::isCulled
bool isCulled(const BoundingBox &bb)
Definition CullingSet:215
osg::CullingSet::CullingSet
CullingSet(const CullingSet &cs)
Definition CullingSet:39
osg::CullingSet::getSmallFeatureCullingPixelSize
float & getSmallFeatureCullingPixelSize()
Definition CullingSet:168
osg::CullingSet::setCullingMask
void setCullingMask(Mask mask)
Definition CullingSet:144
osg::CullingSet::_smallFeatureCullingPixelSize
float _smallFeatureCullingPixelSize
Definition CullingSet:351
osg::CullingSet::isCulled
bool isCulled(const BoundingSphere &bs)
Definition CullingSet:244
osg::CullingSet::_frustum
Polytope _frustum
Definition CullingSet:347
osg::CullingSet::pixelSize
float pixelSize(const BoundingSphere &bs) const
Definition CullingSet:177
osg::CullingSet::_stateFrustumList
StateFrustumList _stateFrustumList
Definition CullingSet:348
osg::CullingSet::setPixelSizeVector
void setPixelSizeVector(const Vec4 &v)
Definition CullingSet:159
osg::CullingSet::pushCurrentMask
void pushCurrentMask()
Definition CullingSet:274
osg::CullingSet::set
void set(const CullingSet &cs, const Matrix &matrix, const Vec4 &pixelSizeVector)
Definition CullingSet:92
osg::CullingSet::disableAndPushOccludersCurrentMask
void disableAndPushOccludersCurrentMask(NodePath &nodePath)
osg::CullingSet::clampedPixelSize
float clampedPixelSize(const BoundingSphere &bs) const
Definition CullingSet:183
osg::CullingSet::getCullingMask
Mask getCullingMask() const
Definition CullingSet:145
osg::CullingSet::computePixelSizeVector
static osg::Vec4 computePixelSizeVector(const Viewport &W, const Matrix &P, const Matrix &M)
osg::CullingSet::CullingSet
CullingSet()
osg::Polytope
Definition Polytope:26
osg::Referenced::Referenced
Referenced()
osg::ShadowVolumeOccluder
Definition ShadowVolumeOccluder:27
osg::StateSet
Definition StateSet:46
osg::Viewport
Definition Viewport:24
osg
Definition AlphaFunc:19
osg::Vec3
Vec3f Vec3
Definition Vec3:21
osg::BoundingBox
BoundingBoxd BoundingBox
Definition BoundingBox:257
osg::BoundingSphere
BoundingSphered BoundingSphere
Definition BoundingSphere:314
osg::Matrix
Matrixd Matrix
Definition Matrix:27
osg::Vec4
Vec4f Vec4
Definition Vec4:21
osg::NodePath
std::vector< Node * > NodePath
Definition Node:47
OSG_EXPORT
#define OSG_EXPORT
Definition osg/Export:39