OSG 单视图与相机:宽屏变形示例

osgViewer::Viewer的继承图:

  • osg::View : 主要用来管理所有相机视图。它包含一个主相机和N个从属相机,如果View仅有一个主相机,则该主相机用来负责控制在和渲染视图场景。如果包含从属相机,则主相机用来负责控制管理视图,从属相机用于渲染场景。
  • osgViewer::View : 可以挂节事件,处理事件,并负责创建相机和创建图形环境窗口。
  • osgViewer::ViewerBase : 具有管理渲染的线程,负责设置线程模式,启动相关线程等功能。
  • osgGA::GUIActionAdapter : GUI动作适配器,用来向系统发送一些请求,以实现一些特定的操作。

osgViewer::Viewer中, 只允许单视图,单视图可以同时包含多个相机渲染,也可以在多窗口中渲染。为了能够进行正常的渲染,还需要创建一个图形环境。


创建图形环境的主要步骤如下:

1、通过WindowingSystemInterface类得到系统窗口接口,该系统接口主要是为了关联窗口系统与图形环境。

2、下面是OSG中图形环境的主要特性:


3、通过图形环境特性创建图形环境。通过调用一个静态成员函数创建图形环境:
osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext( traits.get()); 


4、通过图形环境创建窗口(句柄hwnd)

  
#include <osgViewer/Viewer>  
  
#include <osg/Node>  
#include <osg/Geode>  
#include <osg/Group>  
#include <osg/Camera>  
  
#include <osgDB/ReadFile>  
#include <osgDB/WriteFile>  
  
#include <osgUtil/Optimizer>  
  
#include <iostream>  
  
int main()  
{  
    //创建Viewer对象, 场景浏览器  
    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();  
  
    //创建场景节点  
    osg::ref_ptr<osg::Group> root = new osg::Group();  
  
    //读取模型  
    osg::ref_ptr<osg::Node> node = osgDB::readNodeFile("cow.osg");  
  
    root->addChild(node.get());  
  
  
    //设置图形环境特性  
    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits();  
    traits->x = 0;  
    traits->y = 0;  
    traits->width = 1000;  
    traits->height = 800;  
    traits->windowDecoration = true;  
    traits->doubleBuffer = true;  
    traits->sharedContext = 0;  
  
    //场景图形环境特性  
    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;  
  
        //清楚窗口颜色及清楚颜色和深度缓存  
        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;  
    }  
  
    //根据分辨率确定合适的投影来保证显示的图形不变形  
    double fovy, aspectRatio, zNear, zFar;  
    viewer->getCamera()->getProjectionMatrixAsPerspective(fovy, aspectRatio, zNear, zFar);  
    double newAspectRatio = double(traits->width) / double(traits->height);  
    double aspectRatioChange = newAspectRatio / aspectRatio;  
    if (aspectRatioChange != 1.0)  
    {  
        //设置投影矩阵  
        viewer->getCamera()->getProjectionMatrix() *= osg::Matrix::scale(1.0 / aspectRatioChange, 1.0, 1.0);  
    }  
  
    //设置视口  
    viewer->getCamera()->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));  
    //设置图形环境  
    viewer->getCamera()->setGraphicsContext(gc.get());  
  
    //优化场景  
    osgUtil::Optimizer optimizer;  
    optimizer.optimize(root.get());  
  
    viewer->setSceneData(root.get());  
    viewer->realize();  
    viewer->run();  
    return 0;  
}  


猜你喜欢

转载自blog.csdn.net/HelloEarth_/article/details/80015937