• Learning OSG programing---Multi Camera in one window 在单窗口中创建多相机


      在学习OSG提供的例子osgCamera中,由于例子很长,涉及很多细节,考虑将其分解为几个小例子。本文介绍实现在一个窗口中添加多个相机的功能。

      此函数接受一个Viewer引用类型参数,设置图形上下文的特征变量traits,并由它建立图形上下文。根据需要差创建的相机数量,循环创建相机变量,并将其添加到观察器变量中,作为从相机。每次创建相机,都需要获取当前图形上下文,并设置相机的起点(左下角)坐标和相机的宽度与高度。相机间的区别仅在于相机起点的不同。完整的程序代码如下。

    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;
        osg::GraphicsContext::ScreenIdentifier main_screen_id;
    
        main_screen_id.readDISPLAY();
        main_screen_id.setUndefinedScreenDetailsToDefaultScreen();
        wsi->getScreenResolution(main_screen_id, 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;
        traits->readDISPLAY();
        traits->setUndefinedScreenDetailsToDefaultScreen();
    
        osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
        if (gc.valid())
        {
            osg::notify(osg::INFO)<<"  GraphicsWindow has been created successfully."<<std::endl;
    
            // need to ensure that the window is cleared make sure that the complete window is set the correct colour
            // rather than just the parts of the window that are under the camera's viewports
            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 = 3;
        double aspectRatioScale = 1.0;///(double)numCameras;
        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));
        }
    }

      之后在主函数中调用创建多相机函数,为观察器添加从相机,并向观察器中加入模型,进行显示。主函数代码如下:

    int main(){
        osgViewer::Viewer viewer;
        singleWindowMultipleCameras(viewer);
        viewer.setSceneData(osgDB::readRefNodeFile("cow.osg"));
        return viewer.run();
    }

      运行程序,可显示多相机效果,如下所示:

      可以更改singleWindowMultipleCameras函数中的numCameras变量,创建更多的从相机。

      创建多相机的关键是相机视点的设置和向观察器中添加从相机。而程序最开始的创建窗口系统接口,图形上下文,图形上下文特征以及相机的缓冲设置,则是一些辅助性工作。

  • 相关阅读:
    Root Android and Install Recovery linux shell script & Android root原理
    MPEG4 144962和H.264什么区别?
    Android root的两种方法 udev漏洞和setuid漏洞
    [转载]android三个特殊的资源目录 /res/xml /res/raw 和 /assets
    avast注册号|好用的avast注册号|没过期avast注册号
    忘记烦恼|如何忘记烦恼|忘记烦恼的方法
    第六感|最近又出现第六感了这个是什么问题呢
    移动手机为什么还有话费,就停机了,和客服的谈话
    .net保存中文到cookie时是乱码取出来的时候
    skype账号|超值skype账号|14分钟skype账号|1元40个|15天有效期
  • 原文地址:https://www.cnblogs.com/SupremeGIS-Developer/p/10685269.html
Copyright © 2020-2023  润新知