|
这几天拿来以前的一个程序不小心多想了一下,发现了一个小问题,但是让我痛苦不堪,那就是原来我做过一个多视口的程序,如下:
#include "../Common/Common.h"
#include <Windows.h>
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/stateSetManipulator>
#include <osgGA/GUIEventHandler>
#include <osg/Camera>
#ifdef _DEBUG
#pragma comment(lib,"../Debug/Common.lib")
#else
#pragma comment(lib,"../Release/Common.lib")
#endif
class ChangWinSize : public osgGA::GUIEventHandler
{
public:
ChangWinSize(){ _first = true;}
bool handle(const osgGA::GUIEventAdapter &ea,osgGA::GUIActionAdapter &aa)
{
if( _first ){
_first = false;
osgViewer::Viewer *viewer = dynamic_cast<osgViewer::Viewer*> (&aa);
osgViewer::Viewer::Windows ws;
viewer->getWindows(ws);
if(!ws.empty()){
for(osgViewer::Viewer::Windows::iterator iter = ws.begin() ; iter<ws.end() ; ++iter){
(*iter)->setWindowRectangle(50,50,600,300);
//(*iter)->setWindowDecoration(false);
}
}
}
return false;
}
private:
bool _first;
};
int main()
{
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
osg::ref_ptr<osg::Camera> masterCamera = new osg::Camera;
osg::ref_ptr<osg::Camera> slaveCamera = new osg::Camera;
//
osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
if( !wsi ){
std::cout<<"Get wsi null"<<std::endl;
return NULL;
}
unsigned int width,height;
wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0),width,height);
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
traits->x = 0;
traits->y = 0;
traits->width = width;
traits->height = height;
traits->doubleBuffer = true;
traits->sharedContext = 0;
traits->windowDecoration = true;
osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits);
if( !gc->valid() ){
std::cout<<"gc is null"<<std::endl;
return NULL;
}
//width = 600,height = 300;
//设置相机属性
masterCamera->setGraphicsContext(gc);
masterCamera->setViewMatrix(osg::Matrix::identity());
masterCamera->setProjectionMatrix(osg::Matrix::identity());
masterCamera->setViewport(0,0,width/2,height);
masterCamera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
slaveCamera->setGraphicsContext(gc);
slaveCamera->setViewport(width/2,0,width/2,height);
slaveCamera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
//添加从相机数据
slaveCamera->addChild(osgDB::readNodeFile("cow.osg"));
viewer->addSlave(masterCamera);
viewer->addSlave(slaveCamera,false);
viewer->addEventHandler(new ChangWinSize);
viewer->setSceneData(osgDB::readNodeFile("cow.osg"));
viewer->run();
return 0;
}
这样的话显示的图片1确实是两个视口,如图:
但是如果这时候我发现主相机和从相机的视口场合宽中的数据均为:
masterCamera->setViewport(0,0,width/2,height);
slaveCamera->setViewport(width/2,0,width/2,height);
width = 1920
height = 1080
这个是我的显示器的分辨率,不过这时候窗口不是利用回调改成了如下:
(*iter)->setWindowRectangle(50,50,600,300);
600*300了吗?
这是怎么回事?
我做了如下改动,把设置相机的代码如下改一下,其他的均不改变:
masterCamera->setViewport(0,0,600/2,300);
slaveCamera->setViewport(600/2,0,600/2,300);
得到的图像如下:
图2
不应该视口是以像素为单位的话,这样才应该是正常均分这个窗口吗?
哎,查了好些个窗口、视口、以及设备坐标的东西,讲的都乱糟糟的,现在把以前的理解全部推翻了,这个越想越不正确,该怎么解释,请各位大神指点指点。 |
|