i am trying to get the depth values of all my scene in a texture. I copy the z-buffer in my texture, but all values are clamped to [0.0, 1.0] and my far clip plane is at 500. How can i get the real value in my system coordinate space of the depth (ie beetwen 0.1 and 500) ?

thanks.

ChinaShrimp

05-27-2004, 04:17 AM

I also want to know how to solve this problem, anyone can help us?

Aeluned

05-27-2004, 08:40 AM

Why don't you just multiply the z-value in the depth buffer by 500?

-NiCo-

05-27-2004, 02:36 PM

Actually it's not that simple. The multiplication by 500 would be valid if the near plane is at 0.0 which is not the case in most situations.

Thanks a lot,
I found another which is to use the function gluUnProject with the depth value.

vincoof

06-02-2004, 08:06 AM

Nico it's very nice from you to help people, but let me correct some things you wrote. No offense intended. Just trying to make things a bit better.

1)

The multiplication by 500 would be valid if the near plane is at 0.0 which is not the case in most situations.This is true in orthographic view. But in perspective view, near=0 leads to nothing (all depth values get the same result, just like 1-bit depth buffer, which is pointless obviously).

2)
In the matrix, the C and D values are in fact the opposite of what the picture shows, that is :
C = -(far+near)/(far-near)
D = -2*far*near/(far-near)
It leads to negative Z values, so it's common use to use the opposite Z in the end, but at this point it's better to keep Z as negative since it's not "the end" yet (see below).

I found another which is to use the function gluUnProject with the depth valueThis might work pretty well for a single transformation, but if you have to deal with tons of depth computations, you might want to use your own conversion function because gluProject and gluUnproject are not very well optimized (since they take into account the general case for any kind of modelview/projection matrices)