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

Author Topic: BlendInvert: invert blend mode  (Read 4686 times)

0 Members and 1 Guest are viewing this topic.

Tenry

  • Full Member
  • ***
  • Posts: 120
  • Experienced Programmer
    • View Profile
    • Simon-Burchert.com
BlendInvert: invert blend mode
« on: March 12, 2012, 04:55:45 pm »
What about an additional blend mode for inverting?

E.g., for an editor it's useful that the grid does not have a static color (which might be similar to the background) but just inverting what's behind? (black -> white, or white -> black, for example).
Another case would be hit enemies: it's usefull to invert an enemy's sprite for a short time to indicate that it has been hit.


In OpenGL, it seems to be done like this (haven't tested it):
Code: [Select]
glBlend(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
Please note that my previous display name was "Shy Guy".

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
BlendInvert: invert blend mode
« Reply #1 on: March 12, 2012, 06:14:51 pm »
Quote
Code: [Select]
glBlend(GL_ONE_MINUS_DST_COLOR, GL_ZERO);

That would be a multiplicative blending, between source and inverted destination.

Given that we'd like to get this:
Code: [Select]
pixel.rgb = (1 - source.rgb)
pixel.a = source.a

... and that OpenGL blending follows this formula:
Code: [Select]
pixel.rgba = source.rgba * arg1 + dest.rgba * arg2
... I don't think we can get the desired effect.

Blending modes are a way to choose how source and destination are mixed together, not a way to modify how the source is drawn. You have shaders for that ;)
Laurent Gomila - SFML developer

Tenry

  • Full Member
  • ***
  • Posts: 120
  • Experienced Programmer
    • View Profile
    • Simon-Burchert.com
BlendInvert: invert blend mode
« Reply #2 on: March 12, 2012, 06:33:49 pm »
Quote from: "Laurent"
Quote
Code: [Select]
glBlend(GL_ONE_MINUS_DST_COLOR, GL_ZERO);

That would be a multiplicative blending, between source and inverted destination.

Given that we'd like to get this:
Code: [Select]
pixel.rgb = (1 - source.rgb)
pixel.a = source.a

... and that OpenGL blending follows this formula:
Code: [Select]
pixel.rgba = source.rgba * arg1 + dest.rgba * arg2
... I don't think we can get the desired effect.

Blending modes are a way to choose how source and destination are mixed together, not a way to modify how the source is drawn.


Hm... let's assume we are drawing a white image (source) onto a red surface (target). Source values are 1.f, 1.f, 1.f. Target value is 1.f, 0.f, 0.f.
For the ONE_MINUS_DST_COLOR part, 1.f is multiplied with either 1.f-1.f (red) or with 1.f-0.f (green, blue). The other parameter is "ignored" so nothing is added to the result.

1.f * (1.f-1.f) + ZERO = 0.f (RED is now 0% and hence inverted)
1.f * (1.f-0.f) + ZERO = 1.f (GREEN)
1.f * (1.f-0.f) + ZERO = 1.f (BLUE)
And that is inverted. Am I not right?


Quote from: "Laurent"
You have shaders for that ;)

I have never worked with shaders yet...
Please note that my previous display name was "Shy Guy".

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
BlendInvert: invert blend mode
« Reply #3 on: March 12, 2012, 08:27:21 pm »
You draw a white sprite and what was drawn behind it becomes inverted. Is that what you call "it works"? ;)

People who use a Invert blend mode will most likely expect what they draw to be inverted.
Laurent Gomila - SFML developer

Tenry

  • Full Member
  • ***
  • Posts: 120
  • Experienced Programmer
    • View Profile
    • Simon-Burchert.com
BlendInvert: invert blend mode
« Reply #4 on: March 12, 2012, 10:10:10 pm »
Quote from: "Laurent"
You draw a white sprite and what was drawn behind it becomes inverted. Is that what you call "it works"? ;)

People who use a Invert blend mode will most likely expect what they draw to be inverted.

Well, in my experience, I usually draw a sprite (or something else) normally, then I have a black/white image, where the white regions means "invert", and the black one "don't revert" (e.g. a grid), and draw it on top of it. If I would draw the second sprite (with the same color) with invert blending, the result is completely inverted.
Please note that my previous display name was "Shy Guy".

 

anything