osg示例解析之osgcamera

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

简介

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

  • 具体解析

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

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

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

在singleWindowMultipleCameras函数中,首先获取了当前使用的显示器的分辨率,据此创建了一个用来渲染场景的GraphicContext(其实就是渲染的窗口),之后创建了2个相机,并设置它们的视口大小,将相机设置为Slave相机。代码并不复杂,但是其中设置的内容可以细说一下。
  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. }  
void singleWindowMultipleCameras(osgViewer::Viewer& viewer)
{
	//获取屏幕分辨率
    osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
    if (!wsi) 
    {
        osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
        return;
    }
    
    unsigned int width, height;
    wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
	
	//创建渲染窗口
    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
    traits->x = 0;
    traits->y = 0;
    traits->width = width;
    traits->height = height;
    traits->windowDecoration = true;
    traits->doubleBuffer = true;
    traits->sharedContext = 0;

    osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
    if (gc.valid())
    {
        gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.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;
    }

    unsigned int numCameras = 2;
    double aspectRatioScale = 1.0;
	//创建两个相机并添加到Viewer的Slave中
    for(unsigned int i=0; i<numCameras;++i)
    {
        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
        camera->setGraphicsContext(gc.get());
        camera->setViewport(new osg::Viewport((i*width)/numCameras,(i*height)/numCameras, width/numCameras, height/numCameras));
		GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
		camera->setDrawBuffer(buffer);
		camera->setReadBuffer(buffer);

        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::scale(aspectRatioScale,1.0,1.0));
    }
}
在OSG中场景要想被渲染出来,它必须位于一个Camera节点之下,当我们使用osgViewer::Viewer调用
  1. viewer->setSceneData(node);  
viewer->setSceneData(node);
实际上我们是将node节点添加到了viewer中的主相机之中。osgViewer::Viewer继承自osg::View,osgView的成员如下:

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

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

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


可以知道另外3个窗口理应出现在第二块显示屏上,但是由于没有第二块显示屏幕,给出了出错的信息。
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是在 Visual Studio 2022 中使用 osgEarth 的示例: 1. 安装 osgEarth 首先需要在你的系统中安装 osgEarth。可以从官网下载安装包进行安装,也可以使用 CMake 进行源码编译安装。 2. 创建一个 osgEarth 应用程序 在 Visual Studio 2022 中创建一个新的 C++ 控制台应用程序项目。然后,将 osgEarth 的 include 和 library 路径添加到项目中: - 右键单击项目,选择“属性”。 - 在“VC++ 目录”中添加包含路径和库路径。 - 在“链接器 -> 输入”中添加 osgEarth 的库文件。 3. 创建一个 osgEarth 地图节点 在应用程序中创建一个 osgEarth 地图节点,并将其添加到场景图中: ```cpp #include <osgEarth/MapNode> #include <osgEarthUtil/EarthManipulator> int main(int argc, char** argv) { // 初始化 osgEarth 库 osgEarth::initialize(); // 创建一个 osgEarth 地图节点 osg::ref_ptr<osgEarth::MapNode> mapNode = osgEarth::MapNode::create(osgEarth::MapNodeOptions()); // 创建一个 osgEarth 视图并添加地图节点 osg::ref_ptr<osgViewer::View> view = new osgViewer::View; view->setSceneData(mapNode); // 设置地球操纵器 osg::ref_ptr<osgEarth::Util::EarthManipulator> manipulator = new osgEarth::Util::EarthManipulator; view->setCameraManipulator(manipulator); // 显示视图 osgViewer::Viewer viewer; viewer.addView(view); return viewer.run(); } ``` 以上代码创建了一个 osgEarth 地图节点,并使用 EarthManipulator 对其进行控制。注意要在程序结束前调用 osgEarth::shutdown(),以释放资源。 4. 运行应用程序 点击“生成”按钮编译应用程序,并运行应用程序。如果一切正常,应该可以看到一个 osgEarth 地球视图。 以上是在 Visual Studio 2022 中使用 osgEarth 的简单示例。根据需要,还可以使用 osgEarth 的其他功能来创建更复杂的地图应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值