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?
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.
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.
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?
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 = zsqrt[(4tan^2(phi/w^2)xs^2 + (4tan(theta)/h^2)*ys^2 + 1]
I hope this helps.
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.
Cheers zeckensack I’ve only been using the calculation for God knows how long and I didn’t think of that
[This message has been edited by thelamberto (edited 03-15-2002).]