Background is that I want to do kind of a “soft blending” effect for light coronas, avoiding sharp edges where they hit the geometry by blending them with pixels in front of them, using the distance of a corona pixel to the pixel hiding it as an color scale (additive blending intended).
This stuff here doesn’t work, but I cannot tell why.
glGenTextures (1, &hDepthBuffer);
glBindTexture (GL_TEXTURE_2D, hDepthBuffer);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
glTexParameteri (GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_ALPHA);
glTexImage2D (GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, <width>, <height>, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glCopyTexImage2D (GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, <width>, <height>, 0);
Here is the shader, screen res (800x600) and z-near, far (1.0, 6553.5) hard coded. The current back buffer has also been copied to a texture and is passed to the shader in renderTex. Is there a way to directly access the back buffer (draw buffer) in the fragment shader?
uniform sampler2D glareTex;
uniform sampler2D renderTex;
uniform sampler2D depthTex;
vec2 depthScale = vec2 (6553.5 / 6552.5, 6553.5 / -6552.5);
void main (void)
{
vec2 scrCoord = vec2 (1.0 / 800.0, 1.0 / 600.0) * gl_FragCoord.xy;
vec4 glareColor = texture2D (glareTex, gl_TexCoord [0].xy);
vec4 renderColor = texture2D (renderTex, scrCoord);
float depthZ = depthScale.y / (depthScale.x - texture2D (depthTex, scrCoord).a);
float fragZ = gl_FragCoord.z; /*depthScale.y / (depthScale.x - gl_FragCoord.z);*/
if (fragZ < depthZ) glareColor /= depthZ - fragZ;
gl_FragColor = renderColor + glareColor * gl_Color;
}