2015-09-15 146 views
1

我試圖控制一個osg ::相機與笛卡爾座標(X,Y,Z)和歐拉角度(偏航,瀝青,卷),我從文件讀入。使用笛卡爾座標和歐拉角控制OpenSceneGraph相機

第1部分

出於某種原因,我的設置卷將導致預期相機繞Z軸,而不是y軸旋轉。

以下是先前寫入的攝像機操縱器,我正在將我的位置數據提供給。

class EulerManipulator : public osgGA::CameraManipulator 
{ 
    public: 
     EulerManipulator(osg::ref_ptr<osg::View> x) : 
      view(x), 
      camView(new osg::CameraView) 
     { 
     } 

     virtual ~EulerManipulator(){} 

     virtual osg::Matrixd getMatrix() const 
     { 
      // Note: (x, y, z) and (yaw, pitch, roll) are read in from file and saved in memory 

      // Position works fine 
      this->camView->setPosition(osg::Vec3d(x, y, z)); 

      // Possibly something wrong with rotation 
      osg::Quat rotationQuat; 

      const osg::Vec3d headingAxis(0.0, 0.0, 1.0); 
      const osg::Vec3d pitchAxis(1.0, 0.0, 0.0); 
      const osg::Vec3d rollAxis(0.0, 1.0, 0.0); 

      std::array<double, 3> rotation = {yaw, pitch, roll}; 

      rotationQuat.makeRotate(
       deg2rad(rotation[2]), rollAxis, 
       deg2rad(rotation[1] + 90.0f), pitchAxis, 
       -deg2rad(rotation[0]), headingAxis); 

      this->camView->setAttitude(rotationQuat); 

      // Not 100% sure what this does but assume it works as Heading and Pitch seem to work fine. 
      auto nodePathList = this->camView->getParentalNodePaths(); 
      return osg::computeLocalToWorld(nodePathList[0]); 
     } 

    private: 
     osg::ref_ptr<osg::View> view; 
     osg::ref_ptr<osg::CameraView> camView; 
}; 

注意:上面的代碼不是我自己的代碼,而是用於驅動攝像頭的代碼。我的目標是從我自己的相機中獲取笛卡爾位置和歐拉角,並寫出文件。但在我能做到這一點之前,我需要弄清楚爲什麼這段代碼不像預期的那樣工作。

上面的代碼有什麼明顯的錯誤嗎?

第2部分

對於這個問題,我捕捉相機的位置數據,並將其寫入文件的第二部分。

我已經有了一點成功,但它仍然不能正常工作。下面是我爲附加到我的視圖的事件處理程序編寫的重載句柄函數。我的目標是在場景內移動相機時捕捉相機的位置數據。

這裏我運行我的程序並水平旋轉相機(偏航),但由於某種原因,俯仰角急劇減小。如果我從0度偏航到90度偏航。我的俯仰角將從0減小到大約90度。我希望我的水平保持與我水平旋轉攝像機大致相同。

virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter&, osg::Object*, osg::NodeVisitor*) override 
    { 
     switch(ea.getEventType()) 
     { 
      case osgGA::GUIEventAdapter::FRAME: 
       { 
        if((this->camera != nullptr)) 
        { 
         osg::Vec3d center; 
         osg::Vec3d vecDontCare; 
         this->camera->getViewMatrixAsLookAt(vecDontCare, center, vecDontCare); 

         auto rotation = this->camera->getViewMatrix().getRotate(); 

         std::array<double, 4> quat = {{rotation.x(), rotation.y(), rotation.z(), rotation.w()}}; 

         // Assuming this conversion is correct 
         auto ypr = Quaternion2YawPitchRoll(quat); 

         // Convert the rotation into my coordinate system. 
         ypr[0] *= -1.0; 
         ypr[1] -= 90.0; 

         // The output angles for YPR don't appear to align with the manual rotation I perform with the camera 
         Debug << "Y: " << ypr[0] << " P: " << ypr[1] << " R: " << ypr[2]; 
         // Write XYZ and YPR to file 
        } 
       } 
       break; 

      default: 
       break; 
     } 

問題是,我抓取相機的位置數據的方式有什麼明顯的錯誤嗎?有沒有更好的方法來做到這一點?

+0

對於第1部分: 我想你遇到類似萬向鎖https://en.wikipedia.org/wiki/Gimbal_lock 萬向鎖是因爲你是串聯轉的情況下,並根據您執行的順序,他們可以更改執行後續旋轉的軸。 您試圖使用的標題,音高,滾動值的來源是什麼?它們將從3D方向分解,並且必須將應用程序順序與分解順序相匹配,否則它們都會變得棘手。 – XenonofArcticus

回答

1

第1部分

的問題是在我加入我的角度,四元數的順序。

rotationQuat.makeRotate(
    deg2rad(rotation[1] + 90.0f), pitchAxis, 
    deg2rad(rotation[2]), rollAxis, 
    -deg2rad(rotation[0]), headingAxis); 

瀝青的,因爲它繞X軸

第2部分

一對夫婦的事情是錯在這裏旋轉,首先加入。

首先,我需要抓取反向視圖矩陣。我不是100%確定爲什麼,也許有更多知識的人可以發表評論,但它是有效的。

auto rotation = this->camera->getViewInverseMatrix().getRotate(); 

其次,假設轉換函數被證明是錯誤的。 我寫了一個單元測試,該功能缺失,事實證明這是錯誤的。

// Assuming this conversion is correct 
auto ypr = Quaternion2YawPitchRoll(quat);