## Depth buffer values

### Depth buffer values

I have a zNear value of 5.0, and a zFar value of 60.0.
When I click on an object in my scene, and call:

I get a value of 0.95 for my selectedThingZ.

What, actually, is that z value given my zNear and zFar (I have loked at the
FAQ, but find nothing on this). Also, what would happen in glOrthoMode?
Thanks, Ralph

### Depth buffer values

> I have a zNear value of 5.0, and a zFar value of 60.0.
> When I click on an object in my scene, and call:

> I get a value of 0.95 for my selectedThingZ.

> What, actually, is that z value given my zNear and zFar (I have loked at the
> FAQ, but find nothing on this). Also, what would happen in glOrthoMode?

The redbook fig. 3-18 shows that depth coord values are in the range of 0->1 and not linear.

I'm no mathematician, if I'm not mistaken the z-buffer value is a number representing the non-linear location in zNear-zFar
space, so I would guess, that .95 squared of the z range + the znear is the actual worldspace location, i.e.:

znear+pow(.95,2)*(zfar-znear)

5  +   .9025  *( 60 -  5  ) or 54.6375?

pow may not be the precise way to convert the z value but it looks about right.

zin

### Depth buffer values

> > I have a zNear value of 5.0, and a zFar value of 60.0.
> > When I click on an object in my scene, and call:

> > I get a value of 0.95 for my selectedThingZ.

> > What, actually, is that z value given my zNear and zFar (I have loked at the
> > FAQ, but find nothing on this). Also, what would happen in glOrthoMode?

>    The redbook fig. 3-18 shows that depth coord values are in the range of 0->1 and not linear.

>    I'm no mathematician, if I'm not mistaken the z-buffer value is a number representing the non-linear location in zNear-zFar
> space, so I would guess, that .95 squared of the z range + the znear is the actual worldspace location, i.e.:

>     znear+pow(.95,2)*(zfar-znear)

>       5  +   .9025  *( 60 -  5  ) or 54.6375?

>    pow may not be the precise way to convert the z value but it looks about right.

The exact maths for doing this are described at the OpenGL SIGGRAPH
courses:

Csar

### Depth buffer values

> > > I have a zNear value of 5.0, and a zFar value of 60.0.
> > > When I click on an object in my scene, and call:

> > > I get a value of 0.95 for my selectedThingZ.

> > > What, actually, is that z value given my zNear and zFar (I have loked at the
> > > FAQ, but find nothing on this). Also, what would happen in glOrthoMode?

> >    The redbook fig. 3-18 shows that depth coord values are in the range of 0->1 and not linear.

> >    I'm no mathematician, if I'm not mistaken the z-buffer value is a number representing the non-linear location in zNear-zFar
> > space, so I would guess, that .95 squared of the z range + the znear is the actual worldspace location, i.e.:

> >     znear+pow(.95,2)*(zfar-znear)

> >       5  +   .9025  *( 60 -  5  ) or 54.6375?

> >    pow may not be the precise way to convert the z value but it looks about right.

> The exact maths for doing this are described at the OpenGL SIGGRAPH
> courses:

Like I said, I'm no mathematician.

I can't get a resaonable value, can you/someone compute the example provided?

zin

### Depth buffer values

Hi George,

Hmmm....I'm thinking, after all this, its not what I need anyhow, for the
third parameter to gluUnProject(). Sorry to give everyone the runaround. I
thought I needed an actual Z value. My problem must reside elsewhere -- and
I was surethis was it.

BTW, do you need this function yourself? I'll be glad to look t it and put
it into code for the group if  anyone really needs it. -Ralph

> > > > I have a zNear value of 5.0, and a zFar value of 60.0.
> > > > When I click on an object in my scene, and call:

> > > > I get a value of 0.95 for my selectedThingZ.

> > > > What, actually, is that z value given my zNear and zFar (I have
loked at the
> > > > FAQ, but find nothing on this). Also, what would happen in
glOrthoMode?

> > >    The redbook fig. 3-18 shows that depth coord values are in the

range of 0->1 and not linear.
Quote:

> > >    I'm no mathematician, if I'm not mistaken the z-buffer value is a

