关于GLSL,texture2D()来获取纹素

时间:2014-04-08 09:48:26

标签: glsl texture2d boundary

我想使用以下代码模糊图像

{
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");
}

我想你已经注意到我必须为计算提取相邻的纹素。

我担心的是,当当前纹素位于边界时,它的一些邻居肯定会在输入图像之外。

那么,texture2d()还可以返回有效值吗?

1 个答案:

答案 0 :(得分:0)

你应该夹住你的纹理。因此,如果您尝试在纹理外部获取纹理元素,您实际上会获得边界上最近的纹素。

最好选择夹住纹理,而不是重复纹理,因为它可以避免纹理边缘对面的不连续性。

看看this link