Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML 2 shader  (Read 3870 times)

0 Members and 1 Guest are viewing this topic.

mimipim

  • Newbie
  • *
  • Posts: 49
    • View Profile
SFML 2 shader
« 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.

lolz123

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
Re: SFML 2 shader
« Reply #1 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.
Have you heard about the new Cray super computer?  It’s so fast, it executes an infinite loop in 6 seconds.

mimipim

  • Newbie
  • *
  • Posts: 49
    • View Profile
Re: SFML 2 shader
« Reply #2 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.
« Last Edit: June 29, 2012, 09:01:07 pm by mimipim »

lolz123

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
Re: SFML 2 shader
« Reply #3 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
Have you heard about the new Cray super computer?  It’s so fast, it executes an infinite loop in 6 seconds.