-2

我正在尝试渲染一个四边形,然后使用转换矩阵在显示器周围移动它。我已将问题追溯到我的 createTransformationMatrix 方法。我知道问题很可能出在我从矩阵类调用函数的方式上。有问题的代码如下:

public static Matrix4f createTransformationMatrix(Vector3f translation, float rx, float ry, float rz, float scale) {
        Matrix4f matrix = new Matrix4f();
        Matrix4f.translate(translation.x, translation.y, translation.z);
        Matrix4f.rotate((float) Math.toDegrees(rx), 1, 0, 0);
        Matrix4f.rotate((float) Math.toDegrees(ry), 0, 1, 0);
        Matrix4f.rotate((float) Math.toDegrees(rz), 0, 0, 1);
        Matrix4f.scale(scale, scale, scale);

        return matrix;
}

我认为问题在于调用 Matrix4f 进行转换,但它们是静态的,所以我不确定如何解决这个问题。如果需要,转换功能如下。

public static Matrix4f translate(float x, float y, float z) {
        Matrix4f translation = new Matrix4f();

        translation.m03 = x;
        translation.m13 = y;
        translation.m23 = z;

        return translation;
}

public static Matrix4f rotate(float angle, float x, float y, float z) {
        Matrix4f rotation = new Matrix4f();

        float c = (float) Math.cos(Math.toRadians(angle));
        float s = (float) Math.sin(Math.toRadians(angle));
        Vector3f vec = new Vector3f(x, y, z);
        if (vec.length() != 1f) {
            vec = vec.normalize();
            x = vec.x;
            y = vec.y;
            z = vec.z;
        }

        rotation.m00 = x * x * (1f - c) + c;
        rotation.m10 = y * x * (1f - c) + z * s;
        rotation.m20 = x * z * (1f - c) - y * s;
        rotation.m01 = x * y * (1f - c) - z * s;
        rotation.m11 = y * y * (1f - c) + c;
        rotation.m21 = y * z * (1f - c) + x * s;
        rotation.m02 = x * z * (1f - c) + y * s;
        rotation.m12 = y * z * (1f - c) - x * s;
        rotation.m22 = z * z * (1f - c) + c;

        return rotation;
}

public static Matrix4f scale(float x, float y, float z) {
        Matrix4f scaling = new Matrix4f();

        scaling.m00 = x;
        scaling.m11 = y;
        scaling.m22 = z;

        return scaling;
}

值得注意的是,我的矩阵类是由 SilverTiger 制作的,因为我对矩阵数学和线性代数不够熟悉,无法编写这样的类。

非常感谢您的帮助。

4

1 回答 1

1

matrix您永远不会在该createTransformationMatrix方法中写入。您需要保存对平移、旋转和缩放矩阵的引用,然后将它们相乘。 scale * rotation * translation应该给你正确的转换矩阵。如果变换顺序感觉“颠倒”,只需转置乘法顺序即可。

于 2021-10-06T13:45:58.300 回答