gpt4 book ai didi

java - openGL透视投影矩阵不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 13:18:00 25 4
gpt4 key购买 nike

我目前正在尝试使用 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

最佳答案

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

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

应该是

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

关于java - openGL透视投影矩阵不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33364996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com