Hi! I try to increment the stencil buffer value each time a fragment is drawn so I do like this :
glCheck(glStencilFunc(GL_ALWAYS, 0, 0xFFFF));
glCheck(glStencilOp(GL_INCR, GL_INCR, GL_INCR));
depthBuffer.create(resolution.x, resolution.y,settings);
settings.stencilBits = 16;
stencilBuffer.create(resolution.x, resolution.y, settings, true, true);
But, when I use the stencil texture in the fragment shader it doesn’t work.
const std::string addDstColorFragShader =
"#version 130
"
"in mat4 projMat;"
"uniform vec3 resolution;"
"uniform sampler2D frontBuffer;"
"uniform sampler2D depthBuffer;"
"uniform sampler2D stencilBuffer;"
"uniform sampler2D texture;"
"uniform float haveTexture;"
"void main() {"
"vec2 position = (gl_FragCoord.xy / resolution.xy);"
"vec4 front_color = texture2D(frontBuffer, position);"
"float max_z = texture2D(depthBuffer, position).z;"
"float nbLayers = texture2D(stencilBuffer, position).a;"
"vec4 texel = texture2D(texture, gl_TexCoord[0].xy);"
"float z = (gl_FragCoord.w != 1.f) ? (inverse(projMat) * vec4(0, 0, 0, gl_FragCoord.w)).w : gl_FragCoord.z;"
"vec4 colors[2];"
"colors[1] = texel * gl_Color;"
"colors[0] = gl_Color;"
"bool b = (haveTexture > 0.9);"
"vec4 color = colors[int(b)];"
"if (z >= max_z && nbLayers > 0.1f && nbLayers <= 1) {"
" gl_FragColor = front_color;"
"} else if (z >= max_z && nbLayers > 1) {"
" gl_FragColor = vec4(0, 0, 0, 0);"
"} else if (z < max_z) {"
" gl_FragColor = vec4(front_color.rgb * front_color.a, front_color.a);"
"}"
"}";
NbLayers is greater than 1 even if they are multiple layers. -
Why ?