1
我目前正在尝试使用java和LWJGL 3库的openGL进行编程。我试图实现透视投影矩阵,但在当前状态下,模型不会显示出来。透视投影矩阵不工作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类提供了包含4 * 4矩阵的“elements”数组。该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
你有,你实际调用LWJGL程序代码? –
已编辑,我插入了主要代码 –
FOV是什么单位?你使用它两次来计算'tan()'。一旦你把它转换成弧度,你第二次没有。另外,它看起来像是按照行优先顺序构建投影矩阵。矩阵通常在OpenGL中使用,它们需要按照列主要顺序存储。 –