osg的幾個矩陣

#include <osg/Group>
#include <osg/Camera>
#include <osgGA/CameraManipulator>
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>

#include <iostream>
using namespace std;

class Follow :public osgGA::CameraManipulator
{
public:
  Follow(){

    _position=osg::Vec3(0,0,3);
    _rotate=osg::Vec3(osg::PI_2,0,0);//一般讓相機繞x軸旋轉90度,否則相機會從上空看模型(一般,一般會這樣,看你模型怎麼方了)
    _speed=2.0;
    _angle=2.5;
  }
  virtual ~Follow(){
   
  }

/*
在OSG裏,所有的視圖矩陣操作都是通過矩陣來完成的,不同攝像機之間的交互也通過矩陣,
這樣就提供了一個通用的模型,不管你習慣使用gluLookAt方式的,還是習慣操作攝像機位置姿態方
式的,都可以很容易嵌入OSG的框架中,因爲所有方式的最後結果就是矩陣
*/
  /** set the position of the matrix manipulator using a 4x4 Matrix.*/
/*這個函數在從一個攝像機切換到另一個攝像機時調用,用來把上一個攝像機的視圖矩陣傳過來,
  這樣就可依此設定自己的初始位置了。*/
  virtual void setByMatrix(const osg::Matrixd& matrix){
   
  }
  /** set the position of the matrix manipulator using a 4x4 Matrix.*/
/*這個方法當在外部直接調用Viewer的setViewByMatrix方法時,把設置的矩陣傳過來,讓
  攝像機記住新更改的位置*/
  virtual void setByInverseMatrix(const osg::Matrixd& matrix){
   
  }
  /** get the position of the manipulator as 4x4 Matrix.*/
/*SetByMatrix方法需要的矩陣就是用這個方法得到的,用來向下一個攝像機傳遞矩陣。*/
  virtual osg::Matrixd getMatrix() const {
    osg::Matrixd mat;
    mat.makeRotate(_rotate.x(),osg::Vec3(1,0,0),
                   _rotate.y(),osg::Vec3(0,1,0),
                   _rotate.z(),osg::Vec3(0,0,1));
    cout<<"getMatrix"<<endl;
    return mat*osg::Matrixd::translate(_position);
  }
  /** get the position of the manipulator as a inverse matrix of the manipulator, typically used as a model view matrix.*/
/*視圖矩陣(觀察矩陣)是變換矩陣的逆矩陣)   這個是最重要的方法,這個方法每幀會被調用,它返回當前的視圖矩陣。
在這個方法裏進行時間的處理,改變自己的狀態,進而在 getInverseMatrix 被調用時,改變
場景內攝像機的位置姿態。這個函數在 void Viewer::updateTraversal()中被調用
 _camera->setViewMatrix(_cameraManipulator->getInverseMatrix());
*/
  virtual osg::Matrixd getInverseMatrix() const{   
    osg::Matrixd mat;
    mat.makeRotate(_rotate.x(),osg::Vec3(1,0,0),
                   _rotate.y(),osg::Vec3(0,1,0),
                   _rotate.z(),osg::Vec3(0,0,1));
    return osg::Matrixd::inverse(mat*osg::Matrixd::translate(_position));
  }
/*
在這個方法裏,有兩個參數,第一個是GUI事件的供給者,第二個參數用來handle方法對GUI
進行反饋,它可以讓GUIEventHandler根據輸入事件讓GUI進行一些動作。
如果要進行事件處理,可以從GUIEventHandler繼承出自己的類,然後覆蓋handle方法,在裏
面進行事件處理。osgProducer::Viewer類維護一個GUIEventHandler隊列,事件在這個隊列裏依次傳
遞,handle的返回值決定這個事件是否繼續讓後面的GUIEventHandler處理,如果返回true,則停止
處理,如果返回false,後面的GUIEventHandler還有機會繼續對這個事件進行響應。*/
  bool handle(const osgGA::GUIEventAdapter &ea,osgGA::GUIActionAdapter &aa){
//操作邏輯
    return false;
  }


private:
  osg::Vec3 _position;
  osg::Vec3 _rotate;
  float _speed;
  float _angle;
};

int main(int argc, char *argv[])
{
  osg::ref_ptr<osgViewer::Viewer>viewer=new osgViewer::Viewer;
  viewer->setSceneData(osgDB::readNodeFile("ceep.ive"));
  viewer->setCameraManipulator(new Follow);
  return viewer->run();
}

 

 轉自 http://blog.csdn.net/zhuyingqingfen/article/details/8249501

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章