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

Author Topic: Subtractiv Blend mode  (Read 6345 times)

0 Members and 2 Guests are viewing this topic.

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Subtractiv Blend mode
« on: July 19, 2014, 12:30:27 pm »
Hello everybody!
I wanted to give the newly implemented blend modes a try and see if they can solve an issue cleaner than what I use right now (a shader). What I am trying to acchive is that a render texture fades out over time.
So my idea is to simply draw a black rect with very low alpha over the entire render texture each frame to make it slowly fade. When I use the default alpha blending mode I get weird visual artefacts. (it seems to be related to some value being very small and causing problems when multiplyed; seems to be a know problem) Thats why I wanted to try a subtractive blend mode.
I tryed to simply take the Alpha Mode and exchange the blend function from add to subtract, but that didn't work.
const sf::BlendMode BlendSubtractive(sf::BlendMode::SrcAlpha, sf::BlendMode::OneMinusSrcAlpha, sf::BlendMode::Subtract,
                                     sf::BlendMode::One, sf::BlendMode::OneMinusSrcAlpha, sf::BlendMode::Subtract);
I am a little confused to what option to choose. Could somebody with a better OpenGL knowledge explain the correct way to me?
Thanks in advance,
Foaly

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Subtractiv Blend mode
« Reply #1 on: July 20, 2014, 10:16:17 am »
I can't offer a definitive answer but this site offers a good visual explanation of the different openGL blend modes

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Subtractiv Blend mode
« Reply #2 on: July 20, 2014, 10:25:03 am »
Quote
What I am trying to acchive is that a render texture fades out over time.
You should explain that better. Because it can be many things:
- fade from opaque to transparent
- fade to black
...
Laurent Gomila - SFML developer

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Subtractiv Blend mode
« Reply #3 on: July 20, 2014, 12:03:23 pm »
Hmm good question. Now that I think about it I would say fading from color to transparent would be the best, because I use a renderTexture anyway then I have the option to chose a different background color when I draw to the window.

@fallahn: Awesome site! I am still overwhelmed by all the options, but I will play around a bit.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Subtractiv Blend mode
« Reply #4 on: July 20, 2014, 12:12:15 pm »
To fade to transparent, you just need to slowly decrease the sprite (the one which draws the content of the render-texture)'s alpha over time.
Laurent Gomila - SFML developer

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Subtractiv Blend mode
« Reply #5 on: July 20, 2014, 04:09:06 pm »
Yeah that is how I usually do it. But in this case that technique won't work, because I constantly draw to a rendertexture without clearing it. That's why I want to draw a semi-transparent rectangle over it, to make "older" stuff fade out.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Subtractiv Blend mode
« Reply #6 on: July 20, 2014, 04:23:11 pm »
Why don't you clear it? Can you explain what you're trying to do (other than fading it)? Please try to be as precise as possible so that we don't have to ask a third time ;)
Laurent Gomila - SFML developer

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Subtractiv Blend mode
« Reply #7 on: July 20, 2014, 06:57:11 pm »
That's why I want to draw a semi-transparent rectangle over it, to make "older" stuff fade out.
In your case I think you could clear your render texture with a semi-transparent color.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Subtractiv Blend mode
« Reply #8 on: July 20, 2014, 07:06:34 pm »
G., that won't work; clear does not consider blend modes, it overwrites the buffer's content with the specified color.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Subtractiv Blend mode
« Reply #9 on: July 20, 2014, 07:37:03 pm »
I want to draw a semi-transparent rectangle over it, to make "older" stuff fade out.
This suggests that you want to have different alpha values on one render texture. That would require an alpha mask, which I don't think render textures support.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Subtractiv Blend mode
« Reply #10 on: July 20, 2014, 07:52:09 pm »
Sorry for not being percise. Ok here is what I am trying to acchive.
Think of it as if you are drawing with a pen on a paper. Every frame I draw a line from the mouse position of the previous frame to the mouse position of the current frame. The line is then drawn into a rendertexture. The rendertexture is drawn into the window. The rendertexture is never cleared to keep the picture.
Now if I want to let the lines slowly fade out based on how long they have been on the rendertexture I thought I could simply draw a black/transparent rect with a little alpha on top of it with a subtractive blend mode. And since I have problems understanding the blend mode equations I thought I'd ask :D

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Subtractiv Blend mode
« Reply #11 on: July 20, 2014, 08:05:04 pm »
I think you need a sprite for each different alpha value so multiple render textures might be needed for full window control, or you could just adjusting each object's alpha separately as it is drawn (seems the simpler, more practical route).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Subtractiv Blend mode
« Reply #12 on: July 20, 2014, 08:38:12 pm »
Quote
The rendertexture is never cleared to keep the picture.
That's the problem. This is not the typical way of doing things. I would rather store the visible lines in a container, together with their current alpha value (or their lifetime, to be able to deduce an alpha value) and redraw everything everytime, adjusting the alpha values between two draws.
Laurent Gomila - SFML developer

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Subtractiv Blend mode
« Reply #13 on: July 20, 2014, 09:44:07 pm »
Hmm I know this isn't the typical way of doing this, but it should still be possible, right?
I mean right now I am pretty much doing the same thing. I implemented this before SFML supported different render modes, so I am using a shader that subtracts a factor from the current pixels alpha and two render textures to make the effect persistent.
Shouldn't it be possible to do the exact same thing with the correct subtractive blend mode?