class dynamicAdd:public osgGA::GUIEventHandler { public: dynamicAdd(){} ~dynamicAdd(){} bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa) { osg::ref_ptr<osgViewer::Viewer>viewer=dynamic_cast<osgViewer::Viewer*>(&aa); if (viewer) { osg::ref_ptr<osg::Group> root=viewer->getSceneData()->asGroup(); osg::ref_ptr<osgGA::TrackballManipulator>trackball=dynamic_cast<osgGA::TrackballManipulator*>(viewer->getCameraManipulator()); switch(ea.getEventType()) { case osgGA::GUIEventAdapter::KEYDOWN: if (ea.getKey()=='a') { osg::ref_ptr<osg::Node>child=osgDB::readNodeFile("lz.osg"); root->addChild(child); trackball->home(ea,aa); } break; default: break; } } return false; } };
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()); viewer->addEventHandler(new dynamicAdd); //优化场景数据 osgUtil::Optimizer optimizer ; optimizer.optimize(root.get()) ; viewer->setSceneData(root.get()); viewer->realize(); viewer->run(); return 0 ; }
osg场景中一开始添加某个对象,然后动态添加一个比较大的模型后,操作鼠标追踪球操作器trackball,发现当放到到一定程度后就不能放大了,不知道是怎么回事: |