我试图用文件中读入的笛卡尔坐标(X,Y,Z)和欧拉角(Yaw,Pitch,Roll)控制osg :: Camera。
第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)
第1部分
问题是我将角度添加到四元数的顺序。
rotationQuat.makeRotate(
deg2rad(rotation[1] + 90.0f), pitchAxis,
deg2rad(rotation[2]), rollAxis,
-deg2rad(rotation[0]), headingAxis);
当围绕X轴旋转
时,必须首先添加音高第2部分
这里有一些错误。
首先,我需要抓住逆视图矩阵。我不是百分百肯定为什么,也许有更多知识的人可以发表评论,但它确实有效。
auto rotation = this->camera->getViewInverseMatrix().getRotate();
其次,假设转换功能是正确的,结果证明是坏的。 我写了一个单元测试,这个功能缺失了,结果证明这是错误的。
// Assuming this conversion is correct
auto ypr = Quaternion2YawPitchRoll(quat);