|
发表于 2012-4-16 21:04:49
|
显示全部楼层
class CPlayHandler : public osgGA::GUIEventHandler
{
//构造和析构自己写
public:
virtual bool handle (const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa, \
osg::Object *object, osg::NodeVisitor *nv)
{
osg::ref_ptr<osg:ositionAttitudeTransform> pat = dynamic_cast<osg::PositionAttitudeTransform*>(object); //假设您的模型是加载到PositionAttitudeTransform节点下面的
switch (ea.getEventType())
{
case osgGA::GUIEventAdapter::KEYDOWN:
{
switch (ea.getKey())
{
case 'w':
pat->setPosition(pat->getPosition() + osg::Vec3(0, 2, 0)); //按W键向Y轴正方向移动
break;
}
}
}
return true;
}
}
void main()
{
osg::ref_ptr<osg::PositionAttitudeTransform> pat = new osg::PositionAttitudeTransform;
pat->addChild(osgDB::readNodeFile("XXXXX")).get()l); //加载模型
pat->setEventCallback(new CPlayHandler); //事件回调函数
//在把Pat节点加入到您的屏幕中去
//一般是这样:
osg::Group *root = new osg::Group();
root->addChild(pat.get());
osgViewer::Viewer viewer;
viewer.setSceneData(root);
viewer.realize();
viewer.run();
}
上面是随手写的一段代码,具体的话,您看着修改嘛,反正这个思路是可以达到您的目的的~~~ |
|