如何通过平移、旋转和缩放变换转换 4x4 矩阵?

How do I convert a 4x4 matrix from translation, rotation and scale transformations?

我有 3 个人体骨骼的变换(以臀骨为例):

  <node id="hips" name="hips_bone" type="JOINT">
    <translate>0 0.941045 -0.02637</translate>
    <rotate sid="rotateZ">0 0 1 90</rotate>
    <rotate sid="rotateY">0 1 0 -12.0004</rotate>
    <rotate sid="rotateX">1 0 0 0</rotate>
    <scale>1 1 1</scale>
  </node>

有没有办法将这些数据转换成 4x4 矩阵? (我想我们可以取消比例变换,因为它总是 1 1 1)。

更准确地说,我想要达到的结果:

<matrix sid="matrix">
  0.978148 -0.207912 0.000000 0.000000
  0.207912 0.978148 0.000000 -0.000012
  -0.000000 0.000000 1.000000 0.000000
  0.000000 0.000000 0.000000 1.000000
</matrix>

(结果矩阵不等于上面的转换数据)。

C# 中是否有任何有用的结构来处理 4x4 矩阵?

如果您想通过 C# 使用 3D 图形,您可以使用专用框架,例如 OpenTK (https://www.nuget.org/packages/OpenTK/). It provides a way to use OpenGL functionalities with C#. It also has many useful methods to deal with transformations, including using 4x4 matrices. With OpenTK library you can get also some sample projects to see how it works, or get through one of the tutorials (e.g. http://genericgamedev.com/tutorials/opengl-in-csharp-an-object-oriented-introduction-to-opentk/)。

我喜欢 OpenTK 的另一个原因是它可以像任何其他控件一样注入到 WinForms 或 WPF 项目中,或者在游戏 Window 模式中使用类似于 OpenGL 中使用的模式。

希望能帮到你

Is there a way to convert this data into 4x4 matrix? (I suppose we can throw scale transformation off because it's always 1 1 1).

先建立一个ID矩阵,然后应用平移,应用旋转...等等

我的实现 (ak_transformCombine) 它从各个变换构建 4x4 矩阵: https://github.com/recp/assetkit/blob/master/src/transform/ak_trans.c#L50

这是 C 代码而不是 C# 但数学是一样的, 将来我可能会为它编写 C# 包装器

不要忘记其中一些转换具有 sid 属性,例如 sid="rotateZ"。如果这些 sid-ed 变换 used/referenced 在动画等其他地方,它将禁用动画...