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

Author Topic: Blend with previous frame using coefficent  (Read 2700 times)

0 Members and 1 Guest are viewing this topic.

dbiton

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Blend with previous frame using coefficent
« on: May 29, 2020, 11:46:30 pm »
Is there a way to draw to a RenderTexture and apply a blend using a coefficient?
i.e:
out.rgb = new.rgb * c + old.rgb * (1-c)

I have looked into BlendMode, but it seems like only predefined multipliers are defined (I might be missing something).
I know this can be solved by looping over every pixel in the texture and doing it manually, but this is obviously more suited for the GPU.
I can probably use a shader, but I'm very inexperienced with them so I figured I would ask you guys before delving into that.

If it's relevant, I'm trying to achieve an effect where the previous frames linger on and slowly decay away.

Thanks in advance!

Edit: I've also tried to clear the texture with a color whose alpha is not 255 to "partially clear" it, but it didn't work.
« Last Edit: May 29, 2020, 11:52:51 pm by dbiton »

unlight

  • Newbie
  • *
  • Posts: 33
    • View Profile
    • Email
Re: Blend with previous frame using coefficent
« Reply #1 on: May 30, 2020, 01:59:26 am »
I think that a shader is going to be the most effective solution for this problem. Try not to worry about experience, the SFML shaders are incredibly easy to use. See the Fragment Shader heading in this tutorial which is similar to the logic you would need:

https://www.sfml-dev.org/tutorials/2.5/graphics-shader.php

When multiplying the color of the texture, you can do something like:

gl_FragColor = gl_Color * vec4(pixel.r, pixel.g, pixel.b, myCustomAlphaValue);

You can bind multiple textures simultaneously for access in the shader code so you can combine the pixel data however you see fit.
« Last Edit: May 30, 2020, 02:01:23 am by unlight »

dbiton

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Blend with previous frame using coefficent
« Reply #2 on: May 30, 2020, 08:04:41 pm »
I think that a shader is going to be the most effective solution for this problem. Try not to worry about experience, the SFML shaders are incredibly easy to use.

You were right, it wasn't hard at all. thanks for the help! :)