Added support for updating the SceneView projection matrix from the values
of near and far computed during the cull traversal.
This commit is contained in:
@@ -144,6 +144,7 @@ EXAMPLE_DIRS = \
|
||||
osgforest\
|
||||
osgimpostor\
|
||||
osgkeyboard\
|
||||
osgkeyboardmouse\
|
||||
osglight\
|
||||
osglightpoint\
|
||||
osglogo\
|
||||
@@ -162,6 +163,7 @@ EXAMPLE_DIRS = \
|
||||
osgscalarbar\
|
||||
osgspheresegment\
|
||||
osgsimulation\
|
||||
osgsimple\
|
||||
osgshaders\
|
||||
osgshadowtexture\
|
||||
osgshape\
|
||||
|
||||
@@ -213,6 +213,8 @@ class OSGUTIL_EXPORT CullVisitor : public osg::NodeVisitor, public osg::CullStac
|
||||
|
||||
/** reimplement CullStack's popProjectionMatrix() adding clamping of the projection matrix to the computed near and far.*/
|
||||
virtual void popProjectionMatrix();
|
||||
|
||||
bool clampProjectionMatrix(osg::Matrix& projection, value_type& znear, value_type& zfar) const;
|
||||
|
||||
void setState(osg::State* state) { _state = state; }
|
||||
osg::State* getState() { return _state.get(); }
|
||||
|
||||
@@ -180,27 +180,37 @@ void CullVisitor::popProjectionMatrix()
|
||||
// so it doesn't cull them out.
|
||||
osg::Matrix& projection = *_projectionStack.back();
|
||||
|
||||
clampProjectionMatrix(projection, _computed_znear, _computed_zfar);
|
||||
}
|
||||
|
||||
CullStack::popProjectionMatrix();
|
||||
}
|
||||
|
||||
bool CullVisitor::clampProjectionMatrix(osg::Matrix& projection, value_type& znear, value_type& zfar) const
|
||||
{
|
||||
if (zfar>0.0f)
|
||||
{
|
||||
|
||||
if (projection(0,3)==0.0f && projection(1,3)==0.0f && projection(2,3)==0.0f)
|
||||
{
|
||||
//std::cout << "Orthographic projection "<<projection<<std::endl;
|
||||
|
||||
value_type span = (_computed_zfar-_computed_znear);
|
||||
value_type desired_znear = _computed_znear - span*0.02f;
|
||||
value_type desired_zfar = _computed_zfar + span*0.02f;
|
||||
value_type span = (zfar-znear);
|
||||
value_type desired_znear = znear - span*0.02f;
|
||||
value_type desired_zfar = zfar + span*0.02f;
|
||||
|
||||
// near plane clamping.
|
||||
//double min_near_plane = _computed_zfar*_nearFarRatio;
|
||||
//if (desired_znear<min_near_plane) desired_znear=min_near_plane;
|
||||
|
||||
// assign the clamped values back to the computed values.
|
||||
_computed_znear = desired_znear;
|
||||
_computed_zfar = desired_zfar;
|
||||
znear = desired_znear;
|
||||
zfar = desired_zfar;
|
||||
|
||||
projection(2,2)=-2.0f/(desired_zfar-desired_znear);
|
||||
projection(3,2)=-(desired_zfar+desired_znear)/(desired_zfar-desired_znear);
|
||||
|
||||
//projection(2,2)=-2.0f/(-desired_zfar+desired_znear);
|
||||
//projection(3,2)=(desired_zfar+desired_znear)/(desired_zfar-desired_znear);
|
||||
return true;
|
||||
|
||||
}
|
||||
else
|
||||
@@ -211,22 +221,16 @@ void CullVisitor::popProjectionMatrix()
|
||||
//std::cout << "_computed_znear"<<_computed_znear<<std::endl;
|
||||
//std::cout << "_computed_zfar"<<_computed_zfar<<std::endl;
|
||||
|
||||
|
||||
value_type desired_znear = _computed_znear *0.98;
|
||||
value_type desired_zfar = _computed_zfar *1.02;
|
||||
// value_type desired_znear = _computed_znear *0.5;
|
||||
// value_type desired_zfar = _computed_zfar *2.0;
|
||||
value_type desired_znear = znear *0.98;
|
||||
value_type desired_zfar = zfar *1.02;
|
||||
|
||||
// near plane clamping.
|
||||
double min_near_plane = _computed_zfar*_nearFarRatio;
|
||||
double min_near_plane = zfar*_nearFarRatio;
|
||||
if (desired_znear<min_near_plane) desired_znear=min_near_plane;
|
||||
|
||||
//std::cout << " desired_znear"<<desired_znear<<std::endl;
|
||||
//std::cout << " desired_zfar"<<desired_zfar<<std::endl;
|
||||
|
||||
// assign the clamped values back to the computed values.
|
||||
_computed_znear = desired_znear;
|
||||
_computed_zfar = desired_zfar;
|
||||
znear = desired_znear;
|
||||
zfar = desired_zfar;
|
||||
|
||||
value_type trans_near_plane = (-desired_znear*projection(2,2)+projection(3,2))/(-desired_znear*projection(2,3)+projection(3,3));
|
||||
value_type trans_far_plane = (-desired_zfar*projection(2,2)+projection(3,2))/(-desired_zfar*projection(2,3)+projection(3,3));
|
||||
@@ -238,14 +242,14 @@ void CullVisitor::popProjectionMatrix()
|
||||
0.0f,1.0f,0.0f,0.0f,
|
||||
0.0f,0.0f,ratio,0.0f,
|
||||
0.0f,0.0f,center*ratio,1.0f));
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
CullStack::popProjectionMatrix();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void CullVisitor::updateCalculatedNearFar(const osg::Matrix& matrix,const osg::BoundingBox& bb)
|
||||
{
|
||||
#if 0
|
||||
|
||||
@@ -149,7 +149,7 @@ void RenderStage::drawImplementation(osg::State& state,RenderLeaf*& previous)
|
||||
|
||||
|
||||
glClear( _clearMask );
|
||||
|
||||
|
||||
#ifdef USE_SISSOR_TEST
|
||||
glDisable( GL_SCISSOR_TEST );
|
||||
#endif
|
||||
|
||||
@@ -389,12 +389,27 @@ void SceneView::cull()
|
||||
_cullVisitor->setTraversalMask(_cullMaskLeft);
|
||||
cullStage(computeLeftEyeProjection(_projectionMatrix),computeLeftEyeView(_viewMatrix),_cullVisitor.get(),_rendergraph.get(),_renderStage.get());
|
||||
|
||||
if (_cullVisitor->getComputeNearFarMode()!=osgUtil::CullVisitor::DO_NOT_COMPUTE_NEAR_FAR)
|
||||
{
|
||||
CullVisitor::value_type zNear = _cullVisitor->getCalculatedNearPlane();
|
||||
CullVisitor::value_type zFar = _cullVisitor->getCalculatedFarPlane();
|
||||
_cullVisitor->clampProjectionMatrix(_projectionMatrix,zNear,zFar);
|
||||
}
|
||||
|
||||
}
|
||||
else if (_displaySettings->getStereoMode()==osg::DisplaySettings::RIGHT_EYE)
|
||||
{
|
||||
// set up the right eye.
|
||||
_cullVisitor->setTraversalMask(_cullMaskRight);
|
||||
cullStage(computeRightEyeProjection(_projectionMatrix),computeRightEyeView(_viewMatrix),_cullVisitor.get(),_rendergraph.get(),_renderStage.get());
|
||||
|
||||
if (_cullVisitor->getComputeNearFarMode()!=osgUtil::CullVisitor::DO_NOT_COMPUTE_NEAR_FAR)
|
||||
{
|
||||
CullVisitor::value_type zNear = _cullVisitor->getCalculatedNearPlane();
|
||||
CullVisitor::value_type zFar = _cullVisitor->getCalculatedFarPlane();
|
||||
_cullVisitor->clampProjectionMatrix(_projectionMatrix,zNear,zFar);
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -407,8 +422,6 @@ void SceneView::cull()
|
||||
if (!_rendergraphRight.valid()) _rendergraphRight = dynamic_cast<RenderGraph*>(_rendergraph->cloneType());
|
||||
if (!_renderStageRight.valid()) _renderStageRight = dynamic_cast<RenderStage*>(_renderStage->clone(osg::CopyOp::DEEP_COPY_ALL));
|
||||
|
||||
|
||||
|
||||
_cullVisitorLeft->setDatabaseRequestHandler(_cullVisitor->getDatabaseRequestHandler());
|
||||
_cullVisitorLeft->setTraversalMask(_cullMaskLeft);
|
||||
cullStage(computeLeftEyeProjection(_projectionMatrix),computeLeftEyeView(_viewMatrix),_cullVisitorLeft.get(),_rendergraphLeft.get(),_renderStageLeft.get());
|
||||
@@ -419,6 +432,14 @@ void SceneView::cull()
|
||||
_cullVisitorRight->setTraversalMask(_cullMaskRight);
|
||||
cullStage(computeRightEyeProjection(_projectionMatrix),computeRightEyeView(_viewMatrix),_cullVisitorRight.get(),_rendergraphRight.get(),_renderStageRight.get());
|
||||
|
||||
if (_cullVisitorLeft->getComputeNearFarMode()!=osgUtil::CullVisitor::DO_NOT_COMPUTE_NEAR_FAR &&
|
||||
_cullVisitorRight->getComputeNearFarMode()!=osgUtil::CullVisitor::DO_NOT_COMPUTE_NEAR_FAR)
|
||||
{
|
||||
CullVisitor::value_type zNear = osg::minimum(_cullVisitorLeft->getCalculatedNearPlane(),_cullVisitorRight->getCalculatedNearPlane());
|
||||
CullVisitor::value_type zFar = osg::maximum(_cullVisitorLeft->getCalculatedFarPlane(),_cullVisitorRight->getCalculatedFarPlane());
|
||||
_cullVisitor->clampProjectionMatrix(_projectionMatrix,zNear,zFar);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -428,6 +449,12 @@ void SceneView::cull()
|
||||
_cullVisitor->setTraversalMask(_cullMask);
|
||||
cullStage(_projectionMatrix,_viewMatrix,_cullVisitor.get(),_rendergraph.get(),_renderStage.get());
|
||||
|
||||
if (_cullVisitor->getComputeNearFarMode()!=osgUtil::CullVisitor::DO_NOT_COMPUTE_NEAR_FAR)
|
||||
{
|
||||
CullVisitor::value_type zNear = _cullVisitor->getCalculatedNearPlane();
|
||||
CullVisitor::value_type zFar = _cullVisitor->getCalculatedFarPlane();
|
||||
_cullVisitor->clampProjectionMatrix(_projectionMatrix,zNear,zFar);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user