SFML shader not working

2019-09-19 04:23发布

问题:

I'm trying to create a outline effect to my buttons when they are clicked but atm I'm just testing stuff with shaders... whenever I draw something with my shader tho it renders the shape completely black

#include <SFML\Graphics.hpp>

int main(){
    sf::RenderWindow window(sf::VideoMode(500, 500), "hello world", sf::Style::Close);

    sf::RectangleShape rect;
    rect.setSize(sf::Vector2f(100, 100));
    rect.setFillColor(sf::Color::Red);

    sf::Shader blur;
    if (!blur.loadFromFile("blur.frag", sf::Shader::Fragment)){
        return 1;
    }
    if (!blur.isAvailable()){
        return 1;
    }
    blur.setUniform("texture", sf::Shader::CurrentTexture);
    blur.setUniform("blur_radius", 0.002f);

    while (window.isOpen()){

        sf::Event evnt;

        while (window.pollEvent(evnt)){
            if (evnt.type == sf::Event::Closed)
                window.close();
        }

        window.clear(sf::Color(0, 100, 100));

        sf::RenderStates state;
        state.shader = &blur;
        window.draw(rect, state);

        window.display();
    }
    return 0;
}

and my shader code.

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);
}

I would expect a blurred red rectangle to appear in the top left cornor but instead theres a black solid rectangle.

回答1:

Because rect does not have a texture attach to it, all the pixels used in blur.frag are invalid, I guess in this case it use black pixels, hence the black rectangle.

You should create a texture (use sf::RenderTexture), draw whatever you want on it, then create a sf::Sprite on top of it and draw this sprite. Alternatively you may load a texture from an image.

#include <SFML/Graphics.hpp>

int main(){
    sf::RenderWindow window(sf::VideoMode(200, 200), "hello world", sf::Style::Close);
    window.setFramerateLimit(60);

    sf::RectangleShape rectBlue;
    rectBlue.setSize(sf::Vector2f(100, 50));
    rectBlue.setFillColor(sf::Color::Blue);
    sf::RectangleShape rectYellow;
    rectYellow.setSize(sf::Vector2f(100, 50));
    rectYellow.setFillColor(sf::Color::Yellow);
    rectYellow.setPosition(0, 50);

    sf::RenderTexture renderTexture;
    if (!renderTexture.create(100, 100)){ //create a render texture
        return 1;
    }
    renderTexture.clear();
    renderTexture.draw(rectBlue); //draw blue rect on the render texture
    renderTexture.draw(rectYellow); //draw yellow rect
    renderTexture.display();
    sf::Sprite sprite(renderTexture.getTexture()); //create a sprite from the created texture

    sf::Shader blur;
    if (!blur.loadFromFile("blur.frag", sf::Shader::Fragment)){
        return 1;
    }
    if (!blur.isAvailable()){
        return 1;
    }
    blur.setUniform("texture", sf::Shader::CurrentTexture);
    blur.setUniform("blur_radius", 0.05f);

    while (window.isOpen()){

        sf::Event evnt;

        while (window.pollEvent(evnt)){
            if (evnt.type == sf::Event::Closed)
                window.close();
        }

        window.clear(sf::Color(0, 100, 100));
        window.draw(sprite, &blur); //draw the sprite with blur shader
        window.display();
    }
    return 0;
}

Result:



标签: c++ sfml