SFML community forums

Help => Graphics => Topic started by: mimipim on June 29, 2012, 01:44:23 pm

Title: SFML 2 shader
Post by: mimipim on June 29, 2012, 01:44:23 pm
Imagine a big sprite for level background. I have to draw the sprite with its own texture applying more than one shaders, for example I want to put 3 light shaders on the texture. How can I achive this?

Currently what I have is:
sf::Shader light;
light.loadFromFile(....);
// Here setting some glsl parameters including light position

sf::RenderStates rs;
rs.shader = &light;

win.draw(bg, rs);
 

Well what design I need to put 3 lights on the bg sprite's texture.
Title: Re: SFML 2 shader
Post by: lolz123 on June 29, 2012, 04:18:28 pm
You can't have multiple shaders active at once.

The easiest solution is to just draw bounding geometry for your lights with a shader active, one at a time, and just use additive blending to accumulate the lighting.
Title: Re: SFML 2 shader
Post by: mimipim on June 29, 2012, 08:59:17 pm
Can you (or someone else) please write a example code of the design.
EDIT: Because I don't really understand some of the english terms and can't understand the idea correct.
Title: Re: SFML 2 shader
Post by: lolz123 on June 30, 2012, 09:40:25 pm
I can't give you a short and sweet example, but I can give you an explanation:

After drawing your scene:

- switch to a render texture the size of the screen
- clear it
- bind shader
- set blend mode to additive
- loop through lights:
       > set the light uniforms
       > draw square that represents the light (tightest fit possible to limit the number of fragments the shader is run on)
- switch to main framebuffer
- switch blend mode to multiplicative
- render the render texture

Also, you can use Let There Be Light if you don't want to do it yourself, you will get shadows as well then  ;) http://en.sfml-dev.org/forums/index.php?topic=6635.0