PDA

View Full Version : glReadPixel :: Occlusion Query

bobGL
06-25-2007, 05:52 PM
Ok I know that glReadPixel is slow and evil BUT I have to use it to make a quick and dirty occlusion query done by CPU... As strange as it sounds to my ears when the object ( the occluder ) distance is below 100 the code below work fine and I can determine if the point is occluded or not... BUT if the distance of the object is greater than 100 the depth < sz ALWAYS return false can someone explain me how I can fix that or point me another direction...

gluProject( _x, _y, _z,
mat_modelview,
mat_projection,
mat_viewport,
_sx, _sy, &amp;sz );

(GLint)*_sy,
1,
1,
GL_DEPTH_COMPONENT,
GL_FLOAT,
&amp;depth );

if( depth < sz )
{ return GL_TRUE; }
else
{ return GL_FALSE; }Cheers,

CrazyButcher
06-25-2007, 11:54 PM
depth normally is not the real depth, but must be calculated using front/backplane values

search the forums here about depth value calculation, I remember some topics about it here

Relic
06-26-2007, 01:59 AM
Nope, above gluProject calculation results in window coordinates and the depth buffer contains window z-coordinates as well.

Since gluProject calculates in doubles, the OpenGL calculates in floats and the number of depth bits are normally 24 or even only 16, you need to expect a precision difference.
Make sure you do not use only 16 bits depth buffer.

If you experience this from a specific distance on, check what the resulting depth values are. I bet they are very near to 1.0 (farthest) and the depth precision there is worst.

There is not much you can do about that. Wrap your zNear and zFar values as closely as posible around your geometry. If you need precise results you might want to try HW occlusion culling instead. In your case you'd just need to render a single GL_POINT without writing into any buffer.
If you don't have occlusion queries in HW you could use the stencil depth pass or fail parameter and the occlusion result would be a value in the stencil buffer you can fetch with glReadPixels using GL_STENCIL_INDEX and GL_UNSIGNED_BYTE.

BTW, depth and stencil often share a 32 bit word and the internal representation is not a float.
It's fastest to read back the depth buffer as GL_UNSIGNED_INT then.