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变量,创建更多的从相机。

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

 

posted @ 2019-04-10 18:34  技术狂人djc  阅读(495)  评论(0编辑  收藏  举报