场景图形管理-多视图多窗口渲染示例(4)

        多视图多窗口渲染示例的代码如程序清单8-6所示

// 多视图多窗口渲染示例
void compositeViewer_8_6(const string &strDataFolder)
{
	// 创建一个CompositeViewer对象
	osg::ref_ptr viewer = new osgViewer::CompositeViewer();

	// 创建两个视图
	osg::ref_ptr viewer1 = new osgViewer::View();
	osg::ref_ptr viewer2 = new osgViewer::View();

	int xoffset = 50;
	int yoffset = 200;

	// 视图1的节点
	string strDataPath = strDataFolder + "cessna.osg";
	osg::ref_ptr viewer1Node = osgDB::readNodeFile(strDataPath);

	// 视图2的节点
	strDataPath = strDataFolder + "cow.osg";
	osg::ref_ptr viewer2Node = osgDB::readNodeFile(strDataPath);

	// 优化产经数据
	osgUtil::Optimizer optimizer;
	optimizer.optimize(viewer1Node.get());
	optimizer.optimize(viewer2Node.get());

	// 视图窗口1
	{
		// 设置图形环境特性
		osg::ref_ptr 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_ptrgc = osg::GraphicsContext::createGraphicsContext(traits.get());

		// 创建相机
		osg::ref_ptr 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 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_ptrgc = osg::GraphicsContext::createGraphicsContext(traits.get());

		// 创建相机
		osg::ref_ptr 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所示。

场景图形管理-多视图多窗口渲染示例(4)_第1张图片

图8-15多视图多窗口渲染示例图

你可能感兴趣的:(c++,图形渲染,3d)