From f25eed80a570ae049f03b098757a070188efbc39 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Wed, 3 Oct 2012 01:01:13 +0200 Subject: [PATCH] Add vertical blurring, and fix a bug where not all taps would be used. --- blur_effect.cpp | 16 ++++++++++++++-- blur_effect.frag | 32 ++++++++++++++++---------------- blur_effect.h | 3 +++ main.cpp | 11 +++++++++-- 4 files changed, 42 insertions(+), 20 deletions(-) diff --git a/blur_effect.cpp b/blur_effect.cpp index 7ca895a..00c8fa0 100644 --- a/blur_effect.cpp +++ b/blur_effect.cpp @@ -3,14 +3,17 @@ #include #include #include +#include #include "blur_effect.h" #include "util.h" BlurEffect::BlurEffect() - : radius(3.0f) + : radius(3.0f), + direction(HORIZONTAL) { register_float("radius", (float *)&radius); + register_int("direction", (int *)&direction); } std::string BlurEffect::output_fragment_shader() @@ -37,7 +40,16 @@ void BlurEffect::set_uniforms(GLuint glsl_program_num, const std::string &prefix glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, base_mipmap_level); check_error(); - set_uniform_float(glsl_program_num, prefix, "pixel_offset", pixel_size / 1280.0f); // FIXME + // FIXME + if (direction == HORIZONTAL) { + float ps[] = { pixel_size / 1280.0f, 0.0f }; + set_uniform_vec2(glsl_program_num, prefix, "pixel_offset", ps); + } else if (direction == VERTICAL) { + float ps[] = { 0.0f, pixel_size / 720.0f }; + set_uniform_vec2(glsl_program_num, prefix, "pixel_offset", ps); + } else { + assert(false); + } // Simple Gaussian weights for now. float weight[15], total = 0.0f; diff --git a/blur_effect.frag b/blur_effect.frag index 51ff39a..8c0a01d 100644 --- a/blur_effect.frag +++ b/blur_effect.frag @@ -1,24 +1,24 @@ -// A simple, very stupid horizontal blur. Will be fixed soonish. +// A simple unidirectional blur. -uniform float PREFIX(pixel_offset); +uniform vec2 PREFIX(pixel_offset); uniform float PREFIX(weight)[15]; vec4 FUNCNAME(vec2 tc) { vec4 x = LAST_INPUT(tc); return - vec4(PREFIX(weight)[ 0]) * LAST_INPUT(vec2(tc.x - 7.0 * PREFIX(pixel_offset), tc.y)) + - vec4(PREFIX(weight)[ 1]) * LAST_INPUT(vec2(tc.x - 6.0 * PREFIX(pixel_offset), tc.y)) + - vec4(PREFIX(weight)[ 2]) * LAST_INPUT(vec2(tc.x - 5.0 * PREFIX(pixel_offset), tc.y)) + - vec4(PREFIX(weight)[ 3]) * LAST_INPUT(vec2(tc.x - 4.0 * PREFIX(pixel_offset), tc.y)) + - vec4(PREFIX(weight)[ 4]) * LAST_INPUT(vec2(tc.x - 3.0 * PREFIX(pixel_offset), tc.y)) + - vec4(PREFIX(weight)[ 5]) * LAST_INPUT(vec2(tc.x - 2.0 * PREFIX(pixel_offset), tc.y)) + - vec4(PREFIX(weight)[ 6]) * LAST_INPUT(vec2(tc.x - PREFIX(pixel_offset), tc.y)) + + vec4(PREFIX(weight)[ 0]) * LAST_INPUT(tc - 7.0 * PREFIX(pixel_offset)) + + vec4(PREFIX(weight)[ 1]) * LAST_INPUT(tc - 6.0 * PREFIX(pixel_offset)) + + vec4(PREFIX(weight)[ 2]) * LAST_INPUT(tc - 5.0 * PREFIX(pixel_offset)) + + vec4(PREFIX(weight)[ 3]) * LAST_INPUT(tc - 4.0 * PREFIX(pixel_offset)) + + vec4(PREFIX(weight)[ 4]) * LAST_INPUT(tc - 3.0 * PREFIX(pixel_offset)) + + vec4(PREFIX(weight)[ 5]) * LAST_INPUT(tc - 2.0 * PREFIX(pixel_offset)) + + vec4(PREFIX(weight)[ 6]) * LAST_INPUT(tc - PREFIX(pixel_offset)) + vec4(PREFIX(weight)[ 7]) * LAST_INPUT(tc) + - vec4(PREFIX(weight)[ 8]) * LAST_INPUT(vec2(tc.x + PREFIX(pixel_offset), tc.y)) + - vec4(PREFIX(weight)[ 9]) * LAST_INPUT(vec2(tc.x + 2.0 * PREFIX(pixel_offset), tc.y)) + - vec4(PREFIX(weight)[10]) * LAST_INPUT(vec2(tc.x + 3.0 * PREFIX(pixel_offset), tc.y)) + - vec4(PREFIX(weight)[11]) * LAST_INPUT(vec2(tc.x + 4.0 * PREFIX(pixel_offset), tc.y)) + - vec4(PREFIX(weight)[12]) * LAST_INPUT(vec2(tc.x + 5.0 * PREFIX(pixel_offset), tc.y)); - vec4(PREFIX(weight)[13]) * LAST_INPUT(vec2(tc.x + 6.0 * PREFIX(pixel_offset), tc.y)); - vec4(PREFIX(weight)[14]) * LAST_INPUT(vec2(tc.x + 7.0 * PREFIX(pixel_offset), tc.y)); + vec4(PREFIX(weight)[ 8]) * LAST_INPUT(tc + PREFIX(pixel_offset)) + + vec4(PREFIX(weight)[ 9]) * LAST_INPUT(tc + 2.0 * PREFIX(pixel_offset)) + + vec4(PREFIX(weight)[10]) * LAST_INPUT(tc + 3.0 * PREFIX(pixel_offset)) + + vec4(PREFIX(weight)[11]) * LAST_INPUT(tc + 4.0 * PREFIX(pixel_offset)) + + vec4(PREFIX(weight)[12]) * LAST_INPUT(tc + 5.0 * PREFIX(pixel_offset)) + + vec4(PREFIX(weight)[13]) * LAST_INPUT(tc + 6.0 * PREFIX(pixel_offset)) + + vec4(PREFIX(weight)[14]) * LAST_INPUT(tc + 7.0 * PREFIX(pixel_offset)); } diff --git a/blur_effect.h b/blur_effect.h index 0657136..09c6f14 100644 --- a/blur_effect.h +++ b/blur_effect.h @@ -13,8 +13,11 @@ public: void set_uniforms(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num); + enum Direction { HORIZONTAL = 0, VERTICAL = 1 }; + private: float radius; + Direction direction; }; #endif // !defined(_BLUR_EFFECT_H) diff --git a/main.cpp b/main.cpp index 6fb92b1..a5890a3 100644 --- a/main.cpp +++ b/main.cpp @@ -172,7 +172,8 @@ int main(int argc, char **argv) chain.add_input(inout_format); Effect *lift_gamma_gain_effect = chain.add_effect(EFFECT_LIFT_GAMMA_GAIN); Effect *saturation_effect = chain.add_effect(EFFECT_SATURATION); - Effect *blur_effect = chain.add_effect(EFFECT_BLUR); + Effect *hblur_effect = chain.add_effect(EFFECT_BLUR); + Effect *vblur_effect = chain.add_effect(EFFECT_BLUR); Effect *vignette_effect = chain.add_effect(EFFECT_VIGNETTE); //chain.add_effect(EFFECT_MIRROR); chain.add_output(inout_format); @@ -216,7 +217,13 @@ int main(int argc, char **argv) vignette_effect->set_float("radius", radius); vignette_effect->set_float("inner_radius", inner_radius); //vignette_effect->set_vec2("center", (float[]){ 0.7f, 0.5f }); - blur_effect->set_float("radius", blur_radius); + + hblur_effect->set_int("direction", 0); + hblur_effect->set_float("radius", blur_radius); + + vblur_effect->set_int("direction", 1); + vblur_effect->set_float("radius", blur_radius); + chain.render_to_screen(src_img); glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1); -- 2.39.2