perspection投影矩阵不工作openGL

时间:2015-10-27 10:08:13

标签: java math matrix lwjgl

我目前正在尝试使用java和LWJGL 3库使用openGL进行编程。我试图实现透视投影矩阵,但是在当前状态下,模型不会出现。

public static Matrix4f pespectiveProjectionMatrix(float screenWidth, float screenHeight, float FOV, float near, float far) {
    Matrix4f result = identity();

    float aspectRatio = screenWidth / screenHeight;

    result.elements[0 + 0 * 4] = (float) ((1 / tan(toRadians(FOV / 2))) / aspectRatio);
    result.elements[1 + 1 * 4] = (float) (1 / tan(FOV / 2));
    result.elements[2 + 2 * 4] = -(far + near) / (far - near);
    result.elements[2 + 3 * 4] = -1;
    result.elements[3 + 2 * 4] = -(2 * far * near) / (far - near);
    result.elements[3 + 3 * 4] = 0;

    return result;
}

Matrix4f类提供了一个“elements”数组,它包含一个4 * 4矩阵.identity()方法返回一个简单的单位矩阵。

这是当前矩阵的样子:

0.75     |0.0      |0.0       |0.0      |
0.0      |0.6173696|0.0       |0.0      |
0.0      |0.0      |-1.0001999|-1.0     |
0.0      |0.0      |-0.20002  |0.0      |

顶点着色器:

#version 400 core

in vec3 position;
in vec2 textureCoords;

out vec2 pass_textureCoords;

uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;

void main(void) {

    gl_Position = projectionMatrix * transformationMatrix * vec4(position, 1.0);
    pass_textureCoords = textureCoords;

}

渲染:

Matrix4f projectionMatrix = Matrix4f.pespectiveProjectionMatrix(800.0f, 600.0f, 90.0f, 0.1f, 1000.0f); //Creates the projection matrix (screenWidth, screenHeight, FOV, near cutting plane, far cutting plane)

shader.loadTransformationMatrix(transformationMatrix); //loads the transformationMatrix
shader.loadProjectionMatrix(projectionMatrix); //Load the projection matrix in a uniform variable

1 个答案:

答案 0 :(得分:0)

我的问题是,我忘了在第二行将FOV转换为弧度:

result.elements[1 + 1 * 4] = (float) (1 / tan(FOV / 2));

应该是

result.elements[1 + 1 * 4] = (float) (1 / tan(toRadians(FOV / 2)));