Promote MultiplyEffect to a real effect.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 6 Nov 2013 23:20:59 +0000 (00:20 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 6 Nov 2013 23:20:59 +0000 (00:20 +0100)
The intended use is for overlays, where you'd want to do e.g.
0.2*x atop y instead of just x atop y, fading the overlay in or out.

Also, give it full RGBA inputs, as that might potentially be useful
for someone. It certainly was useful for adapting it to continue to
be used in the EffectChain unit test, at least. (It doesn't have
its own unit test, since it's so trivial.)

effect_chain_test.cpp
multiply_effect.cpp [new file with mode: 0644]
multiply_effect.frag [moved from multiply.frag with 100% similarity]
multiply_effect.h [new file with mode: 0644]

index 442c2fb..8814e1f 100644 (file)
@@ -11,6 +11,7 @@
 #include "gtest/gtest.h"
 #include "input.h"
 #include "mirror_effect.h"
+#include "multiply_effect.h"
 #include "resize_effect.h"
 #include "test_util.h"
 #include "util.h"
@@ -638,18 +639,6 @@ TEST(EffectChainTest, ResizeDownByFourThenUpByFour) {
        expect_equal(expected_data, out_data, 4, 16);
 }
 
-// An effect that multiplies with a constant. Used below.
-class MultiplyEffect : public Effect {
-public:
-       MultiplyEffect() { register_float("factor", &factor); }
-       virtual std::string effect_type_id() const { return "MultiplyEffect"; }
-       std::string output_fragment_shader() { return read_file("multiply.frag"); }
-       virtual AlphaHandling alpha_handling() const { return DONT_CARE_ALPHA_TYPE; }
-
-private:
-       float factor;
-};
-
 // An effect that adds its two inputs together. Used below.
 class AddEffect : public Effect {
 public:
@@ -680,11 +669,14 @@ TEST(EffectChainTest, DiamondGraph) {
        };
        float out_data[2 * 2];
 
+       const float half[] = { 0.5f, 0.5f, 0.5f, 0.5f };
+       const float two[] = { 2.0f, 2.0f, 2.0f, 0.5f };
+
        MultiplyEffect *mul_half = new MultiplyEffect();
-       ASSERT_TRUE(mul_half->set_float("factor", 0.5f));
+       ASSERT_TRUE(mul_half->set_vec4("factor", half));
        
        MultiplyEffect *mul_two = new MultiplyEffect();
-       ASSERT_TRUE(mul_two->set_float("factor", 2.0f));
+       ASSERT_TRUE(mul_two->set_vec4("factor", two));
 
        EffectChainTester tester(NULL, 2, 2);
 
@@ -726,11 +718,14 @@ TEST(EffectChainTest, DiamondGraphWithOneInputUsedInTwoPhases) {
        };
        float out_data[2 * 2];
 
+       const float half[] = { 0.5f, 0.5f, 0.5f, 0.5f };
+       const float two[] = { 2.0f, 2.0f, 2.0f, 0.5f };
+
        MultiplyEffect *mul_half = new MultiplyEffect();
-       ASSERT_TRUE(mul_half->set_float("factor", 0.5f));
+       ASSERT_TRUE(mul_half->set_vec4("factor", half));
        
        MultiplyEffect *mul_two = new MultiplyEffect();
-       ASSERT_TRUE(mul_two->set_float("factor", 2.0f));
+       ASSERT_TRUE(mul_two->set_vec4("factor", two));
        
        BouncingIdentityEffect *bounce = new BouncingIdentityEffect();
 
diff --git a/multiply_effect.cpp b/multiply_effect.cpp
new file mode 100644 (file)
index 0000000..f3610d3
--- /dev/null
@@ -0,0 +1,15 @@
+#include <GL/glew.h>
+
+#include "multiply_effect.h"
+#include "util.h"
+
+MultiplyEffect::MultiplyEffect()
+       : factor(1.0f, 1.0f, 1.0f, 1.0f)
+{
+       register_vec4("factor", (float *)&factor);
+}
+
+std::string MultiplyEffect::output_fragment_shader()
+{
+       return read_file("multiply_effect.frag");
+}
similarity index 100%
rename from multiply.frag
rename to multiply_effect.frag
diff --git a/multiply_effect.h b/multiply_effect.h
new file mode 100644 (file)
index 0000000..8de261a
--- /dev/null
@@ -0,0 +1,24 @@
+#ifndef _MOVIT_MULTIPLY_EFFECT_H
+#define _MOVIT_MULTIPLY_EFFECT_H 1
+
+// An effect that multiplies every pixel by a constant (separate for each of
+// R, G, B, A). A common use would be to reduce the alpha of an overlay before
+// sending it through OverlayEffect, e.g. with R=G=B=A=0.3 to get 30% alpha
+// (remember, alpha is premultiplied).
+
+#include <GL/glew.h>
+#include <string>
+
+#include "effect.h"
+
+class MultiplyEffect : public Effect {
+public:
+       MultiplyEffect();
+       virtual std::string effect_type_id() const { return "MultiplyEffect"; }
+       std::string output_fragment_shader();
+
+private:
+       RGBATuple factor;
+};
+
+#endif // !defined(_MOVIT_MULTIPLY_EFFECT_H)