0

我正在尝试围绕Y世界轴旋转相机,在我的项目中用 3D 十字标记。

我所做的是围绕它自己的轴旋转它,这很酷,但这不是我想要的。

如何通过使用四元数和一些数学来实现这一点,而不是 osg::PositionAttitudeTransforms或任何易于使用但难以理解的基础设施。

我想接触它背后的数学。

相关的代码是

osg::Matrixd camM;
std::stringstream oss;
osg::Quat x_rot_q(osg::DegreesToRadians(-DEG), osg::Vec3d(1.0, 0.0, 0.0));
osg::Quat y_rot_q(osg::DegreesToRadians(DEG), osg::Vec3d(0.0, 1.0, 0.0));
camM = cameraManipulator->getMatrix();
camM.makeRotate(x_rot_q * y_rot_q);
camM.setTrans(250.0, 300.0, 250.0);
cameraManipulator->setByMatrix(camM);

osg::Quat y_delta_trans(osg::DegreesToRadians(DEG_DELTA), osg::Vec3d(0.0, 1.0, 0.0));

while(!viewer.done()) {
    oss.str(std::string());
    oss.clear();
    camM = cameraManipulator->getMatrix();
    camM.makeRotate(camM.getRotate() * y_delta_trans);
    camM.setTrans(250.0, 300.0, 250.0);
    cameraManipulator->setByMatrix(camM);
    oss << getMatrixRepresentation(camM);
    hudGeode->setStatus(oss.str());
    viewer.frame();
}

您可以在文件底部找到它Simple.cpp。整个项目可以在本次修订中的要点中找到。

在 GNU/Linux 机器上,只需键入make,它应该可以在安装了适当的工具的情况下开箱即用。

4

1 回答 1

3

Rotating the camera around another axis than its own is fairly simple, just perform these steps in order:

  • Translate the camera to the origin of your world
    • if the camera is at (Xc,Yc,Zc) relative to the world's origin that would imply a translation of (-Xc,-Yc,-Zc).
  • Rotate the camera the way you want it rotated
  • Reverse the previous translation, i.e. perform a translation of (Xc,Yc,Zc)

Or mathematically as a matrix product:

                        [ 1 0 0 -Xc ]                 [ 1 0 0 Xc ]
Transformation Matrix = [ 0 1 0 -Yc ] Rotation Matrix [ 0 1 0 Yc ]
                        [ 0 0 1 -Zc ]                 [ 0 0 1 Zc ]
                        [ 0 0 0  1  ]                 [ 0 0 0 1  ]

Edit:

Note that this is the general way of rotating an object around an origin not its own.

于 2012-07-22T15:54:17.557 回答