# compute the depth value explicitly

Hi all,
For some reason, I need to compute the depth values in the main memory and upload it into the Z buffer. Could anybody tell me how openGL computes the depth value given the z coordinate (world coordinate). I am using orthogonal projection.

I know this question might have been asked before, but I really couldn’t find what I need.It would be greatly appreciated if anyone could help me with this.

Compute the depth value of what?

It matters because if you want to transform a vertex, then you can use something like gluProject.

If you need a the depth of a certain fragment, then that involves a rasterizer’s algorithm, in which case it depends on primitive, depends on the hw.

Hello,

The formula for Z-buffer depth based on the z-value of a vertex is :

Z_buffer = A * (B - C / Z_vertex)

Where :
A = 2^(N-1), N being the number of bits in your z-buffer (probably 24).
B = F/(F-N),
C = F*N/(F-N),
F = your far clipping plane Z-value
N = your near clipping plane Z-value

It considers that you’re in modelview projection, and I don’t know if it changes anything that you’re in Ortho projection, so you’ll have to figure that one out (or get another reply !)

Hope it helps,

SuperTomate

Thanks for help.

It seems that the above formula will give depth values greater than 1. However, the values read back from Z buffer are all clamped between 0 and 1. What else do I need to know?

Actually, I want to calculate the new depth value of a pixel from its old one assuming that the z-coordinate of the pixel is changed. More specifically, let d be the depth value of a pixel with z = z1. what would be its depth value if z = 2*z1-R where R is a constant?