number representing the non-linear location in zNear-zFar
Quote:> > > space, so I would guess, that .95 squared of the z range + the znear

is the actual worldspace location, i.e.:
Quote:

> > >     znear+pow(.95,2)*(zfar-znear)

> > >       5  +   .9025  *( 60 -  5  ) or 54.6375?

> > >    pow may not be the precise way to convert the z value but it looks

> > The exact maths for doing this are described at the OpenGL SIGGRAPH
> > courses:

- Show quoted text -

Quote:

>    Like I said, I'm no mathematician.

>    I can't get a resaonable value, can you/someone compute the example
provided?

> zin

### Depth buffer values

> Hi George,

> Hmmm....I'm thinking, after all this, its not what I need anyhow, for the
> third parameter to gluUnProject(). Sorry to give everyone the runaround. I
> thought I needed an actual Z value. My problem must reside elsewhere -- and
> I was surethis was it.

> BTW, do you need this function yourself? I'll be glad to look t it and put
> it into code for the group if  anyone really needs it. -Ralph

Thanks, I can write the func if I need it, which I don't.   I used project/unproject once.

I would however like to see someone do the calculation on your data, since I got a ridiculous number. (and no point writing a
func if I can't do it on paper first!)

zin

### Depth buffer values

[...]

>    Like I said, I'm no mathematician.

>    I can't get a resaonable value, can you/someone compute the example provided?

Yes: (5.0 * 60.0) / ((60.0 - 5.0)*0.95 -60.0) = -38

Note that for Zwin=0.0, Zeye=-5, and for Zwin=1.0, Zeye=-60

I guess you don't like the negative values :) They're correct! The Zeye
is expected to be negative. However, if you search GoogleGroups, you may
find a different version of this formula, which returns positive values
fix it if the link line gets splitted in several lines):

Csar

### Depth buffer values

Wait a minute Cesar,

That .95 value is coming from clicking with the mouse on an object on the
screen, then performing a
THe .95 is the value returned for selectedThingZ, and SHOULD represent NOT
the Zeye, BUT, the Z value for WHERE on the element I click on is. THats
what I was looking to obtain anyhow. Are you saying here the Eye is 38 back
from this thing? If so, how do I determine then the Z value of the point I
clicked on? Thanks -Ralph

> [...]

Quote:

> >    Like I said, I'm no mathematician.

> >    I can't get a resaonable value, can you/someone compute the example
provided?

> Yes: (5.0 * 60.0) / ((60.0 - 5.0)*0.95 -60.0) = -38

> Note that for Zwin=0.0, Zeye=-5, and for Zwin=1.0, Zeye=-60

> I guess you don't like the negative values :) They're correct! The Zeye
> is expected to be negative. However, if you search GoogleGroups, you may
> find a different version of this formula, which returns positive values
> fix it if the link line gets splitted in several lines):

rnum=1&prev=/groups%3Fas_q%3Dblecua%2520depth%26as_ugroup%3Dcomp.sys.sgi.gra
phics%26hl%3Den

- Show quoted text -

> Csar

### Depth buffer values

> Wait a minute Cesar,

> That .95 value is coming from clicking with the mouse on an object on the
> screen, then performing a
> THe .95 is the value returned for selectedThingZ, and SHOULD represent NOT
> the Zeye, BUT, the Z value for WHERE on the element I click on is. THats
> what I was looking to obtain anyhow. Are you saying here the Eye is 38 back
> from this thing? If so, how do I determine then the Z value of the point I
> clicked on? Thanks -Ralph

Ralph,

