Reputation: 61
I'm trying to implement a SFML Shader following their example and it doesn't show up.
GameObject
is a class that inherits and implements sf::Drawable
. Inside GameObject
I have a sf::Texture
and a sf::Sprite
objects, responsible for the image of the game object. I'm trying to apply a blur effect on it. Here's the .cpp where I load the image and shader:
x = new GameObject("Images/apple.png", 100, 100, 1, 1);
mshade.loadFromFile("Shaders/blur.frag", sf::Shader::Fragment);
mshade.setParameter("texture", *(x)->objectTexture);
mshade.setParameter("blur_radius", 200);
And this is how I draw it:
gameWindow->draw(*x, &mshade);
And this is the GLSL code to create the blur shader:
uniform sampler2D texture;
uniform float blur_radius;
void main()
{
vec2 offx = vec2(blur_radius, 0.0);
vec2 offy = vec2(0.0, blur_radius);
vec4 pixel = texture2D(texture, gl_TexCoord[0].xy) * 4.0 +
texture2D(texture, gl_TexCoord[0].xy - offx) * 2.0 +
texture2D(texture, gl_TexCoord[0].xy + offx) * 2.0 +
texture2D(texture, gl_TexCoord[0].xy - offy) * 2.0 +
texture2D(texture, gl_TexCoord[0].xy + offy) * 2.0 +
texture2D(texture, gl_TexCoord[0].xy - offx - offy) * 1.0 +
texture2D(texture, gl_TexCoord[0].xy - offx + offy) * 1.0 +
texture2D(texture, gl_TexCoord[0].xy + offx - offy) * 1.0 +
texture2D(texture, gl_TexCoord[0].xy + offx + offy) * 1.0;
gl_FragColor = gl_Color * (pixel / 16.0);
}
But for some reason the image is displayed just as normally, without any effect. I also don't get any error. Does anyone know why the blur effect doesn't show up?
Upvotes: 1
Views: 1787
Reputation: 13
I also used the shader from the sfml examples, the problem is that gl_Color is used there, but you don't initialize it in any way, that is, it = 0.
Replace gl_FragColor = gl_Color * (pixel / 16.0);
to gl_FragColor = pixel / 16.0;
Upvotes: 0
Reputation: 1
Well in my question at: SFML shader not working I got help on this issue aswell I recoomend you go check out tntxtnt's answer that will probably help you. but basicly it dosent blur the outline of the textures it only blurs the colors inside the sprite.
Upvotes: 0
Reputation: 2455
I don't know anything about SFML, but i looks like your blur_radius is not well chosen. Your image has the size 100x100 and you Blur-Shader samples the image with distance of 200px (blur_radius). So if your texture has gl_repeat enabled , you hit exactly the same pixel as the original one. -> no blur
Try using a lower blur_radius maybe something around 2-5
Upvotes: 1