PDA

View Full Version : Projection Matrix: Mapping the Z

05-30-2006, 11:48 AM
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?

05-30-2006, 12:50 PM
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.

q

zeoverlord
05-30-2006, 01:09 PM
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.

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

05-30-2006, 02:59 PM
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) :)

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 :)

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 stuffAgain, 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!

05-30-2006, 05:39 PM
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 :D

Thanks.

zeoverlord
05-30-2006, 06:34 PM
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) :) For cliping the rules are the same, but it's a bad idea to draw something on the very edge of the cliping threshold.

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 :) 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.

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?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.

However setting the z-far to 1 in perspective means that you will miss out on a lot of stuffAgain, I'm not sure I'm following you on this. What do my near/far values have to do with the mapping question? 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.

05-30-2006, 07:05 PM
Thanks for that :)

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

qbit
05-30-2006, 08:37 PM