|
楼主 |
发表于 2012-11-22 15:14:47
|
显示全部楼层
smash 发表于 2012-11-19 14:15
这种情况应该是调整一下ViewPort,把三个图像拼接起来。不用改ProjectionMatrix。
还是不行啊!
代码如下:
//创建从相机,设置视口
osg::Camera* createCamera(int x, int y, int w, int h)
{
osg::ref_ptr<osg::GraphicsContext::Traits> trait = new osg::GraphicsContext::Traits();
trait->x = x;
trait->y = y;
trait->width = w;
trait->height = h;
trait->windowDecoration = false;
trait->doubleBuffer = true;
osg:isplaySettings *ds = osg::DisplaySettings::instance();
trait->alpha = ds->getMinimumNumAlphaBits();
trait->stencil = ds->getMinimumNumStencilBits();
trait->sampleBuffers = ds->getMultiSamples();
trait->samples = ds->getNumMultiSamples();
osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(trait.get());
osg::ref_ptr<osg::Camera> camera = new osg::Camera();
camera->setGraphicsContext(gc.get());
camera->setViewport(new osg::Viewport(0,0,w,h));
//camera->setReferenceFrame(osg::Camera::ReferenceFrame::ABSOLUTE_RF_INHERIT_VIEWPOINT);
double fov;
double aspectRatio;
double nearPlane;
double farPlane;
double newAspectRatio;
camera->setProjectionResizePolicy(osg::Camera:rojectionResizePolicy::FIXED);
//换取对称视景个参数并显示,每个屏幕都有对应的ratio,所以必须先get在set
camera->getProjectionMatrixAsPerspective( fov,aspectRatio,nearPlane,farPlane);
newAspectRatio = double(w)/double(h);
//double aspectRatiochange = newAspectRatio/aspectRatio;
//设置视景参数,将yz平面视野角度设为50°
camera->setProjectionMatrixAsPerspective(60.0f, aspectRatio, 1, 500.0);
return camera.release();
}
//主相机设置
//得到窗口的图形环境
osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
if (!wsi)
{
osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
}
unsigned int tileWidth, tileHeight;
wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), tileWidth ,tileHeight);
//初始化主相机图形环境
osg::ref_ptr<osg::GraphicsContext::Traits> mastertrait = new osg::GraphicsContext::Traits();
mastertrait->x = tileWidth/3;
mastertrait->y = 0;
mastertrait->width = tileWidth/3;
mastertrait->height = tileHeight;
mastertrait->windowDecoration = false;
mastertrait->doubleBuffer = true;
mastertrait->useCursor = false;
osg::DisplaySettings *ds = osg::DisplaySettings::instance();
mastertrait->alpha = ds->getMinimumNumAlphaBits();
mastertrait->stencil = ds->getMinimumNumStencilBits();
mastertrait->sampleBuffers = ds->getMultiSamples();
mastertrait->samples = ds->getNumMultiSamples();
osg::ref_ptr<osg::GraphicsContext> mastergc = osg::GraphicsContext::createGraphicsContext(mastertrait.get());
osg::ref_ptr<osg::Camera> masterCamera = new osg::Camera();
masterCamera = viewer->getCamera();
masterCamera->setGraphicsContext(mastergc.get());
masterCamera->setViewport(new osg::Viewport(0,0,mastertrait->width,mastertrait->height));
//添加从相机,设置从相机的投影矩阵的偏移矩阵,设置好各个从相机的视口
viewer->addSlave(createCamera(0,0,tileWidth/3,tileHeight),osg::Matrixd::translate(2,0.0,0.0),osg::Matrixd());
viewer->addSlave(createCamera(2*tileWidth/3,0,tileWidth/3,tileHeight),osg::Matrixd::translate(-2,0.0,0.0),osg::Matrixd());
viewer->getCamera()->setProjectionResizePolicy(osg::Camera::ProjectionResizePolicy::FIXED);
viewer->getCamera()->getProjectionMatrixAsPerspective( fovy,aspectRatio,nearPlane,farPlane);
newAspectRatio = double(tileWidth)/double(tileHeight);
double aspectRatiochange = newAspectRatio/aspectRatio;
viewer->getCamera()->setProjectionMatrixAsPerspective(60.0f, aspectRatio, 1, 500.0);
if(aspectRatiochange!=1.0)
{
viewer->getCamera()->getProjectionMatrix() *= osg::Matrix::scale(1.0/aspectRatiochange,1.0,1.0);
}
需求是:
主机利用分屏器外接了三个投影仪,每个投影仪都有对应的屏幕。这样做主需要是想用三个投影仪显示一个大的场景。程序中使用的是透视投影,设置的fovy是60度,宽高比为屏幕的宽高比。可是,显示出来以后两侧的投影仪存在很大的变形,感觉被横向拉伸。后来看了array的osgcookbook里边的威力强的例子,想到用三个相机(一个主相机,两个从相机,主相机对应中间的屏幕,从相机对应的是两边的屏幕)来渲染同一个场景,然后三个相机设置不同的投影矩阵。设置的代码如上,但是显示出来的结果还是一样。中间的主相机对应的屏幕的场景没有拉伸,两边的从相机对应的场景渲染出来有很大的拉伸,不知道这个是不是正常的,有没有什么解决方案,我用三个相机来渲染为什么还是不行呢?感觉您之前接触过,还望多指教。两个从相机的视口还有偏移矩阵我都设置好了的,三个相机刚好组成了同一场景,没有什么重叠的地方,就是两侧的从相机渲染出来的场景有横向拉伸。另外还有一个问题,用从相机渲染一个大的场景,为什么会卡呢??
|
|