When adding a perspective projection matrix to my vertex shader the textured quad is not visible.
shader.vert
#version 400
in vec3 position;
in vec2 textureCoordinate;
out vec3 colour;
out vec2 passTextureCoordinate;
uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
void main() {
gl_Position = projectionMatrix * transformationMatrix * vec4(position, 1.0);
passTextureCoordinate = textureCoordinate;
colour = vec3(position.x+.5f, 0.0, position.y+.5f);
}
When I set the projectionMatrix to identity its rendering fine. Also, when I set it to orthographic projection it renders too.
Creating the perspective projection matrix:
private static final float FOV = 70f;
private static final float NEAR_PLANE = 1.0f;
private static final float FAR_PLANE = 1000.0f;
private void createProjectionMatrix(){
IntBuffer w = BufferUtils.createIntBuffer(4);
IntBuffer h = BufferUtils.createIntBuffer(4);
GLFW.glfwGetWindowSize(WindowManager.getWindow(), w, h);
float width = w.get(0);
float height = h.get(0);
float aspectRatio = width / height;
float yScale = (float) ((1f / Math.tan(Math.toRadians(FOV / 2f))) * aspectRatio);
float xScale = y_scale / aspectRatio;
float frustumLength = FAR_PLANE - NEAR_PLANE;
projectionMatrix = new Matrix4f();
projectionMatrix.m00 = xScale;
projectionMatrix.m11 = yScale;
projectionMatrix.m22 = -((FAR_PLANE + NEAR_PLANE) / frustumLength);
projectionMatrix.m23 = -1;
projectionMatrix.m32 = -((2 * NEAR_PLANE * FAR_PLANE) / frustumLength);
projectionMatrix.m33 = 0;
}
The result matrix:
0.8925925 0.0 0.0 0.0
0.0 1.428148 0.0 0.0
0.0 0.0 -1.002002 -2.002002
0.0 0.0 -1.0 0.0
And this is the code for the orthographic projection matrix.
Orthographic projection matrix:
private void createOrthographicMatrix() {
projectionMatrix = Matrix4f.orthographic(-10f, 10f, -10f * 9f / 16f, 10f * 9f / 16f, -1f, 1f);
}
The result matrix:
0.1 0.0 0.0 0.0
0.0 0.177778 0.0 0.0
0.0 0.0 -1.0 0.0
0.0 0.0 0.0 1.0
I am suspecting that I am missing something in the setup. But have not been able to figure it out.
m00
andm01
both as here – electMatrix4f
? – elect