How To: Create an Orthographic Projection Matrix for OpenGL

This version of the code uses the Matrix4f class from LWJGL Utils, but you can obviously use any 4×4 matrix class you’d like:

To place the origin in the bottom-left corner of the window (positive y-axis pointing upwards) you’d then create a matrix like this:

While to place the origin in the top-left corner (positive y-axis pointing downwards) then just switch the windowHeight and 0.0f to make the line:

As a final note, for 2D graphics sitting on z = 0.0f (you can easily have a 3D orthographic projection – for example in a CAD package) we typically specify the near and far values as -1.0f to 1.0f in a right-handed coordinate system (such as OpenGL) – putting them both as 0.0f can cause issues, so its’ best not to.