2006-07-18 23:21:48 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2003-01-22 00:45:36 +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
|
|
|
|
* (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.
|
|
|
|
*/
|
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;
|
2003-12-12 00:46:45 +08:00
|
|
|
_maximumNumberOfActiveOccluders = 10;
|
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();
|
2003-12-12 00:46:45 +08:00
|
|
|
_occluderSet.clear();
|
2002-06-10 21:50:25 +08:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2006-12-16 01:27:18 +08:00
|
|
|
float CollectOccludersVisitor::getDistanceToViewPoint(const Vec3& pos, bool withLODScale) const
|
|
|
|
{
|
|
|
|
if (withLODScale) return (pos-getViewPointLocal()).length()*getLODScale();
|
|
|
|
else return (pos-getViewPointLocal()).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();
|
|
|
|
|
2007-02-21 21:48:01 +08:00
|
|
|
ref_ptr<osg::RefMatrix> matrix = createOrReuseMatrix(*getModelViewMatrix());
|
2003-12-09 22:07:44 +08:00
|
|
|
node.computeLocalToWorldMatrix(*matrix,this);
|
2007-02-08 00:32:14 +08:00
|
|
|
pushModelViewMatrix(matrix.get(), node.getReferenceFrame());
|
2002-06-12 03:52:55 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2003-12-12 00:46:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
if (_occluderSet.size()<=_maximumNumberOfActiveOccluders) return;
|
|
|
|
|
|
|
|
// move the iterator to the _maximumNumberOfActiveOccluders th occluder.
|
|
|
|
occludeeItr = _occluderSet.begin();
|
|
|
|
for(unsigned int i=0;i<_maximumNumberOfActiveOccluders;++i)
|
|
|
|
++occludeeItr;
|
|
|
|
|
|
|
|
// discard last occluders.
|
|
|
|
_occluderSet.erase(occludeeItr,_occluderSet.end());
|
|
|
|
|
2002-06-19 06:35:48 +08:00
|
|
|
}
|