|
1、动态添加从属相机的函数如下(参数是viewer):
void addCameras(osg::ref_ptr<osgViewer::Viewer> viewer)
{
static int numofwin = 0;
//控制添加的从属相机,上限为10个
if((numofwin+1) > 10)
{
printf("No more windows will be created!!!!\n");
return ;
}
//创建窗口系统接口
osg::ref_ptr<osg::GraphicsContext::WindowingSystemInterface> wsi =
osg::GraphicsContext::getWindowingSystemInterface();
//得到窗口分辨率
unsigned int width, height;
wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
//计算一行能放多少窗口
int row_num = width / (320 + 20);
int col_num = height / 256;
//设置图形环境特性
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
traits->windowDecoration = true;
traits->doubleBuffer = true;
traits->sharedContext = false;
traits->windowName = "飞行器窗口";
traits->supportsResize = 0;
//计算所要添加的从属相机所在的图形窗口的位置
traits->x = ((numofwin) % row_num) * (320 + 10) + 10;
traits->y = (numofwin) / row_num * (256 + 30) + 30;
traits->width = 320;
traits->height = 256;
//创建图形环境
osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
//创建从属相机
osg::ref_ptr<osg::Camera> cameraClient = new osg::Camera();
cameraClient->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
cameraClient->setGraphicsContext(gc.get());
cameraClient->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));
cameraClient->setProjectionMatrixAsPerspective(30,(double)traits->width/(double)traits->height,0.000001,1000000);
cameraClient->setViewMatrixAsLookAt(eye,center,up);
GLenum bufferClient = traits->doubleBuffer ? GL_BACK : GL_FRONT;
cameraClient->setDrawBuffer(bufferClient);
cameraClient->setReadBuffer(bufferClient);
//添加从属相机
viewer->addSlave(cameraClient);
numofwin++;
}
2、按下a键将调用上述函数来添加从属相机
class myHandler: public osgGA::GUIEventHandler
{
public:
ImageHandler(){}
~ImageHandler(){}
//重载handle()函数
bool ImageHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
{
osg::ref_ptr<osgViewer::Viewer> viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
if(viewer == NULL)
{
return false;
}
//定义一个静态变量
static int _screenCaptureSequence = 0;
switch(ea.getEventType())
{
case osgGA::GUIEventAdapter::KEYDOWN:
{
if(ea.getKey() == 'a' || ea.getKey() == 'A')
{
viewer->stopThreading();
addCameras(viewer.get());
viewer->startThreading();
viewer->realize();
}
}
break;
default:
return false;
}
return true;
}
};
3、从属相机是为了观察场景中右边的cow,但是实际运行出现了显示的问题,如截图所示,小窗口中的牛变成白色了。截图右下角为主相机的图形窗口,上面10个小窗口为按下a键添加的窗口。
4、有哪位大神能帮忙解决一下,谢谢了。 |
-
实际运行截图
|