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

Author Topic: Frag shader doesn't work as intended  (Read 1846 times)

0 Members and 2 Guests are viewing this topic.

simps

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Frag shader doesn't work as intended
« on: October 04, 2013, 05:01:04 pm »
Hello
Basically, I'm doing the following:
(SFML 2.1 statically linked)

void TestFunc ()
{
        sf::RenderWindow Window;
        Window.create(sf::VideoMode(800, 600), "TestFunc");

        //rect
        sf::RectangleShape Rect;
        Rect.setPosition(300.0f, 200.0f);
        Rect.setSize(sf::Vector2f(200.0f, 100.0f));
        Rect.setOutlineThickness(4);
        Rect.setFillColor(sf::Color(0, 0, 0, 0));
        Rect.setOutlineColor(sf::Color(0x00, 0x33, 0xff));//light blue

        //texture to render into
        sf::RenderTexture RenderTexture;
        bool bRes = RenderTexture.create(512, 512);
        if (bRes == false) return;

        //pixelate shader from examples
        sf::Shader ShaderPixelateF;
        bRes = ShaderPixelateF.loadFromFile("dat/shaders/pixelate_f.glsl", sf::Shader::Type::Fragment);
        if (bRes == false) return;
        ShaderPixelateF.setParameter("texture", sf::Shader::CurrentTexture);
        ShaderPixelateF.setParameter("pixel_threshold", 10.0f);

        //sprite to render shader with
        sf::Sprite Sprite;

        //main cycle
        while (Window.isOpen())
        {
                sf::Event Event;
                while (Window.pollEvent(Event))
                {
                        if (Event.type == sf::Event::Closed || (Event.type == sf::Event::KeyPressed && Event.key.code == sf::Keyboard::Key::Escape))
                        {
                                Window.close();
                        }
                }

                RenderTexture.clear(sf::Color(0, 0, 0, 0));
                RenderTexture.draw(Rect);
                RenderTexture.display();

                Sprite.setTexture(RenderTexture.getTexture());

                Window.clear(sf::Color(0, 128, 0));//green

                Window.draw(Sprite, &ShaderPixelateF);

//              Window.draw(Sprite);

                Window.display();
        }
}
 

and pixelate_f.glsl is:

uniform sampler2D texture;
uniform float pixel_threshold;

void main()
{
    float factor = 1.0 / (pixel_threshold + 0.001);
        vec2 pos = floor(gl_TexCoord[0].xy * factor + 0.5) / factor;
        gl_FragColor = texture2D(texture, pos) * gl_Color;

//      gl_FragColor = vec4 (1.0, 1.0, 0.0, 1.0);//yellow
}
 

I'm seeing only green background.
Uncommenting line in cpp brings rect (rendering to texture works, sprite has texture properly set).
Uncommenting line in shader brings yellow box (shader works).
Where I'm doing wrong?

Thanks in advance
simps

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Frag shader doesn't work as intended
« Reply #1 on: October 04, 2013, 10:25:51 pm »
Try getting rid of gl_Color in the shader.  I think that's supposed to be a color interpolated from vertices rather than a property of the texture (obviously you're already getting the texture color from texture2D()).

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Frag shader doesn't work as intended
« Reply #2 on: October 04, 2013, 11:11:50 pm »
void main()
{
    float factor = 1.0 / (pixel_threshold + 0.001);//factor is 0.1 basically(approximation - good enough)
    vec2 pos = floor(gl_TexCoord[0].xy * factor + 0.5) / factor;// xy are 0.0..1.0 so they become 0.0..0.1 + 0.5 which is 0.5..0.6 and 0.0 after floor, then they get divided so they are still zero, and pixels at 0.0,0.0 in your render texture have zero alpha because you cleared it with transparent
    gl_FragColor = texture2D(texture, pos) * gl_Color;//basically 0.0(corner of render texture) * 1.0(color) in all four channels
    gl_FragColor.a=1.0;//now you can see rect
    gl_FragColor.rb=vec2(1.0,1.0)-pos.xy;//now you can see 100% magenta rect because pos.xy are both 0.0
}
Whatever you're doing you got your algorithm wrong.

Quote
Try getting rid of gl_Color in the shader.  I think that's supposed to be a color interpolated from vertices rather than a property of the texture (obviously you're already getting the texture color from texture2D()).
Doesn't matter, defaults to 1.0 in all four channels in sf::Sprite, does nothing when multiplied by other vec4.
« Last Edit: October 04, 2013, 11:16:16 pm by FRex »
Back to C++ gamedev with SFML in May 2023

simps

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Frag shader doesn't work as intended
« Reply #3 on: October 08, 2013, 03:04:52 pm »
Thanks for detailed answer!
I haven't checked shader code myself because I've snatched it from \SFML-2.1-windows-vc9-32bits\SFML-2.1\examples\shader\resources\pixelate.frag
And it's a mystery how it works in the example.

simps

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Frag shader doesn't work as intended
« Reply #4 on: October 08, 2013, 03:12:56 pm »
Quote
And it's a mystery how it works in the example.
Then why don't you simply look at how it is used in the example? You would easily notice that pixel_threshold is supposed to be less than 1.
Laurent Gomila - SFML developer

simps

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Frag shader doesn't work as intended
« Reply #5 on: October 08, 2013, 05:19:04 pm »
Oh, mouse coords are in [0..1] range
Thank you Laurent
I was terribly inattentive  :-\