Projection Matrix: Mapping the Z

Do I loose z precision if I set create my ortho/perspective projection matrix to map z into [0,1] instead of [-1,1]? Will this hurt my zbuffer?

I’ll turn the question on its head: Why does OpenGL map the z into [-1,1] instead of [0,1]? For symmetry with x and y? Or is there a numerical advantage to this?

I’m trying to get a better understanding of how all this stuff works.

Thanks for any comments/speculation :slight_smile:

q

Originally posted by <qbit>:
I’ll turn the question on its head: Why does OpenGL map the z into [-1,1] instead of [0,1]? For symmetry with x and y? Or is there a numerical advantage to this?
q

I think it has something to do with 2d ortho mode.
If z near is 0 then if you draw polys at z=0 then the z-near culling might be undefined, it should draw it, but you never know how the round is going to turn out, those driver coders are sneaky bastards.
so (-1,1) is a better choice.

on to your original question.
No, it will not hurt your precision in the z buffer, infact it will increase it.
But it doesn’t matter, because with a 24 bit z buffer you have 16777216 possible z levels and in ortho mode that is 16777215 to many.
However setting the z-far to 1 in perspective means that you will miss out on a lot of stuff

If z near is 0 then if you draw polys at z=0 then the z-near culling might be undefined, it should draw it, but you never know how the round is going to turn out, those driver coders are sneaky bastards.
so (-1,1) is a better choice.
yeah, I thought of this also. But shouldn’t [0, 1] or [-1, 1] map to the frustum [near, far]? I don’t see how it’s a culling issue. For clipping, it would seem to me that the rules are the same, regardless of the range (give or take a sneaky bastard) :slight_smile:

No, it will not hurt your precision in the z buffer, infact it will increase it.
Could you please explain why this is the case? I’m inclined to agree, but my numerical analysis is a bit rusty :slight_smile:

But it doesn’t matter, because with a 24 bit z buffer you have 16777216 possible z levels and in ortho mode that is 16777215 to many.
I don’t understand the last part of this sentence. What do you mean that it’s too many?

However setting the z-far to 1 in perspective means that you will miss out on a lot of stuff
Again, I’m not sure I’m following you on this. What do my near/far values have to do with the mapping question?

Thanks a lot for your help!

Just to clarify the question again, I’m not talking about the “love your zbuffer” stuff. I’m well aware of the issues surrounding poorly chosen near/far values and the limits of a 24bit zbuffer.

What I’m asking is whether there’s a numerical difference in choosing a matrix that maps eye space z to [0,1] instead of [-1,1], and if so, why? (Recall that by default OpenGL maps the [-1,1] back to [0,1], anyway.)

If anyone has some insight on this I would be very thankful. Like, it’s kinda freaking me out :smiley:

Thanks.

[quote]If z near is 0 then if you draw polys at z=0 then the z-near culling might be undefined, it should draw it, but you never know how the round is going to turn out, those driver coders are sneaky bastards.
so (-1,1) is a better choice.
yeah, I thought of this also. But shouldn’t (0, 1) or (-1, 1) map to the frustum [near, far]? I don’t see how it’s a culling issue. For clipping, it would seem to me that the rules are the same, regardless of the range (give or take a sneaky bastard) :slight_smile: [/QUOTE]For cliping the rules are the same, but it’s a bad idea to draw something on the very edge of the cliping threshold.

[quote]No, it will not hurt your precision in the z buffer, infact it will increase it.
Could you please explain why this is the case? I’m inclined to agree, but my numerical analysis is a bit rusty :slight_smile: [/QUOTE]Because the precision depends on the zfar/znear range, and because you rarely draw anything in the space with z less than 0 and z-precision is logaritmic(i think) meaning that the if you have zfar/znear (-1,1) the space higher than 0 has less z levels than the one with less than 0.
But if you have (0,1) you will have all z levels infront of the “camera”.
But this only matters if you are drawing a lot of z dependant objects in perspective mode and you have a astronomically high z-far, but the visual quality difference between (0,1) and (-1,1) is undetectable by the human eye.

[quote]But it doesn’t matter, because with a 24 bit z buffer you have 16777216 possible z levels and in ortho mode that is 16777215 to many.
I don’t understand the last part of this sentence. What do you mean that it’s too many?[/QUOTE]Normaly when you draw in ortho mode you don’t need to use the z buffer, or at least not a lot of it anyway.
But i guess that depends on what you are drawing.

[quote]However setting the z-far to 1 in perspective means that you will miss out on a lot of stuff
Again, I’m not sure I’m following you on this. What do my near/far values have to do with the mapping question? [/QUOTE]znear and zfar has a lot of things to do with your mapping question if you are using prespective mode that is.
In ortho mode it’s less important.

Thanks for that :slight_smile:

Should I move this to the advanced forum? Seems like an interesting discussion, and there’s clearly some doubt in this area.

Moderators: My appologies for starting new thread in advanced forum.

Big thanks to zeoverlord for help!

Edit: This turned out to be a viewport transformation problem:
Zw = (f - n) / 2 * Zd + (n + f) / 2.
So with Zd in [0, 1] the best you can have is Zw in [1/2, 1].