如何使用转换矩阵进行翻译?

How to translate using a transformation matrix?

我正在使用 imguizmo 库来转换我正在使用 directx 11 进行的项目场景中的对象。imguizmo 采用模型矩阵来转换 gizmo。它工作正常,但为了将 Gizmo 捕捉到顶点,我需要给它一个转换矩阵,将其转换为该顶点,这就是我制作 4x4 转换矩阵的方法

[ 1.f, 0.f, 0.f,  x  ]
[ 0.f, 1.f, 0.f,  y  ]
[ 0.f, 0.f, 1.f,  z  ]
[ 0.f, 0.f, 0.f, 1.f ]

xyz 是我要翻译 gizmo 的坐标。

然而它不起作用。在此之后我的小发明消失了。这不是翻译矩阵的工作原理吗???

这取决于您使用的是行优先矩阵、行向量和预乘法(Direct3D 样式)还是列优先矩阵、列向量和 post 乘法(OpenGL 样式)。

上面的翻译乍一看好像是'OpenGL style'。

对于Direct3D,我通常希望这样写:

[ 1.f, 0.f, 0.f,  0  ]
[ 0.f, 1.f, 0.f,  0  ]
[ 0.f, 0.f, 1.f,  0  ]
[ x,   y,   z,    1.f ]

通常一个是另一个的转置。

这还取决于您是否为 HLSL 着色器使用列优先或行优先矩阵,这可能需要在将矩阵放入常量缓冲区时转置矩阵。

this blog post

If you are new to DirectX programming, you should consider the SimpleMath helper wrapper for DirectXMath that is included in the DirectX Tool Kit.