Search code examples
javaopengllwjgl

Replacement for glOrtho in LWJGL?


I'm in the process of moving from legacy OpenGL to modern OpenGL, but I need a replacement for glOrtho, as it is now removed. I can't seem to figure out exactly how to do some of the things done in C++ in Java due to the differences in the imports.

The relevant code: GL11.glOrtho(0, Display.getWidth(), 0, Display.getWidth(), 100, -100); This is the current ortho function I use, along with the old glEnables that are now not used.


Solution

  • The good news is glOrtho (...) is trivial to implement, you do not even need any trigonometric functions like you would for a perspective projection matrix.

    You will want to construct an LWJGL Matrix4f as follows:


    (source: microsoft.com)
    , given:
    (source: microsoft.com)

    Keep in mind that this matrix is column-major, so you would fill it like this:

    Matrix4f.m00 = 2.0f/(right-left);
    Matrix4f.m01 = 0.0f;
    Matrix4f.m02 = 0.0f;
    Matrix4f.m03 = 0.0f;
    
    Matrix4f.m10 = 0.0f;
    Matrix4f.m11 = 2.0f/(top-bottom);
    Matrix4f.m12 = 0.0f;
    Matrix4f.m13 = 0.0f;
    
    Matrix4f.m20 = 0.0f;
    Matrix4f.m21 = 0.0f;
    Matrix4f.m22 = -2.0f/(far-near);
    Matrix4f.m23 = 0.0f;
    
    Matrix4f.m30 = -(right+left)/(right-left);
    Matrix4f.m31 = -(top+bottom)/(top-bottom);
    Matrix4f.m32 =   -(far+near)/(far-near);
    Matrix4f.m33 = 1.0f;