PDA

View Full Version : simple filter in fragment shader



hibari
03-03-2015, 08:28 AM
Hi all !

I'm trying to do a simple dilatation in my fragment shader :



#version 300 es

in highp vec2 texCoordOut;
uniform sampler2D video;

layout (location = 0) out highp vec4 outColor0;

void main () {

// colors
highp vec4 black = vec4(.0, .0, .0, 1.0);
highp vec4 white = vec4(1.0, 1.0, 1.0, 1.0);
highp vec4 red = vec4(1.0, .0, .0, 1.0);
highp vec4 blue = vec4(.0, .0, 1.0, 1.0);
highp vec4 green = vec4(.0, 1.0, .0, 1.0);

highp vec2 offsets[49];
//
offsets[0] = vec2(-3.0,-3.0);
offsets[1] = vec2(-3.0,-2.0);
offsets[2] = vec2(-3.0,-1.0);
offsets[3] = vec2(-3.0,0.0);
offsets[4] = vec2(-3.0,1.0);
offsets[5] = vec2(-3.0,2.0);
offsets[6] = vec2(-3.0,3.0);
//
offsets[7] = vec2(-2.0,-3.0);
offsets[8] = vec2(-2.0,-2.0);
offsets[9] = vec2(-2.0,-1.0);
offsets[10] = vec2(-2.0,0.0);
offsets[11] = vec2(-2.0,1.0);
offsets[12] = vec2(-2.0,2.0);
offsets[13] = vec2(-2.0,3.0);
//
offsets[14] = vec2(-1.0,-3.0);
offsets[15] = vec2(-1.0,-2.0);
offsets[16] = vec2(-1.0,-1.0);
offsets[17] = vec2(-1.0,0.0);
offsets[18] = vec2(-1.0,1.0);
offsets[19] = vec2(-1.0,2.0);
offsets[20] = vec2(-1.0,3.0);
//
offsets[21] = vec2(0.0,-3.0);
offsets[22] = vec2(0.0,-2.0);
offsets[23] = vec2(0.0,-1.0);
offsets[24] = vec2(0.0,0.0);
offsets[25] = vec2(0.0,1.0);
offsets[26] = vec2(0.0,2.0);
offsets[27] = vec2(0.0,3.0);
//
offsets[28] = vec2(1.0,-3.0);
offsets[29] = vec2(1.0,-2.0);
offsets[30] = vec2(1.0,-1.0);
offsets[31] = vec2(1.0,0.0);
offsets[32] = vec2(1.0,1.0);
offsets[33] = vec2(1.0,2.0);
offsets[34] = vec2(1.0,3.0);
//
offsets[35] = vec2(2.0,-3.0);
offsets[36] = vec2(2.0,-2.0);
offsets[37] = vec2(2.0,-1.0);
offsets[38] = vec2(2.0,0.0);
offsets[39] = vec2(2.0,1.0);
offsets[40] = vec2(2.0,2.0);
offsets[41] = vec2(2.0,3.0);
//
offsets[42] = vec2(3.0,-3.0);
offsets[43] = vec2(3.0,-2.0);
offsets[44] = vec2(3.0,-1.0);
offsets[45] = vec2(3.0,0.0);
offsets[46] = vec2(3.0,1.0);
offsets[47] = vec2(3.0,2.0);
offsets[48] = vec2(3.0,3.0);

// dilatation
highp vec4 currentColor = texture(video, texCoordOut);
highp vec4 color;
for (int i=0; i<49; i++) {
highp vec4 neighb_color = texture(video, texCoordOut+offsets[i]);
color = min(currentColor, neighb_color);
}
outColor0 = color;

}


This is a picture with a white background an several round black little objects.

My code is doing nothing. Does anybdy see what i did wrong ?

Thank you very much !

GClements
03-04-2015, 05:27 PM
Does anybdy see what i did wrong ?
The texture() function performs texture lookups using normalised texture coordinates (in the range 0..1), not texel indices. Either divide the offsets by textureSize(video) or change them to ivec2 and use textureOffset() instead.