1 #define GL_GLEXT_PROTOTYPES 1
8 #include "blur_effect.h"
11 BlurEffect::BlurEffect()
15 register_float("radius", (float *)&radius);
16 register_int("direction", (int *)&direction);
19 std::string BlurEffect::output_fragment_shader()
21 return read_file("blur_effect.frag");
24 void BlurEffect::set_uniforms(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
26 Effect::set_uniforms(glsl_program_num, prefix, sampler_num);
28 // We only have 15 taps to work with, and we want that to reach out to about 2.5*sigma.
29 // Bump up the mipmap levels (giving us box blurs) until we have what we need.
30 unsigned base_mipmap_level = 0;
31 float adjusted_radius = radius;
32 float pixel_size = 1.0f;
33 while (adjusted_radius * 2.5f > 7.0f) {
35 adjusted_radius *= 0.5f;
39 glActiveTexture(GL_TEXTURE0);
40 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, base_mipmap_level);
44 if (direction == HORIZONTAL) {
45 float ps[] = { pixel_size / 1280.0f, 0.0f };
46 set_uniform_vec2(glsl_program_num, prefix, "pixel_offset", ps);
47 } else if (direction == VERTICAL) {
48 float ps[] = { 0.0f, pixel_size / 720.0f };
49 set_uniform_vec2(glsl_program_num, prefix, "pixel_offset", ps);
54 // Simple Gaussian weights for now.
55 float weight[15], total = 0.0f;
56 for (unsigned i = 0; i < 15; ++i) {
57 float z = (i - 7.0f) / adjusted_radius;
58 weight[i] = exp(-(z*z));
61 printf("[mip level %d] ", base_mipmap_level);
62 for (unsigned i = 0; i < 15; ++i) {
64 printf("%f ", weight[i]);
67 set_uniform_float_array(glsl_program_num, prefix, "weight", weight, 15);