简体   繁体   English

OpenGL透视投影矩阵的麻烦

[英]OpenGL perspective projection matrix trouble

I really don't understand what I am doing wrong in the perspective matrix creation code: 我真的不明白我在透视矩阵创建代码中做错了什么:

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;
    }

Then when I send it to the shader I multiply it before with the modelview matrix like this: 然后,当我将它发送到着色器时,我将它与之前的模型视图矩阵相乘:

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

this means 这意味着

P*M

in other languages that let you override operators, since I don't have a View Matrix from the camera. 在其他语言中,您可以覆盖操作符,因为我没有来自摄像机的View Matrix。

What happens is that the 3d model is flat and when I rotate it, it gets really distorted. 会发生什么是3d模型是平坦的,当我旋转它时,它变得非常扭曲。 I tried the whole day to figure out what is wrong, but I had no luck. 我整天都想弄清楚出了什么问题,但我没有运气。

What am I doing wrong ? 我究竟做错了什么 ? Is there something I forgot about ? 有什么我忘记了吗?

EDIT: 编辑:

Maybe there is something wrong with my multiplication code: 也许我的乘法代码有问题:

Multiplication code: 乘法代码:

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;
    }

I even tried to swap the multiplication order from P * M to M * P but it still don't solve the problem. 我甚至尝试将乘法顺序从P * M交换到M * P但它仍然无法解决问题。

I tried to transpose the matrix after multiplication: 我尝试在乘法后转置矩阵:

Transpose code: 转置代码:

 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;
        }

But that makes even worse distorsions. 但这会让人更加沮丧。

EDIT: 编辑:

Here are the raw printed memory contents of the projection matrix: 以下是投影矩阵的原始打印内存内容:

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

Here is the ModelView Matrix (transformation)(The model position = 0, 0, 5): 这是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

Here is M*P : 这是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

Here is P*M : 这是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

The answer belong to @derhass : 答案属于@derhass

"From the numbers given, it looks like M*P is the correct result. However, you store the matrices transposed to what GL (somewhat) defaults to. So either you use vec4 * mat4 in the shader, and it should work, or you have to transpose the result for the GL and can use the default mat4 * vec4 convetion" “根据给出的数字,看起来M * P是正确的结果。但是,你存储转换为GL(有些)默认的矩阵。所以要么你在着色器中使用vec4 * mat4,它应该工作,或者你必须转换GL的结果,并可以使用默认的mat4 * vec4对流“

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM