Make a new system for meta-effects, and convert the blur to use it. Hides the two...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 3 Oct 2012 14:46:15 +0000 (16:46 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 3 Oct 2012 14:46:15 +0000 (16:46 +0200)
blur_effect.cpp
blur_effect.h
effect.h
effect_chain.cpp
main.cpp

index fd81aa8..f2d5d03 100644 (file)
 
 // Must match blur_effect.frag.
 #define NUM_TAPS 16
+       
+BlurEffect::BlurEffect() {
+       hpass = new SingleBlurPassEffect();
+       hpass->set_int("direction", SingleBlurPassEffect::HORIZONTAL);
+       vpass = new SingleBlurPassEffect();
+       vpass->set_int("direction", SingleBlurPassEffect::VERTICAL);
+}
+
+void BlurEffect::add_self_to_effect_chain(std::vector<Effect *> *chain) {
+       hpass->add_self_to_effect_chain(chain);
+       vpass->add_self_to_effect_chain(chain);
+}
+
+bool BlurEffect::set_float(const std::string &key, float value) {
+       if (!hpass->set_float(key, value)) {
+               return false;
+       }
+       return vpass->set_float(key, value);
+}
 
-BlurEffect::BlurEffect()
+SingleBlurPassEffect::SingleBlurPassEffect()
        : radius(3.0f),
          direction(HORIZONTAL)
 {
@@ -19,12 +38,12 @@ BlurEffect::BlurEffect()
        register_int("direction", (int *)&direction);
 }
 
-std::string BlurEffect::output_fragment_shader()
+std::string SingleBlurPassEffect::output_fragment_shader()
 {
        return read_file("blur_effect.frag");
 }
 
-void BlurEffect::set_uniforms(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
+void SingleBlurPassEffect::set_uniforms(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
 {
        Effect::set_uniforms(glsl_program_num, prefix, sampler_num);
 
index 09c6f14..ee890c2 100644 (file)
@@ -3,16 +3,38 @@
 
 #include "effect.h"
 
+class SingleBlurPassEffect;
+
 class BlurEffect : public Effect {
 public:
        BlurEffect();
+
+       virtual std::string output_fragment_shader() {
+               assert(false);
+       }
+       virtual void set_uniforms(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num) {
+               assert(false);
+       }
+
+       virtual bool needs_many_samples() const { return true; }
+       virtual bool needs_mipmaps() const { return true; }
+       virtual void add_self_to_effect_chain(std::vector<Effect *> *chain);
+       virtual bool set_float(const std::string &key, float value);
+       
+private:
+       SingleBlurPassEffect *hpass, *vpass;
+};
+
+class SingleBlurPassEffect : public Effect {
+public:
+       SingleBlurPassEffect();
        std::string output_fragment_shader();
 
        virtual bool needs_many_samples() const { return true; }
        virtual bool needs_mipmaps() const { return true; }
 
        void set_uniforms(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
-
+       
        enum Direction { HORIZONTAL = 0, VERTICAL = 1 };
 
 private:
index b545f85..cd6f008 100644 (file)
--- a/effect.h
+++ b/effect.h
@@ -3,6 +3,7 @@
 
 #include <map>
 #include <string>
+#include <vector>
 
 #include <GL/gl.h>
 
@@ -36,6 +37,9 @@ public:
        virtual bool needs_srgb_primaries() const { return true; }
        virtual bool needs_many_samples() const { return false; }
        virtual bool needs_mipmaps() const { return false; }
+       virtual void add_self_to_effect_chain(std::vector<Effect *> *chain) {
+               chain->push_back(this);
+       }
 
        virtual std::string output_convenience_uniforms() const;
        virtual std::string output_fragment_shader() = 0;
@@ -43,10 +47,10 @@ public:
        virtual void set_uniforms(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num);
 
        // Neither of these take ownership.
-       bool set_int(const std::string&, int value);
-       bool set_float(const std::string &key, float value);
-       bool set_vec2(const std::string &key, const float *values);
-       bool set_vec3(const std::string &key, const float *values);
+       virtual bool set_int(const std::string&, int value);
+       virtual bool set_float(const std::string &key, float value);
+       virtual bool set_vec2(const std::string &key, const float *values);
+       virtual bool set_vec3(const std::string &key, const float *values);
 
 protected:
        // Neither of these take ownership.
index 85c8be3..6f53008 100644 (file)
@@ -64,7 +64,7 @@ void EffectChain::normalize_to_linear_gamma()
        } else {
                GammaExpansionEffect *gamma_conversion = new GammaExpansionEffect();
                gamma_conversion->set_int("source_curve", current_gamma_curve);
-               effects.push_back(gamma_conversion);
+               gamma_conversion->add_self_to_effect_chain(&effects);
        }
        current_gamma_curve = GAMMA_LINEAR;
 }
@@ -75,7 +75,7 @@ void EffectChain::normalize_to_srgb()
        ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
        colorspace_conversion->set_int("source_space", current_color_space);
        colorspace_conversion->set_int("destination_space", COLORSPACE_sRGB);
-       effects.push_back(colorspace_conversion);
+       colorspace_conversion->add_self_to_effect_chain(&effects);
        current_color_space = COLORSPACE_sRGB;
 }
 
@@ -91,7 +91,7 @@ Effect *EffectChain::add_effect(EffectId effect_id)
                normalize_to_srgb();
        }
 
-       effects.push_back(effect);
+       effect->add_self_to_effect_chain(&effects);
        return effect;
 }
 
index 5e02d70..f00abaa 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -172,8 +172,7 @@ 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 *hblur_effect = chain.add_effect(EFFECT_BLUR);
-       Effect *vblur_effect = chain.add_effect(EFFECT_BLUR);
+       Effect *blur_effect = chain.add_effect(EFFECT_BLUR);
        Effect *vignette_effect = chain.add_effect(EFFECT_VIGNETTE);
        //chain.add_effect(EFFECT_MIRROR);
        chain.add_output(inout_format);
@@ -218,11 +217,7 @@ int main(int argc, char **argv)
                vignette_effect->set_float("inner_radius", inner_radius);
                //vignette_effect->set_vec2("center", (float[]){ 0.7f, 0.5f });
 
-               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);
+               blur_effect->set_float("radius", blur_radius);
 
                chain.render_to_screen(src_img);