Having some trouble with my projection matrix.

I’m just trying to create an orthographic projection matrix, but I am having some serious problems here. I do not have access to glOrtho, so please don’t tell me to use that. I am using LWJGL, and the Matrix4f class is column major so I am pretty sure that this code should not be behaving like it is


	float left = 0;
	float right = 1;
	float bottom = 1;
	float top = 0;
	float near = 0f;
	float far = 100f;
		
	projectionMatrix.m00 = 2 / (right - left);
	projectionMatrix.m11 = 2 / (top - bottom);
	projectionMatrix.m22 = -2 / (far - near);
	projectionMatrix.m30 = -((right + left) / (right - left));
	projectionMatrix.m31 = -((top + bottom) / (top - bottom));
	projectionMatrix.m32 = -((far + near) / (far - near));

I can’t upload an image to this post because this account is new, but it draws my quad in the top left of the screen with the bottom right of the quad in the center of the screen.

When I try to change the values so that it is correctly setup to my screen, as shown below, nothing shows up at all.


	float left = 0;
	float right = Engine.windowWidth;
	float bottom = Engine.windowHeight;
	float top = 0;
	float near = 0f;
	float far = 100f;
		
	projectionMatrix.m00 = 2 / (right - left);
	projectionMatrix.m11 = 2 / (top - bottom);
	projectionMatrix.m22 = -2 / (far - near);
	projectionMatrix.m30 = -((right + left) / (right - left));
	projectionMatrix.m31 = -((top + bottom) / (top - bottom));
	projectionMatrix.m32 = -((far + near) / (far - near));

Changing the right and bottom values slowly move the quad off the screen to the left. I have been struggling with this for a while now and I feel like it’s something really stupid that I am missing.

I didn’t figure it out, but I kind of maybe fixed it?


		float left = -(Engine.windowWidth / 2);
		float right = Engine.windowWidth / 2;
		float bottom = Engine.windowHeight / 2;
		float top = -(Engine.windowHeight / 2);
		float near = 0f;
		float far = 1f;

This maps the center to 0, 0, which is not what I want.

You appear to be trying to incorporate the window dimensions into the projection matrix, but that’s handled by the viewport transformation.

After transformation by the projection matrix and division by w, -1,-1 is mapped to x,y and 1,1 to x+width,y+height, where x, y, width and height are the values passed to glViewport().

Also, your code fragments don’t show any settings for the bottom row; projectionMatrix.m33 is set to 1, right?