Here is the code I use for occlusion testing of single pixels, your code would be very similar:
Code: // return if a point in world space is occluded by on-screen geometry function PointIsOccluded(p: TVec) : boolean; var viewport: TVector4i; mvmatrix, projmatrix: TMatrix4d; winx, winy, winz: GLdouble; bufferZ: float; begin glGetIntegerv (GL_VIEWPORT, addr(viewport)); glGetDoublev (GL_MODELVIEW_MATRIX, addr(mvmatrix)); glGetDoublev (GL_PROJECTION_MATRIX, addr(projmatrix)); // project world space onto screen gluProject(p.x, p.y, p.z, mvmatrix, projmatrix, viewport, @winx, @winy, @winz); // read back pixel from depth buffer glReadPixels(round(winx), round(winy),1,1,GL_DEPTH_COMPONENT, GL_FLOAT, @bufferZ); result := bufferZ < winZ; end;
The last parameter in glReadPixels is a pointer to an array of your own, which is filled by the GL (in this special case of mine, it's a single float value, since I only read back the depth component of one pixel - more interesting (but not for this topic) would be the rectangular version, which is used to brighten/darken light flares, if a light source is partially occluded).
|