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

Author Topic: Create a light effect with BlendMode?  (Read 2647 times)

0 Members and 1 Guest are viewing this topic.

Clink

  • Newbie
  • *
  • Posts: 2
    • View Profile
Create a light effect with BlendMode?
« on: January 04, 2013, 03:40:03 pm »
Hi!

I'm working on a little project and I'm kinda stuck. I'm trying to create a dark screen that you can lit up with a light that follows the mouse.

The effect I'm looking for is something similar to the thing they have in Donkey Kong Country:
http://4.bp.blogspot.com/_PM97N1Dtb10/SjN41jFUNzI/AAAAAAAABRU/UCfF2AfDf2c/s400/torchlight_trouble.jpg

I believe this would be possible by first drawing the background. And then drawing a a compleatly black sprite over the the entire screen, representing the darkness. On top of the darkness sprite I draw a smaller sprite that would be the light.

My thought was that I could use a BlendMode to take the alpha value of the light and "cut through" the darkness. Showing a clear piece of the background.

The code that I have so far is this:
_darkness.setColor(sf::Color(255, 255, 255, 200));
        _light.setColor(sf::Color(0, 0, 0, 0));

        _light.setPosition(static_cast<float>(mousePos.x), static_cast<float>(mousePos.y));

        _pWindow->clear(sf::Color(0, 150, 255, 255));

        _pWindow->draw(_background);

        _pWindow->draw(_darkness);

        _pWindow->draw(_light, sf::BlendMultiply);
       
        _pWindow->display();
 

Am I even on the right track? If so, how can I fix this? If not, what is the right track?

Help is appreciated :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Create a light effect with BlendMode?
« Reply #1 on: January 04, 2013, 03:46:08 pm »
Once you've drawn the dark sprite, the pixels below are... dark. Drawing something on top of it cannot make the pixels magically reappear.

You must do it this way:
- fill a render-texture (which covers the whole window) with the dark color
- draw your light(s) to the render-texture with sf::BlendAdd
- draw the render-texture on top of your scene with sf::BlendMultiply
Laurent Gomila - SFML developer

Clink

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Create a light effect with BlendMode?
« Reply #2 on: January 04, 2013, 05:02:03 pm »
Thanks for the reply! It really made things go forward. Though using sf::BlendAdd on the light and sf::BlendMultiply on the scene didn't work.

But I played around with it and came upp with this:

_light.setColor(sf::Color(0, 0, 0, 0));

        _light.setPosition(static_cast<float>(mousePos.x), static_cast<float>(mousePos.y));

//_target is a sf::RenderTexture
        _target.clear(sf::Color(255, 255, 255, 200));

        _target.draw(_light, sf::BlendMultiply);
        //_target.draw(_light, sf::BlendAdd);

        _target.display();


        _pWindow->clear(sf::Color(0, 150, 255, 255));

        _pWindow->draw(_background);

        _darkness.setTexture(_target.getTexture() );

        _pWindow->draw(_darkness, sf::BlendAdd);
        //_pWindow->draw(_darkness, sf::BlendMultiply);

        _pWindow->display();

This works exactley like I want it to. Just one little problem...

The dark isn't that dark. In fact, it's white. My guess was that it was because of
_target.clear(sf::Color(255, 255, 255, 200)); that fill the render texture with a black color.

So I thought switching it to _target.clear(sf::Color(0, 0, 0, 200)); would give me the opposite effect.

But that made the whole render texture invisible!

What am I missing?

*Edit

Nevermind, I solved it :)

The light was the bad guy. It was also supposed to be black :P

I'll leave the code here if anyone is intrested.

_light.setColor(sf::Color(255, 255, 255, 0));

        _light.setPosition(static_cast<float>(mousePos.x), static_cast<float>(mousePos.y));

        _target.clear(sf::Color(0, 0, 0, 200));

        _target.draw(_light, sf::BlendNone);

        _target.display();


        _pWindow->clear(sf::Color(0, 150, 255, 255));

        _pWindow->draw(_background);

        _darkness.setTexture(_target.getTexture() );

        _pWindow->draw(_darkness);

        _pWindow->display();

Thanks for the help!
« Last Edit: January 04, 2013, 05:23:16 pm by Clink »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Create a light effect with BlendMode?
« Reply #3 on: January 04, 2013, 06:06:56 pm »
My solution is supposed to work (lights add together, and the lighting mask modulates the scene), but it works purely with color components, you don't have to play with alpha at all. Just like in real life :P
Laurent Gomila - SFML developer

 

anything