我一直在使用lwjgl,添加了joml作为创建3D游戏引擎的一种手段。自从我一直遵循Jeffrey(YouTube)的教程以来,我就遇到了一个问题,但是我一直在使用JOML库,而不是他创建的迷你数学库。我已经制作了转换类,该类已从Jeffrey的数学库中复制,我一直在翻译它以使用JOML:
import org.joml.Matrix4f;
import org.joml.Vector3f;
public class Transform {
public static Matrix4f getPerspectiveProjection(float fov, int width, int height, float zNear, float zFar) {
return new Matrix4f().setPerspective(fov, width / height, zNear, zFar);
}
public static Matrix4f getTransformation(Vector3f translation, float rx, float ry, float rz, float scale) {
Matrix4f translationMatrix = new Matrix4f().setTranslation(translation);
// The first problem:
Matrix4f rotationMatrix = new Matrix4f().getRotation(rx, ry, rz);
Matrix4f scaleMatrix = new Matrix4f().initScale(scale);
return translationMatrix.mul(rotationMatrix.mul(scaleMatrix));
}
public static Matrix4f getViewMatrix(Camera camera) {
Vector3f pos = camera.getPosition();
Matrix4f translationMatrix = new Matrix4f().setTranslation(-pos.x, -pos.y, -pos.z);
Matrix4f rotationMatrix = new Matrix4f().initRotation(camera.getForward(), camera.getUp());
return rotationMatrix.mul(translationMatrix);
}
}
从joml文档中,我只能找到使用matrix4f.getRotation与 axisangle4f 。
问题的症结是,如何将 rx , ry 和 rz> rz 角度转换为 axisisangle4f ?
请非常仔细,彻底读取JOML方法的Javadocs。Matrix4f.getRotation()
检索(仿射)矩阵的旋转部分,并将旋转转换为代表相同旋转的轴角。该方法不会创建/构建 旋转矩阵来自三个欧拉角。
因此,要非常精确地陈述/重新提出您的问题:"如何构建一个代表3D旋转的仿射4x4矩阵给定三个欧拉角(即围绕x,y和z轴的角度),在x,y和z上施加旋转,然后z?"
?" ?"的答案是:Matrix4f.rotationXYZ()
或后列出Matrix4f.rotateXYZ()
。如果您需要不同的旋转顺序,也可以查看其他"旋转"旋转Yxz/zyx和rotateyxz/zyx方法,甚至在旋转轴和角度时,甚至可以将三个调用对Matrix4f.rotate()
应用。
我建议您还会在GitHub项目中咨询Wiki和GitHub上JOML-LWJGL3-DEMOS项目的来源。