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

Author Topic: Alpha-Blending on top of Alpha Background  (Read 3751 times)

0 Members and 1 Guest are viewing this topic.

CC_fan

  • Newbie
  • *
  • Posts: 4
    • View Profile
Alpha-Blending on top of Alpha Background
« on: December 12, 2013, 10:48:35 pm »
Hello,
in my Game, i'm prerendering seperate chunks of Tiles, so they don't have to be rendered all in runtime, to get a better FPS. So i'm using an RenderTexture. Works fine for me, but my Textures have semi-transparent pixels on the edges to get them smooth. But when I render the Sprite on top of my Transparent background of the RenderWindow, this pixels get blended on top of a black background, so I get black lines on each Chunk-boader. If I'm using no blending, this black lines are not present, but then I'm overriding the Tiles rendered before, so this also dosn't work.
Where is the problem, or is there an work-arround?

basicly i'm using this code:
sf::RenderTexture rTexture;
rTexture.create(100,100);
rTexture.clear(sf::Transparent);
rTexture.draw(spriteXY);

// and Later
window.draw(rTexture.getTexture);
 
so the rTexture dosn't keep the Alpha of the spriteXY

Jonki

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Alpha-Blending on top of Alpha Background
« Reply #1 on: December 13, 2013, 04:04:04 am »
Sounds like you need to disable blending while drawing the RenderTexture into the window.
dafuq did I just write?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Alpha-Blending on top of Alpha Background
« Reply #2 on: December 13, 2013, 07:56:52 am »
So you want to blend tiles with other tiles, but not with the background? In other words, you'd like some edges of the tile to blend, but not the others? Don't you think you're asking too much? ;)

I don't think you can do what you want, you should find another strategy. And in case you didn't notice, sf::Color::Transparent is black with zero alpha, there's no real "transparent" color, you have to fill the color channels with values when you clear the render-target.
Laurent Gomila - SFML developer

CC_fan

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Alpha-Blending on top of Alpha Background
« Reply #3 on: December 13, 2013, 02:16:56 pm »
Year, I think that the transparent is black with zero alpha is the problem.
The actual Problem is that Alpha blending deletes the alpha chanal form the background:
For Example the Background has an Alpha of 0 and I am drawing an Sprite with an Texture of Alpha 0.5 using Alpha-Blending, I would expect the result to be Alpha 0.5 . Instead the resulting Texture has a Alpha of 1.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Alpha-Blending on top of Alpha Background
« Reply #4 on: December 13, 2013, 05:54:01 pm »
You're wrong: alpha would be 0.5 if you don't apply any blending (= channels are copied without being interpreted). If you apply alpha blending, the alpha channel is used and the result has (in this case) an alpha of 1.
Laurent Gomila - SFML developer

CC_fan

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Alpha-Blending on top of Alpha Background
« Reply #5 on: December 13, 2013, 06:10:28 pm »
Didn't I said that this happens? But still don't understand why
(0,0,0,0.5) Alpha Blended on (0,0,0,0) results in (0,0,0,1) and not (0,0,0,0.5)
So a smoothed Texture, rendered on an RenderTexture and than on the Window with an different background than black is no longer smooth.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Alpha-Blending on top of Alpha Background
« Reply #6 on: December 13, 2013, 09:11:21 pm »
Actually, the formula is different, sorry. The alpha channel is treated differently: in this case, the result is (0, 0, 0, 0.5) as you expect. But you still have the black color blended in the RGB channels.
Laurent Gomila - SFML developer

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: Alpha-Blending on top of Alpha Background
« Reply #7 on: December 13, 2013, 10:41:11 pm »
Well, the solution is to simply use the correct blend mode with premultiplied alpha textures, to avoid the dark halo: http://home.comcast.net/~tom_forsyth/blog.wiki.html#%5B%5BPremultiplied%20alpha%5D%5D
Sadly its currently not accessible through SFML and you need to use OpenGL directly.
« Last Edit: December 13, 2013, 10:45:56 pm by wintertime »

CC_fan

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Alpha-Blending on top of Alpha Background
« Reply #8 on: December 14, 2013, 05:47:45 pm »
Oh, I see and thanks for the link wintertime! Thats exactly what I searched for!