OSG学习:场景图形管理(三)——多视图相机渲染

以下内容来自: 

1、《OpenSceneGraph三维渲染引擎编程指南》肖鹏 刘更代 徐明亮 清华大学出版社 

2、《OpenSceneGraph三维渲染引擎设计与实践》王锐 钱学雷 清华大学出版社

3、自己的总结

下载完整工程OSG_18_MultipleViewCameras

创建C++项目后,首先需要配置OSG环境,具体步骤看OSG学习:WIN10系统下OSG+VS2017编译及运行第六步:新建OSG项目测试。

 

// stdafx.h
 
#include 
#include   
#include 
#include 
 
#include  //移动节点的矩阵类,最常用的移动节点的类。可随动、旋转控制节点
#include  //位置变换节点类,提供模型的位置变换、大小缩放、原点位置的设置、坐标系的变换
#include   //相机节点,管理OSG中的模型——视图矩阵,相机的管理主要是通过各种变换实现的
 
#include  
 
#include 
#include 
 
#include 
#include 
#include  //事件监听
#include 
//.cpp
 
/*
*多视图相机渲染
*
*在OSG中,多视图的管理是通过osgViewer::CompositeViewer来实现的,它负责多个视图的管理及同步工作,继承自osgViewer::ViewerBase类
*
*视图的创建:
*设置图形环境特性
*创建图形环境特性
*创建视图
*设置视图场景数据
*(根据分辨率确定合适的投影来保证显示的图形不变形)
*设置相机视口及图形环境
*设置操作器
*(添加事件处理)
*/
 
 
osg::ref_ptr CreateGraphicsContext(osg::ref_ptr traits)
{
	//设置图形环境特性
	//osg::ref_ptr traits = new osg::GraphicsContext::Traits();
	traits->x = 100;
	traits->y = 100;
	traits->width = 900;
	traits->height = 700;
	traits->windowDecoration = true;
	traits->doubleBuffer = true;
	traits->sharedContext = 0;
 
	//创建图形环境特性
	osg::ref_ptr gc = osg::GraphicsContext::createGraphicsContext(traits.get());
	if (gc->valid())
	{
		osg::notify(osg::INFO) << "GraphicsWindow has been created successfully." << std::endl;
 
		//清除窗口颜色及颜色和深度缓冲
		gc->setClearColor(osg::Vec4f(0.2f, 0.2f, 1.6f, 1.0f));
		gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	}
	else
	{
		osg::notify(osg::NOTICE) << "GraphicsWindow has not been created successfully." << std::endl;
	}
 
	return gc.get();
}
 
//视图一
void View1(osg::ref_ptr viewer, osg::ref_ptr node, osg::ref_ptr traits, osg::ref_ptr gc)
{
	//创建视图一
	osg::ref_ptr view = new osgViewer::View();
	viewer->addView(view.get());
 
	//设置视图场景数据
	view->setSceneData(node.get());
 
	//设置相机视口及图形环境
	view->getCamera()->setViewport(new osg::Viewport(0, 0, traits->width / 2, traits->height / 2));
	view->getCamera()->setGraphicsContext(gc.get());
 
	//设置操作器
	view->setCameraManipulator(new osgGA::TrackballManipulator);
 
	//添加事件处理
	view->addEventHandler(new osgViewer::StatsHandler);
	view->addEventHandler(new osgViewer::WindowSizeHandler);
	view->addEventHandler(new osgViewer::ThreadingHandler);
	view->addEventHandler(new osgViewer::RecordCameraPathHandler);
}
 
//视图二
void View2(osg::ref_ptr viewer, osg::ref_ptr node, osg::ref_ptr traits, osg::ref_ptr gc)
{
	//创建视图二
	osg::ref_ptr view = new osgViewer::View();
	viewer->addView(view.get());
 
	//设置视图场景数据
	view->setSceneData(node.get());
 
	//设置相机视口及图形环境
	view->getCamera()->setViewport(new osg::Viewport(traits->width / 2, 0, traits->width / 2, traits->height / 2));
	view->getCamera()->setGraphicsContext(gc.get());
	
	//设置操作器
	view->setCameraManipulator(new osgGA::TrackballManipulator);
}
 
//视图三
void View3(osg::ref_ptr viewer, osg::ref_ptr node, osg::ref_ptr traits, osg::ref_ptr gc)
{
	//创建视图三
	osg::ref_ptr view = new osgViewer::View();
	viewer->addView(view.get());
 
	//设置视图场景数据
	view->setSceneData(node.get());
 
	//根据分辨率确定合适的投影来保证显示的图形不变形
	double fovy, aspectRatio, zNear, zFar;
	view->getCamera()->getProjectionMatrixAsPerspective(fovy, aspectRatio, zNear, zFar);
	double newAspectRatio = double(traits->width) / double(traits->height / 2);
	double aspectRatioChange = newAspectRatio / aspectRatio;
	if (aspectRatioChange != 1.0)
	{
		view->getCamera()->getProjectionMatrix() *= osg::Matrix::scale(1.0 / aspectRatioChange, 1.0, 1.0);
	}
 
	//设置相机视口及图形环境
	view->getCamera()->setViewport(new osg::Viewport(0, traits->height / 2, traits->width / 2, traits->height / 2));
	view->getCamera()->setGraphicsContext(gc.get());
	
	//设置操作器
	view->setCameraManipulator(new osgGA::TrackballManipulator);
}
 
int main()
{
	//读取模型
	osg::ref_ptr cow = osgDB::readNodeFile("cow.osg");
	osg::ref_ptr cessna = osgDB::readNodeFile("cessna.osg");
 
	//优化场景数据
	osgUtil::Optimizer optimizer;
	optimizer.optimize(cow.get());
	optimizer.optimize(cessna.get());
 
	//设置并创建图形环境
	osg::ref_ptr traits = new osg::GraphicsContext::Traits();
	osg::ref_ptr gc = CreateGraphicsContext(traits);
 
	//创建CompositeViewer对象
	osg::ref_ptr viewer = new osgViewer::CompositeViewer();
	View1(viewer, cow.get(), traits.get(), gc.get());
	View2(viewer, cessna.get(), traits.get(), gc.get());
	View3(viewer, cessna.get(), traits.get(), gc.get());
 
	viewer->realize();
 
	return viewer->run();
}

 

你可能感兴趣的:(C/C++,OSG(Open,Scene,Graph),OSG,Camera)