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

Author Topic: Can't set lower alpha value  (Read 1495 times)

0 Members and 1 Guest are viewing this topic.

Meelusyn

  • Newbie
  • *
  • Posts: 2
    • View Profile
Can't set lower alpha value
« on: August 19, 2020, 07:25:19 pm »
Hello

I'm trying to erase parts of a render_target with a shader by setting the alpha to 0 but it's stay to 1 (255).

here is the fragment shader :
void main() {
     
   gl_FragColor = vec4(0.0, 0.0, 0.0,       0.0       );
}

and here the sfml code :
_renderTarget.clear(sf::Color(0,0,0,      255       ));
_window.clear(sf::Color(255,255,255,255);

_renderTarget.draw(_sprite,&erasingShader);// a 32x32 sprite at mouse location

_renderTarget.display();

sf::Sprite tempSprite(_renderTarget.getTexture());

_window.draw(tempSprite, sf::BlendAlpha);

_window.display();
 

Am I missing something ? it should displays a white square but its all black;




Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Can't set lower alpha value
« Reply #1 on: August 19, 2020, 08:39:04 pm »
The default alpha-blending mode blends using the source alpha. In your case it is 0, so you're doing nothing to the render-texture. What you want is to disable alpha-blending (sf::Blending::None), so that your 0 alpha is actually written instead of interpreted.

And using a shader to generate transparent pixels is a little overkill, a simple shape with a transparent color will do the job.
Laurent Gomila - SFML developer

Meelusyn

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Can't set lower alpha value
« Reply #2 on: August 20, 2020, 12:40:36 am »
Thanks, i didn't know shader wasn't replacing blendmodes;

I put sf::BlendNone instead of the shader and it works, but i am using a shader because i want not to erase all the square of the sprite(i have simplified the code for looking what i was doing wrong);

but when i do
draw(sprite, sf::RenderStates(sf::BlendNone, sprite.getTransform(), sprite.getTexture(), &erasingShader));
it still doesn't work
(when it will works i will put the renderStates as member instead of recreating it each draw call i suppose it would be better ? but here it just for the test)
also i have seen we could use vertex array instead of sprite now, thats great.

Edit : with a renderState outside the calldraw its work :)

« Last Edit: August 20, 2020, 02:30:29 pm by Meelusyn »

 

anything