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
|
|
|
#include <osg/CullStack>
|
2003-06-24 23:40:09 +08:00
|
|
|
#include <osg/Timer>
|
2002-06-10 19:21:21 +08:00
|
|
|
|
2007-02-08 00:32:14 +08:00
|
|
|
#include <osg/Notify>
|
|
|
|
#include <osg/io_utils>
|
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
using namespace osg;
|
|
|
|
|
|
|
|
CullStack::CullStack()
|
|
|
|
{
|
2002-06-15 20:14:42 +08:00
|
|
|
_frustumVolume=-1.0f;
|
2002-07-21 09:29:11 +08:00
|
|
|
_bbCornerNear = 0;
|
|
|
|
_bbCornerFar = 7;
|
2003-01-10 17:25:42 +08:00
|
|
|
_currentReuseMatrixIndex=0;
|
|
|
|
_identity = new RefMatrix();
|
2003-09-26 19:20:43 +08:00
|
|
|
|
|
|
|
_index_modelviewCullingStack = 0;
|
|
|
|
_back_modelviewCullingStack = 0;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-08-11 01:44:54 +08:00
|
|
|
_referenceViewPoints.push_back(osg::Vec3(0.0f,0.0f,0.0f));
|
|
|
|
}
|
|
|
|
|
|
|
|
CullStack::CullStack(const CullStack& cs):
|
|
|
|
CullSettings(cs)
|
|
|
|
{
|
|
|
|
_frustumVolume=-1.0f;
|
|
|
|
_bbCornerNear = 0;
|
|
|
|
_bbCornerFar = 7;
|
|
|
|
_currentReuseMatrixIndex=0;
|
|
|
|
_identity = new RefMatrix();
|
|
|
|
|
|
|
|
_index_modelviewCullingStack = 0;
|
|
|
|
_back_modelviewCullingStack = 0;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2006-12-16 01:27:18 +08:00
|
|
|
_referenceViewPoints.push_back(osg::Vec3(0.0f,0.0f,0.0f));
|
2002-06-10 19:21:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CullStack::~CullStack()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CullStack::reset()
|
|
|
|
{
|
|
|
|
|
|
|
|
//
|
|
|
|
// first unref all referenced objects and then empty the containers.
|
|
|
|
//
|
|
|
|
_projectionStack.clear();
|
|
|
|
_modelviewStack.clear();
|
|
|
|
_viewportStack.clear();
|
2006-12-16 01:27:18 +08:00
|
|
|
|
|
|
|
_referenceViewPoints.clear();
|
|
|
|
_referenceViewPoints.push_back(osg::Vec3(0.0f,0.0f,0.0f));
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
_eyePointStack.clear();
|
2006-12-16 01:27:18 +08:00
|
|
|
_viewPointStack.clear();
|
2002-06-10 19:21:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
_clipspaceCullingStack.clear();
|
|
|
|
_projectionCullingStack.clear();
|
2003-09-26 19:20:43 +08:00
|
|
|
|
|
|
|
//_modelviewCullingStack.clear();
|
|
|
|
_index_modelviewCullingStack=0;
|
|
|
|
_back_modelviewCullingStack = 0;
|
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
osg::Vec3 lookVector(0.0,0.0,-1.0);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
_bbCornerFar = (lookVector.x()>=0?1:0) |
|
|
|
|
(lookVector.y()>=0?2:0) |
|
|
|
|
(lookVector.z()>=0?4:0);
|
|
|
|
|
|
|
|
_bbCornerNear = (~_bbCornerFar)&7;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2006-12-16 01:27:18 +08:00
|
|
|
_currentReuseMatrixIndex=0;
|
2002-06-10 19:21:21 +08:00
|
|
|
}
|
|
|
|
|
2003-09-26 19:20:43 +08:00
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
void CullStack::pushCullingSet()
|
|
|
|
{
|
|
|
|
_MVPW_Stack.push_back(0L);
|
2003-09-26 19:20:43 +08:00
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
if (_index_modelviewCullingStack==0)
|
2002-06-10 19:21:21 +08:00
|
|
|
{
|
2003-09-26 19:20:43 +08:00
|
|
|
if (_modelviewCullingStack.empty())
|
|
|
|
_modelviewCullingStack.push_back(CullingSet());
|
2002-06-10 19:21:21 +08:00
|
|
|
|
2003-09-26 19:20:43 +08:00
|
|
|
_modelviewCullingStack[_index_modelviewCullingStack++].set(_projectionCullingStack.back());
|
2002-06-10 19:21:21 +08:00
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
else
|
2002-06-10 19:21:21 +08:00
|
|
|
{
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
const osg::Viewport& W = *_viewportStack.back();
|
|
|
|
const osg::Matrix& P = *_projectionStack.back();
|
|
|
|
const osg::Matrix& M = *_modelviewStack.back();
|
2003-09-26 19:20:43 +08:00
|
|
|
|
2005-10-13 02:42:36 +08:00
|
|
|
osg::Vec4 pixelSizeVector = CullingSet::computePixelSizeVector(W,P,M);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
|
|
|
if (_index_modelviewCullingStack>=_modelviewCullingStack.size())
|
2003-09-26 19:20:43 +08:00
|
|
|
{
|
|
|
|
_modelviewCullingStack.push_back(CullingSet());
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-09-26 19:20:43 +08:00
|
|
|
_modelviewCullingStack[_index_modelviewCullingStack++].set(_projectionCullingStack.back(),*_modelviewStack.back(),pixelSizeVector);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-09-26 19:20:43 +08:00
|
|
|
_back_modelviewCullingStack = &_modelviewCullingStack[_index_modelviewCullingStack-1];
|
|
|
|
|
2002-08-04 00:37:13 +08:00
|
|
|
// const osg::Polytope& polytope = _modelviewCullingStack.back()->getFrustum();
|
|
|
|
// const osg::Polytope::PlaneList& pl = polytope.getPlaneList();
|
|
|
|
// std::cout <<"new cull stack"<<std::endl;
|
|
|
|
// for(osg::Polytope::PlaneList::const_iterator pl_itr=pl.begin();
|
|
|
|
// pl_itr!=pl.end();
|
|
|
|
// ++pl_itr)
|
|
|
|
// {
|
|
|
|
// std::cout << " plane "<<*pl_itr<<std::endl;
|
|
|
|
// }
|
|
|
|
|
2003-09-26 19:20:43 +08:00
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CullStack::popCullingSet()
|
|
|
|
{
|
|
|
|
_MVPW_Stack.pop_back();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-09-26 19:20:43 +08:00
|
|
|
--_index_modelviewCullingStack;
|
|
|
|
if (_index_modelviewCullingStack>0) _back_modelviewCullingStack = &_modelviewCullingStack[_index_modelviewCullingStack-1];
|
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CullStack::pushViewport(osg::Viewport* viewport)
|
|
|
|
{
|
|
|
|
_viewportStack.push_back(viewport);
|
|
|
|
_MVPW_Stack.push_back(0L);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CullStack::popViewport()
|
|
|
|
{
|
|
|
|
_viewportStack.pop_back();
|
|
|
|
_MVPW_Stack.pop_back();
|
|
|
|
}
|
|
|
|
|
2003-01-10 17:25:42 +08:00
|
|
|
void CullStack::pushProjectionMatrix(RefMatrix* matrix)
|
2002-06-10 19:21:21 +08:00
|
|
|
{
|
|
|
|
_projectionStack.push_back(matrix);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-09-26 19:20:43 +08:00
|
|
|
_projectionCullingStack.push_back(osg::CullingSet());
|
|
|
|
osg::CullingSet& cullingSet = _projectionCullingStack.back();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-14 00:21:00 +08:00
|
|
|
// set up view frustum.
|
2003-09-26 19:20:43 +08:00
|
|
|
cullingSet.getFrustum().setToUnitFrustum(((_cullingMode&NEAR_PLANE_CULLING)!=0),((_cullingMode&FAR_PLANE_CULLING)!=0));
|
|
|
|
cullingSet.getFrustum().transformProvidingInverse(*matrix);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-08-08 21:41:18 +08:00
|
|
|
// set the culling mask ( There should be a more elegant way!) Nikolaus H.
|
2003-09-26 19:20:43 +08:00
|
|
|
cullingSet.setCullingMask(_cullingMode);
|
2002-08-08 21:41:18 +08:00
|
|
|
|
2002-06-14 00:21:00 +08:00
|
|
|
// set the small feature culling.
|
2003-09-26 19:20:43 +08:00
|
|
|
cullingSet.setSmallFeatureCullingPixelSize(_smallFeatureCullingPixelSize);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-14 00:21:00 +08:00
|
|
|
// set up the relevant occluders which a related to this projection.
|
|
|
|
for(ShadowVolumeOccluderList::iterator itr=_occluderList.begin();
|
|
|
|
itr!=_occluderList.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
//std::cout << " ** testing occluder"<<std::endl;
|
|
|
|
if (itr->matchProjectionMatrix(*matrix))
|
|
|
|
{
|
|
|
|
//std::cout << " ** activating occluder"<<std::endl;
|
2003-09-26 19:20:43 +08:00
|
|
|
cullingSet.addOccluder(*itr);
|
2002-06-14 00:21:00 +08:00
|
|
|
}
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
|
2002-06-15 20:14:42 +08:00
|
|
|
// need to recompute frustum volume.
|
|
|
|
_frustumVolume = -1.0f;
|
2002-06-10 19:21:21 +08:00
|
|
|
|
|
|
|
pushCullingSet();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CullStack::popProjectionMatrix()
|
|
|
|
{
|
|
|
|
|
|
|
|
_projectionStack.pop_back();
|
|
|
|
|
|
|
|
_projectionCullingStack.pop_back();
|
|
|
|
|
2002-06-15 20:14:42 +08:00
|
|
|
// need to recompute frustum volume.
|
|
|
|
_frustumVolume = -1.0f;
|
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
popCullingSet();
|
|
|
|
}
|
|
|
|
|
2007-02-08 00:32:14 +08:00
|
|
|
void CullStack::pushModelViewMatrix(RefMatrix* matrix, Transform::ReferenceFrame referenceFrame)
|
2002-06-10 19:21:21 +08:00
|
|
|
{
|
2007-02-08 00:32:14 +08:00
|
|
|
osg::RefMatrix* originalModelView = _modelviewStack.empty() ? 0 : _modelviewStack.back().get();
|
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
_modelviewStack.push_back(matrix);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
pushCullingSet();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2006-12-16 01:27:18 +08:00
|
|
|
osg::Matrix inv;
|
|
|
|
inv.invert(*matrix);
|
2003-06-24 23:40:09 +08:00
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-02-08 00:32:14 +08:00
|
|
|
switch(referenceFrame)
|
|
|
|
{
|
|
|
|
case(Transform::RELATIVE_RF):
|
|
|
|
_eyePointStack.push_back(inv.getTrans());
|
|
|
|
_referenceViewPoints.push_back(getReferenceViewPoint());
|
|
|
|
_viewPointStack.push_back(getReferenceViewPoint() * inv);
|
|
|
|
break;
|
|
|
|
case(Transform::ABSOLUTE_RF):
|
|
|
|
_eyePointStack.push_back(inv.getTrans());
|
|
|
|
_referenceViewPoints.push_back(osg::Vec3(0.0,0.0,0.0));
|
|
|
|
_viewPointStack.push_back(_eyePointStack.back());
|
|
|
|
break;
|
|
|
|
case(Transform::ABSOLUTE_RF_INHERIT_VIEWPOINT):
|
|
|
|
{
|
|
|
|
_eyePointStack.push_back(inv.getTrans());
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-02-08 00:32:14 +08:00
|
|
|
osg::Vec3 referenceViewPoint = getReferenceViewPoint();
|
|
|
|
if (originalModelView)
|
|
|
|
{
|
|
|
|
osg::Matrix viewPointTransformMatrix;
|
|
|
|
viewPointTransformMatrix.invert(*originalModelView);
|
|
|
|
viewPointTransformMatrix.postMult(*matrix);
|
|
|
|
referenceViewPoint = referenceViewPoint * viewPointTransformMatrix;
|
|
|
|
}
|
|
|
|
|
|
|
|
_referenceViewPoints.push_back(referenceViewPoint);
|
|
|
|
_viewPointStack.push_back(getReferenceViewPoint() * inv);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-06-24 23:40:09 +08:00
|
|
|
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
osg::Vec3 lookVector = getLookVectorLocal();
|
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
_bbCornerFar = (lookVector.x()>=0?1:0) |
|
|
|
|
(lookVector.y()>=0?2:0) |
|
|
|
|
(lookVector.z()>=0?4:0);
|
|
|
|
|
|
|
|
_bbCornerNear = (~_bbCornerFar)&7;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CullStack::popModelViewMatrix()
|
|
|
|
{
|
|
|
|
_modelviewStack.pop_back();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
_eyePointStack.pop_back();
|
2007-02-08 00:32:14 +08:00
|
|
|
_referenceViewPoints.pop_back();
|
2006-12-16 01:27:18 +08:00
|
|
|
_viewPointStack.pop_back();
|
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
popCullingSet();
|
|
|
|
|
|
|
|
|
|
|
|
osg::Vec3 lookVector(0.0f,0.0f,-1.0f);
|
|
|
|
if (!_modelviewStack.empty())
|
|
|
|
{
|
|
|
|
lookVector = getLookVectorLocal();
|
|
|
|
}
|
|
|
|
_bbCornerFar = (lookVector.x()>=0?1:0) |
|
|
|
|
(lookVector.y()>=0?2:0) |
|
|
|
|
(lookVector.z()>=0?4:0);
|
|
|
|
|
|
|
|
_bbCornerNear = (~_bbCornerFar)&7;
|
|
|
|
}
|
2002-06-15 20:14:42 +08:00
|
|
|
|
|
|
|
void CullStack::computeFrustumVolume()
|
|
|
|
{
|
|
|
|
osg::Matrix invP;
|
2007-02-21 21:48:01 +08:00
|
|
|
invP.invert(*getProjectionMatrix());
|
2002-06-15 20:14:42 +08:00
|
|
|
|
|
|
|
osg::Vec3 f1(-1,-1,-1); f1 = f1*invP;
|
|
|
|
osg::Vec3 f2(-1, 1,-1); f2 = f2*invP;
|
|
|
|
osg::Vec3 f3( 1, 1,-1); f3 = f3*invP;
|
|
|
|
osg::Vec3 f4( 1,-1,-1); f4 = f4*invP;
|
|
|
|
|
|
|
|
osg::Vec3 b1(-1,-1,1); b1 = b1*invP;
|
|
|
|
osg::Vec3 b2(-1, 1,1); b2 = b2*invP;
|
|
|
|
osg::Vec3 b3( 1, 1,1); b3 = b3*invP;
|
|
|
|
osg::Vec3 b4( 1,-1,1); b4 = b4*invP;
|
|
|
|
|
|
|
|
_frustumVolume = computeVolume(f1,f2,f3,b1,b2,b3)+
|
|
|
|
computeVolume(f2,f3,f4,b1,b3,b4);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-15 20:14:42 +08:00
|
|
|
}
|