/**
 * Filter shader
 * by Andres Colubri. February 2008.
 * 
 * This GLSL fragment shader implements a gaussian convolution filter.
 *
 */
 
/*
  Copyright (c) 2006 Andres Colubri

  This source is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This code is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  A copy of the GNU General Public License is available on the World
  Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also
  obtain it by writing to the Free Software Foundation,
  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#define KERNEL_SIZE 9

// Gaussian kernel
// 1 2 1
// 2 4 2
// 1 2 1
float kernel[KERNEL_SIZE];

uniform sampler2D tex_unit_image;
uniform vec2 tex_scale;

vec2 offset[KERNEL_SIZE];

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

    offset[0] = vec2(-tex_scale.s, -tex_scale.t);
    offset[1] = vec2(0.0, -tex_scale.t);
    offset[2] = vec2(tex_scale.s, -tex_scale.t);

    offset[3] = vec2(-tex_scale.s, 0.0);
    offset[4] = vec2(0.0, 0.0);
    offset[5] = vec2(tex_scale.s, 0.0);

    offset[6] = vec2(-tex_scale.s, tex_scale.t);
    offset[7] = vec2(0.0, tex_scale.t);
    offset[8] = vec2(tex_scale.s, tex_scale.t);

    kernel[0] = 1.0/16.0;   kernel[1] = 2.0/16.0;   kernel[2] = 1.0/16.0;
    kernel[3] = 2.0/16.0;   kernel[4] = 4.0/16.0;   kernel[5] = 2.0/16.0;
    kernel[6] = 1.0/16.0;   kernel[7] = 2.0/16.0;   kernel[8] = 1.0/16.0;

    for(i = 0; i < KERNEL_SIZE; i++)
    {
        vec4 tmp = texture2D(tex_unit_image, gl_TexCoord[0].st + offset[i]);
        sum += tmp * kernel[i];
    }

    gl_FragColor = sum;
}
