2017-09-23 367 views
0

我一直在使用带JOML的LWJGL作为创建3D游戏引擎的手段。 由于我一直松散地遵循Jeffrey的教程(YouTube),所以我遇到了一个问题,但我一直使用JOML库而不是他创建的迷你数学库。我已经做了变换类,它已经从杰弗里的数学库复制,并且我已经转换为使用JOML:JOML(带LWJGL) - 从rx,ry和rz值获取旋转矩阵?

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.getRotationAxisAngle4f

的问题的关键是,我怎么翻译RXRY,并RZ角度为AxisAngle4f

回答

0

请仔细和仔细阅读JOML方法的JavaDocs。 Matrix4f.getRotation()检索(仿射)矩阵的旋转部分并将该旋转转换为表示相同旋转的轴角。该方法不从创建/构建从三个欧拉角的旋转矩阵。

因此,要非常精确地陈述/改写您的问题: “如何建立表示给定三个欧拉角(即分别围绕X,Y和Z轴的角度)的3D旋转的仿射4×4矩阵,应用旋转按照X的顺序,然后是Y,然后是Z?“

答案是:Matrix4f.rotationXYZ()或乘数后Matrix4f.rotateXYZ()。 如果您需要不同的旋转顺序,您还可以查看其他“旋转”旋转YXZ/ZYX和rotateYXZ/ZYX方法,或者甚至在给定旋转轴和角度的情况下应用三个调用Matrix4f.rotate()

我建议你也参考GitHub项目的Wiki和GitHub上的joml-lwjgl3-demos项目的源代码。