Make the blur into a simple, Gaussian horizontal blur. Still not very good.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 2 Oct 2012 22:45:32 +0000 (00:45 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 2 Oct 2012 22:45:32 +0000 (00:45 +0200)
blur_effect.cpp
blur_effect.frag
effect.cpp
effect.h
main.cpp

index e945295..ddf542e 100644 (file)
@@ -8,7 +8,7 @@
 #include "util.h"
 
 BlurEffect::BlurEffect()
-       : radius(0.3f)
+       : radius(3.0f)
 {
        register_float("radius", (float *)&radius);
 }
@@ -21,4 +21,24 @@ std::string BlurEffect::output_fragment_shader()
 void BlurEffect::set_uniforms(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
 {
        Effect::set_uniforms(glsl_program_num, prefix, sampler_num);
+
+       //glActiveTexture(GL_TEXTURE0);
+       //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 3);
+       //check_error();
+
+       set_uniform_float(glsl_program_num, prefix, "pixel_offset", 1.0f / 1280.0f);  // FIXME
+
+       // Simple Gaussian weights for now.
+       float weight[15], total = 0.0f;
+       for (unsigned i = 0; i < 15; ++i) {
+               float z = (i - 7.0f) / radius;
+               weight[i] = exp(-(z*z));
+               total += weight[i];
+       }
+       for (unsigned i = 0; i < 15; ++i) {
+               weight[i] /= total;
+               printf("%f\n", weight[i]);
+       }
+       printf("\n");
+       set_uniform_float_array(glsl_program_num, prefix, "weight", weight, 15);
 }
index bfa1d16..51ff39a 100644 (file)
@@ -1,17 +1,24 @@
 // A simple, very stupid horizontal blur. Will be fixed soonish.
 
+uniform float PREFIX(pixel_offset);
+uniform float PREFIX(weight)[15];
+
 vec4 FUNCNAME(vec2 tc) {
        vec4 x = LAST_INPUT(tc);
        return
-               vec4(0.1) * LAST_INPUT(tc + vec2(-0.010f, 0.0)) +
-               vec4(0.1) * LAST_INPUT(tc + vec2(-0.008f, 0.0)) +
-               vec4(0.1) * LAST_INPUT(tc + vec2(-0.006f, 0.0)) +
-               vec4(0.1) * LAST_INPUT(tc + vec2(-0.004f, 0.0)) +
-               vec4(0.2) * LAST_INPUT(tc + vec2(-0.002f, 0.0)) +
-               vec4(0.3) * LAST_INPUT(tc + vec2(-0.000f, 0.0)) +
-               vec4(0.2) * LAST_INPUT(tc + vec2( 0.002f, 0.0)) +
-               vec4(0.1) * LAST_INPUT(tc + vec2( 0.004f, 0.0)) +
-               vec4(0.1) * LAST_INPUT(tc + vec2( 0.006f, 0.0)) +
-               vec4(0.1) * LAST_INPUT(tc + vec2( 0.008f, 0.0)) +
-               vec4(0.1) * LAST_INPUT(tc + vec2( 0.010f, 0.0));
+               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)[ 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));
 }
index edf100c..4cc1775 100644 (file)
@@ -33,6 +33,18 @@ void set_uniform_float(GLuint glsl_program_num, const std::string &prefix, const
        check_error();
 }
 
+void set_uniform_float_array(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values, size_t num_values)
+{
+       std::string name = prefix + "_" + key;
+       GLint l = glGetUniformLocation(glsl_program_num, name.c_str());
+       if (l == -1) {
+               return;
+       }
+       check_error();
+       glUniform1fv(l, num_values, values);
+       check_error();
+}
+
 void set_uniform_vec2(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values)
 {
        std::string name = prefix + "_" + key;
index 196682f..2f0172a 100644 (file)
--- a/effect.h
+++ b/effect.h
@@ -25,6 +25,7 @@ struct RGBTriplet {
 // Convenience functions that deal with prepending the prefix.
 void set_uniform_int(GLuint glsl_program_num, const std::string &prefix, const std::string &key, int value);
 void set_uniform_float(GLuint glsl_program_num, const std::string &prefix, const std::string &key, float value);
+void set_uniform_float_array(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values, size_t num_values);
 void set_uniform_vec2(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values);
 void set_uniform_vec3(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values);
 
index 17cbeca..e4690ba 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -175,10 +175,6 @@ int main(int argc, char **argv)
        chain.add_output(inout_format);
        chain.finalize();
 
-       //glGenerateMipmap(GL_TEXTURE_2D);
-       //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 4);
-       //check_error();
-
        // generate a PDO to hold the data we read back with glReadPixels()
        // (Intel/DRI goes into a slow path if we don't read to PDO)
        glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);