glOrtho Near-Z and Far-Z max values

Started by Cornix, February 21, 2013, 11:22:32

Previous topic - Next topic

Cornix

Hi,
I am having problems with the values for near-z and far-z with the glOrtho function.

At first i just used some random big number i had in mind and put -10000 and 10000 for near and far z.
It worked.

But then i thought i should much rather use the maximum if possible so I used Float.MIN_VALUE and Float.MAX_VALUE for it.
But now it doesnt work anymore. Somehow quads which are supposed to be drawn "on top" of another are now "behind" it.
As if the depth-buffer doesnt work anymore.

So what are the max values for the near-z and far-z which i can safely use?

GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();
		GL11.glOrtho(0, resolution_width, resolution_height, 0, VIEW_NEAR_Z, VIEW_FAR_Z);

private static final float VIEW_NEAR_Z = -10000;
private static final float VIEW_FAR_Z = 10000;
  => Works.

private static final float VIEW_NEAR_Z = Float.MIN_VALUE;
private static final float VIEW_FAR_Z = Float.MAX_VALUE;
  => Does not work.

abcdef

having a nearZ less than 0 doesn't make sense

nearZ and farZ help form the frustrum in front of you which effectively describes what your computer screen is seeing

Try a nearZ as 0.01 (or something small) so that all objects very close to you are visible on the screen, the farZ should be as far back as you want to see things.

If you incorparate frustrum culling then you don't want farZ too far ahead as you'll be seeing stuff so far a way its not really worth the render time.

Cornix


Fool Running

The reason that you are losing items when using Float.MIN_VALUE and Float.MAX_VALUE is because of the loss of precision. Basically, the depth buffer is only 16 or 24 bits. If you try to make the range span more then that you will lose that precision. However, what abcdef said is mostly correct even in a 2D game. You should use the smallest values you can. In a 2D game you should be able to get away with -1 to 1 since you can control what appears in front of something else just by changing the draw order (and not using the depth buffer). This would also be faster since OpenGL wouldn't have to do depth lookups/writes when drawing.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Cornix

Quote from: Fool Running on February 21, 2013, 13:45:10
The reason that you are losing items when using Float.MIN_VALUE and Float.MAX_VALUE is because of the loss of precision. Basically, the depth buffer is only 16 or 24 bits. If you try to make the range span more then that you will lose that precision. However, what abcdef said is mostly correct even in a 2D game. You should use the smallest values you can. In a 2D game you should be able to get away with -1 to 1 since you can control what appears in front of something else just by changing the draw order (and not using the depth buffer). This would also be faster since OpenGL wouldn't have to do depth lookups/writes when drawing.
But i would have to sort all the sprites. With several hundred sprites which are constantly moving i would probably loose more performance with sorting them by position rather then using the depth buffer, right?
Because the depth buffer is performed by the GPU and therefor lessens the work for the CPU which is already occupied by the game logic.

But thank you for the information, i will definitely keep that in mind.

Edit: I will then use a NEAR_Z of 0 and a FAR_Z just as large as is needed.