Actually set all the right uniforms, so we have a picture on screen.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 1 Oct 2012 18:24:14 +0000 (20:24 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 1 Oct 2012 18:24:14 +0000 (20:24 +0200)
effect.cpp
effect.h
effect_chain.cpp
lift_gamma_gain_effect.cpp
lift_gamma_gain_effect.h
main.cpp

index 927acd5..c17ad3f 100644 (file)
@@ -1,7 +1,37 @@
+#define GL_GLEXT_PROTOTYPES 1
+
 #include <stdio.h>
 #include <string.h>
 #include <assert.h>
 #include "effect.h"
 #include <stdio.h>
 #include <string.h>
 #include <assert.h>
 #include "effect.h"
+#include "util.h"
+
+#include <GL/gl.h>
+#include <GL/glext.h>
+
+void set_uniform_float(GLhandleARB glsl_program_num, const std::string &prefix, const std::string &key, float value)
+{
+       std::string name = prefix + "_" + key;
+       GLuint l = glGetUniformLocation(glsl_program_num, name.c_str());
+       if (l == -1) {
+               return;
+       }
+       check_error();
+       glUniform1f(l, value);
+       check_error();
+}
+
+void set_uniform_vec3(GLhandleARB glsl_program_num, const std::string &prefix, const std::string &key, const float *values)
+{
+       std::string name = prefix + "_" + key;
+       GLuint l = glGetUniformLocation(glsl_program_num, name.c_str());
+       if (l == -1) {
+               return;
+       }
+       check_error();
+       glUniform3fv(l, 1, values);
+       check_error();
+}
 
 bool Effect::set_int(const std::string &key, int value)
 {
 
 bool Effect::set_int(const std::string &key, int value)
 {
@@ -69,3 +99,17 @@ std::string Effect::output_convenience_uniforms()
        }
        return output;
 }
        }
        return output;
 }
+
+void Effect::set_uniforms(GLhandleARB glsl_program_num, const std::string& prefix)
+{
+       for (std::map<std::string, float*>::const_iterator it = params_float.begin();
+            it != params_float.end();
+            ++it) {
+               set_uniform_float(glsl_program_num, prefix, it->first, *it->second);
+       }
+       for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
+            it != params_vec3.end();
+            ++it) {
+               set_uniform_vec3(glsl_program_num, prefix, it->first, it->second);
+       }
+}
index c53a155..3585e2d 100644 (file)
--- a/effect.h
+++ b/effect.h
@@ -4,6 +4,8 @@
 #include <map>
 #include <string>
 
 #include <map>
 #include <string>
 
