So I have a fluid simulation and I'm trying to apply a bilateral filter in order to blur the surface while preserving the edges. My approach is based on this presentation (slide 26 in particular) http://developer.download.nvidia.com/presentations/2010/gdc/Direct3D_Effects.pdf
The problem I'm having is that my edges aren't preserved at all. Even if I render out a single particle, it is being blurred, and as you can see in the image below the blur is incorrect.
Also, as detailed in the pdf, while it is not normally acceptable to separate a bilateral filter, in the case of fluids it is reasonable to do so as there is only minor artifacting. That is the reason for the two passes below, but this should not cause the edges to be blurred.
Blur Shader:
#version 400
in vec2 coord;
uniform sampler2D depthMap;
uniform vec2 screenSize;
uniform mat4 projection;
uniform vec2 blurDir;
const float filterRadius = 10;
const float blurScale = .1;
const float blurDepthFalloff = 1000;
void main() {
float depth = texture(depthMap, coord).x;
float sum = 0.0f;
float wsum = 0.0f;
for (float x = -filterRadius; x <= filterRadius; x += 1.0f) {
float s = texture(depthMap, coord + x*blurDir).x;
float r = x * blurScale;
float w = exp(-r*r);
float r2 = (s - depth) * blurDepthFalloff;
float g = exp(-r2*r2);
sum += s * w * g;
wsum += w * g;
}
if (wsum > 0.0f) {
sum /= wsum;
}
gl_FragDepth = sum;
}
Drawing code:
//--------------------Particle Blur-------------------------
{
glUseProgram(blurShader.program);
//Vertical blur
glBindFramebuffer(GL_FRAMEBUFFER, blurShader.fboV);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glClear(GL_DEPTH_BUFFER_BIT);
blurShader.blurDepthVAO();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, depthShader.tex);
glUniform1i(blurShader.depthMap, 0);
RenderUtility.setVector2(blurShader, screenSize, "screenSize");
RenderUtility.setMatrix(blurShader, projection, "projection");
RenderUtility.setVector2(blurShader, new Vector2(0.0f, 1.0f / screenSize.y), "blurDir");
glEnable(GL_DEPTH_TEST);
glBindVertexArray(blurShader.vao);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
//Horizontal Blur
glBindFramebuffer(GL_FRAMEBUFFER, blurShader.fboH);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glClear(GL_DEPTH_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, blurShader.texV);
glUniform1i(blurShader.depthMap, 0);
RenderUtility.setVector2(blurShader, screenSize, "screenSize");
RenderUtility.setMatrix(blurShader, projection, "projection");
RenderUtility.setVector2(blurShader, new Vector2(1.0f / screenSize.x, 0.0f), "blurDir");
glEnable(GL_DEPTH_TEST);
glBindVertexArray(blurShader.vao);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
Current status pic:

filterRadius(7x7), 1.0 forblurDepthFalloffand 0.2 forblurScale. Those numbers seem more sensible to me. – Andon M. Coleman Dec 26 '14 at 23:21