Hi.
This shader will work if I apply it to, for example, a full screen sf::RenderTexture, but sadly not on a small object such as a sf::Sprite.
Not sure if it's the shader or something else I'm doing incorrectly. It originates from
https://www.shadertoy.com/view/XdfGDHSHADER:
uniform sampler2D texture;
uniform vec2 textureSize;
uniform float blurFactor;
float normpdf(in float x, in float sigma)
{
return 0.39894*exp(-0.5*x*x/(sigma*sigma))/sigma;
}
void main()
{
vec3 c = texture2D(texture, gl_FragCoord.xy / textureSize.xy).rgb;
//declare stuff
const int mSize = 11;
const int kSize = (mSize-1)/2;
float kernel[mSize];
vec3 final_colour = vec3(0.0);
//create the 1-D kernel
float sigma = blurFactor;
float Z = 0.0;
for (int j = 0; j <= kSize; ++j)
{
kernel[kSize+j] = kernel[kSize-j] = normpdf(float(j), sigma);
}
//get the normalization factor (as the gaussian has been clamped)
for (int j = 0; j < mSize; ++j)
{
Z += kernel[j];
}
//read out the texels
for (int i=-kSize; i <= kSize; ++i)
{
for (int j=-kSize; j <= kSize; ++j)
{
final_colour += kernel[kSize+j]*kernel[kSize+i]*texture2D(texture, (gl_FragCoord.xy+vec2(float(i),float(j))) / textureSize.xy).rgb;
}
}
gl_FragColor = vec4(final_colour/(Z*Z), 1.0);
}
C++:
#include <SFML\Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(1920, 1080), "SFML Testing");
window.setVerticalSyncEnabled(true);
window.setKeyRepeatEnabled(false);
sf::Texture texture;
texture.loadFromFile("player.png");
texture.setSmooth(true);
sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setPosition(static_cast<float>(window.getSize().x / 2), static_cast<float>(window.getSize().y / 2));
sf::Shader shader;
shader.loadFromFile("blur.frag", sf::Shader::Fragment);
shader.setUniform("texture", texture);
shader.setUniform("textureSize", static_cast<sf::Glsl::Vec2>(texture.getSize()));
shader.setUniform("blurFactor", 5.f);
sf::RenderStates states;
states.texture = &texture;
states.shader = &shader;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
window.close();
}
window.clear(sf::Color::Yellow);
window.draw(sprite, states);
window.display();
}
return EXIT_SUCCESS;
}