只是想知道 LWJGL 的 Matrix4f.mul 方法是前乘法还是后乘法。或者有没有办法选择。
最佳答案
根据 javadoc,Matrix4f.mul
将:
Multiply the right matrix by the left and place the result in a third matrix.
public static Matrix4f mul(Matrix4f left, Matrix4f right, Matrix4f dest)
因此,要实现后乘法,请将现有矩阵用作 left
参数,将新矩阵用作 right
参数。如果您为 dest
提供空对象,该函数将为您分配一个新的 Matrix4f
,并从函数调用中返回它。
我通常考虑矩阵乘法的方式是首先应用与 vector “最接近”的矩阵。例如:
Perspective * View * Model * Vec4
从距离 vector 最近的矩阵开始,您可以看到首先应用模型变换,然后是 View 变换,最后是透视变换。
在你的代码中,上面的代码看起来像:
Matrix4f MVP = Matrix4f.mul(View, Model, null);
Matrix4f.mul(Perspective, MVP, MVP);
Vector4f result = Matrix4f.transform(MVP, Vec4, null);
希望这对您有所帮助!
编辑:以上是我倾向于连接我的变换的方式,但由于矩阵乘法是关联的,以下也应该有效:
Matrix4f MVP = Matrix4f.mul(Perspective, View, null);
Matrix4f.mul(MVP, Model, MVP);
Vector4f result = Matrix4f.transform(MVP, Vec4, null);
关于java - LWJGL 的 Matrix4f.mul 方法是乘法前还是乘法后?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11060849/