|
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osg/Node>
class UseEventHandler : public osgGA::GUIEventHandler
{
public:
virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
if (!viewer) return false;
switch(ea.getEventType())
{
case osgGA::GUIEventAdapter::KEYDOWN:
{
if (ea.getKey()== 0xFF51)
{
viewer ->getSceneData() ->asGroup() ->getChild(1) ->setNodeMask(0) ;
viewer ->getSceneData() ->asGroup() ->getChild(0) ->setNodeMask(1) ;
}
if (ea.getKey()== 0xFF53)
{
viewer ->getSceneData() ->asGroup() ->getChild(0) ->setNodeMask(0) ;
viewer ->getSceneData() ->asGroup() ->getChild(1) ->setNodeMask(1) ;
}
}
break;
case osgGA::GUIEventAdapter :USH:
if(ea.getButton () == 4)
{
viewer ->getSceneData() ->asGroup() ->getChild(0) ->setNodeMask(0) ;
viewer ->getSceneData() ->asGroup() ->getChild(1) ->setNodeMask(0) ;
}
break;
case osgGA::GUIEventAdapter:OUBLECLICK:
if(ea.getButton() == 1)
{
viewer ->getSceneData() ->asGroup() ->getChild(0) ->setNodeMask(1) ;
viewer ->getSceneData() ->asGroup() ->getChild(1) ->setNodeMask(1) ;
}
break;
default:
break;
}
return false;
}
};
void main()
{
osgViewer::Viewer viewer;
osg::ref_ptr<osg::Group> root = new osg::Group() ;
root ->addChild(osgDB::readNodeFile("cow.osg")) ;
root ->addChild(osgDB::readNodeFile("glider.osg"));
viewer.setSceneData(root.get ());
viewer.addEventHandler(new UseEventHandler) ;
viewer.realize();
viewer.run();
}
看不明白为什么结点1就代表牛(cow),而结点0就代表飞机(glider)呢?
|
|