2002-06-10 21:50:25 +08:00
|
|
|
#include <osg/CollectOccludersVisitor>
|
2002-06-12 03:52:55 +08:00
|
|
|
#include <osg/Transform>
|
|
|
|
#include <osg/Switch>
|
|
|
|
#include <osg/LOD>
|
|
|
|
#include <osg/OccluderNode>
|
|
|
|
#include <osg/Projection>
|
2002-06-10 21:50:25 +08:00
|
|
|
|
2002-06-19 06:35:48 +08:00
|
|
|
#include <algorithm>
|
|
|
|
|
2002-06-10 21:50:25 +08:00
|
|
|
using namespace osg;
|
|
|
|
|
2002-08-12 05:26:58 +08:00
|
|
|
CollectOccludersVisitor::CollectOccludersVisitor():
|
|
|
|
NodeVisitor(COLLECT_OCCLUDER_VISITOR,TRAVERSE_ACTIVE_CHILDREN)
|
2002-06-10 21:50:25 +08:00
|
|
|
{
|
2002-06-15 20:14:42 +08:00
|
|
|
|
2002-06-16 04:57:50 +08:00
|
|
|
setCullingMode(VIEW_FRUSTUM_CULLING|
|
2002-06-15 20:14:42 +08:00
|
|
|
NEAR_PLANE_CULLING|
|
|
|
|
FAR_PLANE_CULLING|
|
2002-06-16 04:57:50 +08:00
|
|
|
SMALL_FEATURE_CULLING);
|
2002-06-14 07:46:02 +08:00
|
|
|
|
2002-06-17 17:10:26 +08:00
|
|
|
_minimumShadowOccluderVolume = 0.005f;
|
2002-06-14 07:46:02 +08:00
|
|
|
_createDrawables = false;
|
|
|
|
|
2002-06-10 21:50:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CollectOccludersVisitor::~CollectOccludersVisitor()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CollectOccludersVisitor::reset()
|
|
|
|
{
|
|
|
|
CullStack::reset();
|
|
|
|
}
|
|
|
|
|
2002-11-12 18:22:38 +08:00
|
|
|
float CollectOccludersVisitor::getDistanceToEyePoint(const Vec3& pos, bool withLODScale) const
|
2002-10-07 04:33:13 +08:00
|
|
|
{
|
2002-11-12 18:22:38 +08:00
|
|
|
if (withLODScale) return (pos-getEyeLocal()).length()*getLODScale();
|
2002-10-07 04:33:13 +08:00
|
|
|
else return (pos-getEyeLocal()).length();
|
|
|
|
}
|
|
|
|
|
2002-11-12 18:22:38 +08:00
|
|
|
float CollectOccludersVisitor::getDistanceFromEyePoint(const Vec3& pos, bool withLODScale) const
|
2002-10-07 04:33:13 +08:00
|
|
|
{
|
|
|
|
const Matrix& matrix = *_modelviewStack.back();
|
|
|
|
float dist = -(pos[0]*matrix(0,2)+pos[1]*matrix(1,2)+pos[2]*matrix(2,2)+matrix(3,2));
|
|
|
|
|
2002-11-12 18:22:38 +08:00
|
|
|
if (withLODScale) return dist*getLODScale();
|
|
|
|
else return dist*getLODScale();
|
2002-10-07 04:33:13 +08:00
|
|
|
}
|
|
|
|
|
2002-06-12 03:52:55 +08:00
|
|
|
void CollectOccludersVisitor::apply(osg::Node& node)
|
2002-06-10 21:50:25 +08:00
|
|
|
{
|
2002-06-12 03:52:55 +08:00
|
|
|
if (isCulled(node)) return;
|
|
|
|
|
|
|
|
// push the culling mode.
|
|
|
|
pushCurrentMask();
|
|
|
|
|
2002-06-14 21:49:59 +08:00
|
|
|
handle_cull_callbacks_and_traverse(node);
|
2002-06-12 03:52:55 +08:00
|
|
|
|
|
|
|
// pop the culling mode.
|
|
|
|
popCurrentMask();
|
2002-06-10 21:50:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CollectOccludersVisitor::apply(osg::Transform& node)
|
|
|
|
{
|
2002-06-12 03:52:55 +08:00
|
|
|
if (isCulled(node)) return;
|
|
|
|
|
|
|
|
// push the culling mode.
|
|
|
|
pushCurrentMask();
|
|
|
|
|
2003-01-10 17:25:42 +08:00
|
|
|
ref_ptr<osg::RefMatrix> matrix = createOrReuseMatrix(getModelViewMatrix());
|
2002-06-12 03:52:55 +08:00
|
|
|
node.getLocalToWorldMatrix(*matrix,this);
|
|
|
|
pushModelViewMatrix(matrix.get());
|
|
|
|
|
2002-06-14 21:49:59 +08:00
|
|
|
handle_cull_callbacks_and_traverse(node);
|
2002-06-12 03:52:55 +08:00
|
|
|
|
|
|
|
popModelViewMatrix();
|
|
|
|
|
|
|
|
// pop the culling mode.
|
|
|
|
popCurrentMask();
|
2002-06-10 21:50:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CollectOccludersVisitor::apply(osg::Projection& node)
|
|
|
|
{
|
2002-06-12 03:52:55 +08:00
|
|
|
if (isCulled(node)) return;
|
|
|
|
|
|
|
|
// push the culling mode.
|
|
|
|
pushCurrentMask();
|
|
|
|
|
2003-01-10 17:25:42 +08:00
|
|
|
ref_ptr<osg::RefMatrix> matrix = createOrReuseMatrix(node.getMatrix());
|
2002-06-12 03:52:55 +08:00
|
|
|
pushProjectionMatrix(matrix.get());
|
|
|
|
|
2002-06-14 21:49:59 +08:00
|
|
|
handle_cull_callbacks_and_traverse(node);
|
2002-06-12 03:52:55 +08:00
|
|
|
|
|
|
|
popProjectionMatrix();
|
|
|
|
|
|
|
|
// pop the culling mode.
|
|
|
|
popCurrentMask();
|
2002-06-10 21:50:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CollectOccludersVisitor::apply(osg::Switch& node)
|
|
|
|
{
|
2002-06-12 03:52:55 +08:00
|
|
|
apply((Group&)node);
|
2002-06-10 21:50:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CollectOccludersVisitor::apply(osg::LOD& node)
|
|
|
|
{
|
2002-06-12 03:52:55 +08:00
|
|
|
if (isCulled(node)) return;
|
|
|
|
|
|
|
|
// push the culling mode.
|
|
|
|
pushCurrentMask();
|
|
|
|
|
2002-10-07 04:33:13 +08:00
|
|
|
handle_cull_callbacks_and_traverse(node);
|
2002-06-12 03:52:55 +08:00
|
|
|
|
|
|
|
// pop the culling mode.
|
|
|
|
popCurrentMask();
|
2002-06-10 21:50:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CollectOccludersVisitor::apply(osg::OccluderNode& node)
|
|
|
|
{
|
2002-06-12 03:52:55 +08:00
|
|
|
// need to check if occlusion node is in the occluder
|
|
|
|
// list, if so disable the appropriate ShadowOccluderVolume
|
2002-06-17 17:10:26 +08:00
|
|
|
disableAndPushOccludersCurrentMask(_nodePath);
|
2002-06-12 03:52:55 +08:00
|
|
|
|
|
|
|
|
2002-06-17 17:10:26 +08:00
|
|
|
if (isCulled(node))
|
|
|
|
{
|
|
|
|
popOccludersCurrentMask(_nodePath);
|
|
|
|
return;
|
|
|
|
}
|
2002-06-12 03:52:55 +08:00
|
|
|
|
2002-06-14 00:21:00 +08:00
|
|
|
// std::cout<<"CollectOccludersVisitor:: We have found an Occlusion node in frustum"<<&node<<std::endl;
|
2002-06-12 17:22:30 +08:00
|
|
|
|
2002-06-12 03:52:55 +08:00
|
|
|
// push the culling mode.
|
|
|
|
pushCurrentMask();
|
|
|
|
|
2002-06-12 17:22:30 +08:00
|
|
|
|
2002-06-14 00:21:00 +08:00
|
|
|
if (node.getOccluder())
|
2002-06-12 17:22:30 +08:00
|
|
|
{
|
2002-06-14 00:21:00 +08:00
|
|
|
// computeOccluder will check if the occluder is the view frustum,
|
|
|
|
// if it ins't then the it will return false, when in it will
|
|
|
|
// clip the occluder's polygons in clip space, then create occluder
|
|
|
|
// planes, all with their normals facing inward towards the volume,
|
|
|
|
// and then transform them back into projection space.
|
|
|
|
ShadowVolumeOccluder svo;
|
2002-06-14 07:46:02 +08:00
|
|
|
if (svo.computeOccluder(_nodePath, *node.getOccluder(), *this,_createDrawables))
|
2002-06-14 00:21:00 +08:00
|
|
|
{
|
2002-06-15 20:14:42 +08:00
|
|
|
|
|
|
|
if (svo.getVolume()>_minimumShadowOccluderVolume)
|
|
|
|
{
|
|
|
|
// need to test occluder against view frustum.
|
2002-06-17 17:10:26 +08:00
|
|
|
//std::cout << " adding in Occluder"<<std::endl;
|
2002-06-19 06:35:48 +08:00
|
|
|
_occluderSet.insert(svo);
|
2002-06-15 20:14:42 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-06-17 17:10:26 +08:00
|
|
|
//std::cout << " rejecting Occluder as its volume is too small "<<svo.getVolume()<<std::endl;
|
2002-06-15 20:14:42 +08:00
|
|
|
}
|
2002-06-14 00:21:00 +08:00
|
|
|
}
|
2002-06-12 17:22:30 +08:00
|
|
|
}
|
|
|
|
|
2002-06-14 21:49:59 +08:00
|
|
|
handle_cull_callbacks_and_traverse(node);
|
2002-06-12 03:52:55 +08:00
|
|
|
|
|
|
|
// pop the culling mode.
|
|
|
|
popCurrentMask();
|
2002-06-17 17:10:26 +08:00
|
|
|
|
|
|
|
// pop the current mask for the disabled occluder
|
|
|
|
popOccludersCurrentMask(_nodePath);
|
2002-06-10 21:50:25 +08:00
|
|
|
}
|
|
|
|
|
2002-06-19 06:35:48 +08:00
|
|
|
void CollectOccludersVisitor::removeOccludedOccluders()
|
|
|
|
{
|
|
|
|
if (_occluderSet.empty()) return;
|
|
|
|
|
|
|
|
ShadowVolumeOccluderSet::iterator occludeeItr=_occluderSet.begin();
|
|
|
|
|
|
|
|
// skip the first element as this can't be occluded by anything else.
|
|
|
|
occludeeItr++;
|
2002-06-10 21:50:25 +08:00
|
|
|
|
2002-06-19 06:35:48 +08:00
|
|
|
// step through the rest of the occluders, remove occluders which are themselves occluded.
|
|
|
|
for(;
|
|
|
|
occludeeItr!=_occluderSet.end();
|
|
|
|
++occludeeItr)
|
|
|
|
{
|
|
|
|
|
|
|
|
// search for any occluders that occlude the current occluder,
|
|
|
|
// we only need to test any occluder near the front of the set since
|
|
|
|
// you can't be occluder by something smaller than you.
|
2002-06-19 18:19:10 +08:00
|
|
|
ShadowVolumeOccluder& occludee = const_cast<ShadowVolumeOccluder&>(*occludeeItr);
|
|
|
|
ShadowVolumeOccluder::HoleList& holeList = occludee.getHoleList();
|
|
|
|
|
2002-06-19 06:35:48 +08:00
|
|
|
for(ShadowVolumeOccluderSet::iterator occluderItr=_occluderSet.begin();
|
|
|
|
occluderItr!=occludeeItr;
|
|
|
|
++occluderItr)
|
|
|
|
{
|
|
|
|
// cast away constness of the std::set element since ShadowVolumeOccluder::contains() is non const,
|
|
|
|
// and the std::set is a const, just for the invariance of the operator <!! Ahhhhh. oh well the below
|
|
|
|
// should be robust since contains won't change the getVolume which is used by the operator <. Honest, :-)
|
|
|
|
ShadowVolumeOccluder* occluder = const_cast<ShadowVolumeOccluder*>(&(*occluderItr));
|
2002-06-19 18:19:10 +08:00
|
|
|
if (occluder->contains(occludee.getOccluder().getReferenceVertexList()))
|
2002-06-19 06:35:48 +08:00
|
|
|
{
|
|
|
|
// erase occluder from set.
|
|
|
|
// take a copy of the iterator then rewind it one element so to prevent invalidating the occludeeItr.
|
|
|
|
ShadowVolumeOccluderSet::iterator eraseItr = occludeeItr--;
|
|
|
|
_occluderSet.erase(eraseItr);
|
|
|
|
break;
|
|
|
|
}
|
2002-06-19 18:19:10 +08:00
|
|
|
|
|
|
|
// now check all the holes in the occludee against the occluder,
|
|
|
|
// do so in reverse order so that the iterators remain valid.
|
|
|
|
for(ShadowVolumeOccluder::HoleList::reverse_iterator holeItr=holeList.rbegin();
|
|
|
|
holeItr!=holeList.rend();
|
2002-06-28 04:29:32 +08:00
|
|
|
)
|
2002-06-19 18:19:10 +08:00
|
|
|
{
|
2002-07-18 00:24:55 +08:00
|
|
|
if (occluder->contains((*holeItr).getReferenceVertexList()))
|
2002-06-19 18:19:10 +08:00
|
|
|
{
|
2002-06-28 04:29:32 +08:00
|
|
|
holeList.erase((++holeItr).base());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++holeItr;
|
2002-06-19 18:19:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-06-19 06:35:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|