如何从 GLM 到 JOML(OpenGL 数学库)
How to go from GLM to JOML (OpenGL Mathematics Libraries)
我有一个用于我的 C++ 转换 class 的代码,它应该为 class 的用户提供一个模型视图矩阵
代码清单 1:
glm::mat4 Transform::GetModel(glm::vec3 pos, glm::vec3 rot, glm::vec3 scale, glm::mat4 parentTransformationMatrix){
glm::mat4 posMat = glm::translate(pos);
glm::mat4 scaleMat = glm::scale(scale);
glm::mat4 rotX = glm::rotate(rot.x, glm::vec3(1.0, 0.0, 0.0));
glm::mat4 rotY = glm::rotate(rot.y, glm::vec3(0.0, 1.0, 0.0));
glm::mat4 rotZ = glm::rotate(rot.z, glm::vec3(0.0, 0.0, 1.0));
glm::mat4 rotMat = rotX * rotY * rotZ;
return parentTransformationMatrix * posMat * rotMat * scaleMat;
}
我已经使用 JOML(对于 LWJGL3)编写了部分代码,但我仍然坚持使用其他部分(已评论)
代码清单 2:
public Matrix4f GetModel(Vector3f pos, Vector3f rot, Vector3f scale, Matrix4f parentTransformationMatrix){
Matrix4f posMat ;// = translate(pos);
Matrix4f scaleMat ;// = scale(scale);
Matrix4f rotX ;// = rotate(rot.x, Vector3f(1.0, 0.0, 0.0));
Matrix4f rotY ;// = rotate(rot.y, Vector3f(0.0, 1.0, 0.0));
Matrix4f rotZ ;// = rotate(rot.z, Vector3f(0.0, 0.0, 1.0));
Matrix4f rotMat = rotX.mul(rotY).mul(rotZ);
return parentTransformationMatrix.mul(posMat).mul(rotMat).mul(scaleMat);
}
与您的 GLM 代码完全相同的是 Java 使用 JOML 的代码:
public Matrix4f GetModel(Vector3f pos, Vector3f rot, Vector3f scale, Matrix4f parent) {
return new Matrix4f(parent)
.translate(pos)
.scale(scale)
.rotateXYZ(rot);
}
与 C/C++ 不同,Java 没有(还)在语言本身中内置堆栈分配(除非在 JIT 字节码时由 HotSpot 优化)。因此,比创建新的 Matrix4f 实例更喜欢使用“dest”/destination/out 参数(或像上面那样修改 parentTransformationMatrix
矩阵,因为调用 mul
会修改它而不是创建新的 Matrix4f ).
所以我们最终得到:
public Matrix4f GetModel(Vector3f pos, Vector3f rot, Vector3f scale, Matrix4f parent, Matrix4f dest) {
return parent.translate(pos, dest)
.scale(scale)
.rotateXYZ(rot);
}
我有一个用于我的 C++ 转换 class 的代码,它应该为 class 的用户提供一个模型视图矩阵
代码清单 1:
glm::mat4 Transform::GetModel(glm::vec3 pos, glm::vec3 rot, glm::vec3 scale, glm::mat4 parentTransformationMatrix){
glm::mat4 posMat = glm::translate(pos);
glm::mat4 scaleMat = glm::scale(scale);
glm::mat4 rotX = glm::rotate(rot.x, glm::vec3(1.0, 0.0, 0.0));
glm::mat4 rotY = glm::rotate(rot.y, glm::vec3(0.0, 1.0, 0.0));
glm::mat4 rotZ = glm::rotate(rot.z, glm::vec3(0.0, 0.0, 1.0));
glm::mat4 rotMat = rotX * rotY * rotZ;
return parentTransformationMatrix * posMat * rotMat * scaleMat;
}
我已经使用 JOML(对于 LWJGL3)编写了部分代码,但我仍然坚持使用其他部分(已评论)
代码清单 2:
public Matrix4f GetModel(Vector3f pos, Vector3f rot, Vector3f scale, Matrix4f parentTransformationMatrix){
Matrix4f posMat ;// = translate(pos);
Matrix4f scaleMat ;// = scale(scale);
Matrix4f rotX ;// = rotate(rot.x, Vector3f(1.0, 0.0, 0.0));
Matrix4f rotY ;// = rotate(rot.y, Vector3f(0.0, 1.0, 0.0));
Matrix4f rotZ ;// = rotate(rot.z, Vector3f(0.0, 0.0, 1.0));
Matrix4f rotMat = rotX.mul(rotY).mul(rotZ);
return parentTransformationMatrix.mul(posMat).mul(rotMat).mul(scaleMat);
}
与您的 GLM 代码完全相同的是 Java 使用 JOML 的代码:
public Matrix4f GetModel(Vector3f pos, Vector3f rot, Vector3f scale, Matrix4f parent) {
return new Matrix4f(parent)
.translate(pos)
.scale(scale)
.rotateXYZ(rot);
}
与 C/C++ 不同,Java 没有(还)在语言本身中内置堆栈分配(除非在 JIT 字节码时由 HotSpot 优化)。因此,比创建新的 Matrix4f 实例更喜欢使用“dest”/destination/out 参数(或像上面那样修改 parentTransformationMatrix
矩阵,因为调用 mul
会修改它而不是创建新的 Matrix4f ).
所以我们最终得到:
public Matrix4f GetModel(Vector3f pos, Vector3f rot, Vector3f scale, Matrix4f parent, Matrix4f dest) {
return parent.translate(pos, dest)
.scale(scale)
.rotateXYZ(rot);
}