|
//得到cameraMaster(主相机)
osg::ref_ptr<osg::Camera> cameraMaster = viewer->getCamera();
//设置图形环境
cameraMaster->setGraphicsContext(gc.get());
//根据分辨率来确定合适的投影来保证显示的图形不变形
double fovy, aspectRatio, zNear, zFar;
cameraMaster->getProjectionMatrixAsPerspective(fovy, aspectRatio, zNear, zFar);
double newAspectRatio = double(traits->width) / double(traits->height);
double aspectRatioChange = newAspectRatio / aspectRatio;
if (aspectRatioChange != 1.0)
{
cameraMaster->getProjectionMatrix() *= osg::Matrix::scale(1.0/aspectRatioChange,1.0,1.0);
}
//设置视口
cameraMaster->setViewport(new osg::Viewport(0,0, width, height));
GLenum bufferMaster = traits->doubleBuffer ? GL_BACK : GL_FRONT;
//设置缓冲区
cameraMaster->setDrawBuffer(bufferMaster);
cameraMaster->setReadBuffer(bufferMaster);
//创建从属相机
osg::ref_ptr<osg::Camera> cameraClient = new osg::Camera();
cameraClient->setGraphicsContext(gc.get());
cameraClient->setViewport(new osg::Viewport(0,0, 400, 400));
GLenum bufferClient = traits->doubleBuffer ? GL_BACK : GL_FRONT;
cameraClient->setDrawBuffer(bufferClient);
cameraClient->setReadBuffer(bufferClient);
//添加从属相机
viewer->addSlave(cameraClient,osg::Matrix::scale(aspectRatio,1.0,1.0),osg::Matrix()) ; |
|