+#include <GL/gl.h>
+
 // Can alias on a float[3].
 struct RGBTriplet {
        RGBTriplet(float r, float g, float b)
 // Can alias on a float[3].
 struct RGBTriplet {
        RGBTriplet(float r, float g, float b)
@@ -12,6 +14,10 @@ struct RGBTriplet {
        float r, g, b;
 };
 
        float r, g, b;
 };
 
+// Convenience functions that deal with prepending the prefix..
+void set_uniform_float(GLhandleARB glsl_program_num, const std::string &prefix, const std::string &key, float value);
+void set_uniform_vec3(GLhandleARB glsl_program_num, const std::string &prefix, const std::string &key, const float *values);
+
 class Effect {
 public: 
        virtual bool needs_linear_light() { return true; }
 class Effect {
 public: 
        virtual bool needs_linear_light() { return true; }
@@ -22,6 +28,8 @@ public:
        virtual std::string output_convenience_uniforms();
        virtual std::string output_glsl() = 0;
 
        virtual std::string output_convenience_uniforms();
        virtual std::string output_glsl() = 0;
 
+       virtual void set_uniforms(GLhandleARB glsl_program_num, const std::string& prefix);
+
        // Neither of these take ownership.
        bool set_int(const std::string&, int value);
        bool set_float(const std::string &key, float value);
        // Neither of these take ownership.
        bool set_int(const std::string&, int value);
        bool set_float(const std::string &key, float value);
index 56d470f..1ff57d7 100644 (file)
@@ -191,9 +191,11 @@ void EffectChain::render_to_screen(unsigned char *src)
        check_error();
        glUniform1i(glGetUniformLocation(glsl_program_num, "input_tex"), 0);
 
        check_error();
        glUniform1i(glGetUniformLocation(glsl_program_num, "input_tex"), 0);
 
-       //for (unsigned i = 0; i < effects.size(); ++i) {
-       //      effects[i]->set_uniforms();
-       //}
+       for (unsigned i = 0; i < effects.size(); ++i) {
+               char effect_id[256];
+               sprintf(effect_id, "eff%d", i);
+               effects[i]->set_uniforms(glsl_program_num, effect_id);
+       }
 
        glDisable(GL_BLEND);
        check_error();
 
        glDisable(GL_BLEND);
        check_error();
index 149cb44..acfeb86 100644 (file)
@@ -1,3 +1,9 @@
+#define GL_GLEXT_PROTOTYPES 1
+
+#include <math.h>
+#include <GL/gl.h>
+#include <GL/glext.h>
+
 #include "lift_gamma_gain_effect.h"
 #include "util.h"
 
 #include "lift_gamma_gain_effect.h"
 #include "util.h"
 
@@ -17,3 +23,20 @@ std::string LiftGammaGainEffect::output_glsl()
 {
        return read_file("lift_gamma_gain_effect.glsl");
 }
 {
        return read_file("lift_gamma_gain_effect.glsl");
 }
+
+void LiftGammaGainEffect::set_uniforms(GLhandleARB glsl_program_num, const std::string &prefix)
+{
+       Effect::set_uniforms(glsl_program_num, prefix);
+
+       RGBTriplet gain_pow_inv_gamma(
+               pow(gain.r, 1.0f / gamma.r),
+               pow(gain.g, 1.0f / gamma.g),
+               pow(gain.b, 1.0f / gamma.b));
+       set_uniform_vec3(glsl_program_num, prefix, "gain_pow_inv_gamma", (float *)&gain_pow_inv_gamma);
+
+       RGBTriplet inv_gamma_22(
+               2.2f / gamma.r,
+               2.2f / gamma.g,
+               2.2f / gamma.b);
+       set_uniform_vec3(glsl_program_num, prefix, "inv_gamma_22", (float *)&gain_pow_inv_gamma);
+}
index 26eb583..0971955 100644 (file)
@@ -8,6 +8,8 @@ public:
        LiftGammaGainEffect();
        std::string output_glsl();
 
        LiftGammaGainEffect();
        std::string output_glsl();
 
+       void set_uniforms(GLhandleARB glsl_program_num, const std::string &prefix);
+
 private:
        RGBTriplet lift, gamma, gain;
        float saturation;
 private:
        RGBTriplet lift, gamma, gain;
        float saturation;
index 3b46450..d823694 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -44,9 +44,10 @@ void update_hsv(Effect *lift_gamma_gain_effect)
        hsv2rgb(gamma_theta, gamma_rad, gamma_v * 2.0f, &gamma.r, &gamma.g, &gamma.b);
        hsv2rgb(gain_theta, gain_rad, gain_v * 4.0f, &gain.r, &gain.g, &gain.b);
 
        hsv2rgb(gamma_theta, gamma_rad, gamma_v * 2.0f, &gamma.r, &gamma.g, &gamma.b);
        hsv2rgb(gain_theta, gain_rad, gain_v * 4.0f, &gain.r, &gain.g, &gain.b);
 
-       lift_gamma_gain_effect->set_vec3("lift", (float *)&lift);
-       lift_gamma_gain_effect->set_vec3("gamma", (float *)&gamma);
-       lift_gamma_gain_effect->set_vec3("gain", (float *)&gain);
+       bool ok = lift_gamma_gain_effect->set_vec3("lift", (float *)&lift);
+       ok = ok && lift_gamma_gain_effect->set_vec3("gamma", (float *)&gamma);
+       ok = ok && lift_gamma_gain_effect->set_vec3("gain", (float *)&gain);
+       assert(ok);
 
        if (saturation < 0.0) {
                saturation = 0.0;
 
        if (saturation < 0.0) {
                saturation = 0.0;