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:
#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:
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?