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

Author Topic: Shader problem  (Read 1139 times)

0 Members and 1 Guest are viewing this topic.

Carbonedioxide

  • Newbie
  • *
  • Posts: 2
    • View Profile
Shader problem
« on: August 15, 2018, 05:33:18 am »
Hello!
I've recently decided to update to 2.5 version and have small issue with shader I use.

That's original image:
(click to show/hide)

that's blurred version in 2.4.2:
(click to show/hide)

and that's blurred version in 2.5:
(click to show/hide)
I've circled most visible change but as you keep looking you find more

Main code:
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>

int main() {
        sf::Sprite sprite;
        sf::Texture texture;
        if (!texture.loadFromFile("background.jpg")) {}
        sprite.setTexture(texture);

        sf::Shader blurShader;
        if (!blurShader.loadFromFile("blur.frag", sf::Shader::Fragment)) {}
        blurShader.setUniform("texture", sf::Shader::CurrentTexture);
        blurShader.setUniform("blur_radius", 0.001f);

        sf::RenderTexture rendertexture;
        rendertexture.create(1280, 720);
        rendertexture.clear();
        for (int i = 100; i >= 0; i--) {
                rendertexture.draw(sprite, &blurShader);
                rendertexture.display();
                sprite.setTexture(rendertexture.getTexture());
        }
        sprite.setPosition(0, 0);

        sf::RenderWindow window(sf::VideoMode(1280, 720), "Peppa pig");
        sf::Event event;
        while (window.isOpen()) {
                while (window.pollEvent(event)) {
                        if (event.type == sf::Event::Closed)
                                exit(0);
                        else if (event.key.code == sf::Keyboard::Escape)
                                exit(0);
                }

                window.clear();
                window.draw(sprite);
                window.display();
        }
}
 
Code of blur shader:
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);
}
 
Compiled using visual studio 2015.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Shader problem
« Reply #1 on: August 15, 2018, 07:26:34 am »
As far as I know, you can't draw a texture onto itself. You'll have to use at least two render-textures (a source and a target) and swap them at each iteration.
Laurent Gomila - SFML developer

Carbonedioxide

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Shader problem
« Reply #2 on: August 15, 2018, 02:35:42 pm »
I've added another rendertexture inside loop and it works perfectly.  :) Thank you much.

Here's code after changes:
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>

int main() {
        sf::Sprite sprite;
        sf::Texture texture;
        if (!texture.loadFromFile("background.jpg")) {}
        sprite.setTexture(texture);

        sf::Shader blurShader;
        if (!blurShader.loadFromFile("blur.frag", sf::Shader::Fragment)) {}
        blurShader.setUniform("texture", sf::Shader::CurrentTexture);
        blurShader.setUniform("blur_radius", 0.001f);

        sf::RenderTexture rendertexture;
        rendertexture.create(1280, 720);
        rendertexture.clear();
        sf::RenderTexture rendertexture2;
        rendertexture2.create(1280, 720);
        rendertexture2.clear();

        for (int i = 50; i >= 0; i--) {
                rendertexture.draw(sprite, &blurShader);
                rendertexture.display();
                sprite.setTexture(rendertexture.getTexture());

                rendertexture2.draw(sprite, &blurShader);
                rendertexture2.display();
                sprite.setTexture(rendertexture2.getTexture());
        }
        sprite.setPosition(0, 0);

        sf::RenderWindow window(sf::VideoMode(1280, 720), "Peppa pig");
        sf::Event event;
        while (window.isOpen()) {
                while (window.pollEvent(event)){
                        if (event.type == sf::Event::Closed)
                                exit(0);
                        else if (event.key.code == sf::Keyboard::Escape)
                                exit(0);
                }

                window.clear();
                window.draw(sprite);
                window.display();
        }
}