I'm trying to make a UI blur using a fragment shader. The current route I'm taking is a sf::Sprite with a constantly updating sf::Texture, then the sf::Sprite is drawn with the shader, but instead the screen is just stuck on a single frame when using the shader.
The constructor:
BackgroundBlur::BackgroundBlur(sf::Vector2f _Position, sf::Vector2f _Size, float _Strength)
: UIElement(_Position, true)
{
sf::Texture _Texture;
Textures.push_back(_Texture);
sf::Sprite _Sprite;
_Sprite.setTexture(Textures[0]);
_Sprite.setTextureRect(sf::IntRect(_Position.x, _Position.y, _Size.x, _Size.y));
Sprites.push_back(_Sprite);
Blur.loadFromFile("Resource/Data/Shaders/Blur.frag", sf::Shader::Fragment);
Blur.setUniform("blur_radius", sf::Glsl::Vec2(Strength, Strength));
Blur.setUniform("texture", Textures[0]);
}
The sf::Texture is created here:
void BackgroundBlur::BeginPlay()
{
Textures[0].create
(
CurrentApp->GetWindow().getSize().x,
CurrentApp->GetWindow().getSize().y
);
}
And the problem resides here, not updating, it works, but no blur:
void BackgroundBlur::Tick(float _DeltaTime)
{
Textures[0].update(CurrentApp->GetWindow());
}
This is the draw function, if I remove the sf::Shader from the draw arguments, it works again, but there isn't blur:
void BackgroundBlur::Draw(sf::RenderTarget & _Target, sf::RenderStates _States) const
{
_Target.draw(Sprites[0], &Blur);
}
I've tried adding
Blur.setUniform("texture", Textures[0]);
to the Tick function, however it seems to do nothing.
Also, the fragment shader I am using is
uniform sampler2D texture;
uniform vec2 blur_radius;
void main() {
vec2 textureCoordinates = gl_TexCoord[0].xy;
vec4 color = vec4(0.0);
color += texture2D(texture, textureCoordinates - 10.0 * blur_radius) * 0.0012;
color += texture2D(texture, textureCoordinates - 9.0 * blur_radius) * 0.0015;
color += texture2D(texture, textureCoordinates - 8.0 * blur_radius) * 0.0038;
color += texture2D(texture, textureCoordinates - 7.0 * blur_radius) * 0.0087;
color += texture2D(texture, textureCoordinates - 6.0 * blur_radius) * 0.0180;
color += texture2D(texture, textureCoordinates - 5.0 * blur_radius) * 0.0332;
color += texture2D(texture, textureCoordinates - 4.0 * blur_radius) * 0.0547;
color += texture2D(texture, textureCoordinates - 3.0 * blur_radius) * 0.0807;
color += texture2D(texture, textureCoordinates - 2.0 * blur_radius) * 0.1065;
color += texture2D(texture, textureCoordinates - blur_radius) * 0.1258;
color += texture2D(texture, textureCoordinates) * 0.1330;
color += texture2D(texture, textureCoordinates + blur_radius) * 0.1258;
color += texture2D(texture, textureCoordinates + 2.0 * blur_radius) * 0.1065;
color += texture2D(texture, textureCoordinates + 3.0 * blur_radius) * 0.0807;
color += texture2D(texture, textureCoordinates + 4.0 * blur_radius) * 0.0547;
color += texture2D(texture, textureCoordinates + 5.0 * blur_radius) * 0.0332;
color += texture2D(texture, textureCoordinates + 6.0 * blur_radius) * 0.0180;
color += texture2D(texture, textureCoordinates - 7.0 * blur_radius) * 0.0087;
color += texture2D(texture, textureCoordinates - 8.0 * blur_radius) * 0.0038;
color += texture2D(texture, textureCoordinates - 9.0 * blur_radius) * 0.0015;
color += texture2D(texture, textureCoordinates - 10.0 * blur_radius) * 0.0012;
gl_FragColor = color;
}
, taken from
https://en.sfml-dev.org/forums/index.php?topic=20155.0EDIT: I'm so stupid, instead of
Blur.setUniform("texture", Textures[0]);
it should be
Blur.setUniform("texture", sf::Shader::CurrentTexture);