|
楼主 |
发表于 2010-5-18 16:58:44
|
显示全部楼层
本帖最后由 yin_savage 于 2010-5-18 17:00 编辑
下面是CullCallback中的实现代码,麻烦大家看看,若要完整的源代码,我也可以上传。
// 0、准备工作
osgUtil::CullVisitor *pCullVisitor = dynamic_cast<osgUtil::CullVisitor *>(pNodeVisitor);
if(!pCullVisitor)
{
traverse(pNode, pNodeVisitor);
return;
}
const osg::RenderInfo &info = pCullVisitor->getRenderInfo();
const osgViewer::View *pView = dynamic_cast<const osgViewer::View *>(info.getView());
const osg::Camera *pCamera = pView->getCamera();
// 1、设置HUD相机的参数
// 1.1、设置HUD相机的视口
osg::Camera *pHudCamera = dynamic_cast<osg::Camera *>(pNode);
const osg::Viewport *pCurViewport = pCamera->getViewport();
const float fltSize = m_fltScopeSize * (pCurViewport->width() < pCurViewport->height() ? pCurViewport->width() : pCurViewport->height());
const float fltX = (pCurViewport->width() - fltSize) * 0.5f;
const float fltY = (pCurViewport->height() - fltSize) * 0.5f;
pHudCamera->setViewport(int(fltX + 0.5f), int(fltY + 0.5f), int(fltSize + 0.5f), int(fltSize + 0.5f));
// 1.2、设置HUD相机的模型视图矩阵
const osg::Matrixd &mtxModelView = pCamera->getViewMatrix();
const osg:uat quatRotate = mtxModelView.getRotate();
pHudCamera->setViewMatrix(osg::Matrixd(quatRotate));
// 2、设置RTT相机的参数
// 2.1、设置RTT相机的投影矩阵
const osg::Matrixd &mtxProjection = pCamera->getProjectionMatrix();
double dblLeft = -1.0, dblRight = 1.0, dblBottom = -1.0, dblTop = 1.0, dblNear = 1.0, dblFar = 2.0;
bool bOrthoProj = false, bFrustumProj = false;
if(mtxProjection.getFrustum(dblLeft, dblRight, dblBottom, dblTop, dblNear, dblFar))
{
bFrustumProj = true;
}
else if(mtxProjection.getOrtho(dblLeft, dblRight, dblBottom, dblTop, dblNear, dblFar))
{
bOrthoProj = true;
}
if(bFrustumProj || bOrthoProj)
{
const double dblWidth = dblRight - dblLeft;
const double dblHeight = dblTop - dblBottom;
const double dblSize = (dblWidth < dblHeight ? dblWidth : dblHeight) * 0.5 * m_fltScopeSize;
if(bFrustumProj)
{
m_pRttCamera->setProjectionMatrixAsFrustum(-dblSize, dblSize, -dblSize, dblSize, dblNear, dblFar);
}
else
{
m_pRttCamera->setProjectionMatrixAsOrtho(-dblSize, dblSize, -dblSize, dblSize, dblNear, dblFar);
}
}
// 2.2、设置RTT相机的模型视图矩阵
const osg::Matrixd mtxRotate(quatRotate.inverse());
m_pRttCamera->setViewMatrix(mtxRotate * mtxModelView);
// 缺省处理
traverse(pNode, pNodeVisitor); |
|