I'm trying to implement a bilateral gaussian blur. I've already implemented a simple gaussian blur (see below), but I don't know how to turn that into a bilateral one. I've read about bilateral filtering, and I know that this way the weights not only depend on the distance between the samples but between the color intensities as well. Despite this I still have no clue how to do this.
In order to accomplish a bilateral blur you will need to use FBOs (Frame Buffer Objects). You essentially render your scene to an FBO and then blur that in one direction. You then render the first FBO to either another FBO or to the screen then you blur that in the other direction.
Your Shaders are going to look something like this, you will have two of them, each in a different direction.
In order to accomplish a bilateral blur you will need to use FBOs (Frame Buffer Objects). You essentially render your scene to an FBO and then blur that in one direction. You then render the first FBO to either another FBO or to the screen then you blur that in the other direction.
Your Shaders are going to look something like this, you will have two of them, each in a different direction.
void main(void)
{
vec4 sum = vec4(0.0);
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y - 4.0*blursize)) * 0.05;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y - 3.0*blursize)) * 0.09;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y - 2.0*blursize)) * 0.12;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y - blursize)) * 0.15;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y)) * 0.16;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y + blursize)) * 0.15;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y + 2.0*blursize)) * 0.12;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y + 3.0*blursize)) * 0.09;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y + 4.0*blursize)) * 0.05;
gl_FragColor = sum;
}
thanks for the reply,
I'm already doing the fbos and ping-ponging between them
to add I'm doing the gaussian blur by 5 taps, as opposed to 9 taps in your example, but I still don't understand how did you calculate the weights:
0.16, 0.15, 0.12, 0.09, 0.05
also these need to be reduced so that I can still do blur by 5 taps...
Bilateral filtering considers samples depending on two weights (often the secondary weight is more or less a boolean function).
I.e. when using a gaussin blur on a SSAO map, you can make it depending on the depth buffer, that is, only pixels which are on a similar depth level then the target pixel are considered for blurring. Therefor you need more or less two filter criteria (both are more or less functions). Here's some pseudo code with a simple threshold function for depth:
[source lang="cpp"]
// all weights should sum up to 1.0
float weight_sum = 0;
// result color
vec3 color = vec3(0);
// sample depth and color for 0...7, where 4 is the center
if(abs(depth_0-depth_4) < threshold)
{
color += color_0 * weight_0;
weight_sum += weight_0;
}
if(abs(depth_1-depth_4) < threshold)
{
color += color_1 * weight_1;
weight_sum += weight_1;
}
...
// consider weight sum
color *= 1/weight_sum;
[/source]
You can optimize this (ie. comparing 4 samples at once).
Bilateral filtering considers samples depending on two weights (often the secondary weight is more or less a boolean function).
I.e. when using a gaussin blur on a SSAO map, you can make it depending on the depth buffer, that is, only pixels which are on a similar depth level then the target pixel are considered for blurring. Therefor you need more or less two filter criteria (both are more or less functions). Here's some pseudo code with a simple threshold function for depth:
[source lang="cpp"]
// all weights should sum up to 1.0
float weight_sum = 0;
// result color
vec3 color = vec3(0);
// sample depth and color for 0...7, where 4 is the center
if(abs(depth_0-depth_4) < threshold)
{
color += color_0 * weight_0;
weight_sum += weight_0;
}
if(abs(depth_1-depth_4) < threshold)
{
color += color_1 * weight_1;
weight_sum += weight_1;
}
...
// consider weight sum
color *= 1/weight_sum;
[/source]
You can optimize this (ie. comparing 4 samples at once).
thank you Ashaman73, this trick pretty much solved the problem (and you're right, I'm doing SSDO blurring )
this is how my shader looks like now:
#version 420 core
for the threshold I chose 0.0001, as this seemed to be "good enough".
Actually the depth buffer contains linear depth (in range [0...1]), so if I want to check for 0.1 units difference, then I should set the threshold to:
far * x = 0.1;
as far is 1000 I needed this value: 0.0001
you're right I could do this, plus since I'm using OpenGL 4.2 I can go ahead and sample all 4 by one texture fetch using textureGatherOffsets
vec4 all_depth_samples = textureGatherOffsets( texture0, i.tex_coord, offsets, 0);
This could be done for the SSAO result too, but for SSDO I need RGB values...