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

Author Topic: BlendMode  (Read 8138 times)

0 Members and 1 Guest are viewing this topic.

cheeseboy

  • Newbie
  • *
  • Posts: 36
    • View Profile
BlendMode
« on: December 24, 2014, 01:41:02 am »
I want to draw lines as the opposite color as the background with alpha. How I do this?
This blend mode draws opposite color of background
states.blendMode = sf::BlendMode(sf::BlendMode::OneMinusDstColor, sf::BlendMode::Zero,sf::BlendMode::Add, sf::BlendMode::One, sf::BlendMode::Zero, sf::BlendMode::Add);
 
but it doesn't draw alpha I set on the lines. How do I make alpha work? I tried this but no good.
states.blendMode = sf::BlendMode(sf::BlendMode::OneMinusDstColor, sf::BlendMode::OneMinusSrcAlpha,sf::BlendMode::Add, sf::BlendMode::One, sf::BlendMode::OneMinusDstAlpha, sf::BlendMode::Add);
 
« Last Edit: December 24, 2014, 01:43:43 am by cheeseboy »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: BlendMode
« Reply #1 on: December 24, 2014, 02:03:38 am »
Short answer: If I understand your request correctly, and blend modes work the way I think they do, then blend modes simply can't do this, and they aren't the right tool for it anyway.  We'll need a better description of the problem to figure out what the appropriate tool is.

Long answer:
(click to show/hide)
« Last Edit: December 24, 2014, 02:31:19 am by Ixrec »

cheeseboy

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: BlendMode
« Reply #2 on: December 24, 2014, 10:35:40 am »
I need lines that are always visible over a dynamic image. I want alpha because when two lines overlap it makes it the intersection darker.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: BlendMode
« Reply #3 on: December 24, 2014, 11:02:06 am »
Not tried this but I would guess that it might look like this:
states.blendMode = sf::BlendMode(sf::BlendMode::OneMinusDstColor, sf::BlendMode::DstColor,sf::BlendMode::Add, sf::BlendMode::SrcAlpha, sf::BlendMode::OneMinusSrcAlpha, sf::BlendMode::Add);

Am I close?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: BlendMode
« Reply #4 on: December 24, 2014, 11:56:34 am »
I think the fundamental problem is that cheeseboy's desired behavior doesn't involve a source color, but blend modes kind of assume that you're trying to blend two different colors (well, arrays of pixels).  Hapax's suggestion may not have an explicit "SrcColor" in it, but the source color is still there in the blend equations it produces (in that case it's something like color = (1 - dc) * sc + dc * dc).

"dynamic image" doesn't tell me much (solid color randomly chosen each frame? complex shader effect? the current state of the game world? a minimap? does the line cross or connect multiple entities?), but I'm going to assume that means you cannot compute or retrieve the "opposite" color yourself on the CPU side, or it changes every frame and you can't compute/retrieve it without copying textures to images every frame, since those would be the "easy" answers.  In that case, I believe your only option is to draw the "dynamic image" with a shader that applies the lines.

Perhaps the better question is: Why do you want this particular effect in the first place?  Off the top of my head, I can't think of a scenario where you need something to be the "opposite color" but you don't know what the original color is going to be.  Maybe there's a better way to approach your problem that doesn't require shader magic.
« Last Edit: December 24, 2014, 12:00:15 pm by Ixrec »

cheeseboy

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: BlendMode
« Reply #5 on: December 24, 2014, 01:19:28 pm »
My application is a sprite polygon mask editor. I display an image beneath a grid. I need the grid to always be visible regardless of what image the user loads behind it. I'm fairly sure I can only do this with blendmode magic and/or shader magic but I can't figure out either..

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: BlendMode
« Reply #6 on: December 24, 2014, 01:52:58 pm »
I can't figure out what a "sprite polygon mask" is, but if this is an editor, then I see two options:

1) You probably need an sf::Image or some other class in RAM for your program to make edits on, so perhaps you could get the desired grid color from that before moving things to the GPU

2) Perhaps it would be better to make the grid color a configurable setting, and/or simply make the default grid lines very difficult to hide by accident (eg, a solid red line bordered by two black lines).  For example, in paint.net a "transparent" part of the image is shown with a checkerboard of gray and white squares; if I happened to be editing an image of gray and white squares, that could cause some trouble, but so far that's never been an issue for me.  Personally, I would find it surprising and disorienting if the grid lines/squares were constantly changing color in response to whatever image I loaded.

cheeseboy

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: BlendMode
« Reply #7 on: December 24, 2014, 02:10:46 pm »
Images aren't one color. If part of image is black I can't see a black line there... and that goes for any color. I need it to invert the color of underlying pixels so its visible anywhere.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: BlendMode
« Reply #8 on: December 24, 2014, 02:17:40 pm »
If you're really sure that's the effect you want, then you definitely need to draw the grid with a shader.

cheeseboy

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: BlendMode
« Reply #9 on: December 24, 2014, 02:22:35 pm »
Ok, great. Now that we've established I want to do what I said I wanted to do. How do I actually do it? I have zero idea how to work a shader,,,

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: BlendMode
« Reply #10 on: December 24, 2014, 02:23:20 pm »
It's in the tutorials: http://www.sfml-dev.org/tutorials/2.2/graphics-shader.php

Quote
Now that we've established I want to do what I said I wanted to do...
Believe it or not, it's important to do that.  A lot of questions here turn out to be the result of someone misunderstanding how things work and asking how to do something they don't actually need to do at all.
« Last Edit: December 24, 2014, 02:25:39 pm by Ixrec »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: BlendMode
« Reply #11 on: December 24, 2014, 03:09:51 pm »
To answer the original question (ie. can you do it with blend modes), you must first write the desired formula:

result = (1 - dest.color) * src.alpha

Then the generic blend mode formula:

result = src.color * src_factor OP dest.color * dest_factor

So the closest that we can achieve is:

result = src.color * 1 - dest.color * src.alpha
sf::BlendMode(sf::BlendMode::One, sf::BlendMode::SrcAlpha, sf::BlendMode::Subtract)

... which will give the desired result if your grid (src) is white. I think it's a valid solution: the grid will always be the inverse of what's behind it (*) -- so it will be black if the background is white.

(*) I haven't tested all this stuff there might be a stupid mistake that ruins the whole thing ;)
« Last Edit: December 24, 2014, 03:12:54 pm by Laurent »
Laurent Gomila - SFML developer

cheeseboy

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: BlendMode
« Reply #12 on: December 24, 2014, 03:38:03 pm »
That gives me opposite colored lines. However it loses the alpha.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: BlendMode
« Reply #13 on: December 24, 2014, 04:53:57 pm »
You can try this:

sf::BlendMode(sf::BlendMode::SrcAlpha, sf::BlendMode::SrcAlpha, sf::BlendMode::Subtract)
« Last Edit: December 25, 2014, 01:30:14 am by Laurent »
Laurent Gomila - SFML developer

cheeseboy

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: BlendMode
« Reply #14 on: December 24, 2014, 05:27:10 pm »
thats pretty close. yours is left. I would like it to look like the right if possible though.

 

anything