Side tip: Copying a Framebuffer to a Window GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER) įprintf(stderr, "glCheckFramebufferStatus: %x\n", status) GlFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth, 0) GlFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0) GlTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT32F, width, height) GlTexStorage2D(GL_TEXTURE_2D, 1, GL_SRGB8_ALPHA8, width, height) You can follow the FBO setup code below as an example:
That means you should be using a floating point depth buffer. The whole Reversed-Z thing is designed for floating point depth, not fixed point. Step Two: Create a Floating Point Depth Buffer SDL_GL_ExtensionSupported("GL_ARB_clip_control"))įprintf(stderr, "glClipControl required, sorry.\n")
Therefore, you could use something like the following code snippet: If you don’t have OpenGL 4.5, it might still be available as an extension (see: hardware supporting GL_ARB_clip_control). Anyways, when you sample from a depth texture in OpenGL, you already get a value between 0 and 1… so switching to the convention for clip coordinates will make everything more consistent. It’s a good decision even if you’re not using Reversed-Z. Give up entirely on the convention, it’s hands-down objectively worse than the convention when it comes to precision. I recommend sticking this line of code at the start of your program, and never changing the clip conventions after that. GlClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE) OpenGL’s default convention is, but you can override that using glClipControl: Reversed-Z is designed for clip-space Z values in the range, not.
So, how to use it in OpenGL? Here’s a no-nonsense step-by-step guide. Reversed-Z visualized: Near objects have a higher Z value than far objects.