z-buffer depth value...

Started by
7 comments, last by RipTorn 21 years, 6 months ago
hey. For quite a while now I''ve been ripping my hair out trying to work this out but I''m having no luck and havn''t been able to dig anything up elsewhere.. What I''d like to know is, given standard z-buffering, on standard 3d hardware, and the following: distance from the cameras plane to a vertex, the near clip plane distance, the far clip plane distance, the projection matrix, view/modelview, etc, then what will be the computed depth value to be stored in the depth buffer for that vertex? I''ve tried many combinations of expressions, some getting pretty huge, but none seem to match the result, or they do, but only for certain values of the near/far clip plane... the closest seem to be along the lines of 1-x/zDist... With x being some constant related to the near and far clip planes... and does this differ depending on api (GL/D3D) - I''m currently assuming it doesn''t... And I''m also ignoring w-buffering, btw. thanks in advance. :-) <-- smile :-)
Project-X
Advertisement
gluProject(vertex.x, vertex.y, vertex.z, modelviewMatrix, projectionMatrix, viewPort, &result.x, &result.y, &result.z);
Use glGetDoublev() to get modelview and projection matices
and glGetIntegerv() to get viewport matrix.
I''ve looked at that in the past,
but according to what I''ve read the returned z value is:

((position*(Modelview*Projection)*viewPortSize).z+1)/2,

not the 0-1 clamped value stored in the depth buffer.. but I''ll still look into it tomorrow. (although I would also need a solution for D3D)

<-- smile :-)

Project-X
gluProject returns z value clamped between 0 and 1. 0 is the near plane and 1 is far plane. x and y are simply the screen position. z is depth.
ok. thanks.

It didn't seem right seeing as if I duplicated the maths behind it (according to pages such as this) I got completly different results. (I'm trying to get a solution that will work for both D3D and GL, and any future renderers, so I really need the maths behind it unfortunatly.)

[edited by - RipTorn on October 23, 2002 9:04:51 PM]
quote:Original post by RipTorn
According to what I''ve read the returned z value is:

((position*(Modelview*Projection)*viewPortSize).z+1)/2,



You are aware, I imagine, that matrix mltiplication is not commutative. Also, OpenGL transforms positions my multiplying the matrix on the left of the position column vector. So the above should read:

z=((projection*modelview)*position).z

This returns a value in [-1,1], at least for points in the view frustum. Now this is scaled/biased into the depth range. If this is [0,1], we do:

z=(z+1)/2

http://users.ox.ac.uk/~univ1234
And ths DOES differ for D3D, where the matrix is multiplied on the right of a row vector. So in D3D we have:

z=(position*(world*view*projection)).z, followed by
z=(z+1)/2

http://users.ox.ac.uk/~univ1234
ok. thats excellent. thank you very much :-)
On second thoughts, I don''t think that z=(z+1)/2 is necessary in D3D.

http://users.ox.ac.uk/~univ1234

This topic is closed to new replies.

Advertisement