I’m trying to get a very accurate world coordinate for a point the user clicks on. I start with the classic calls to glReadPixels and gluUnProject. This method gives me a reasonable world coordinate with a bit of error as expected.
In order to get a more accurate point I though I would then move the near and far clip planes to either side of the point of interest and redraw the scene. This forces all the depth buffer precision in the small area of interest.
I then call glReadPixels and gluUnProject a second time and expect a much more accurate point, but instead I get a world coordinate which is nearly (within 0.01%) the same as with the original method.
Is there a flaw in my understanding of how depth buffer precision works? Is my method invalid?
Thanks for any enlightenment.
Perspective projections can provide very uneven distributions of your depth precision (gets worse with very close near planes). Maybe your second-pass projection just doesn’t change the situation much compared to your view projection.
If you want it to be accurate do it yourself by casting a ray and calculating collision on CPU.
Well I haven’t gotten my technique of moving the clip planes in around the point of interest to work, but I have come up with a couple other ways of getting a more precise world coordinate out of the glReadPixels, gluUnProject method. I’ll share them in case anyone is interested.
The first method is to move the camera along the pick ray toward the point of interest. Re calculate the depth buffer (by redrawing) with the camera very close to the interest point and you will get a Much better result for your world coordinate.
The trick to this method is finding maximum error a point calculated by a depth read can be. Then make sure that you keep your camera outside this maximum error, so you are guaranteed to include the point of interest. Unfortunately in searching around quite a bit I could not find a good way to calculate this error.
The second method is to change your perspective (using glFrustum) so that your field of view is barely around your point of interest. This method is not to difficult and gave me excellent results.