【问题标题】:Java Matrix transformationJava 矩阵变换
【发布时间】:2021-10-03 23:48:22
【问题描述】:

我正在尝试渲染一个四边形,然后使用转换矩阵在显示器周围移动它。我已将问题追溯到我的 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 制作的,因为我对矩阵数学和线性代数不够熟悉,无法编写此类。

非常感谢您的帮助。

【问题讨论】:

  • 经过一些测试,我知道问题在于未写入矩阵。这方面的帮助会很棒。
  • 它没有被写入 CPU 或着色器中的制服没有被写入?

标签: matrix lwjgl transformation-matrix


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2012-02-19
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多