#include <osgViewer/Viewer> #include <osgViewer/ViewerEventHandlers> #include <osgViewer/CompositeViewer> #include <osg/Node> #include <osg/Geode> #include <osg/Group> #include <osg/Geometry> #include <osg/Camera> #include <osg/MatrixTransform> #include <osg/PositionAttitudeTransform> #include <osgDB/ReadFile> #include <osgDB/WriteFile> #include <osgGA/TrackballManipulator> #include <osgUtil/Optimizer> #include <iostream> //动态删除视图窗口事件 class DeletSlaveView:public osgGA::GUIEventHandler { public: DeletSlaveView(osg::ref_ptr<osgViewer::CompositeViewer>compositeViewer,osg::ref_ptr<osgViewer::View>view):m_compositeviewer(compositeViewer),m_view(view){} bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&aa ) { switch(ea.getEventType()) { case osgGA::GUIEventAdapter::KEYDOWN: { if (ea.getKey()=='q') { m_compositeviewer->stopThreading();//使用是否正确? m_compositeviewer->removeView(m_view); m_compositeviewer->startThreading();//使用是否正确? } } break; default: break; } return false; } public: osg::ref_ptr<osgViewer::CompositeViewer>m_compositeviewer; osg::ref_ptr<osgViewer::View>m_view; }; int main() { //创建CompositeViewer对象 osg::ref_ptr<osgViewer::CompositeViewer> viewer = new osgViewer::CompositeViewer() ; //读取牛的模型 osg::ref_ptr<osg::Node> cow = osgDB::readNodeFile("cow.osg") ; //读取飞机模型 osg::ref_ptr<osg::Node> cessna = osgDB::readNodeFile("cessna.osg"); //优化场景数据 osgUtil::Optimizer optimizer ; optimizer.optimize(cow.get()); optimizer.optimize(cessna.get()); //设置图形环境特性 osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits(); traits->x = 100; traits->y = 100; traits->width = 900; traits->height = 700; 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; } //视图一 { //创建视图一 osg::ref_ptr<osgViewer::View> view = new osgViewer::View; viewer->addView(view.get()); //设置视图场景数据 view->setSceneData(cow); //设置相机视口及图形环境 view->getCamera()->setViewport(new osg::Viewport(0,0, traits->width/2, traits->width/2)); view->getCamera()->setGraphicsContext(gc.get()); //设置操作器 view->setCameraManipulator(new osgGA::TrackballManipulator); //添加事件处理 view->addEventHandler( new osgViewer::StatsHandler ); view->addEventHandler( new osgViewer::WindowSizeHandler ); view->addEventHandler( new osgViewer::ThreadingHandler ); view->addEventHandler( new osgViewer::RecordCameraPathHandler ); view->addEventHandler(new ModelMoveHandler); } //视图二 osg::ref_ptr<osgViewer::View> view2 = new osgViewer::View; viewer->addView(view2.get()); view2->setSceneData(cessna.get()); view2->getCamera()->setViewport(new osg::Viewport(traits->width/2,0, traits->width/2, traits->height/2)); view2->getCamera()->setGraphicsContext(gc.get()); view2->setCameraManipulator(new osgGA::TrackballManipulator); //视图三 osg::ref_ptr<osgViewer::View> view3 = new osgViewer::View; viewer->addView(view3.get()); view3->setSceneData(cessna.get()); //根据分辨率来确定合适的投影来保证显示的图形不变形 double fovy, aspectRatio, zNear, zFar; view3->getCamera()->getProjectionMatrixAsPerspective(fovy, aspectRatio, zNear, zFar); double newAspectRatio = double(traits->width) / double(traits->height/2); double aspectRatioChange = newAspectRatio / aspectRatio; if (aspectRatioChange != 1.0) { view3->getCamera()->getProjectionMatrix() *= osg::Matrix::scale(1.0/aspectRatioChange,1.0,1.0); } view3->getCamera()->setViewport(new osg::Viewport(0,traits->height/2, traits->width, traits->height/2)); view3->getCamera()->setGraphicsContext(gc.get()); view3->setCameraManipulator(new osgGA::TrackballManipulator); //视图二调用动态删除事件,删除视图三 view2->addEventHandler(new DeletSlaveView(viewer,view3)); viewer->realize(); //viewer->setThreadingModel(osgViewer::CompositeViewer:rawThreadPerContext); viewer->setThreadingModel(osgViewer::CompositeViewer::SingleThreaded); viewer->run() ; return 0 ; }
以上程序添加了三个视图,之后程序运行后添加事件处理,在视图二上按键删除视图三,但是程序失败是怎么回事?
|