Hey I’m not sure that enabling/disabling is the best approach, but it may be OK.
The blur works by sampling the texture at some distance (step size) around each pixel, multiplying those values by “weights” from a Gaussian, and then summing. Each pixel gets fractional contributions from its neighbors, which blurs the image.
A texture with normalized coordinates should have a fractional step size. The step size could be 1.0 with non-normalized coordinates. So maybe lines 84 and 85 (from the GitHub page of ofxBlur) could be changed to include a uniform vec2 step
, so that the texture is sampled with a fractional step as follows:
// in OF, in psudeo code
glm::vec2 stepSize;
stepSize = glm::vec2(1.f / fbo.getWidth(), 1.f / fbo.getHeight());
shader.setUniform2f("stepSize", stepSize);
// stepSize comes into the fragment shader for sampling the texture
uniform vec2 stepSize;
// before lines 84 and 85
direction *= stepSize;
I have to admit that I have some trouble following ofxBlur to see where to modify it for a sampler2D texture.
If it helps, the Gaussian blur I often use is below; it came from the GitHub page of Adam Ferriss. stepSize
describes both the magnitude and direction of the blur:
// in OF, a basic diagonal step:
stepSize = glm::vec2(1.0 / fbo.getWidth(), 1.0 / fbo.getHeight());
// modify for direction and/or magnitude
stepSize *= glm::vec2(1.0, 0.0); // horizontal
// stepSize *= glm::vec2(0.0, 1.0); // vertical
// stepSize *= glm::vec2(-1.0, 1.0); // opposite diagonal
// stepSize *= 1.3; // a larger step
blur shader:
#version 330
uniform sampler2D tex0;
uniform vec2 stepSize;
in vec2 vTexcoord;
out vec4 fragColor;
vec3 gaussianBlur(sampler2D tex, vec2 texUV, vec2 stepSize){
// a variable for output
vec3 colorOut = vec3(0.0);
const int stepCount = 9;
// gaussian weights
float gWeights[stepCount];
gWeights[0] = 0.10855;
gWeights[1] = 0.13135;
gWeights[2] = 0.10406;
gWeights[3] = 0.07216;
gWeights[4] = 0.04380;
gWeights[5] = 0.02328;
gWeights[6] = 0.01083;
gWeights[7] = 0.00441;
gWeights[8] = 0.00157;
// gaussian offsets
float gOffsets[stepCount];
gOffsets[0] = 0.66293;
gOffsets[1] = 2.47904;
gOffsets[2] = 4.46232;
gOffsets[3] = 6.44568;
gOffsets[4] = 8.42917;
gOffsets[5] = 10.41281;
gOffsets[6] = 12.39664;
gOffsets[7] = 14.38070;
gOffsets[8] = 16.36501;
// 9 samples in the blur
for(int i = 0; i < stepCount; i++)
{
// multiply the texel size by the by the offset value
vec2 texCoordOffset = gOffsets[i] * stepSize;
// sample to the left and to the right of the texture and add them together
vec3 color = texture(tex, texUV + texCoordOffset).rgb;
color += texture(tex, texUV - texCoordOffset).rgb;
// multiply col by the gaussian weight value from the array
color *= gWeights[i];
// add it all up
colorOut += color;
}
// our final value is returned as col out
return colorOut;
}
void main(){
// normalized coords
vec2 tc = vTexcoord;
vec3 color = gaussianBlur(tex0, tc, stepSize);
fragColor = vec4(color, 1.0);
}