read back depth buffer


I’m unable to find a method to read back a single depth value, given a 2D screen coordinate. Just blind? Or is there no such a method.


Attach a render-texture to bitplane RTPDepthStencil of with RTMCopyRam - this will copy the texture from GPU to system RAM every frame (slow) allowing you to store that texture into a PNMImage and reading the values.

Something like: (untested)

texDepth = Texture(), GraphicsOutput.RTMCopyRam, GraphicsOutput.RTPDepthStencil)
img = PNMImage()

# Then, when you want to read it out:

Thanks, but that is a bit overkill. One pixel would be enough (more glReadPixel style). Based on the scene, it sometimes possible to avoid an expensive (glReadPixel has its own drawbacks, I know) collision detection and just (un-)project the mouse coordinates and the depth value back into 3D space.

I’m not sure that glReadPixel would be any faster than the proposed solution, actually. But if you really want it, you could try using the new OpenGL callback mechanism.


if i remember right glReadPixel is not be implemented in hardware at all but hacked in there for compatibility sake. I think its suuuper slow on modern cards.

Yeah, glReadPixel will not be too much faster than the code I showed.
First google result for glReadPixel: … _id=473794

It’s probably better to only sample the center pixel and sending that to CPU. Or, a separate 1px*1px render.