I did try this already but your post made me look into it again and I stumbled upon how render states work. With a custom shader coupled with the sf::BlendAdd blend mode I managed to make the code much cleaner looking
You don't need three shaders. One shader is enough, just set a uniform variable that contains the color. Setting a single uniform is probably cheaper than rebinding the whole shader, but more importantly, you have no code duplication.
By the way, when talking about clean looking code:
1. I would either use the constructor to set all arguments or none, but not mix constructor and member assignment.
sf::RenderStates rs(&shader);
rs.blendMode = sf::BlendAdd;
// ->
sf::RenderStates rs;
rs.shader = &shader;
rs.blendMode = sf::BlendAdd;
2. Use range-based for loops for iteration. Then you don't have that ugly double dereferencing
for (std::vector<LightDemo::MaskedSprite*>::const_iterator it = spriteVector.begin(); it < spriteVector.end(); it++) {
sf::Sprite* s_ptr = (*it)->getSprite();
...
}
// ->
for (LightDemo::MaskedSprite* sprite : spriteVector) {
sf::Sprite* s_ptr = sprite->getSprite();
...
}
I'd also avoid identifiers such as "s_ptr" because they contain zero useful information. Identifiers should contain the variable's purpose. The type ("ptr") is not so important and reminescent of outdated Hungarian Notation.
And this here:
// Masking
if ((*it)->drawToRed())
rt_sceneMask_RGB.draw(*s_ptr, rs_red);
if ((*it)->drawToGreen())
rt_sceneMask_RGB.draw(*s_ptr, rs_green);
if ((*it)->drawToBlue())
rt_sceneMask_RGB.draw(*s_ptr, rs_blue);
can also be handled in the shader. Set a uniform to tell the shader which color channels to mask, then draw
once, not three times. This can be handled super-fast by float multiplication, you don't even need if statements in your GLSL code.
And for further optimizations, if they are necessary, we really need concrete numbers (i.e. time measurements).
sf::VertexArray would be an option, for example, but it's pointless to complicate the whole code if the bottleneck lies somewhere else.