# Blur gets displaced compared to original image

I have implemented a SSAO and I'm using a blur step to smooth it out. The problem is that the blurred texture is slightly displaced compared to the original. I'm blurring using a 4x4 kernel since that was my noise kernel in SSAO. The following is the blurring shader:


float result = 0.0;
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
vec2 offset = vec2(TEXEL_SIZE.x * i, TEXEL_SIZE.y * j);
result += texture(aoSampler, TexCoord + offset).r;
}
}

out_AO = vec4(vec3(0.0), result * 0.0625);


Where TEXEL_SIZE is one over my window resolution.

I was thinking that this is was an error based on how OpenGL counts the Texel center, so I tried displacing the texture coordinate I was using by 0.5 * TEXEL_SIZE, but there was still a slight displacement.

The texture input to my blur shader, has wrap parameters:


glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);


When I tell the blur shader to just output the the value of the pixel, the result is not displaced, so it must have something to do with how neighboring pixels are sampled.

Any thoughts?

I think you should change your for loop to something like this (you should take current pixel and few pixels around it, not only one way from current pixel):

for(int i = -2; i < 2; i++){
for(int j = -2; j < 2; j++){
vec2 offset = vec2(TEXEL_SIZE.x * i, TEXEL_SIZE.y * j);
result += texture(aoSampler, TexCoord + offset).r;
}
}


Because your kernel is 4*4, it's even count, it could be still shifted. If it's odd (for example 3*3 or 5*5), it would be better.

• Thanks a lot, that worked! Only you should change the <2 to <=2 – Grieverheart Jun 28 '12 at 11:39
• < 2 is correct for a 4x4 kernel; <= 2 will give you a 5x5 kernel (which you should probably be using anyway). – Maximus Minimus Jun 28 '12 at 11:56
• Yes, I'm using a 5x5 kernel now, cause I thought the whole point was to sample symmetrically around the current pixel. – Grieverheart Jun 28 '12 at 23:39