简体   繁体   中英

Change the values of OpenGL's z-buffer

I want to pass a matrix with depth values into the z-buffer of openGL. Somewhere I found that I can use glDrawPixels(640,480,GL_DEPTH_COMPONENT,GL_FLOAT,normalizedMappedDepthMat.ptr());

where mat is an opencv Mat.Is it possible to change the z-buffer values in OpenGL using a texture binding?If so,how?

With the programmable pipeline, you can write to gl_FragCoord in the fragment shader, effectively setting a per-pixel z value. With that feature, you can implement a render-to-depth-buffer feature quite easily by rendering a full-screen quad (or something else, if you want to overwrite just a sub-region of the whole buffer). With reasonably modern GL, you will be able to use single-channle texture formats with enough precision like GL_R32F. With older GL versions, you can manually combine the RGB or RGBA channels of standard 8Bit textures to 24 or 32 bit values.

However, there are some little details you have to take into account. Writing to the depth buffer only occurs if the GL_DEPTH_TEST is enabled. This of course might discard some of your fragments (if the depth-buffer is not cleared before). One way to get around this is setting glDepthFunc() to GL_ALWAYS during your depth-buffer rendering.

You must also keep in mind that rendering writes to all buffers, not just the depth buffer. If you don't want to modify the color buffer, you can set glDrawBuffer() to GL_NONE , or can use glColorMask() to prevent overwriting it. If you use a stencil buffer, you should also disable or mask out writing to it, of course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM