// OSG_5_6.cpp : 定义控制台应用程序的入口点。 //投影墙显示:4个视景器辅助相机拼接在一起,同时显示一个场景的效果。 #include "stdafx.h" #include<osg/Group> #include<osgDB/ReadFile> #include<osgViewer/Viewer> /*用于创建一个与相机对应的图形设备,也是窗口设备,其属性参数使用特性类Traits定义,并返回这个相机对象。*/ osg::Camera* creatCamera(int x, int y, int w, int h) { //在windows系统中,这将构建一个w*y的不带标题栏的普通窗口,使用双缓存来实现渲染内容的显示与交换。 osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; traits->windowDecoration = false; traits->x = x; traits->y = y; traits->width = w; traits->height = h; traits->doubleBuffer = true; osg::DisplaySettings* ds = osg::DisplaySettings::instance(); traits->alpha = ds->getMinimumNumAccumAlphaBits();//图形设备的alpha缓存 traits->stencil = ds->getMinimumNumStencilBits();//图形设备的模板缓存 traits->sampleBuffers = ds->getMultiSamples();//图形设备的重采样缓存 traits->samples = ds->getNumMultiSamples();//图形设备的重采样数设置 osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); osg::ref_ptr<osg::Camera> camera = new osg::Camera; camera->setGraphicsContext(gc.get()); camera->setViewport(new osg::Viewport(0, 0, w, h)); return camera.release(); } int _tmain(int argc, char ** argv) { osg::ArgumentParser arguments(&argc,argv); osg::Node* model = osgDB::readNodeFiles(arguments); if (!model) model = osgDB::readNodeFile("cow.osg"); osgViewer::Viewer viewer; //向视景器重添加多个从属相机,它们共享主相机的观察矩阵和投影矩阵,以及同一个场景树结构,并有适当的偏移设置。 viewer.addSlave(creatCamera(100, 100, 400, 300), osg::Matrixd::translate(1.0, -1.0, 0.0), osg::Matrixd()); viewer.addSlave(creatCamera(505, 100, 400, 300), osg::Matrixd::translate(-1.0, -1.0, 0.0), osg::Matrixd()); viewer.addSlave(creatCamera(100, 405, 400, 300), osg::Matrixd::translate(1.0, 1.0, 0.0), osg::Matrixd()); viewer.addSlave(creatCamera(505, 405, 400, 300), osg::Matrixd::translate(-1.0, 1.0, 0.0), osg::Matrixd()); viewer.setSceneData(model); return viewer.run(); }
当设置一个视景器的视图是,需要定义以下几个部分: