SFML community forums

Help => Graphics => Topic started by: Ancurio on January 03, 2013, 08:03:37 am

Title: sf::BlendAdd explained...
Post by: Ancurio 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.
Title: Re: sf::BlendAdd explained...
Post by: Laurent 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?
Title: Re: sf::BlendAdd explained...
Post by: Ancurio 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" ^^"
Title: Re: sf::BlendAdd explained...
Post by: Laurent on January 03, 2013, 08:34:53 pm
I fixed it, should be ok now.
Title: Re: sf::BlendAdd explained...
Post by: Ancurio 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!
Title: Re: sf::BlendAdd explained...
Post by: Laurent on January 03, 2013, 10:27:42 pm
If was just one line of code to copy :P