2

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: Depth rendered out to the screen after applying blur

  • Did you pick those coefficients at random? That falloff produces some ridiculously small numbers when you have large depth discrepancies (which is actually the opposite of what you want if you want to detect edges using depth). I think you actually want a number smaller than 1.0 there rather than a number a thousand times larger. – Andon M. Coleman Dec 26 '14 at 23:01
  • I've tried a ton of different combinations for blurScale and falloff. Using a number less than 1.0 for the falloff (with the same scale) gives me the same result as above except that I get two weird black lines at the edge of the screen. If I use a number less than 1 for the falloff and a number larger than 1 for the scale, I get no blurring at all. – Kinru Dec 26 '14 at 23:10
  • A 21x21 sample window seems a bit excessive too. I found an actual implementation of this shader that includes coefficients and it used 3.0 for filterRadius (7x7), 1.0 for blurDepthFalloff and 0.2 for blurScale. Those numbers seem more sensible to me. – Andon M. Coleman Dec 26 '14 at 23:21
  • Tried those numbers, results are still poor. The edges are still clearly blurred while the overall blur is just less. – Kinru Dec 27 '14 at 0:41
1

Finally figured it out. The part that is missing is the fact that pixels with a depth greater than .9999 (or some other max depth number) need to be discarded so that you aren't trying to blur over pixels that aren't part of the simulation. Obvious in retrospect, took me an incredibly long time to realize though.

So the correct shader looks as follows:

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;
    if (depth == 1.0f) {
       discard;
    }

    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;
}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.