From 7679213e2649d84058a34c23d681500b67dac02f Mon Sep 17 00:00:00 2001 From: Vincent Lejeune Date: Sun, 2 Nov 2014 02:35:02 +0100 Subject: [PATCH] Use exp algorithm for the gaussian blur --- data/shaders/gaussian6h.frag | 23 +++++++++++++++-------- data/shaders/gaussian6v.frag | 23 +++++++++++++++-------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/data/shaders/gaussian6h.frag b/data/shaders/gaussian6h.frag index ee18af23f..f8d43c27d 100644 --- a/data/shaders/gaussian6h.frag +++ b/data/shaders/gaussian6h.frag @@ -1,6 +1,8 @@ uniform sampler2D tex; uniform vec2 pixel; +float sigma = 1.; + // Gaussian separated blur with radius 6. out vec4 FragColor; @@ -8,17 +10,22 @@ out vec4 FragColor; void main() { vec2 uv = gl_FragCoord.xy * pixel; - vec4 sum = vec4(0.0); float X = uv.x; float Y = uv.y; - sum += texture(tex, vec2(X - 5.13333 * pixel.x, Y)) * 0.00640869; - sum += texture(tex, vec2(X - 3.26667 * pixel.x, Y)) * 0.083313; - sum += texture(tex, vec2(X - 1.4 * pixel.x, Y)) * 0.305481; - sum += texture(tex, vec2(X, Y)) * 0.209473; - sum += texture(tex, vec2(X + 1.4 * pixel.x, Y)) * 0.305481; - sum += texture(tex, vec2(X + 3.26667 * pixel.x, Y)) * 0.083313; - sum += texture(tex, vec2(X + 5.13333 * pixel.x, Y)) * 0.00640869; + float g0, g1, g2; + g0 = 1.0 / (sqrt(2.0 * 3.14) * sigma); + g1 = exp(-0.5 / (sigma * sigma)); + g2 = g1 * g1; + vec4 sum = texture(tex, vec2(X, Y)) * g0; + g0 *= g1; + g1 *= g2; + for (int i = 1; i < 3; i++) { + sum += texture(tex, vec2(X - i * pixel.x, Y)) * g0; + sum += texture(tex, vec2(X + i * pixel.x, Y)) * g0; + g0 *= g1; + g1 *= g2; + } FragColor = sum; } diff --git a/data/shaders/gaussian6v.frag b/data/shaders/gaussian6v.frag index f9765a2cd..7576c02b0 100644 --- a/data/shaders/gaussian6v.frag +++ b/data/shaders/gaussian6v.frag @@ -1,6 +1,8 @@ uniform sampler2D tex; uniform vec2 pixel; +float sigma = 1.; + // Gaussian separated blur with radius 6. out vec4 FragColor; @@ -8,17 +10,22 @@ out vec4 FragColor; void main() { vec2 uv = gl_FragCoord.xy * pixel; - vec4 sum = vec4(0.0); float X = uv.x; float Y = uv.y; - sum += texture(tex, vec2(X, Y - 5.13333 * pixel.y)) * 0.00640869; - sum += texture(tex, vec2(X, Y - 3.26667 * pixel.y)) * 0.083313; - sum += texture(tex, vec2(X, Y - 1.4 * pixel.y)) * 0.305481; - sum += texture(tex, vec2(X, Y)) * 0.209473; - sum += texture(tex, vec2(X, Y + 1.4 * pixel.y)) * 0.305481; - sum += texture(tex, vec2(X, Y + 3.26667 * pixel.y)) * 0.083313; - sum += texture(tex, vec2(X, Y + 5.13333 * pixel.y)) * 0.00640869; + float g0, g1, g2; + g0 = 1.0 / (sqrt(2.0 * 3.14) * sigma); + g1 = exp(-0.5 / (sigma * sigma)); + g2 = g1 * g1; + vec4 sum = texture(tex, vec2(X, Y)) * g0; + g0 *= g1; + g1 *= g2; + for (int i = 1; i < 3; i++) { + sum += texture(tex, vec2(X, Y - i * pixel.y)) * g0; + sum += texture(tex, vec2(X, Y + i * pixel.y)) * g0; + g0 *= g1; + g1 *= g2; + } FragColor = sum; }