SFML community forums

Help => Graphics => Topic started by: GamDev on July 14, 2020, 04:06:54 pm

Title: Alpha blendMode
Post by: GamDev on July 14, 2020, 04:06:54 pm
Hi, please tell me how to make the alpha channel merge and not duplicate
https://imgur.com/a/EyvuxJH

sf::RenderStates states;

states.blendMode = sf::BlendMode(sf::BlendMode::One, sf::BlendMode::OneMinusSrcAlpha);
states.shader = &myShader;

Shader:
uniform sampler2D texture;

void main()
{
    float alpha = texture2D(texture, gl_TexCoord[0].xy).a;
    if(alpha < 0.1)
                {
                        discard;
                }
    vec4 color = vec4(0.098, 0.098, 0.098, 0.5);
    gl_FragColor = color;      
}

 
Title: Re: Alpha blendMode
Post by: Hapax on July 14, 2020, 09:19:54 pm
You could draw them all to a render texture first at full alpha and then draw the render texture with adjusted alpha. Wouldn't even need blend modes for this.
Title: Re: Alpha blendMode
Post by: GamDev on July 24, 2020, 06:24:23 pm
you can show an example, I don't really understand how to do this
Title: Re: Alpha blendMode
Post by: Hapax on July 27, 2020, 11:41:00 pm
Since I don't your code, I'll make many assumptions.

This is just the bit we're talking about...

// setup
const sf::Uint8 transparencyValue{ 128u };
const sf::Vector2u windowSize{ 800u 600u };
sf::RenderWindow window(sf::VideoMode(windowSize.x, windowSize.y));
sf::RenderTexture renderTexture;
renderTexture.create(windowSize.x, windowSize.y);

// main loop
while (window.isOpen())
{
    // process events

    // perform updates

    // draw to render texture
    renderTexture.clear(sf::Color::Transparent);
    for (auto& shadow : shadows)
        renderTexture.draw(shadow);
    renderTexture.display();

    // prepare render sprite (to draw render texture to window)
    sf::Sprite renderSprite(renderTexture.getTexture());
    renderSprite.setColor(sf::Color(255u, 255u, 255u, transparencyValue));

    // update window
    window.clear();
    window.draw(renderSprite);
    window.draw(otherStuffOnTop);
    window.display();
}

I didn't test this code but it should show you the idea behind it.
Title: Re: Alpha blendMode
Post by: GamDev on August 22, 2020, 09:21:58 am
This is how it works thanks.
But there is one problem:
I have a 200x200 tile map, (tile size is 64x64), there are different objects on the map (trees)
Sprites are drawn only those that are visible on the screen, if you move the camera then the shadows remain only at 0,0 coordinates, why?

Title: Re: Alpha blendMode
Post by: Paul on August 22, 2020, 04:43:44 pm
Because renderTexture where you draw shadows have coordinates [0; 0,] you must update renderSprite position to match the view position
Title: Re: Alpha blendMode
Post by: GamDev on August 24, 2020, 10:42:35 am
Because renderTexture where you draw shadows have coordinates [0; 0,] you must update renderSprite position to match the view position

i change the coordinates of the sprite.but no shadows are drawn. Why ?
    sf::Sprite renderSprite(renderTexture.getTexture());
    renderSprite.setColor(sf::Color(255u, 255u, 255u, transparencyValue));
        renderSprite.setPosition(cameraPosition);
Title: Re: Alpha blendMode
Post by: Paul on August 24, 2020, 02:04:33 pm
I'm not sure without code how is cameraPosition calculated but don't forget you must also recalculate the position of the shadows which are drawn into renderTexture. For example renderTexture have dimensions 800 x 600 and you move camera to position [250; 0] (top left corner), then simply if you draw shadow with position [950; 0] you'll never see it. It must be corrected.

Something like:
shadowSpritePosition -= cameraPosition;


Title: Re: Alpha blendMode
Post by: GamDev on August 24, 2020, 09:17:00 pm
I'm not sure without code how is cameraPosition calculated but don't forget you must also recalculate the position of the shadows which are drawn into renderTexture. For example renderTexture have dimensions 800 x 600 and you move camera to position [250; 0] (top left corner), then simply if you draw shadow with position [950; 0] you'll never see it. It must be corrected.

Something like:
shadowSpritePosition -= cameraPosition;

I do something like this, but the shadows do not move further
also i noticed if add this code:
renderTexture.setView(camera);
shadows began to move behind the camera, but their positions do not correspond to objects