简体   繁体   English

SFML加载着色器错误

[英]SFML loading shader error

So i am working on my game project using SFML and I tried to implement a blur shader. 因此,我正在使用SFML进行游戏项目,并尝试实现模糊着色器。 I have no idea why , but this block of code makes the program show a white screen and then close.I didn't even use the shader to draw anything , just the fact that I am trying to load it gives the unexpected result :( . 我不知道为什么,但是这段代码使程序显示白色屏幕然后关闭。我什至没有使用着色器绘制任何东西,只是我试图加载它的事实给出了意外的结果:( 。

void button::setText(std::string _text)
{
    text.setString(_text);
    text.setFont(font);
    text.setCharacterSize(80);
    text.move(0,-(text.getLocalBounds().top));
    text.setColor(sf::Color(255,255,255,255));
    width=text.getLocalBounds().width+text.getLocalBounds().left;
    height=text.getLocalBounds().height+text.getLocalBounds().top;
    rndtexture.create(width, height);
    rndtexture.setSmooth(true);
    rndtexture.clear(sf::Color::Transparent);
    rndtexture.draw(text);
    rndtexture.display();
    rTexture=rndtexture.getTexture();
    spriteR.setTexture(rTexture);

    rndtexture.clear(sf::Color::Transparent);
    text.setColor(sf::Color(0,0,0,100));
    rndtexture.draw(text);
    rndtexture.display();
    pTexture=rndtexture.getTexture();
    spriteP.setTexture(pTexture);

    //commenting this out will make it work just fine
    if (!shader.loadFromFile("shader.frag", sf::Shader::Type::Fragment))
    {
        std::cout<<"lol";
    }
    //

    shader.setParameter("texture", sf::Shader::CurrentTexture);
    shader.setParameter("blur_radius", 5.0);
}

This is my shader file: 这是我的着色器文件:

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

What is wrong with this.. ? 这有什么问题呢?

What's the return value when you call: 调用时的返回值是多少:

sf::Shader::isAvailable()

Make sure you can use a shader program before you call it. 调用它之前,请确保可以使用着色器程序。 Also, a shader can't crash a program, so it's definitely not your shader. 而且,着色器不会使程序崩溃,因此绝对不是您的着色器。 If something was wrong with your shader, it wouldn't compile. 如果您的着色器出了点问题,它将无法编译。 If your shader didn't compile, then SFML automatically switches to a built-in shader. 如果您的着色器没有编译,则SFML会自动切换到内置着色器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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