简体   繁体   中英

SFML Blur Shader shows no effect

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM