0

I'm saving the depth value via fragmend shader in the color buffer of the main framebuffer (in 'opengl es' there is no way to read the depth buffer via glReadPixels()). Additionally I need the maximum precision of 32-bit, that's why I use the rgba of the colorbuffer. But I realized that using also the alpha channel the results are not always correct, maybe because of some post fragmend operations. The following works:

  • set fragColor.a = 1 (but then I'm loosing one channel) or
  • set gl_FragDepth = 0.0 (works fine also with alpha != 1)

fragShader

void main(void) {
    gl_FragDepth = 0.0;//gl_FragCoord.z;
}

But why does this work if the depth value in depth buffer is always 0.0?

PowerNow
  • 363
  • 1
  • 2
  • 15
  • Depth test is enabled, Blending is disabled. – PowerNow May 11 '18 at 10:03
  • I tried this methode because of https://stackoverflow.com/questions/19199706/opengl-dont-write-depth-if-alpha – PowerNow May 11 '18 at 10:06
  • Yes I clear the depthbuffer. But I just saw also very rar some deviations, It seems as sometimes it takes the depth from the point behind the mountain of my terrain model. But I just decided to use first methode (alpha = 1.0). I use now a linear depth and 24bit is sufficient. But here for me it's also not clear why i must set alpha=1.0. Are there otherwise some post fragmend operations? – PowerNow May 11 '18 at 10:21
  • Post a complete sample please - there is something odd going on in your setup - and it's not entirely clear why from the tiny piece of code you've posted. – solidpixel May 11 '18 at 20:00
  • @solidpixel; I think setting gl_FragDepth = 0.0 is nonsens. Setting fragColor.a = 1 works. The problem is originally "only" reading the depth buffer on opengl es. – PowerNow May 14 '18 at 15:49
  • not sure about GL-ES but in standard GL `glReadPixels` can read the depth buffer too ... [depth buffer got by glReadPixels is always 1](https://stackoverflow.com/a/51130948/2521214). If you want still to use render to texture use non clamped format or disable clamping see [Access to 3D array in fragment shader](https://stackoverflow.com/a/56072875/2521214) – Spektre Jun 21 '19 at 23:15

0 Answers0