How can I read the depth buffer

I want to read the Z Buffer (depth buffer) at each time tick.  Is there a way to get at this information?  This depth information would be useful for robot simulations.




There's no need to do all that. If you know which point(s) you want to read the Z information from, you can render the scene with a scissor rectangle that contains the point(s), then use ReadPixels on the points to read the Z data. Alternatively you can process the Z information within a pixel shader and then submit the results in a texture, that way you save the ReadPixels call and also get a speed boost from GPU's parallel computation.

Hehe yeah, my goal was to achieve it all with "pure" jME, partly out of academic interest, and partly (the bigger part) because I don't speak OpenGL very well. But hey, I never suggested my solution was a good one :slight_smile:

Re: your two approaches, Momoko_Fan:

I think I am roughly doing approach #1, but taking the detour via the fullscreen quad. I can see how not doing that and doing readPixels directly on the depth buffer will help performance. :smiley:

About approach #2 (use a pixel shader), how do I "submit" data to a Texture so that it can be read from my app? A keyword, just as "readPixels" above, will do for an answer…

thanks for the ideas.  What is a scissor rectangle, and how can I call ReadPixels in jME? 

Thanks again!


This won’t work in pure jME.

In order to use the methods I described, you need to use OpenGL commands, for that you need to import the LWJGL library used by jME into your classpath.

Once you imported LWJGL, you can use opengl calls by using one of the static GL classes (GL11, GL12, etc)

glReadPixels is used to read pixels from the framebuffer into system memory, and scissor rectangle is used to limit the region to which fragments are rasterized.