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

Author Topic: Alpha blendMode  (Read 4506 times)

0 Members and 1 Guest are viewing this topic.

GamDev

  • Newbie
  • *
  • Posts: 29
    • View Profile
Alpha blendMode
« 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;      
}

 
« Last Edit: July 14, 2020, 04:10:52 pm by GamDev »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Alpha blendMode
« Reply #1 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

GamDev

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: Alpha blendMode
« Reply #2 on: July 24, 2020, 06:24:23 pm »
you can show an example, I don't really understand how to do this

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Alpha blendMode
« Reply #3 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

GamDev

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: Alpha blendMode
« Reply #4 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?


Paul

  • Jr. Member
  • **
  • Posts: 78
    • View Profile
Re: Alpha blendMode
« Reply #5 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

GamDev

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: Alpha blendMode
« Reply #6 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);

Paul

  • Jr. Member
  • **
  • Posts: 78
    • View Profile
Re: Alpha blendMode
« Reply #7 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;


« Last Edit: August 24, 2020, 02:12:13 pm by Paul »

GamDev

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: Alpha blendMode
« Reply #8 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