Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Applying a blur shader gives black square  (Read 1892 times)

0 Members and 1 Guest are viewing this topic.

Jove

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • http://www.jestofevekites.com/
Applying a blur shader gives black square
« on: June 21, 2020, 08:11:23 pm »
Hi.

This shader will work if I apply it to, for example, a full screen sf::RenderTexture, but sadly not on a small object such as a sf::Sprite.

Not sure if it's the shader or something else I'm doing incorrectly. It originates from https://www.shadertoy.com/view/XdfGDH

SHADER:
uniform sampler2D       texture;
uniform vec2            textureSize;
uniform float           blurFactor;

float normpdf(in float x, in float sigma)
{
        return 0.39894*exp(-0.5*x*x/(sigma*sigma))/sigma;
}

void main()
{
        vec3 c = texture2D(texture, gl_FragCoord.xy / textureSize.xy).rgb;
       
        //declare stuff
        const int mSize = 11;
        const int kSize = (mSize-1)/2;
        float kernel[mSize];
        vec3 final_colour = vec3(0.0);
       
        //create the 1-D kernel
        float sigma = blurFactor;
        float Z = 0.0;
        for (int j = 0; j <= kSize; ++j)
        {
                kernel[kSize+j] = kernel[kSize-j] = normpdf(float(j), sigma);
        }
               
        //get the normalization factor (as the gaussian has been clamped)
        for (int j = 0; j < mSize; ++j)
        {
                Z += kernel[j];
        }
               
        //read out the texels
        for (int i=-kSize; i <= kSize; ++i)
        {
                for (int j=-kSize; j <= kSize; ++j)
                {
                        final_colour += kernel[kSize+j]*kernel[kSize+i]*texture2D(texture, (gl_FragCoord.xy+vec2(float(i),float(j))) / textureSize.xy).rgb;
                }
        }
               
        gl_FragColor = vec4(final_colour/(Z*Z), 1.0);
}
 


C++:
#include <SFML\Graphics.hpp>

int main()
{
        sf::RenderWindow window(sf::VideoMode(1920, 1080), "SFML Testing");
        window.setVerticalSyncEnabled(true);
        window.setKeyRepeatEnabled(false);

        sf::Texture texture;
        texture.loadFromFile("player.png");
        texture.setSmooth(true);
       
        sf::Sprite sprite;
        sprite.setTexture(texture);
        sprite.setPosition(static_cast<float>(window.getSize().x / 2), static_cast<float>(window.getSize().y / 2));
       
        sf::Shader shader;
        shader.loadFromFile("blur.frag", sf::Shader::Fragment);
        shader.setUniform("texture", texture);
        shader.setUniform("textureSize", static_cast<sf::Glsl::Vec2>(texture.getSize()));
        shader.setUniform("blurFactor", 5.f);
       
        sf::RenderStates states;
        states.texture = &texture;
        states.shader = &shader;

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                        if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
                                window.close();
                }

                window.clear(sf::Color::Yellow);
                window.draw(sprite, states);
                window.display();
        }

        return EXIT_SUCCESS;
}
« Last Edit: June 21, 2020, 08:17:12 pm by Jove »
{much better code}

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Applying a blur shader gives black square
« Reply #1 on: June 22, 2020, 10:18:45 pm »
Note that FragCoord is the pixel co-ordinate within the window, not within the texture's size.
Maybe subtracting the pixel position of the sprite's top-left from that FragCoord might help.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*