Orthographic matrix in OpenGL 3?

Started by Andrew_3ds, December 22, 2014, 19:35:32

Previous topic - Next topic

Andrew_3ds

I am trying to draw GUI in OpenGL 3, and nothing is appearing. I think the problem is my ortho matrix.

        OrthoMatrix = new Matrix4f();
        OrthoMatrix.setIdentity();
        
        float zNear = 1;
        float zFar = -1;
        float m00 = 2 / (Display.getWidth());
        float m11 = 2 / (Display.getHeight());
        float m22 = 1 / (zFar-zNear);
        float m23 = -zNear / (zFar-zNear);
        float m33 = 1;
        
        OrthoMatrix.m00 = m00;
        OrthoMatrix.m11 = m11;
        OrthoMatrix.m22 = m22;
        OrthoMatrix.m23 = m23;
        OrthoMatrix.m33 = m33;


If this code looks ok, I can put my actual GUI code and see what else I did wrong...

Kai

Have a look at: http://www.songho.ca/opengl/gl_projectionmatrix.html
under the section "Ortographic Projection" you will find the final matrix elements.

There, you will also find that m22 and m23 are calculated instead as:

m22 = -2 / (zFar - zNear);
m23 = -(zFar + zNear)/(zFar - zNear);

Every other matrix element seems to be okay, though.

Also keep in mind that OpenGL uses column-major matrices, whereas you could be using row-major.
So, before you upload your matrix to OpenGL, make sure to transpose to obtain column-major.

Andrew_3ds

Hm... Even after changing it nothing appeared, so I tried OrthoMatrix.transpose, and still nothing is appearing...

Kai

Well, what would I say...
From what you provided, the matrix setting is correct now, however.

Maybe one thing: You realize that your coordinates are now ranging from -width/2 to +width/2 in X and -height/2 to +height/2 in Y, where width = Display.getWidth() and height = Display.getHeight()?

So basically when rendering something now, the vertex coordinates are interpreted as the actual pixel coordinates, with the origin being in the center of the viewport.

Other than that, it could be, that your shader has an error somewhere, or you uploading the mat4 uniform somehow wrongly.

xsupermetroidx


Andrew_3ds

EDIT: Wow, the problem was a very simple fix... I forgot to cast the 2's in the createOrtho method to floats so the most of the matrix was set as 0.0... Thanks for the help guys. One more question though :p.
How do I set the origin to the top-left corner instead of the middle of the screen?


Yes, I have set the viewport. The 3D matrices are working correctly, so I don't know what is wrong...
I know this is a lot of code but here are my classes if someone can try to find out whats wrong. I read them a few times, I don't know what could be wrong with them because they work for 3D, the only difference is the matrix. No need to read VAO or Shader, because they work fine, but if those could be the problem then check them out.

GUIElement.class: http://pastebin.com/wC7nhvsn
VAO.class: http://pastebin.com/M7euijAy
Shader.class: http://pastebin.com/J6fsvPuf
Vertex shader: http://pastebin.com/VmA0LQQh
Frag shader: http://pastebin.com/QS7bjXTK

SHC

I use this matrix for Orthographic projection.



This allows you to set the boundaries. Call the code like

Matrix4 ortho = TransformUtil.createOrthoProjection(0, width, height, 0, 0.01f, 100f);

where the parameters are left, right, bottom, top, near, far and it sets the top left corner as origin. Hope this helps.