SFML着色器无法正常工作

时间:2017-06-01 07:15:10

标签: c++ sfml

我尝试在单击按钮时为我的按钮创建轮廓效果但是我只是使用着色器测试内容...每当我使用着色器绘制某些内容时,它会使形状完全变黑

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

和我的着色器代码。

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

我希望在左上角的玉米花中出现一个模糊的红色矩形,而不是一个黑色的实心矩形。

This is the result of the code above

1 个答案:

答案 0 :(得分:2)

因为rect没有附加纹理,所以blur.frag中使用的所有像素都无效,我想在这种情况下它使用黑色像素,因此是黑色矩形。

您应该create a texture(使用sf::RenderTexture),在其上绘制您想要的任何内容,然后在其上创建一个sf::Sprite并绘制此精灵。或者,您可以加载纹理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;
}

结果:

enter image description here