【问题标题】:OpenGL perspective projection matrix troubleOpenGL透视投影矩阵麻烦
【发布时间】:2015-03-06 21:15:20
【问题描述】:

我真的不明白我在透视矩阵创建代码中做错了什么:

public static Matrix4f perspective(float fov, float width, float height,
            float Near, float Far) {
        Matrix4f projection = new Matrix4f(0);

        float a = width / height;
        float h = 1.0f/(float) Math.tan(Math.toRadians(fov / 2));
        float depth = Near - Far;

        // x&y
        projection.values[0][0] = h/a;
        projection.values[1][1] = h;
        projection.values[2][2] = ((Far + Near)/depth);
        projection.values[2][3] = 2.0f *(Far * Near) / depth;
        projection.values[3][2] = -1.0f;

        return projection;
    }

然后,当我将它发送到着色器时,我将它与模型视图矩阵相乘,如下所示:

math.mul(camera.getMatrix(), mesh.transform.getMatrix());

这意味着

P*M

在其他允许您覆盖运算符的语言中,因为我没有来自相机的视图矩阵。

发生的情况是 3d 模型是平的,当我旋转它时,它会变得非常扭曲。我尝试了一整天来找出问题所在,但我没有运气。

我做错了什么? 有什么我忘记了吗?

编辑:

也许我的乘法代码有问题:

乘法码:

public static Matrix4f mul(Matrix4f m1, Matrix4f m2) {
        Matrix4f result = new Matrix4f();
        for (int j = 0; j < 4; j++) {
            for (int i = 0; i < 4; i++) {
                float sum = 0;

                for (int n = 0; n < 4; n++) {
                    sum += m1.values[n][i] * m2.values[j][n];
                }

                result.values[j][i] = sum;
            }
        }
        return result;
    }

我什至尝试将乘法顺序从P * M 交换为M * P,但仍然没有解决问题。

我尝试在乘法后转置矩阵:

转置代码:

 public static Matrix4f transpose(Matrix4f data)
        {
            Matrix4f result = new Matrix4f();
            for(int y = 0; y < 4; y++)
            {
                for(int x = 0; x < 4; x++)
                {
                    result.values[x][y] = data.values[y][x];
                }
            }
            return result;
        }

但这会造成更严重的失真。

编辑:

以下是投影矩阵的原始打印内存内容:

matrix[0][0] = 1.071111
matrix[0][1] = 0.0
matrix[0][2] = 0.0
matrix[0][3] = 0.0
matrix[1][0] = 0.0
matrix[1][1] = 1.428148
matrix[1][2] = 0.0
matrix[1][3] = 0.0
matrix[2][0] = 0.0
matrix[2][1] = 0.0
matrix[2][2] = -1.00002
matrix[2][3] = -0.0200002
matrix[3][0] = 0.0
matrix[3][1] = 0.0
matrix[3][2] = -1.0
matrix[3][3] = 0.0

这里是ModelView矩阵(变换)(模型位置=0,0,5):

model[0][0] = 1.0
model[0][1] = 0.0
model[0][2] = 0.0
model[0][3] = 0.0
model[1][0] = 0.0
model[1][1] = 1.0
model[1][2] = 0.0
model[1][3] = 0.0
model[2][0] = 0.0
model[2][1] = 0.0
model[2][2] = 1.0
model[2][3] = 5.0
model[3][0] = 0.0
model[3][1] = 0.0
model[3][2] = 0.0
model[3][3] = 1.0

这里是M*P

matrix[0][0] = 1.071111
matrix[0][1] = 0.0
matrix[0][2] = 0.0
matrix[0][3] = 0.0
matrix[1][0] = 0.0
matrix[1][1] = 1.428148
matrix[1][2] = 0.0
matrix[1][3] = 0.0
matrix[2][0] = 0.0
matrix[2][1] = 0.0
matrix[2][2] = -1.00002
matrix[2][3] = -5.0201
matrix[3][0] = 0.0
matrix[3][1] = 0.0
matrix[3][2] = -1.0
matrix[3][3] = -5.0

这里是P*M

matrix[0][0] = 1.071111
matrix[0][1] = 0.0
matrix[0][2] = 0.0
matrix[0][3] = 0.0
matrix[1][0] = 0.0
matrix[1][1] = 1.428148
matrix[1][2] = 0.0
matrix[1][3] = 0.0
matrix[2][0] = 0.0
matrix[2][1] = 0.0
matrix[2][2] = -6.00002
matrix[2][3] = -0.0200002
matrix[3][0] = 0.0
matrix[3][1] = 0.0
matrix[3][2] = -1.0
matrix[3][3] = 0.0

【问题讨论】:

  • 根据我对 Java 文档的了解,Matrix4f 是行主要的。您应该考虑交换这些操作数 (M * P)。
  • Matrix4f 不是 java 的,它是我做的一个类。我仍然会尝试交换乘法。
  • @user5819: 你能打印最终矩阵的原始内存内容吗,就像 16 个浮点数一样,按照存储在内存中的顺序?
  • 我添加了它们。也许有人可以给我一个矩阵,以便我可以比较。
  • @user5819:好吧。我实际上是指最终相乘的P * MM * P 矩阵。到目前为止,使用您的投影矩阵,事情仍然很奇怪:您的代码设置为 matrix[3[2] = -1,但现在元素 appaers 为 1。

标签: java opengl matrix perspective


【解决方案1】:

答案属于@derhass

“从给出的数字来看,M*P 是正确的结果。但是,您将矩阵转置为 GL(有点)默认值。因此,要么在着色器中使用 vec4 * mat4,它应该工作,或者您必须为 GL 转置结果并且可以使用默认的 mat4 * vec4 convetion"

【讨论】:

    猜你喜欢
    • 2013-04-17
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    相关资源
    最近更新 更多