Math 调整轴指示器的视图矩阵

Math 调整轴指示器的视图矩阵,math,3d,camera,openscenegraph,Math,3d,Camera,Openscenegraph,我正在尝试为另一个视口的摄影机的轴指示器配置osg::Camera行为(在大多数CAD应用程序中,每个3D视口中都有一个表示每个世界空间轴的小模型) 我通过拉动视口摄影机的视图矩阵来执行此操作: // fCam is the 'owning' viewport's camera i.e. the followed camera. Matrixd newMatrix = fCam->getViewMatrix(); newMatrix.setTrans( eye * 4.0f ); new

我正在尝试为另一个视口的摄影机的轴指示器配置
osg::Camera
行为(在大多数CAD应用程序中,每个3D视口中都有一个表示每个世界空间轴的小模型)

我通过拉动视口摄影机的视图矩阵来执行此操作:

// fCam is the 'owning' viewport's camera i.e. the followed camera.
Matrixd newMatrix = fCam->getViewMatrix();
newMatrix.setTrans( eye * 4.0f );
newMatrix.invert( newMatrix );

viewCam->setViewMatrix( newMatrix );
然后我将其反转,以获得摄影机的变换矩阵,并获得摄影机的逆视图向量:

newMatrix.invert( newMatrix );

Vec3d eye, focus, up;
newMatrix.getLookAt( eye, focus, up );

eye = focus - eye;
eye.normalize();
我使用视图向量将相机定位在与原点(轴模型所在的位置)固定距离的位置,然后再次反转矩阵以获得生成的相机视图矩阵:

// fCam is the 'owning' viewport's camera i.e. the followed camera.
Matrixd newMatrix = fCam->getViewMatrix();
newMatrix.setTrans( eye * 4.0f );
newMatrix.invert( newMatrix );

viewCam->setViewMatrix( newMatrix );
这应该具有以下效果:将轴指示器摄影机设置为跟随“拥有”视口的摄影机方向,但将其与原点保持固定距离,并使原点始终位于视口的死点


但它不起作用。方向看起来正确,但位置似乎没有改变,导致轴模型移出屏幕。我正在使用OpenSceneGraph进行此操作,并且
视图上没有附加摄影机操纵器。所以我不明白为什么相机没有像我期望的那样移动-有什么建议吗?

更新

我误解了数学要求,但在看了你的答案后发现了问题

newMatrix.getLookAt( eye, focus, up );
getLookAt
方法在返回
eye、focus
up
之前取给定矩阵本身的逆矩阵。因为您提供的是倒矩阵,所以您的原始代码失败了,所以应该是这样的:

Matrixd newMatrix = fCam->getViewMatrix();

Vec3d eye, focus, up;
newMatrix.getLookAt( eye, focus, up );

eye = eye - focus;
eye.normalize();

newMatrix.invert( newMatrix );
newMatrix.setTrans( eye * 4.0f );
newMatrix.invert( newMatrix );

viewCam->setViewMatrix( newMatrix );

我认为问题来自:

newMatrix.setTrans( eye * 4.0f );
此方法设置相对于原点(0,0,0)的位置。你应该使用这样的东西:

osg::Vec3 direction = focus - eye;
direction.normalize();

newMatrix.setTrans( eye + direction * 4.0f );

我通过使用内置的
*LookAt(..)
方法来获取和设置以下内容,从而“解决”了该问题:


我看不出我的“手动”代码和这些方便的方法有什么区别。我认为OSG可能会在渲染管道中执行一些额外的转换,但我在它的源代码中找不到任何东西。

如问题中所述:“但保持它与原点的固定距离,并且原点始终位于视口的死点”,因此这方面的数学是正确的。如果问题解决了,接受答案,使问题显示为已解决:)