|
发表于 2010-5-16 19:30:56
|
显示全部楼层
class EventOP:public osgGA::GUIEventHandler
{
public:
EventOP(osg::MatrixTransform* node);//构造函数
virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa);
private:
osg::MatrixTransform * OPNode;
};
EventOP::EventOP(osg::MatrixTransform* node)
{
OPNode=node;
}
bool EventOP::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
{
osg::Matrix M0,M1;
if (ea.getEventType()==osgGA::GUIEventAdapter::KEYDOWN)
{
if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Up)
M1.makeTranslate(osg::Vec3(0,0.3,0));//前
if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Down)
M1.makeTranslate(osg::Vec3(0,-0.3,0));//后
if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Left)
M1.makeRotate(0.03,osg::Vec3(0,0,1));//左
if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Right)
M1.makeRotate(-0.03,osg::Vec3(0,0,1));//右
if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Shift_L)
M1.makeRotate(0.03,osg::Vec3(1,0,0));//上
if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Control_L)
M1.makeRotate(-0.03,osg::Vec3(1,0,0));//下
}
else return false;
M0=OPNode->getMatrix();
OPNode->setMatrix(M1*M0);
return true;
}
//////////////////////////////
void main()
{
osg::ref_ptr<osgViewer::Viewer> viewer=new osgViewer::Viewer();
osg::ref_ptr<osg::Group> root=new osg::Group();
osg::ref_ptr<osg::Node> lz=osgDB::readNodeFile("lz.osg");
osg::ref_ptr<osg::Node> cessna=osgDB::readNodeFile("cessna.osg");
osg::ref_ptr<osg::MatrixTransform> maintran=new osg::MatrixTransform();
osg::ref_ptr<osg::MatrixTransform> justtran=new osg::MatrixTransform();
osg::Matrix mt;
mt.makeRotate(3.14159,osg::Vec3(0,0,1));
justtran->setMatrix(mt);
mt.makeTranslate(osg::Vec3(0,0,150));
maintran->setMatrix(mt);
justtran->addChild(cessna.get());
maintran->addChild(justtran.get());
root->addChild(lz.get());
root->addChild(maintran.get());
viewer->setSceneData(root.get());
viewer->addEventHandler(new EventOP(maintran.get()));
viewer->run();
}
碰巧上几天刚做的 |
|