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

Author Topic: [SFML 2.0] Simple color changing frag-shader does not work  (Read 6377 times)

0 Members and 1 Guest are viewing this topic.

Blublop

  • Newbie
  • *
  • Posts: 13
    • View Profile
[SFML 2.0] Simple color changing frag-shader does not work
« on: October 13, 2012, 07:49:56 pm »
Hey

I would like to change the color palette as an effect.
So I could replace all colors with just two different colors, so basically changing the color depth to 1 bit.
I decided to use the fragment shader :

uniform vec4 on_bit;
uniform vec4 off_bit;

void main()
{
        vec4 pixel = gl_Color;
        if (pixel.r < 255 || pixel.g < 255 || pixel.b < 255)
                pixel = on_bit;
        else
                pixel = off_bit;
       
        gl_FragColor = pixel;
}

 

And the sfml-part :

    if (!sf::Shader::isAvailable() ||
        !m_ShaderContainer[0].loadFromFile("data//Shader//1bbp.frag", sf::Shader::Fragment))
    {
          // error messages
    }
    else
    {

        std::cout << "Engine : Shader OK !\n";

        m_ShaderContainer[SHADER_1BPP].setParameter("on_bit", sf::Color::Black);
        m_ShaderContainer[SHADER_1BPP].setParameter("off_bit",sf::Color::White);
    }

But all what happens is that the the sprite applied with this shader goes black.
Any help would be appreciated!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: [SFML 2.0] Simple color changing frag-shader does not work
« Reply #1 on: October 13, 2012, 10:53:40 pm »
Quote
if (pixel.r < 255 || pixel.g < 255 || pixel.b < 255)
In shaders, color components are in [0 .. 1], not [0 .. 255].
Laurent Gomila - SFML developer

Blublop

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: [SFML 2.0] Simple color changing frag-shader does not work
« Reply #2 on: October 14, 2012, 03:40:24 am »
So if I want to replace every color(but white) with a certain color the following would be valid ?

vec4 pixel = gl_Color;
if (pixel.r < 1.0 || pixel.g < 1.0 || pixel.b < 1.0) // all non-white pixels are replaced ?
        pixel = on_bit;
else
        pixel = off_bit;
       
gl_FragColor = pixel;

While changing the code to
m_ShaderContainer[SHADER_1BPP].setParameter("on_bit", sf::Color::Black);
m_ShaderContainer[SHADER_1BPP].setParameter("off_bit",sf::Color::Blue);

The sprite goes all blue, indicating gl_Color is always blank ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: [SFML 2.0] Simple color changing frag-shader does not work
« Reply #3 on: October 14, 2012, 08:58:27 am »
It means that no color is pure white.

Try to add a small offset (remember that floating point numbers are sometimes not exact), like comparing to 0.99 instead of 1.
Laurent Gomila - SFML developer

Blublop

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: [SFML 2.0] Simple color changing frag-shader does not work
« Reply #4 on: October 14, 2012, 04:52:13 pm »
It means that no color is pure white.

Try to add a small offset (remember that floating point numbers are sometimes not exact), like comparing to 0.99 instead of 1.

I can understand that, but this time I changed the whole condition to test if the pixel is pure white and the shader always detects only pure white pixels. (meaning it goes all blue)

if (pixel.r == 1.0 && pixel.g == 1.0 && pixel.b == 1.0)
                pixel = off_bit;
(off_bit == sf::Color::Blue -> every pixel in the frag shader is pure white?)

I also tried it with the vertex shader but it doesn't help.
It would be great if someone could take a quick look into my code.




[attachment deleted by admin]

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: [SFML 2.0] Simple color changing frag-shader does not work
« Reply #5 on: October 14, 2012, 09:49:20 pm »
I'm not sure if it's the proper way to do it, but here's how I do it. (at least it works ^^ )

Add to your cpp Frag.setParameter("texture", sf::Shader::CurrentTexture);
Add to your your fragment shader uniform sampler2D texture;
and use vec4 pixel = gl_Color * texture2D(texture, gl_TexCoord[0].st);

It sets the current Texture (the one used by the sprite) and gets the color of the current pixel.

Blublop

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: [SFML 2.0] Simple color changing frag-shader does not work
« Reply #6 on: October 14, 2012, 10:54:39 pm »
I'm not sure if it's the proper way to do it, but here's how I do it. (at least it works ^^ )

Add to your cpp Frag.setParameter("texture", sf::Shader::CurrentTexture);
Add to your your fragment shader uniform sampler2D texture;
and use vec4 pixel = gl_Color * texture2D(texture, gl_TexCoord[0].st);

Thanks a lot ! It's working !
Thanks for taking a look, makes me glad you took the time = )

But still I'm quite curious about a method without using an access to the texture.
I mean if :
void main()
{
   gl_FragColor = gl_Color;
}
is actually what the standard pipeline of OpenGL does, than there must be data flowing, which represents the sprites vertex color ?

I think when applying the shader to the sprite, the shader does only retrieve the pixels behind the sprite, which is indeed the white background drawn with
App.clear() (therefor the pure white pixels I received in the shader)

I'll keep this question here for some time, because it's SFML related, but I think I might visit some OpenGL forums for more information about it ...

It sets the current Texture (the one used by the sprite) and gets the color of the current pixel.

I thought only techniques like bump-mapping or effects on textures itself would require such a method.