osg漫游器

  1. #include <osg/Group>  
  2. #include <osg/Camera>  
  3. #include <osgGA/CameraManipulator>  
  4. #include <osgViewer/Viewer>  
  5. #include <osgDB/ReadFile>  
  6.   
  7. #include <iostream>  
  8. using namespace std;  
  9.   
  10. class Follow :public osgGA::CameraManipulator   
  11. {  
  12. public:  
  13.   Follow(){  
  14.   
  15.     _position=osg::Vec3(0,0,3);  
  16.     _rotate=osg::Vec3(osg::PI_2,0,0);//一般让相机绕x轴旋转90度,否则相机会从上空看模型(一般,一般会这样,看你模型怎么方了)  
  17.     _speed=2.0;  
  18.     _angle=2.5;  
  19.   }  
  20.   virtual ~Follow(){  
  21.       
  22.   }  
  23.   
  24. /* 
  25. 在OSG里,所有的视图矩阵操作都是通过矩阵来完成的,不同摄像机之间的交互也通过矩阵, 
  26. 这样就提供了一个通用的模型,不管你习惯使用gluLookAt方式的,还是习惯操作摄像机位置姿态方 
  27. 式的,都可以很容易嵌入OSG的框架中,因为所有方式的最后结果就是矩阵 
  28. */  
  29.   /** set the position of the matrix manipulator using a 4x4 Matrix.*/  
  30. /*这个函数在从一个摄像机切换到另一个摄像机时调用,用来把上一个摄像机的视图矩阵传过来, 
  31.   这样就可依此设定自己的初始位置了。*/  
  32.   virtual void setByMatrix(const osg::Matrixd& matrix){  
  33.       
  34.   }  
  35.   /** set the position of the matrix manipulator using a 4x4 Matrix.*/  
  36. /*这个方法当在外部直接调用Viewer的setViewByMatrix方法时,把设置的矩阵传过来,让 
  37.   摄像机记住新更改的位置*/  
  38.   virtual void setByInverseMatrix(const osg::Matrixd& matrix){  
  39.       
  40.   }  
  41.   /** get the position of the manipulator as 4x4 Matrix.*/  
  42. /*SetByMatrix方法需要的矩阵就是用这个方法得到的,用来向下一个摄像机传递矩阵。*/  
  43.   virtual osg::Matrixd getMatrix() const {  
  44.     osg::Matrixd mat;  
  45.     mat.makeRotate(_rotate.x(),osg::Vec3(1,0,0),  
  46.                    _rotate.y(),osg::Vec3(0,1,0),  
  47.                    _rotate.z(),osg::Vec3(0,0,1));  
  48.     cout<<"getMatrix"<<endl;  
  49.     return mat*osg::Matrixd::translate(_position);  
  50.   }  
  51.   /** get the position of the manipulator as a inverse matrix of the manipulator, typically used as a model view matrix.*/  
  52. /*视图矩阵(观察矩阵)是变换矩阵的逆矩阵)   这个是最重要的方法,这个方法每帧会被调用,它返回当前的视图矩阵。 
  53. 在这个方法里进行时间的处理,改变自己的状态,进而在 getInverseMatrix 被调用时,改变 
  54. 场景内摄像机的位置姿态。这个函数在 void Viewer::updateTraversal()中被调用 
  55.  _camera->setViewMatrix(_cameraManipulator->getInverseMatrix()); 
  56. */  
  57.   virtual osg::Matrixd getInverseMatrix() const{      
  58.     osg::Matrixd mat;  
  59.     mat.makeRotate(_rotate.x(),osg::Vec3(1,0,0),  
  60.                    _rotate.y(),osg::Vec3(0,1,0),  
  61.                    _rotate.z(),osg::Vec3(0,0,1));  
  62.     return osg::Matrixd::inverse(mat*osg::Matrixd::translate(_position));  
  63.   }  
  64. /* 
  65. 在这个方法里,有两个参数,第一个是GUI事件的供给者,第二个参数用来handle方法对GUI 
  66. 进行反馈,它可以让GUIEventHandler根据输入事件让GUI进行一些动作。 
  67. 如果要进行事件处理,可以从GUIEventHandler继承出自己的类,然后覆盖handle方法,在里 
  68. 面进行事件处理。osgProducer::Viewer类维护一个GUIEventHandler队列,事件在这个队列里依次传 
  69. 递,handle的返回值决定这个事件是否继续让后面的GUIEventHandler处理,如果返回true,则停止 
  70. 处理,如果返回false,后面的GUIEventHandler还有机会继续对这个事件进行响应。*/  
  71.   bool handle(const osgGA::GUIEventAdapter &ea,osgGA::GUIActionAdapter &aa){  
  72. //操作逻辑  
  73.     return false;  
  74.   }  
  75.   
  76.   
  77. private:  
  78.   osg::Vec3 _position;  
  79.   osg::Vec3 _rotate;  
  80.   float _speed;  
  81.   float _angle;  
  82. };  
  83.   
  84. int main(int argc, char *argv[])  
  85. {  
  86.   osg::ref_ptr<osgViewer::Viewer>viewer=new osgViewer::Viewer;  
  87.   viewer->setSceneData(osgDB::readNodeFile("ceep.ive"));  
  88.   viewer->setCameraManipulator(new Follow);  
  89.   return viewer->run();  
  90. }  

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值