多视图多窗口渲染示例的代码如程序清单8-6所示
// 多视图多窗口渲染示例
void compositeViewer_8_6(const string &strDataFolder)
{// 创建一个CompositeViewer对象osg::ref_ptr<osgViewer::CompositeViewer> viewer = new osgViewer::CompositeViewer();// 创建两个视图osg::ref_ptr<osgViewer::View> viewer1 = new osgViewer::View();osg::ref_ptr<osgViewer::View> viewer2 = new osgViewer::View();int xoffset = 50;int yoffset = 200;// 视图1的节点string strDataPath = strDataFolder + "cessna.osg";osg::ref_ptr<osg::Node> viewer1Node = osgDB::readNodeFile(strDataPath);// 视图2的节点strDataPath = strDataFolder + "cow.osg";osg::ref_ptr<osg::Node> viewer2Node = osgDB::readNodeFile(strDataPath);// 优化产经数据osgUtil::Optimizer optimizer;optimizer.optimize(viewer1Node.get());optimizer.optimize(viewer2Node.get());// 视图窗口1{// 设置图形环境特性osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits();traits->x = xoffset + 0;traits->y = yoffset + 0;traits->width = 600;traits->height = 480;traits->windowDecoration = true;traits->doubleBuffer = true;traits->sharedContext = 0;// 绘制图形环境osg::ref_ptr<osg::GraphicsContext>gc = osg::GraphicsContext::createGraphicsContext(traits.get());// 创建相机osg::ref_ptr<osg::Camera> camera1 = viewer1->getCamera();// 设置图形环境camera1->setGraphicsContext(gc.get());// 设置视口camera1->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));// 设置缓冲GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;camera1->setDrawBuffer(buffer);camera1->setReadBuffer(buffer);// 设置场景数据viewer1->setSceneData(viewer1Node.get());}// 视图窗口2{// 设置图形环境特性osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits();traits->x = xoffset + 600;traits->y = yoffset + 0;traits->width = 600;traits->height = 480;traits->windowDecoration = true;traits->doubleBuffer = true;traits->sharedContext = 0;// 绘制图形环境osg::ref_ptr<osg::GraphicsContext>gc = osg::GraphicsContext::createGraphicsContext(traits.get());// 创建相机osg::ref_ptr<osg::Camera> camera2 = viewer2->getCamera();// 设置图形环境camera2->setGraphicsContext(gc.get());// 设置视口camera2->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));// 设置缓冲GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;camera2->setDrawBuffer(buffer);camera2->setReadBuffer(buffer);// 设置场景数据viewer2->setSceneData(viewer2Node.get());}// 添加视图viewer->addView(viewer1.get());viewer->addView(viewer2.get());viewer->realize();viewer->run();
}
截图如图8-15所示。
图8-15多视图多窗口渲染示例图