c++ - SFML 着色器不工作

标签 c++ sfml

我试图在单击按钮时为我的按钮创建轮廓效果,但 atm 我只是在用着色器测试东西......每当我用我的着色器绘制东西时它都会将形状呈现为完全黑色

#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

最佳答案

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

你应该create a texture (使用 sf::RenderTexture),在上面绘制任何你想要的东西,然后在它上面创建一个 sf::Sprite 并绘制这个 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

关于c++ - SFML 着色器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44300911/

相关文章:

c++ - 如何为 SFML 游戏创建 macOS .app bundle ?

c++ - "Undefined symbols for architecture arm64"使用 g++-12 在 M1 mac 上构建基本 SFML 项目

c++ - 编译器何时为派生类生成默认构造函数

c++ - 错误LNK2001 : unresolved external symbol "public: static class sf::RenderStates const sf::RenderStates::Default"

c++ - 为什么代码不能返回正确的值?

c++ - 使用std::hash <uint64_t>作为自定义类

c++ - 使用 SFML 在屏幕上居中文本

c++ - 使用 MinGW 编译 SFML 项目

c++ - 将 GetAsyncKeyState() 重置为默认值

c++ - 在异常规范中仅使用 std::exception