Add a glow effect, and an effect that linearly mixes two sources (because glow needed...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 5 Oct 2012 23:17:39 +0000 (01:17 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 5 Oct 2012 23:18:24 +0000 (01:18 +0200)
Makefile
effect_chain.cpp
effect_id.h
glow_effect.cpp [new file with mode: 0644]
glow_effect.h [new file with mode: 0644]
mix_effect.cpp [new file with mode: 0644]
mix_effect.frag [new file with mode: 0644]
mix_effect.h [new file with mode: 0644]

index b79bd78..7f7e7c1 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@ CXX=g++
 CXXFLAGS=-Wall -g
 LDFLAGS=-lSDL -lSDL_image -lGL
 OBJS=main.o util.o widgets.o effect.o effect_chain.o input.o
-OBJS += lift_gamma_gain_effect.o gamma_expansion_effect.o gamma_compression_effect.o colorspace_conversion_effect.o saturation_effect.o vignette_effect.o mirror_effect.o blur_effect.o diffusion_effect.o sandbox_effect.o
+OBJS += lift_gamma_gain_effect.o gamma_expansion_effect.o gamma_compression_effect.o colorspace_conversion_effect.o saturation_effect.o vignette_effect.o mirror_effect.o blur_effect.o diffusion_effect.o glow_effect.o mix_effect.o sandbox_effect.o
 
 test: $(OBJS)
        $(CXX) -o test $(OBJS) $(LDFLAGS)
index f4706a4..a8b66ce 100644 (file)
@@ -22,6 +22,8 @@
 #include "vignette_effect.h"
 #include "blur_effect.h"
 #include "diffusion_effect.h"
+#include "glow_effect.h"
+#include "mix_effect.h"
 #include "input.h"
 
 EffectChain::EffectChain(unsigned width, unsigned height)
@@ -85,6 +87,10 @@ Effect *instantiate_effect(EffectId effect)
                return new BlurEffect();
        case EFFECT_DIFFUSION:
                return new DiffusionEffect();
+       case EFFECT_GLOW:
+               return new GlowEffect();
+       case EFFECT_MIX:
+               return new MixEffect();
        }
        assert(false);
 }
index 545a7be..227cf97 100644 (file)
@@ -17,6 +17,10 @@ enum EffectId {
        EFFECT_VIGNETTE,
        EFFECT_BLUR,
        EFFECT_DIFFUSION,
+       EFFECT_GLOW,
+
+       // Combining.
+       EFFECT_MIX,
 };
 
 #endif // !defined(_EFFECT_ID_H)
diff --git a/glow_effect.cpp b/glow_effect.cpp
new file mode 100644 (file)
index 0000000..e4c9a42
--- /dev/null
@@ -0,0 +1,33 @@
+#include <math.h>
+#include <assert.h>
+
+#include "glow_effect.h"
+#include "blur_effect.h"
+#include "mix_effect.h"
+#include "effect_chain.h"
+#include "util.h"
+
+GlowEffect::GlowEffect()
+       : blur(new BlurEffect),
+         mix(new MixEffect)
+{
+       mix->set_float("strength_first", 1.0f);
+       mix->set_float("strength_second", 0.3f);
+}
+
+void GlowEffect::add_self_to_effect_chain(EffectChain *chain, const std::vector<Effect *> &inputs) {
+       assert(inputs.size() == 1);
+       blur->add_self_to_effect_chain(chain, inputs);
+
+       std::vector<Effect *> mix_inputs;
+       mix_inputs.push_back(inputs[0]);
+       mix_inputs.push_back(chain->last_added_effect());  // FIXME
+       mix->add_self_to_effect_chain(chain, mix_inputs);
+}
+
+bool GlowEffect::set_float(const std::string &key, float value) {
+       if (key == "blurred_mix_amount") {
+               return mix->set_float("strength_second", value);
+       }
+       return blur->set_float(key, value);
+}
diff --git a/glow_effect.h b/glow_effect.h
new file mode 100644 (file)
index 0000000..7ee164d
--- /dev/null
@@ -0,0 +1,32 @@
+#ifndef _GLOW_EFFECT_H
+#define _GLOW_EFFECT_H 1
+
+// Glow: Simply add a blurred version of the image to itself.
+
+#include "effect.h"
+
+class BlurEffect;
+class MixEffect;
+
+class GlowEffect : public Effect {
+public:
+       GlowEffect();
+
+       virtual bool needs_srgb_primaries() const { return false; }
+
+       virtual void add_self_to_effect_chain(EffectChain *chain, const std::vector<Effect *> &input);
+       virtual bool set_float(const std::string &key, float value);
+
+       virtual std::string output_fragment_shader() {
+               assert(false);
+       }
+       virtual void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num) {
+               assert(false);
+       }
+
+private:
+       BlurEffect *blur;
+       MixEffect *mix;
+};
+
+#endif // !defined(_GLOW_EFFECT_H)
diff --git a/mix_effect.cpp b/mix_effect.cpp
new file mode 100644 (file)
index 0000000..0f80a8f
--- /dev/null
@@ -0,0 +1,14 @@
+#include "mix_effect.h"
+#include "util.h"
+
+MixEffect::MixEffect()
+       : strength_first(0.5f), strength_second(0.5f)
+{
+       register_float("strength_first", &strength_first);
+       register_float("strength_second", &strength_second);
+}
+
+std::string MixEffect::output_fragment_shader()
+{
+       return read_file("mix_effect.frag");
+}
diff --git a/mix_effect.frag b/mix_effect.frag
new file mode 100644 (file)
index 0000000..f76881b
--- /dev/null
@@ -0,0 +1,5 @@
+vec4 FUNCNAME(vec2 tc) {
+       vec4 first = INPUT1(tc);
+       vec4 second = INPUT2(tc);
+       return vec4(PREFIX(strength_first)) * first + vec4(PREFIX(strength_second)) * second;
+}
diff --git a/mix_effect.h b/mix_effect.h
new file mode 100644 (file)
index 0000000..7342b90
--- /dev/null
@@ -0,0 +1,20 @@
+#ifndef _MIX_EFFECT_H
+#define _MIX_EFFECT_H 1
+
+// Combine two images: a*x + b*y. (If you set a within [0,1] and b=1-a, you will get a fade.)
+
+#include "effect.h"
+
+class MixEffect : public Effect {
+public:
+       MixEffect();
+       std::string output_fragment_shader();
+
+       virtual bool needs_srgb_primaries() const { return false; }
+       virtual unsigned num_inputs() const { return 2; }
+
+private:
+       float strength_first, strength_second;
+};
+
+#endif // !defined(_MIX_EFFECT_H)