|
- #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时改变一次
- class EventStateType
- {
- public:
- EventStateType(void)
- {
- helpBoard = true;
- }
- ~EventStateType(void)
- {
- }
- bool helpBoard;
- };
- //定义一个人机交互事件处理器
- 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类中)的值
- keyState->helpBoard = !keyState->helpBoard;
- //根据keyState->helpBoard的值来设定switch节点值
- if(keyState->helpBoard == true)
- {
- helpNode->setValue(0, true);
- helpNode->setValue(1, false);
- }
- else if(keyState->helpBoard == false)
- {
- helpNode->setValue(0, false);
- helpNode->setValue(1, true);
- }
- }
- }
- default:
- break;
- }
- return false;
- }
- protected:
- EventStateType* keyState;
- };
- 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();
- }
复制代码 参考《OSG三维渲染引擎设计与实践》中处理键盘事件的范例设计一个键盘响应程序,打算实现按下F1画面由正常飞机切换为着火飞机,再次按F1画面由着火飞机切换回正常飞机,就是用F1单键实现切换如此往复的一个过程。可是总是报错,不知道什么原因,请大家帮帮给我指点指点啊,谢谢~~
|
|