My orthographic matrix doesn’t seem to be properly translating/scaling the pixels on my screen.
Here is how it is built:
public static Matrix4f orthographicMatrix(float near, float far)
{
float width = Display.getWidth();
float height = Display.getHeight();
return orthographicMatrix(width, height, near, far);
}
public static Matrix4f orthographicMatrix(float width, float height, float near, float far)
{
return orthographicMatrix(0, width, height, 0, near, far);
}
public static Matrix4f orthographicMatrix(float left, float right, float bottom, float top, float near, float far)
{
Matrix4f mat = new Matrix4f();
mat.m00 = 2 / (right - left);
mat.m11 = 2 / (top - bottom);
mat.m22 = -2 / (far - near);
mat.m03 = -1 * ((right + left) / (right - left));
mat.m13 = -1 * ((top + bottom) / (top - bottom));
mat.m23 = -1 * ((far + near) / (far - near));
return mat;
}
Here’s where I create a new one:
ortho = Matrix4f.orthographicMatrix(1, -1);
Here’s how it’s sent to the vertex shader:
program.setUniformMatrix4("ortho", ortho);
And here’s the vertex shader:
# version 330
uniform mat4 ortho;
in vec3 vert;
void main()
{
gl_Position = ortho * vec4(vert, 1);
}
When I pass in 2 vectors (0, 0, 0) and (100f, 100f, 0) and draw them as points, this is what I get:
The screen size for that is 800x600. It seems that the x and y directions reversed as intended, but the origin is still in the center. I can’t find what it is that I missed though for that too happen.
The calculated matrix is the same as one that I calculated by hand. So, I feel that I have the wrong expressions to compute the orthographic matrix. Though, I’ve verified from several locations that this is how you calculate it.