Reputation: 25
I'm using Java and JOGL to do some 3D rendering stuff. I generate my view and projection matrices with the JOML library:
Matrix4f view = new Matrix4f()
.lookAt(new Vector3f(1.0f, 0.0f, 0.0f),
new Vector3f(0.0f, 0.0f, 0.0f),
new Vector3f(0.0f, 1.0f, 0.0f));
Matrix4f proj = new Matrix4f()
.ortho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
I'm sending these matrices to the shaders like this:
gl.glUniformMatrix4fv(viewTransformLocation, 1, false, viewTransformMatrix, 0);
gl.glUniformMatrix4fv(projectionTransformLocation, 1, false, projectionTransformMatrix, 0);
The matrices are not transposed during the process.
After filling my VAO and the VBOs, i simply use the following state to render the mesh:
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glEnable(GL.GL_CULL_FACE);
gl.glCullFace(GL.GL_BACK);
gl.glEnable(GL.GL_DEPTH_TEST);
...
The generated image is quite strange, it looks like the depth buffer is reversed. After I tried to reverse the mapping, it worked like a charm.
gl.glDepthRange(1,0);
Can anyone tell me why is this happening? I wrote a similar program in C++, using the glm library and the same settings (except the reverse mapping, of course), the problem didnt show up.
Upvotes: 1
Views: 1771
Reputation:
You are probably experiencing that surfaces with a higher Z coordinate (towards +inf) hide surfaces with a small Z coordinate (towards -inf).
This is correct behaviour if you use a negative value for zMin and a positive one for zMax. Look at http://www.songho.ca/opengl/gl_projectionmatrix.html#ortho. In the section "Orthographic Projection" you see the first image, where the +Z vector points "out of the screen" towards the viewer. This is how your Z coordinates will also be interpreted. Bigger values are nearer to the viewer than smaller ones.
Things change when you swap the sign of zNear and zFar, making zNear positive and zFar negative. Then, your +Z points "into the screen" and away from the viewer. There, smaller Z values (towards -inf) of your surfaces would be nearer to the viewer.
I did a test with JOML, with GL11.glOrtho as well as GLU.gluOrtho2D. All three show the same behaviour.
Upvotes: 2
Reputation: 22707
It seems odd that you're passing -1 for the zNear
parameter in your ortho
call. Normally both near and far would be positive.
OK, it is legal to pass these values to glOrtho
, but maybe it is not doing what you expect. The far plane is 1 unit ahead of the camera, and the near plane is 1 unit behind. But since the camera looks in the -z
direction the far plane would be at z = -1
, and the near plane would be at z=1
.
Upvotes: 2