簡體   English   中英

OpenGL透視投影矩陣的麻煩

[英]OpenGL perspective projection matrix trouble

我真的不明白我在透視矩陣創建代碼中做錯了什么:

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

在其他語言中,您可以覆蓋操作符,因為我沒有來自攝像機的View Matrix。

會發生什么是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

答案屬於@derhass

“根據給出的數字,看起來M * P是正確的結果。但是,你存儲轉換為GL(有些)默認的矩陣。所以要么你在着色器中使用vec4 * mat4,它應該工作,或者你必須轉換GL的結果,並可以使用默認的mat4 * vec4對流“

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM