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

Author Topic: Custom blend modes  (Read 10946 times)

0 Members and 1 Guest are viewing this topic.

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Custom blend modes
« on: August 12, 2012, 11:33:45 pm »
Hello there,
I'd like to bring up the discussion about the custom blend modes. They have been mentioned in a couple threads, but there has never been a decision or a detailed discussion about them. I think blend modes are an important part of graphics programming and that a custom blend mode functionality would be really useful.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Custom blend modes
« Reply #1 on: August 13, 2012, 12:12:34 am »
You're aware of the blend modes SFML already provides (see documentation which can be applied to the RenderState)?
What should be added or what's needed/important?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Custom blend modes
« Reply #2 on: August 13, 2012, 08:03:22 am »
Yes, when you request a new feature, please tell us:
- what you want
- why you think it should be added to SFML

Just saying "blend modes should be added" won't help the discussion to start ;)
Laurent Gomila - SFML developer

Acrobat

  • Full Member
  • ***
  • Posts: 153
    • View Profile
Re: Custom blend modes
« Reply #3 on: September 24, 2012, 10:23:12 am »
Up  ;D

About blends :
Currentrly there is no blend mode to make sprite brighter (Color -> White, only Color -> Black).
With blend mode BlendAlpha and White color, we can see sprite as it suppose to be (Color), if we change the color to black, sprite will become dim. But if we want it to change from Color to White, we can't.
I modified the function that set blend state :
void RenderTarget::applyBlendMode(BlendMode mode)
{
    switch (mode)
    {
        // Alpha blending
        // glBlendFuncSeparateEXT is used when available to avoid an incorrect alpha value when the target
        // is a RenderTexture -- in this case the alpha value must be written directly to the target buffer
        default :
        case BlendAlpha :
                        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
            if (GLEW_EXT_blend_func_separate)
                glCheck(glBlendFuncSeparateEXT(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA));
            else
                glCheck(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
            break;

        // Additive blending
        case BlendAdd :
                        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
            glCheck(glBlendFunc(GL_SRC_ALPHA, GL_ONE));
            break;

        // Multiplicative blending
        case BlendMultiply :
                        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
            glCheck(glBlendFunc(GL_DST_COLOR, GL_ZERO));
            break;

        // No blending
        case BlendNone :
                        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
            glCheck(glBlendFunc(GL_ONE, GL_ZERO));
            break;
                       
                // BlendAlphaAdd
                case BlendAlphaAdd :
                        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
                        glCheck(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
                        break;
    }

    m_cache.lastBlendMode = mode;
}
 

now you can use new mode to highlight sprite
BlendAlpha + White == BlendAlphaAdd + Black
BlendAlpha + Black = Black Sprite
BlendAlphaAdd + White = White Sprite
with this combination you can highlight or dim sprites

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Custom blend modes
« Reply #4 on: September 24, 2012, 10:31:19 am »
What is the use case for such a blend mode?

(by the way, I don't think that BlendAlphaAdd is a good name, it sounds like a synonym for BlendAdd, which also applies alpha)
Laurent Gomila - SFML developer

Acrobat

  • Full Member
  • ***
  • Posts: 153
    • View Profile
Re: Custom blend modes
« Reply #5 on: September 24, 2012, 10:39:05 am »
It used in many effects, look :


Uploaded with ImageShack.us
thats the same AlphaBlend but addictive (not modulative)
P.S. maybe you will find something interesting here http://www.andersriggelsen.dk/glblendfunc.php
« Last Edit: September 24, 2012, 11:18:01 am by Acrobat »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Custom blend modes
« Reply #6 on: September 24, 2012, 11:09:21 am »
Your image is broken.
Laurent Gomila - SFML developer

Acrobat

  • Full Member
  • ***
  • Posts: 153
    • View Profile
Re: Custom blend modes
« Reply #7 on: September 24, 2012, 11:18:09 am »
edited

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Custom blend modes
« Reply #8 on: September 24, 2012, 11:47:55 am »
Thanks.

I see what it does, but when do you use it? What useful effects can you achieve with it?
Laurent Gomila - SFML developer

Acrobat

  • Full Member
  • ***
  • Posts: 153
    • View Profile
Re: Custom blend modes
« Reply #9 on: September 24, 2012, 11:51:29 am »
we are making ho game, when user don't click anywhere we highlight the next item to click (something like hint), also we have gui system, and to make nice element we have to use some special blends + animations (mouse enter/leave/down/up) and so on

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Custom blend modes
« Reply #10 on: September 24, 2012, 02:08:56 pm »
I see.

It's hard to integrate, because it's not exactly a blend mode. Blend modes define how the drawn entity will blend with the background; yours define how the entity's color blends with the entity's texture. So if I want to integrate it, I have to think about it first.
Laurent Gomila - SFML developer

Acrobat

  • Full Member
  • ***
  • Posts: 153
    • View Profile
Re: Custom blend modes
« Reply #11 on: September 24, 2012, 02:43:11 pm »
well.... we use this for some time and no problems (Win + Mac), btw the grey color at the background is white sprite with 127 rgb values

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Custom blend modes
« Reply #12 on: September 24, 2012, 03:04:20 pm »
Quote
we use this for some time and no problems
Sure it works. It's just an extra OpenGL state to handle. I was talking about design issues.
Laurent Gomila - SFML developer

Acrobat

  • Full Member
  • ***
  • Posts: 153
    • View Profile
Re: Custom blend modes
« Reply #13 on: September 24, 2012, 03:10:49 pm »
ohh, i see now. (we don't use push/pop states  ;D)

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Custom blend modes
« Reply #14 on: September 24, 2012, 05:20:19 pm »
I'm sorry I completely forgot about this issue. I would really like to see subtractive blending being added. Also what I intentionally asked for in this thread is a possibility to add your own custom blend modes, by letting you specify the way the colors are blended. Of course this is a feature for advanced users, but it would make discussions like "can you add blend mode xy" obsolete, because you could just add it yourself.