(Supposing you don't modify the default values of glDepthRange)

Zwin is in range [0,1]
Zeye is in range [near,far]

I think you said you want this for calling gluUnProject. In that case,
you don't need to care about Zeye, because gluUnProject expects Zwin
values.

Hope this helps,

Csar

### Depth buffer values

> Wait a minute Cesar,

> That .95 value is coming from clicking with the mouse on an object on the
> screen, then performing a
> THe .95 is the value returned for selectedThingZ, and SHOULD represent NOT
> the Zeye, BUT, the Z value for WHERE on the element I click on is. THats
> what I was looking to obtain anyhow. Are you saying here the Eye is 38 back
> from this thing? If so, how do I determine then the Z value of the point I
> clicked on? Thanks -Ralph

Oops! On a second read, I realize you may be misunderstanding the
meaning of Zeye and Zwin. Zeye is NOT the Z of the eye, but the Z of a
fragment in eye coordinates. Zwin is the Z of a fragment in window
coordinates. Both of them define the Z of a pixel, but in a different
coordinate space.

Csar

### Depth buffer values

> > Wait a minute Cesar,

> > That .95 value is coming from clicking with the mouse on an object on the
> > screen, then performing a
> > THe .95 is the value returned for selectedThingZ, and SHOULD represent NOT
> > the Zeye, BUT, the Z value for WHERE on the element I click on is. THats
> > what I was looking to obtain anyhow. Are you saying here the Eye is 38 back
> > from this thing? If so, how do I determine then the Z value of the point I
> > clicked on? Thanks -Ralph

> Oops! On a second read, I realize you may be misunderstanding the
> meaning of Zeye and Zwin. Zeye is NOT the Z of the eye, but the Z of a
> fragment in eye coordinates.

Of course!  I assume the final z in the buffes, i.e. .95.

Quote:> Zwin is the Z of a fragment in window
> coordinates.

World space?

Quote:> Both of them define the Z of a pixel, but in a different
> coordinate space.

I'm still waiting for an answer to the problem given that is more accurate than the one I provided w/ my non-mathematician
formula! (not to say it is correct!)

zin

### Depth buffer values

> [...]

> >    Like I said, I'm no mathematician.

> >    I can't get a resaonable value, can you/someone compute the example provided?

> Yes: (5.0 * 60.0) / ((60.0 - 5.0)*0.95 -60.0) = -38

> Note that for Zwin=0.0, Zeye=-5, and for Zwin=1.0, Zeye=-60

> I guess you don't like the negative values :)

I try to be positive!  38 doesn't sound right w/ a .95 value being so close to 1.0

Quote:> They're correct! The Zeye
> is expected to be negative. However, if you search GoogleGroups, you may
> find a different version of this formula, which returns positive values
> fix it if the link line gets splitted in several lines):

will check it thanks.

zin

### Depth buffer values

Ok, I et it now, thanks. -Ralph

### Depth buffer values

> > [...]

> > >    Like I said, I'm no mathematician.

> > >    I can't get a resaonable value, can you/someone compute the example provided?

> > Yes: (5.0 * 60.0) / ((60.0 - 5.0)*0.95 -60.0) = -38

> > Note that for Zwin=0.0, Zeye=-5, and for Zwin=1.0, Zeye=-60

> > I guess you don't like the negative values :)

>    I try to be positive!  38 doesn't sound right w/ a .95 value being so close to 1.0

Just draw the Zwin/Zeye curve yourself. The endpoints (0.0 and 1.0)
return the expected results (-5 and -60, respectively).

Csar

### Depth buffer values

> > > Wait a minute Cesar,

> > > That .95 value is coming from clicking with the mouse on an object on the
> > > screen, then performing a
> > > THe .95 is the value returned for selectedThingZ, and SHOULD represent NOT
> > > the Zeye, BUT, the Z value for WHERE on the element I click on is. THats
> > > what I was looking to obtain anyhow. Are you saying here the Eye is 38 back
> > > from this thing? If so, how do I determine then the Z value of the point I
> > > clicked on? Thanks -Ralph

> > Oops! On a second read, I realize you may be misunderstanding the
> > meaning of Zeye and Zwin. Zeye is NOT the Z of the eye, but the Z of a
> > fragment in eye coordinates.

>    Of course!  I assume the final z in the buffes, i.e. .95.

> > Zwin is the Z of a fragment in window
> > coordinates.

>    World space?

No, window coordinates (ie: after modelview, projection and viewport
tranformations). I think there's a graph in the red book or the OpenGL
spec which explains all the coordinate systems used across the vertex
transformation pipeline.

Csar

Hello,

I am wondering how to get at the depth buffer.  The reason is I want to
make a stereogram of some renderings I have.  I'd like to be able to
simpily copy the depth buffer into an array of floats and use the array
or write it to a file or something.

Is there a way to do this?

Thanks,
Matt Cohen