View Full Version : Distance from eyepoint

03-13-2002, 10:08 AM
Hi all,
I'm trying to get the distance from the eyepoint to a pixel in the sceen. I was able to get the pixel's world coordiantes by using gluUnProject. However, to get the distance from the pixel to the eye position, I have to use the distance equation each time I need that value. Is there another, maybe faster way of getting this distance?


03-13-2002, 02:36 PM

You could read the z-buffer value at your mouse coordinate if you are using z-buffer. But if it is faster I don't know.


03-13-2002, 03:26 PM
Maybe you can get away with not taking the sqrt(that is x^2+y^2)if you need the distance to compare it with the distance of another pixel.Other than that,the only thing I can think of is using a quick sqrt calculation routine.I've never tried any of them though and I think you should put that off until the optimization step.

03-13-2002, 06:07 PM
Guys, thanks. One question, how do you get the z-buffer? I thought I was doing that? If I use glReadPixel, the value I get is clamped between [0,1]. Is that the z-buffer?

Thanks again,

03-14-2002, 02:18 AM
The colour buffer that you are reading from with glReadPixels is set using glReadBuffer.

By specifying the format of glReadPixels to be GL_DEPTH_COMPONENT, depth values are read from the depth (z) buffer. This is converted to floating point in the range [0,1], with 0 on the near clipping plane and 1 on the far clipping plane. The components are scaled with GL_DEPTH_SCALE and added to GL_DEPTH_BIAS before claping to [0,1]. So if you unscale the result you have the depth of the plane of that point, z.

The range, as I'm sure you know, to a point is
range = sqrt(x^2 + y^2 + z^2)

If you have VFOV of 2*theta and a HFOV of 2*phi and the image is of width, w, and height, h. So if your pixels is at coordinates xs and ys then the range is

range = z*sqrt[(4*tan^2(phi/w^2)*xs^2 + (4*tan(theta)/h^2)*ys^2 + 1]

I hope this helps.

03-14-2002, 03:29 AM
You can also read depth values as GL_UNSIGNED_SHORT or GL_UNSIGNED_INT. If you match this up with your depth buffer depth (haha) it'll be mucho faster than reading floats.

03-14-2002, 11:22 PM
Cheers zeckensack I've only been using the calculation for God knows how long and I didn't think of that http://www.opengl.org/discussion_boards/ubb/biggrin.gif

[This message has been edited by thelamberto (edited 03-15-2002).]