|
我写了一个小程序,源代码如下文所示,根节点helpNode下有"cessna.osg"和"cessnafire.osg"两个模型,使用switch进行切换,当按下F1时切换到"cessnafire.osg",再次按下F1切换到"cessna.osg",如此往复……
现在问题是,如果"cessna.osg"和"cessnafire.osg"不是通过
helpNode->addChild(osgDB::readNodeFile("cessna.osg"),true);
helpNode->addChild(osgDB::readNodeFile("cessnafire.osg"),false);
直接添加到根节点helpNode下,而是helpNode只是场景树根节点root下的一个子节点(root->addChild(helpNode)),那么该如何控制这个子节点helpNode的切换实现上述程序相同的功能呢?- #include <osg/Group>
- #include <osg/Geode>
- #include <osg/PositionAttitudeTransform>
- #include <osgViewer/Viewer>
- #include <osgDB/readFile>
- #include <osg/ShapeDrawable>
- #include <osg/TexEnv>
- #include <osgDB/Registry>
- #include <iostream>
- #include <osgSim/MultiSwitch>
- #include <osg/DrawPixels>
- #include <osgUtil/Optimizer>
- //helpBoard的值在每次按下F1时改变一次
- bool helpBoard = true;
- //定义一个人机交互事件处理器
- class helpNodeCallback : public osgGA :: GUIEventHandler
- {
- public:
- virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object*, osg::NodeVisitor*)
- {
- //动作适配器的对象为当前的场景视图
- osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
- switch(ea.getEventType())
- {
- case osgGA::GUIEventAdapter::KEYDOWN:
- {
- osg::Switch* helpNode = dynamic_cast<osg::Switch*>(viewer->getSceneData());
- if(ea.getKey() == osgGA::GUIEventAdapter::KEY_F1)
- {
- //当捕获到键盘F1按下时改变bool型变量helpBoard(上文keyState类中)的值
- helpBoard = !helpBoard;
- //根据keyState->helpBoard的值来设定switch节点值
- if(helpBoard == true)
- {
- helpNode->setValue(0, true);
- helpNode->setValue(1, false);
- }
- else if(helpBoard == false)
- {
- helpNode->setValue(0, false);
- helpNode->setValue(1, true);
- }
- }
- }
- default:
- break;
- }
- return false;
- }
- };
- int main(int argc, char** argv)
- {
- osg::ref_ptr<osg::Switch> helpNode = new osg::Switch;
- helpNode->addChild(osgDB::readNodeFile("cessna.osg"),true);
- helpNode->addChild(osgDB::readNodeFile("cessnafire.osg"),false);
- osgViewer::Viewer viewer;
- viewer.setSceneData(helpNode.get());
- viewer.addEventHandler(new helpNodeCallback);
- return viewer.run();
- }
复制代码 |
|