jarcode-foss / glava

GLava - OpenGL audio spectrum visualizer
GNU General Public License v3.0
1.17k stars 59 forks source link

Bloom based on volume #204

Open Marshy00 opened 3 years ago

Marshy00 commented 3 years ago

Bloom around drawn bars based on volume would be a really nice touch, and I couldn't get it working on my own.

LuanAdemi commented 3 years ago

I recently tried to achieve a bloom/glow effect with glava. (Although not based on volume)

Since glava renders it's visualizers using fragment shaders, this is actually pretty straight forward. I modified this shader to be able to use it as a second fragment shader for the bars module.

Here is the code:

in vec4 gl_FragCoord;
#request uniform "prev" tex
uniform sampler2D tex;    /* screen texture    */

out vec4 fragment; /* output */

#include "@bars.glsl"
#include ":bars2.glsl"

uniform float glow_size = .6;
uniform vec3 glow_colour = vec3(0, 0, 0);
uniform float glow_intensity = 1.4;
uniform float glow_threshold = .5;

void main() {
    fragment = texture(tex, gl_FragCoord.xy);

    // if the alpha value is below a certain threshold
    if (fragment.a <= glow_threshold) {
      ivec2 size = textureSize(tex, 0);
      float uv_x = gl_FragCoord.x;
      float uv_y = gl_FragCoord.y;

      float sum = 0.0;
      for (int n = 0; n < 9; ++n) {
          uv_y = (gl_FragCoord.y) + (glow_size * float(n - 4.5));
          float h_sum = 0.0;
          h_sum += texelFetch(tex, ivec2(uv_x - (4.0 * glow_size), uv_y), 0).a;
          h_sum += texelFetch(tex, ivec2(uv_x - (3.0 * glow_size), uv_y), 0).a;
          h_sum += texelFetch(tex, ivec2(uv_x - (2.0 * glow_size), uv_y), 0).a;
          h_sum += texelFetch(tex, ivec2(uv_x - glow_size, uv_y), 0).a;
          h_sum += texelFetch(tex, ivec2(uv_x, uv_y), 0).a;
          h_sum += texelFetch(tex, ivec2(uv_x + glow_size, uv_y), 0).a;
          h_sum += texelFetch(tex, ivec2(uv_x + (2.0 * glow_size), uv_y), 0).a;
          h_sum += texelFetch(tex, ivec2(uv_x + (3.0 * glow_size), uv_y), 0).a;
          h_sum += texelFetch(tex, ivec2(uv_x + (4.0 * glow_size), uv_y), 0).a;
          sum += h_sum / 9.0;
      }
        fragment = vec4(COLOR.rgb, (sum / 9.0) * glow_intensity);
    }
}

This is the result:

You might be able to adjust this shader to achieve your desired effect.