From f4516cf70e80bf1153759919c8b2ab1cee37d9ec Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Tue, 2 Oct 2012 00:45:55 +0200 Subject: [PATCH] Add a vignette effect. --- Makefile | 2 +- effect_chain.cpp | 3 +++ effect_id.h | 3 +++ vignette_effect.cpp | 33 +++++++++++++++++++++++++++++++++ vignette_effect.glsl | 18 ++++++++++++++++++ vignette_effect.h | 18 ++++++++++++++++++ 6 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 vignette_effect.cpp create mode 100644 vignette_effect.glsl create mode 100644 vignette_effect.h diff --git a/Makefile b/Makefile index b61f78b..11482ba 100644 --- 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) diff --git a/effect_chain.cpp b/effect_chain.cpp index a15a566..623d64a 100644 --- a/effect_chain.cpp +++ b/effect_chain.cpp @@ -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); } diff --git a/effect_id.h b/effect_id.h index 623bb39..644393d 100644 --- a/effect_id.h +++ b/effect_id.h @@ -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 index 0000000..08dadab --- /dev/null +++ b/vignette_effect.cpp @@ -0,0 +1,33 @@ +#define GL_GLEXT_PROTOTYPES 1 + +#include +#include +#include + +#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 *)¢er); + 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 index 0000000..4e4f26b --- /dev/null +++ b/vignette_effect.glsl @@ -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 index 0000000..6caff36 --- /dev/null +++ b/vignette_effect.h @@ -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) -- 2.39.2