Hello guys,
I wanted to start making some light effects with a fragment shader, but I get some strange behaviour.
Thats the issue:
I made a simple class which draws the light on a renderTexture to have multiple lights, however the mixing seems not like it should be for me and I get the strange flickering. I have a vector of the lights and if I make green after blue and red, the flickering is gone, which is quite strange to me.
I can't find the mistake though.
Thats how I draw the lights on the texture:
game.cpp:
void Game::render()
{
// Clears the texture
texture.clear();
// Draws all lights on the texture
for(auto&& light : lightList)
light.render(texture);
// Draws the texture
window.draw(sf::Sprite(texture.getTexture()));
}
And pointLight.cpp:
void PointLight::render(sf::RenderTexture& texture)
{
// Sets the parameters
shader.setParameter("radius", light.getRadius());
shader.setParameter("color", light.getFillColor());
shader.setParameter("texture", sf::Shader::CurrentTexture);
shader.setParameter("center", sf::Vector2f(light.getPosition().x + light.getRadius(), light.getPosition().y + light.getRadius()));
// Draws the light on the texture
texture.draw(sf::Sprite(texture.getTexture()), &shader);
}
And of course the shader:
uniform vec4 color;
uniform vec2 center;
uniform float radius;
uniform sampler2D texture;
void main()
{
// Takes the pixel and calculates the difference to the center of the light
vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);
float difference = distance(vec2(gl_FragCoord), center) / radius;
// Calculates the alpha value and sets the light color
float alpha = 1.0 - difference;
vec4 light = vec4(color.x, color.y, color.z, alpha);
// The actual color is the addition of the pixel color and the light color
vec4 col = pixel + light;
gl_FragColor = vec4(col.xyz, alpha);
}
Hopefully somebody knows what I am making wrong.
Thx and Greetings!
Geheim