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

Author Topic: Question about shader sfml2  (Read 2473 times)

0 Members and 1 Guest are viewing this topic.

sormo

  • Newbie
  • *
  • Posts: 6
    • View Profile
Question about shader sfml2
« on: June 26, 2011, 12:31:21 pm »
Hello,
I have question about using fragment shader in sfml 2. I am trying to apply shader to whole screen and replace one particular color with different one.
This is my code:
Code: [Select]

#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
    sf::Shape shape = sf::Shape::Rectangle(400,300,100,100,sf::Color(0,0,255));
    sf::Shader shader;
    sf::Image image;

    if (sf::Shader::IsAvailable())
        shader.LoadFromFile("shader.frag");

    while (window.IsOpened())
    {
        sf::Event event;
        while (window.PollEvent(event))
        {
            if (event.Type == sf::Event::Closed)
                window.Close();
        }
        window.Clear(sf::Color(0,0,0));
        window.Draw(shape);
        if (sf::Shader::IsAvailable())
        {
            image.CopyScreen(window);
            window.Draw(sf::Sprite(image), shader);
        }
        window.Display();
    }
    return EXIT_SUCCESS;
}

Here is my fragment shader:
Code: [Select]

void main(void)
{
    vec4 shadow = vec4(0.5, 0.5, 0.5, 0.5);

    if (gl_Color.r == 0.0f && gl_Color.g == 0.0f && gl_Color.b == 1.0f)
        gl_FragColor = shadow;
    else
        gl_FragColor = gl_Color;
}

When I do not apply shader, screen is black with blue square, but when I paint screen with shader, all pixels are white. There are no error messages on console so looks like shader is compiled succesfully. For some reason rgb values of gl_Color is set to 1. Can someone please tell me what am I doing wrong?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Question about shader sfml2
« Reply #1 on: July 02, 2011, 10:39:47 pm »
gl_Color is the overall color (the one you set with SetColor), not the pixel color. You must do texture lookup to get the actual pixel color. Lookup at the provided examples to know how to do this.
Laurent Gomila - SFML developer

sormo

  • Newbie
  • *
  • Posts: 6
    • View Profile
Question about shader sfml2
« Reply #2 on: July 11, 2011, 12:29:00 pm »
Thank you for reply. With those modifications it works:

Code: [Select]

...
    if (sf::Shader::IsAvailable())
    {
        shader.LoadFromFile("default.frag");
        std::string texName("tex");
        shader.SetTexture(texName, image);
        shader.SetCurrentTexture(texName);
    }
...

shader:
Code: [Select]

uniform sampler2D tex;

void main(void)
{
    vec4 curColor = texture2D(tex, gl_TexCoord[0].xy);
    vec4 shadow = vec4(0.5, 0.5, 0.5, 1.0);

    if (curColor.r == 0.0f && curColor.g == 0.0f && curColor.b == 1.0f)
        gl_FragColor = shadow;
    else
        gl_FragColor = curColor;
}