osg示例解析之osgcamera

来源:互联网 发布:假币在淘宝里叫什么 编辑:程序博客网 时间:2024/05/23 13:02

转载自:http://blog.csdn.net/csxiaoshui/article/details/51517203

简介

osgcamera这个示例向我们展示了在osg中如何使用多个相机和多个视口的方法,例子中提供了多窗口多相机以及窗口多相机的方式。

  • 具体解析

首先本例中使用了一个名为ModelHandler的事件处理类,这个类的作用比较简单,就是用来让我们切换场景中加载不同模型的,对本例来说意义不大,实现如下:
[cpp] view plain copy
print?
  1. case(osgGA::GUIEventAdapter::KEYUP):  
  2. {  
  3.     if (ea.getKey()=='l')  
  4.     {                      
  5.         osg::ref_ptr<osg::Node> model = osgDB::readNodeFile( _filenames[_position] );  
  6.         ++_position;  
  7.         if (_position>=_filenames.size()) _position = 0;  
  8.           
  9.         if (model.valid())  
  10.         {  
  11.             viewer->setSceneData(model.get());  
  12.         }  
  13.           
  14.         return true;  
  15.     }  
  16. }  
当按下L键时,可以读取ModelHandler中不同的文件到场景中。

本例中还定义了EnableVBOVisitor这个访问器,它的主要作用是设置场景中Drawable(具体来说是Geometry几何体)的绘制方式是VBO的模式,关于VBO的一些介绍可以参考我的另一篇文章《VA、VAO和VBO  API备忘》  EnableVBOVisitor仅仅是将OSG默认的几何体绘制方式由显示列表修改为VBO的方式,并不是本例的核心内容。

本例中最关键的两个函数是singleWindowMultipleCameras和multipleWindowMultipleCameras,这两个函数分别实现了单窗口多相机和多窗口多相机的模式,下面我们详细看一下它们的实现:

在singleWindowMultipleCameras函数中,首先获取了当前使用的显示器的分辨率,据此创建了一个用来渲染场景的GraphicContext(其实就是渲染的窗口),之后创建了2个相机,并设置它们的视口大小,将相机设置为Slave相机。代码并不复杂,但是其中设置的内容可以细说一下。
[cpp] view plain copy
print?
  1. void singleWindowMultipleCameras(osgViewer::Viewer& viewer)  
  2. {  
  3.     //获取屏幕分辨率  
  4.     osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();  
  5.     if (!wsi)   
  6.     {  
  7.         osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl;  
  8.         return;  
  9.     }  
  10.       
  11.     unsigned int width, height;  
  12.     wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);  
  13.       
  14.     //创建渲染窗口  
  15.     osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;  
  16.     traits->x = 0;  
  17.     traits->y = 0;  
  18.     traits->width = width;  
  19.     traits->height = height;  
  20.     traits->windowDecoration = true;  
  21.     traits->doubleBuffer = true;  
  22.     traits->sharedContext = 0;  
  23.   
  24.     osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());  
  25.     if (gc.valid())  
  26.     {  
  27.         gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f));  
  28.         gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
  29.     }  
  30.     else  
  31.     {  
  32.         osg::notify(osg::NOTICE)<<"  GraphicsWindow has not been created successfully."<<std::endl;  
  33.     }  
  34.   
  35.     unsigned int numCameras = 2;  
  36.     double aspectRatioScale = 1.0;  
  37.     //创建两个相机并添加到Viewer的Slave中  
  38.     for(unsigned int i=0; i<numCameras;++i)  
  39.     {  
  40.         osg::ref_ptr<osg::Camera> camera = new osg::Camera;  
  41.         camera->setGraphicsContext(gc.get());  
  42.         camera->setViewport(new osg::Viewport((i*width)/numCameras,(i*height)/numCameras, width/numCameras, height/numCameras));  
  43.         GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;  
  44.         camera->setDrawBuffer(buffer);  
  45.         camera->setReadBuffer(buffer);  
  46.   
  47.         viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::scale(aspectRatioScale,1.0,1.0));  
  48.     }  
  49. }  
在OSG中场景要想被渲染出来,它必须位于一个Camera节点之下,当我们使用osgViewer::Viewer调用
[cpp] view plain copy
print?
  1. viewer->setSceneData(node);  
实际上我们是将node节点添加到了viewer中的主相机之中。osgViewer::Viewer继承自osg::View,osgView的成员如下:

可以知道osgViewer::Viewer中除了包含主相机_camera之外,还包含了一个从相机的数组_slaves。事实上我们可以设置将场景添加到主相机或者从相机之中,它们都可以被渲染出来,这两者其实并没有什么本质上的不同,上文中的代码实际上并没有设置主相机,也就是说两个视口里面渲染的场景都来自于从相机。
在添加从相机时使用了下面的函数:
[cpp] view plain copy
print?
  1. bool  addSlave (osg::Camera *camera, const osg::Matrix &projectionOffset, const osg::Matrix &viewOffset, bool useMastersSceneData=true)   
这里面的projectionOffset是传入一个矩阵,让主相机中和从相机中的视角出现一些偏差,以此来达到从不同角度观看场景的效果。userMasterSceneData的意思是从相机中渲染的场景是否来自于主相机,如果设置为true,那么从相机就和主相机渲染相同的场景,否则它自己渲染自己的场景,和主相机中的场景完全无关。

下面我们接着看multipleWindowMultipleCameras的实现,这个函数包含两个参数:
[cpp] view plain copy
print?
  1. void multipleWindowMultipleCameras(osgViewer::Viewer& viewer, bool multipleScreens)  
第一个参数很好理解,就是管理场景的视景器,第二个参数的含义是 是否启用多屏模式,也就是说当我们的电脑外接了多块显示器的时候可以开启这个选项。

我自己的电脑只有一块显示屏,当我运行osgcamera的例子并使用命令行参数 -3时(也就是multipleScreens=true),得到的效果如下图所示,只有3个窗口,并且给出如下的输出结果:


可以知道另外3个窗口理应出现在第二块显示屏上,但是由于没有第二块显示屏幕,给出了出错的信息。
原创粉丝点击