相机沿着场景旋转

class Test : public osgGA::AnimationPathManipulator
{
public:
 Test(osg::AnimationPath* path) : AnimationPathManipulator(path)
 {
 }
 ~Test(){}
private:
 osg::Quat _rotation;
 osg::Vec3d _center;
 double _distance;
 double _current;
public:
    virtual void setByMatrix(const osg::Matrixd& matrix) { _matrix = matrix; }

        /** set the position of the matrix manipulator using a 4x4 Matrix.*/
        virtual void setByInverseMatrix(const osg::Matrixd& matrix) { _matrix.invert(matrix); }

        /** get the position of the manipulator as 4x4 Matrix.*/
        virtual osg::Matrixd getMatrix() const { return _matrix; }

        /** get the position of the manipulator as a inverse matrix of the manipulator, typically used as a model view matrix.*/
        virtual osg::Matrixd getInverseMatrix() const
  {
   return  _matrix;
  }

  void home(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us)
  {
   _current = osg::Timer::instance()->tick();
  }
  
  bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& us)
  {
   if( !valid() ) return false;
       
   switch(ea.getEventType())
   {
   case osgGA::GUIEventAdapter::FRAME:
    {
     osg::AnimationPath::ControlPoint cp;

     double t = _animationPath->getLastTime() - _animationPath->getFirstTime();
     double kk = osg::Timer::instance()->tick();
     double duration = osg::Timer::instance()->delta_s(_current, kk);
     duration = fmod(duration, t);
     duration += _animationPath->getFirstTime();
     _animationPath->getInterpolatedControlPoint(duration, cp);
     cp.getMatrix(_matrix);
    }
   case osgGA::GUIEventAdapter::KEYUP:
    {
     if (ea.getKey() == 's')
     { 
                       
     }
    }
    case osgGA::GUIEventAdapter::KEYUP
    {
     if (ea.getKey() == 'p')
     {

     }
     
    }
   }

   return true;

  }

};

 

 

 

osgViewer::Viewer viewer;
 osg::Group* group = new osg::Group;
 
 osg::Node* node = osgDB::readNodeFile("D:/osgBin/bin/cow.osg");
 group->addChild(node);

 osg::MatrixTransform* transform = new osg::MatrixTransform;
 transform->setMatrix(osg::Matrixd::translate(20, 0, 0));

 osg::Node* node1 = osgDB::readNodeFile("D:/osgBin/bin/cow.osg");

 transform->addChild(node1);
 group->addChild(transform);

 osg::AnimationPath* path = new osg::AnimationPath;
 path->setLoopMode(osg::AnimationPath::LOOP);

 for (int i = 0; i < 36; i++)
 {
  osg::Quat quat(10 * i, osg::Vec3d(0.0, 1.0, 0.0));
  osg::Matrixd m = osg::Matrixd::translate(-group->getBound().center()) * osg::Matrixd::rotate(quat) * osg::Matrixd::translate(0, 0, -group->getBound().radius() * 3);
  osg::Vec3f pos = m.getTrans();
  osg::Quat rot = m.getRotate();

  path->insert( i * 5, osg::AnimationPath::ControlPoint(pos, rot) );
 }
 
   viewer.setCameraManipulator(new Test(path));
   viewer.setSceneData(node);

    return viewer.run();

 

你可能感兴趣的:(转)