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中使用,它們需要按照列主要順序存儲。 –