Solved: Incorrect values while using out variables

Hi,

(This post should be named: “HowTo: correct DepthBuffer, but incorrect ColorBuffer!”)

While writing this post, I discovered my error. Maybe I will help someone by this post.

I want to draw random strokes. All strokes should look like they have been drawn on top of each other, but the depth buffer should provide the depth value of the nearest fragment.

When no depth test is used, the resulting color buffer is exactly what I expected, but no depth informations are created. By switching the depth test on, I get a correct depth buffer with incorrect drawn strokes. (I use transparent Strokes, so the result isn’t pretty.) Changing the glDepthFunc() to GL_ALLWAYS, provides me with both, but the depth buffer is still incorrect.

I try another solution involving a fragment shader. Adding the current depth buffer as texture didn’t solved the problem. I only get 0s or 1s as a result. I really could figure this out.

Fragment shader code:


#version 150

uniform sampler2DShadow depths; 

out vec4 fragColor;

void main (void)  
{
  vec3 texCoord = vec3(gl_FragCoord.xy / textureSize(depths, 0), 1);
  float depth = texture(depths, texCoord);
  
  if(depth < gl_FragCoord.z){
    gl_FragDepth = depth;
  }

  fragColor = ...;
}

With a quick search in the GLSL Spec 1.5 I found out, that gl_FragDepth is set by the fixed function pipeline. By introducing a static assignment to gl_DepthFrag, gl_FragDepth will not be set by the fixed function pipeline in any way.
This means, you have to guarantee that every possible code path sets gl_FragDepth.

The code has been modified accordingly:


  if(depth < gl_FragCoord.z){
    gl_FragDepth = depth;
  } else {
    gl_FragDepth = gl_FragCoord.z;
  }

glDepthFunc() has changed to GL_LEQUAL.

Note, by introducing the current depth buffer as a texture to the fragment shader, the OpenGL spec states that looked up texture value are undefined.


Greetings,
Preafos

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.