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

Author Topic: sf::BlendAdd explained...  (Read 3541 times)

0 Members and 1 Guest are viewing this topic.

Ancurio

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
sf::BlendAdd explained...
« on: January 03, 2013, 08:03:37 am »
Hi all!
I was under the impression that I understood how BlendAdd works... but I don't apparently.
Take this testcase:
sf::RenderTexture source;
source.create(400, 400);
source.clear(sf::Color(255, 0, 0));

sf::RenderTexture target;
target.create(400, 400);
target.clear(sf::Color(0, 0, 0, 0));

sf::Sprite sprite(source.getTexture());
sprite.setColor(sf::Color(255, 255, 255, 127));
target.draw(sprite, sf::BlendAlpha);

sf::Color c = target.getTexture().copyToImage().getPixel(200, 200);
qDebug() << c.r << c.g << c.b << c.a;
 
(replace qDebug() with your favorite out stream..)

We create a source texture, fill it with 100% opaque red.
Then we paint the source texture over the (0, 0, 0, 0) cleared
target texture, with opacity set to half (127) in Sprite::setColor().

Now, in using BlendAlpha, everything works as expected:
"127 0 0 127"
The opacity halves the red color value, and it is painted over the (0, 0, 0, 0) pixel.
The alpha value itself(!) remains unchanged, it is still 127.

Now, change the BlendAlpha above to BlendAdd. What happens is:
"127 0 0 63"
This is very unexpected. I was thinking the red value would get halved again,
the alpha would remain unchanged(!), and everything would just be added
to the (0, 0, 0, 0) resulting in (127, 0, 0, 127), but apparently, for some reason,
the alpha is multiplied by itself (ie. halved too)? How does that work, is it openGL specific?
And how could I possibly work around it?
What I would like to achieve is to have the target texture at full red (255, 0, 0, 255)
when I paint it using BlendAdd and half-opacity red.

Thanks.
« Last Edit: January 03, 2013, 08:46:54 am by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::BlendAdd explained...
« Reply #1 on: January 03, 2013, 09:00:25 am »
There's a workaround to avoid the alpha being multiplied by itself, but it is only applied to the BlendAlpha mode. I should definitely add it for BlendAdd too, thanks for your report.

Quote
What I would like to achieve is to have the target texture at full red (255, 0, 0, 255)
when I paint it using BlendAdd and half-opacity red.
You start with alpha = 0, then you add alpha = 127; how could you get 255 as a result?
Laurent Gomila - SFML developer

Ancurio

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Re: sf::BlendAdd explained...
« Reply #2 on: January 03, 2013, 11:03:47 am »

Quote
What I would like to achieve is to have the target texture at full red (255, 0, 0, 255)
when I paint it using BlendAdd and half-opacity red.
You start with alpha = 0, then you add alpha = 127; how could you get 255 as a result?
Yeah, sorry, that doesn't make sense. I meant to say "when I paint it with half-opacity twice" ^^"
« Last Edit: January 03, 2013, 11:05:52 am by Ancurio »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::BlendAdd explained...
« Reply #3 on: January 03, 2013, 08:34:53 pm »
I fixed it, should be ok now.
Laurent Gomila - SFML developer

Ancurio

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Re: sf::BlendAdd explained...
« Reply #4 on: January 03, 2013, 09:34:23 pm »
I fixed it, should be ok now.
Wow, on GitHub already? YOU'RE THE BEST! Thank you so much!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::BlendAdd explained...
« Reply #5 on: January 03, 2013, 10:27:42 pm »
If was just one line of code to copy :P
Laurent Gomila - SFML developer

 

anything