|
楼主 |
发表于 2011-7-27 08:49:28
|
显示全部楼层
回复 2# array
这是海军教程上的例子,我做了修改!新增加了3个参数,来动态控制相机的位置!我就是用这个类 class orbit : public osg::NodeCallback 来做的。
这个程序和我上贴图的程序,唯一区别就是给物体(tank或者plane)增加了个animationpathcallback,相机的控制都是一样的用这个方法的!
- #include <osg/NodeCallback>
- #include <osg/PositionAttitudeTransform>
- #include <osgViewer/Viewer>
- #include <osg/MatrixTransform>
- #include <osgDB/ReadFile>
- #include <osgGA/TrackballManipulator>
- #include "KeyboardHandler.h"
- float LeftRigtMove =3.1415926/1800.0;//新增参数
- float UpDownMove = -20.0;//新增参数
- float movestep = -40;//新增参数
- class PickHandle: public osgGA::GUIEventHandler
- {
- public:
- PickHandle(osgViewer::Viewer *vt)
- {
- viewer = vt;
- controls = new osg::Vec3dArray;
- }
- bool handle (const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
- {
- switch (ea.getEventType())
- {
- case osgGA::GUIEventAdapter::KEYDOWN:
- {
- if (ea.getKey() == 'm' || ea.getKey() == 'M')
- {
- std::cout<<"oasis"<<std::endl;
- }
- if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Left)
- {
- LeftRigtMove += 3.1415926/180.0;
- std::cout<<"Press Key_Left!"<<std::endl;
- }
- if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Right)
- {
- LeftRigtMove -= 3.1415926/180.0;
- std::cout<<"Press KEY_Right!"<<std::endl;
- }
- if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Down)
- {
- UpDownMove -= 3.1415926/180.0;
- std::cout<<"Press KEY_Down!"<<std::endl;
- }
- if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Up)
- {
- UpDownMove += 3.1415926/180.0;
- std::cout<<"Press KEY_Up!"<<std::endl;
- }
- if (ea.getKey() == '1')
- {
- movestep += 10;
- std::cout<<"Press 1 key!"<<std::endl;
- }
- if (ea.getKey() == '2')
- {
- movestep -= 10;
- std::cout<<"Press 2 key!"<<std::endl;
- }
- }
- break;
- default:
- break;
- }
- return false;
- }
- private:
- osgViewer::Viewer *viewer;
- osg::ref_ptr<osg::Vec3dArray> controls;
- };
- class orbit : public osg::NodeCallback
- {
- public:
- orbit(): heading(3.1415926/2.0) {}
- osg::Matrix getWCMatrix(){return worldCoordMatrix;}
- virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
- {
- osg::MatrixTransform *tx = dynamic_cast<osg::MatrixTransform *>(node);
- if( tx != NULL )
- {
- heading += 3.1415926/1800.0;
- osg::Matrixd orbitRotation;
- orbitRotation.makeRotate(
- osg::DegreesToRadians(0.0), osg::Vec3(0,1,0), // roll
- UpDownMove, osg::Vec3(1,0,0) , // pitch
- LeftRigtMove, osg::Vec3(0, 0, 1) ); // heading
- osg::Matrixd orbitTranslation;
- orbitTranslation.makeTranslate( 0,movestep, 0 );
- tx->setMatrix ( orbitTranslation * orbitRotation);
- worldCoordMatrix = osg::computeWorldToLocal( nv->getNodePath() );
- }
- traverse(node, nv);
- }
- private:
- osg::Matrix worldCoordMatrix;
- float heading;
- };
- bool useTankOrbiterView = false;
- void toggleTankOrbiterView()
- {
- if (! useTankOrbiterView)
- useTankOrbiterView = true;
- else
- useTankOrbiterView = false;
- }
- int main()
- {
- osg::Node* groundNode = NULL;
- osg::Node* tankNode = NULL;
- osg::Group* root = NULL;
- osgViewer::Viewer viewer;
- osg::PositionAttitudeTransform* tankXform = NULL;
- groundNode = osgDB::readNodeFile("JoeDirt/JoeDirt/JoeDirt.flt");
- tankNode = osgDB::readNodeFile("t72-tank/T72-tank/t72-tank_des.flt");
- root = new osg::Group();
- // Create green Irish sky
- osg::ClearNode* backdrop = new osg::ClearNode;
- backdrop->setClearColor(osg::Vec4(0.1f,0.8f,0.0f,1.0f));
- root->addChild(backdrop);
- tankXform = new osg::PositionAttitudeTransform();
- root->addChild(groundNode);
- root->addChild(tankXform);
- tankXform->addChild(tankNode);
- tankXform->setPosition( osg::Vec3(10,10,8) );
- tankXform->setAttitude(
- osg::Quat(osg::DegreesToRadians(-45.0), osg::Vec3(0,0,1) ) );
- osgGA::TrackballManipulator *Tman = new osgGA::TrackballManipulator();
- viewer.setCameraManipulator(Tman);
- viewer.setSceneData( root );
- viewer.realize();
- osg::MatrixTransform* orbitTankXForm = new osg::MatrixTransform();
- orbit* tankOrbitCallback = new orbit();
- orbitTankXForm->setUpdateCallback( tankOrbitCallback );
- tankXform->addChild(orbitTankXForm);
- keyboardEventHandler* keh = new keyboardEventHandler();
- keh->addFunction('v',toggleTankOrbiterView);
- viewer.addEventHandler(keh);
- viewer.addEventHandler(new PickHandle(&viewer));
- while( !viewer.done() )
- {
- if (useTankOrbiterView)
- {
- Tman->setByInverseMatrix(tankOrbitCallback->getWCMatrix()
- *osg::Matrix::rotate( -3.1415926/2.0, 1, 0, 0 ));
- }
- viewer.frame();
- }
- }
复制代码 |
|