1、效果
在osg的实际应用场景中,有时候需要同时创建多个场景视图,并保证各个场景视图中有不一样的显示和操作:例如:漫游器、照相机、粒子效果、多个模型组合等。此时就要用到OSG提供的osgViewer::CompositeViewer类来实现这个需求了。
2、源码
#include <iostream>
#include <osgUtil/Optimizer>
#include <osgDB/ReadFile>
#include <osg/Material>
#include <osg/Geode>
#include <osg/BlendFunc>
#include <osg/Depth>
#include <osg/Projection>
#include <osg/PolygonOffset>
#include <osg/MatrixTransform>
#include <osg/Camera>
#include <osg/FrontFace>
#include <osgText/Text>
#include <osgGA/TerrainManipulator>
#include <osgGA/FlightManipulator>
#include <osgGA/StateSetManipulator>
#include <osgViewer/ViewerEventHandlers>
#include <osgViewer/CompositeViewer>
#include <osg/io_utils>
#pragma comment(lib, "OpenThreadsd.lib")
#pragma comment(lib, "osgd.lib")
#pragma comment(lib, "osgDBd.lib")
#pragma comment(lib, "osgUtild.lib")
#pragma comment(lib, "osgGAd.lib")
#pragma comment(lib, "osgViewerd.lib")
#pragma comment(lib, "osgTextd.lib")
#pragma comment(lib, "osgSimd.lib")
int main(int argc, char** argv)
{
//创建CompositeViewer
osgViewer::CompositeViewer viewer;
if (viewer.getNumViews() == 0)
{
osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
if (!wsi)
{
osg::notify(osg::NOTICE) << "Error, no WindowSystemInterface available, cannot create windows." << std::endl;
return 1;
}
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 = 600;
traits->y = 400;
traits->width = 1000;
traits->height = 800;
traits->windowDecoration = true;
traits->doubleBuffer = true;
traits->sharedContext = 0;
traits->readDISPLAY();
traits->setUndefinedScreenDetailsToDefaultScreen();
osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
// 第一个视图
{
osgViewer::View* view = new osgViewer::View;
osg::ref_ptr<osg::Node> scene = osgDB::readNodeFile("D:\\osg3.6.5_oe3.1\\OpenSceneGraph_Data\\dumptruck.osg");
view->setName("View one");
viewer.addView(view);
view->setSceneData(scene);
view->getCamera()->setName("Cam three");
view->getCamera()->setProjectionMatrixAsPerspective(30.0, double(traits->width) / double(traits->height / 2), 1.0, 1000.0);
view->getCamera()->setViewport(new osg::Viewport(0, traits->height / 2, traits->width / 2, traits->height / 2));
view->getCamera()->setGraphicsContext(gc.get());
view->setCameraManipulator(new osgGA::TerrainManipulator);
}
// 第二个视图
{
osgViewer::View* view = new osgViewer::View;
view->setName("View two");
osg::ref_ptr<osg::Node> scene = osgDB::readNodeFile("D:\\osg3.6.5_oe3.1\\OpenSceneGraph_Data\\cow.osgt");
viewer.addView(view);
view->setSceneData(scene);
view->getCamera()->setName("Cam two");
view->getCamera()->setViewport(new osg::Viewport(traits->width / 2, traits->height / 2, traits->width / 2, traits->height / 2));
view->getCamera()->setGraphicsContext(gc.get());
view->setCameraManipulator(new osgGA::TerrainManipulator);
}
// 第三个视图
{
osgViewer::View* view = new osgViewer::View;
view->setName("View three");
osg::ref_ptr<osg::Node> scene = osgDB::readNodeFile("D:\\osg3.6.5_oe3.1\\OpenSceneGraph_Data\\cessnafire.osgt");
viewer.addView(view);
view->setSceneData(scene);
view->getCamera()->setName("Cam one");
view->getCamera()->setViewport(new osg::Viewport(0, 0, traits->width / 2, traits->height / 2));
view->getCamera()->setGraphicsContext(gc.get());
osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator;
statesetManipulator->setStateSet(view->getCamera()->getOrCreateStateSet());
view->setCameraManipulator(new osgGA::TerrainManipulator);
view->addEventHandler(statesetManipulator.get());
view->addEventHandler(new osgViewer::StatsHandler);
view->addEventHandler(new osgViewer::HelpHandler);
view->addEventHandler(new osgViewer::WindowSizeHandler);
view->addEventHandler(new osgViewer::ThreadingHandler);
view->addEventHandler(new osgViewer::RecordCameraPathHandler);
}
// 第四个视图
{
osgViewer::View* view = new osgViewer::View;
view->setName("View four");
osg::ref_ptr<osg::Node> scene = osgDB::readNodeFile("D:\\osg3.6.5_oe3.1\\OpenSceneGraph_Data\\robot.osg");
viewer.addView(view);
view->setSceneData(scene);
view->getCamera()->setName("Cam one");
view->getCamera()->setViewport(new osg::Viewport(traits->width / 2, 0, traits->width / 2, traits->height / 2));
view->getCamera()->setGraphicsContext(gc.get());
// add the state manipulator
osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator;
statesetManipulator->setStateSet(view->getCamera()->getOrCreateStateSet());
view->setCameraManipulator(new osgGA::TerrainManipulator);
view->addEventHandler(statesetManipulator.get());
view->addEventHandler(new osgViewer::StatsHandler);
view->addEventHandler(new osgViewer::HelpHandler);
view->addEventHandler(new osgViewer::WindowSizeHandler);
view->addEventHandler(new osgViewer::ThreadingHandler);
view->addEventHandler(new osgViewer::RecordCameraPathHandler);
}
}
return viewer.run();
}