Add a vignette effect.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 1 Oct 2012 22:45:55 +0000 (00:45 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 1 Oct 2012 22:45:55 +0000 (00:45 +0200)
Makefile
effect_chain.cpp
effect_id.h
vignette_effect.cpp [new file with mode: 0644]
vignette_effect.glsl [new file with mode: 0644]
vignette_effect.h [new file with mode: 0644]

index b61f78b..11482ba 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
-OBJS += lift_gamma_gain_effect.o gamma_expansion_effect.o gamma_compression_effect.o colorspace_conversion_effect.o saturation_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
 
 test: $(OBJS)
        $(CXX) -o test $(OBJS) $(LDFLAGS)
index a15a566..623d64a 100644 (file)
@@ -14,6 +14,7 @@
 #include "lift_gamma_gain_effect.h"
 #include "colorspace_conversion_effect.h"
 #include "saturation_effect.h"
+#include "vignette_effect.h"
 #include "texture_enum.h"
 
 EffectChain::EffectChain(unsigned width, unsigned height)
@@ -44,6 +45,8 @@ Effect *instantiate_effect(EffectId effect)
                return new LiftGammaGainEffect();
        case EFFECT_SATURATION:
                return new SaturationEffect();
+       case EFFECT_VIGNETTE:
+               return new VignetteEffect();
        }
        assert(false);
 }
index 623bb39..644393d 100644 (file)
@@ -10,6 +10,9 @@ enum EffectId {
        // Color.
        EFFECT_LIFT_GAMMA_GAIN,
        EFFECT_SATURATION,
+
+       // Spatial.
+       EFFECT_VIGNETTE,
 };
 
 #endif // !defined(_EFFECT_ID_H)
diff --git a/vignette_effect.cpp b/vignette_effect.cpp
new file mode 100644 (file)
index 0000000..08dadab
--- /dev/null
@@ -0,0 +1,33 @@
+#define GL_GLEXT_PROTOTYPES 1
+
+#include <math.h>
+#include <GL/gl.h>
+#include <GL/glext.h>
+
+#include "vignette_effect.h"
+#include "util.h"
+
+VignetteEffect::VignetteEffect()
+       : center(0.5f, 0.5f),
+         radius(0.3f),
+         inner_radius(0.3f)
+{
+       register_vec2("center", (float *)&center);
+       register_float("radius", (float *)&radius);
+       register_float("inner_radius", (float *)&inner_radius);
+}
+
+std::string VignetteEffect::output_glsl()
+{
+       return read_file("vignette_effect.glsl");
+}
+
+void VignetteEffect::set_uniforms(GLhandleARB glsl_program_num, const std::string &prefix)
+{
+       Effect::set_uniforms(glsl_program_num, prefix);
+
+       set_uniform_float(glsl_program_num, prefix, "inv_radius", 1.0f / radius);
+
+       Point2D aspect(16.0f / 9.0f, 1.0f);  // FIXME
+       set_uniform_vec2(glsl_program_num, prefix, "aspect_correction", (float *)&aspect);
+}
diff --git a/vignette_effect.glsl b/vignette_effect.glsl
new file mode 100644 (file)
index 0000000..4e4f26b
--- /dev/null
@@ -0,0 +1,18 @@
+// A simple, circular vignette, with a cosĀ² falloff.
+       
+uniform float PREFIX(inv_radius);
+uniform vec2 PREFIX(aspect_correction);
+
+vec4 FUNCNAME(vec2 tc) {
+       vec4 x = LAST_INPUT(tc);
+
+       const float pihalf = 3.14159265358979324 / 2;
+
+       vec2 normalized_pos = (tc - PREFIX(center)) * PREFIX(aspect_correction);
+       float dist = (length(normalized_pos) - PREFIX(inner_radius)) * PREFIX(inv_radius);
+       float linear_falloff = clamp(dist, 0.0, 1.0) * pihalf;
+       float falloff = cos(linear_falloff) * cos(linear_falloff);
+       x.rgb *= vec3(falloff);
+
+       return x;
+}
diff --git a/vignette_effect.h b/vignette_effect.h
new file mode 100644 (file)
index 0000000..6caff36
--- /dev/null
@@ -0,0 +1,18 @@
+#ifndef _VIGNETTE_EFFECT_H
+#define _VIGNETTE_EFFECT_H 1
+
+#include "effect.h"
+
+class VignetteEffect : public Effect {
+public:
+       VignetteEffect();
+       std::string output_glsl();
+
+       void set_uniforms(GLhandleARB glsl_program_num, const std::string &prefix);
+
+private:
+       Point2D center;
+       float radius, inner_radius;
+};
+
+#endif // !defined(_VIGNETTE_EFFECT_H)