OpenGL How does OpenGL create its depth values?

This topic is 3770 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi. I was under the impression, from reading some articles/books, that OpenGL calculated its depth values like this: (let's look at a vertex, for simplicity)
vec4 clipspaceVertex = projectionMatrix * modelViewMatrix * gl_Vertex;
float depth = clipspaceVertex.z / clipspaceVertex.w * 0.5 + 0.5;

I.e. calculate the clip-space coordinate, perform the perspective division (which makes -1 <= z <= 1), scale and bias the z-value (so that 0 <= z <= 1) and use this final z-value as the depth. But when i try this, the depth value doesn't seem right. I'm probably missing something. How do i calculate the depth-value? (given a point in clipspace) Thanx for your help! (yeah, i know i could get the depth of a fragment with gl_FragCoord.z in a fragment program, but i'm not actually interested in the depth from the cameras point of view, but rather from the point of view of a lightsource)

Share on other sites
Thanx for the links. Just to follow up: (in case it can help someone else)

I tried looking at the perspective matrix from DirectX (see [1], if you have the book). After the matrix multiplication and the division by w (done on paper), i got the same expression for the depth as in "Learning to love your Z buffer". (it seems like he assumes that w=1 before the multiplication with the projection matrix - which is ok by me) (and that he assumes DirectX - not ok by me)

Looking at the OpenGL-matrix (again, see [1]), i of course get another expression for the depth. If i plug in z=znear into the expression, i get depth = -1 and if i plug in z=zfar i get depth = 1. (for DirectX i get 0 and 1)

So it seems that DirectX has clipcoords in the range: (for points not outside the frustum)
(x,y,z,1) = [-1;1]x[-1;1]x[0;1]x{1}
while OpenGL has clipcoords in the range: (for points not outside the frustum)
(x,y,z,1) = [-1;1]x[-1;1]x[-1;1]x{1}

But depth values are in [0;1], so in OpenGL the clip z-coord has to be scaled/biased to get into [0;1] instead of [-1;1] i guess (as i wrote in the first post).

So it seems to me that my way of calculating the depth in the first post was correct. Or?
(probably something else is wrong in my code)

[1] Möller and Haines: "Real-time rendering", page 65.

Share on other sites
Quote:
 But depth values are in [0;1], so in OpenGL the clip z-coord has to be scaled/biased to get into [0;1] instead of [-1;1] i guess (as i wrote in the first post).

Yes that is correct. glDepthRange controls that and by default it is set as glDepthRange(0.0, 1.0)
This is a function that is not available in D3D.

Share on other sites
typedef struct D3DVIEWPORT9 {
DWORD X;
DWORD Y;
DWORD Width;
DWORD Height;
float MinZ;
float MaxZ;
} D3DVIEWPORT9, *LPD3DVIEWPORT9;

I'm not an expert in Direct3D (mostly use OpenGL), but I believe the MinZ and MaxZ are the values that set the depth range in D3D.

Jeroen

Share on other sites
Thanks, it's always good to know.

Share on other sites
Some corrections:

In OpenGL for the depth values there are 2 co-ordinates systems:
1) normalized device co-ordinates, this is _ALWAYS_ -1 to 1.
2) window co-ordinates, this is _ALWAYS_ 0 to 1.

now, glDepthRange(near,far) sets the transformation from device co-ordinates to window co-ordinates, as the linear mapping so that T(-1)=near and T(1)=far, thus glDepthRange(0,1) uses the full range of the depth buffer, and glDepthRange(0,0.5) would only use half of it's prescision (since -1-->0 and 1-->0.5) The actual stored value is the window co-ordinate, typically as a 24bit fixed point value.

on a unix system type man glDepthRange to see more.

Share on other sites
OpenGL spec is your friend. Check out Section 2.11 in glspec15:
zw = [(f-n)/2]zd + (n+f)/2 , where
n and f are by default 0 and 1, so
zw = 0.5 * zd + 0.5, where zd = zc/wc
zw is then converted to the integer range of the depth buffer.

Seeing that you got the transformation right, how exactly don't the values seem right?

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 30
• 13
• 11
• 11
• Forum Statistics

• Total Topics
631780
• Total Posts
3002314
×