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

Author Topic: Getting Rid Of Alphas Below A Certain Threshold  (Read 2196 times)

0 Members and 1 Guest are viewing this topic.

jdogg2727

  • Newbie
  • *
  • Posts: 3
    • View Profile
Getting Rid Of Alphas Below A Certain Threshold
« on: February 13, 2017, 11:25:05 pm »
I am currently following a tutorial on making 2D metaballs, http://nullcandy.com/2d-metaballs-in-xna/ and I'm trying to translate it to SFML.

currently I have the additive blending working, but I can't figure out how to get rid of alpha values below a certain point and make the alpha values above a certain point opaque.

From this...


To this...


Can I do that with SFML BlendModes? I'm still really new to a lot of the graphical side of SFML so I don't know which way would be the right way to go about it, thanks for any help ahead of time!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Getting Rid Of Alphas Below A Certain Threshold
« Reply #1 on: February 14, 2017, 01:04:50 am »
This should be possible with blend modes. Checkout the description in the documentation, especially the custom equation part. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Getting Rid Of Alphas Below A Certain Threshold
« Reply #2 on: February 14, 2017, 01:20:57 am »
I don't think this is possible purely with blend modes. They merely change the way incoming fragment data is combined into the target frame buffer, they don't discriminate let alone discard entire fragments as I understand from the pictures/description. The cleanest way, as one might guess from what I just said, is to use a simple fragment shader that lifts the alpha value of fragments or outright discards them.

Something similar to this:
uniform float upper;
uniform float lower;

void main()
{
    vec4 pixel = gl_Color;

    if(pixel.a > upper)
        pixel.a = 1.0;

    if(pixel.a < lower)
        discard;

    gl_FragColor = pixel;
}
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: Getting Rid Of Alphas Below A Certain Threshold
« Reply #3 on: February 14, 2017, 03:08:45 am »
 i have similar problem with opengl transparency in 3D app. the blend might be useful when depth buffer is enable since the depth test does not care if the fragment has transparency or not so the transparent parts are written to the depth buffer as any other value. but not in case, so i tried as possible to avoid blending.

if you are using shader, i believe  what binary1248 suggests will work for you. i wrote simple test based on the link to see if it could be done without using shader or blending since the fragment equation is known to us and need to construct color pattern for destination texture. the result is bit close to the yours.


« Last Edit: February 14, 2017, 03:13:04 am by Mortal »

jdogg2727

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Getting Rid Of Alphas Below A Certain Threshold
« Reply #4 on: February 14, 2017, 06:48:31 pm »
Ok cool, I would like to try the shader implementaion, would I need a Vertex shader and a Fragment shader? I tried implementing the shader and I'm just getting a white screen or artifacts, and I'm using just a fragment shader.

uniform float threshold;

void main()
{
    vec4 pixel = gl_Color;
    if (pixel.a >= threshold){
        pixel.a = 0.5f;
    }
    else{
        discard;
    }

    gl_FragColor = pixel;
}