Okay, I'll try something out. Do you know any great tutorials on GLSL?
Sorry, I don't
But here are my shaders for rendering a sprite with some brightness and saturation (I use it for blinking animations)
sprite_shader.loadFromMemory(
"uniform float brightness, min_saturation, max_saturation;" \
"uniform sampler2D texture;" \
"void main() {" \
" vec4 pixel = gl_Color * texture2D(texture, gl_TexCoord[0].xy);" \
" pixel = vec4(clamp(pixel.rgb, min_saturation, max_saturation), pixel.a);" \
" gl_FragColor = vec4(pixel.rgb * brightness, pixel.a);" \
"}",
sf::Shader::Fragment
);
and for rendering a lighttexture (which is a major part of my lighting implementation).
light_shader.loadFromMemory(
"uniform vec2 center;" \
"uniform float radius;" \
"uniform float intensity;" \
"void main() {" \
" float dist = distance(gl_TexCoord[0].xy, center);" \
" float color = exp(-9.0*dist/radius);" \
" gl_FragColor = vec4(gl_Color.xyz * color, 1.0);" \
"}",
sf::Shader::Fragment
);
Setting up the
sprite_shader before rendering looks like this:
sprite_shader.setParameter("brightness", brightness);
sprite_shader.setParameter("min_saturation", min_saturation);
sprite_shader.setParameter("max_saturation", max_saturation);
sprite_shader.setParameter("texture", sf::Shader::CurrentTexture);
Prerendering a light texture is shown here:
sf::RenderTexture buffer;
buffer.create(size, size);
// prepare shadeable object
sf::VertexArray array{sf::Quads, 4u};
array[0].position = {0.f, 0.f};
array[1].position = {size, 0.f};
array[2].position = {size, size};
array[3].position = {0.f, size};
for (std::size_t i = 0u; i < 4u; ++i) {
array[i].texCoords = array[i].position;
array[i].color = sf::Color::White;
}
// render lightmap
buffer.clear(sf::Color::Black);
light_shader.setParameter("radius", radius);
light_shader.setParameter("center", {size / 2.f, size / 2.f});
buffer.draw(array, &light_shader);
buffer.display();
Later the resulting texture is applied to the scene using a suitable blend mode. The lighting texture is attached (so you can see the result).
Maybe those shaders can help to when experimenting.