2015-09-15 63 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);