I've been using 2-pass box blur written in GLSL in Blender without problem.
Blur horizontally:
uniform sampler2D bgl_RenderedTexture;
void main() {
float blur = 0.3;
float resolution = blur * 0.1;
int n = 10;
int i = -n;
int totalWeight = 0;
vec2 st = gl_TexCoord[0].st;
vec4 outColor = vec4(0.0);
for(; i < n; ++i) {
vec2 uv = st + vec2(float(i) / 2.0 / float(n) * resolution, 0);
outColor += texture2D(bgl_RenderedTexture, uv);
totalWeight += 1;
}
gl_FragColor = outColor / float(totalWeight);
}
Blur vertically:
uniform sampler2D bgl_RenderedTexture;
void main() {
float blur = 0.3;
float resolution = blur * 0.1;
int n = 10;
int j = -n;
int totalWeight = 0;
vec2 st = gl_TexCoord[0].st;
vec4 outColor = vec4(0.0);
for(; j < n; ++j) {
vec2 uv = st + vec2(0, float(j) / 2.0 / float(n) * resolution);
outColor += texture2D(bgl_RenderedTexture, uv);
totalWeight += 1;
}
gl_FragColor = outColor / float(totalWeight);
}
But when I try to do it with single pass, it's only blurring in the direction of the inner loop.
uniform sampler2D bgl_RenderedTexture;
void main() {
float blur = 0.3;
float resolution = blur * 0.01;
int n = 10;
int i = -n;
int j = -n;
int totalWeight = 0;
vec2 st = gl_TexCoord[0].st;
vec4 outColor = vec4(0.0);
for(; i <= n; ++i) {
for(; j <= n; ++j) {
vec2 uv = st + vec2(float(i), float(j))
* resolution / float(n) / 2.0;
outColor += texture2D(bgl_RenderedTexture, uv);
totalWeight += 1;
}
}
gl_FragColor = outColor/ float(totalWeight);
}
I must be missing something really simple but it's not visible to me. Can anyone help me?
vec2 uv = st + vec2(float(i), float(j)) / 2.0 / float(n) * resolution;
and the result is same. \$\endgroup\$for(;i <= n; ++i)
andfor(;j <=n; ++j)
withfor(i = -n; i <= n; ++i)
andfor(j = -n; j <=n; ++j)
and it worked. Is it optimization? \$\endgroup\$n
andresolution
const, dont devide by two(make the base const smaller) and removetotalWeight
, it is redundant (you know how many samples are in 2n+1 wide square, dont you) \$\endgroup\$