Bloom Shader

Thanks to Gabor’s new fbo-pixels branch, we now have render to texture functionality working in fluxus, as an exciting extension to the pixels primitive. This should be in the main branch soon. One of the things I want to use this for is full screen effects, such as bloom/glow effects which are applied to a rendered scene as a single pass with a glsl hardware shader.

The jpeg compression really isn’t kind, but this video shows a before and after comparison:

There will be a fluxus example with this soon, but here is my work-in-progress shader:

varying vec2 texc;
uniform sampler2D tex;
uniform float kernel;
uniform float scale;
uniform float thresh;

void main()
{
    vec4 sum = vec4(0);
    int i=0;

    // mess of for loops due to gpu compiler/hardware limitations
    int j=-2;
    for(i=-2; i<=2; i++) sum+=texture2D(tex,texc+vec2(i,j)*kernel);
    j=-1;
    for(i=-2; i<=2; i++) sum+=texture2D(tex,texc+vec2(i,j)*kernel);
    j=0;
    for(i=-2; i<=2; i++) sum+=texture2D(tex,texc+vec2(i,j)*kernel);
    j=1;
    for(i=-2; i<=2; i++) sum+=texture2D(tex,texc+vec2(i,j)*kernel);
    j=2;
    for(i=-2; i<=2; i++) sum+=texture2D(tex,texc+vec2(i,j)*kernel);
    sum/=25.0;

    vec4 s=texture2D(tex, texc);
    gl_FragColor=s;

    // use the blurred colour if it's bright enough
    if (length(sum)>thresh)
    {
        gl_FragColor=sum*scale;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *