Question

I want to blur an image using the follow code,

{
const GLchar fShaderText[] = FRAGMENT_SHADER_SOURCE
(
 uniform sampler2D imageTexture;
 uniform highp float w0; 
 uniform highp float w1;
 uniform highp float w2;
 uniform highp float w3;
 uniform highp float w4;
 uniform highp float w5;
 uniform highp float w6;

 varying highp vec2 uv0;
 void main()
 {
     highp float u_dx = 1./imageWH.x;
     highp float sum = 0.;
     highp vec4 col = texture2D(imageTexture, uv0);

     sum += texture2D(imageTexture, vec2(uv0.x - 6. * u_dx, uv0.y)).x * w6;
     sum += texture2D(imageTexture, vec2(uv0.x - 5. * u_dx, uv0.y)).x * w5;
     sum += texture2D(imageTexture, vec2(uv0.x - 4. * u_dx, uv0.y)).x * w4;
     sum += texture2D(imageTexture, vec2(uv0.x - 3. * u_dx, uv0.y)).x * w3;
     sum += texture2D(imageTexture, vec2(uv0.x - 2. * u_dx, uv0.y)).x * w2;
     sum += texture2D(imageTexture, vec2(uv0.x - u_dx, uv0.y)).x * w1;
     sum += col.x * w0;
     sum += texture2D(imageTexture, vec2(uv0.x + u_dx, uv0.y)).x * w1;
     sum += texture2D(imageTexture, vec2(uv0.x + 2. * u_dx, uv0.y)).x * w2;
     sum += texture2D(imageTexture, vec2(uv0.x + 3. * u_dx, uv0.y)).x * w3;
     sum += texture2D(imageTexture, vec2(uv0.x + 4. * u_dx, uv0.y)).x * w4;
     sum += texture2D(imageTexture, vec2(uv0.x + 5. * u_dx, uv0.y)).x * w5;
     sum += texture2D(imageTexture, vec2(uv0.x + 6. * u_dx, uv0.y)).x * w6;


     gl_FragColor = vec4(sum, 0., 0., 1.);
 }
 );

// Store the progrm, compute uniform locations
ProgramUniforms &pu = (_programs["HorizontalBlur"] = ProgramUniforms());
pu.program = compileShaders(gVertexShaderText, fShaderText);
pu.uniformMap["mvpMatrix"]    = glGetUniformLocation(pu.program, "mvpMatrix");
pu.uniformMap["imageTexture"] = glGetUniformLocation(pu.program, "imageTexture");
pu.uniformMap["imageWH"]      = glGetUniformLocation(pu.program, "imageWH");

pu.uniformMap["w0"]           = glGetUniformLocation(pu.program, "w0");
pu.uniformMap["w1"]           = glGetUniformLocation(pu.program, "w1");
pu.uniformMap["w2"]           = glGetUniformLocation(pu.program, "w2");
pu.uniformMap["w3"]           = glGetUniformLocation(pu.program, "w3");
pu.uniformMap["w4"]           = glGetUniformLocation(pu.program, "w4");
pu.uniformMap["w5"]           = glGetUniformLocation(pu.program, "w5");
pu.uniformMap["w6"]           = glGetUniformLocation(pu.program, "w6");
}

I think you have noticed that i have to fetch the neighbor texels for the the calculation.

My concern is that when the current texel is located at the boundary, then some of its neighbors are sure to being outside of the input image.

So, texture2d() could also return an effective value?

Was it helpful?

Solution

You should clamp your texture. So if you try to fetch the texel outside the texture, you would actually get the nearest texel on the border.

It's better option to clamp the texture, than is to repeat it, because it will avoid discontinuities at the texture's borders on the opposite sides of it.

Take a look at this link.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top