]> git.sesse.net Git - movit/commitdiff
Add vec2 parameter support.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 1 Oct 2012 22:45:08 +0000 (00:45 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 1 Oct 2012 22:45:08 +0000 (00:45 +0200)
effect.cpp
effect.h

index cab91bf51814ae435632381754486c8f72063d80..08e9c3af2dea5ffb3d95751d194c71f6b00e7598 100644 (file)
@@ -21,6 +21,18 @@ void set_uniform_float(GLhandleARB glsl_program_num, const std::string &prefix,
        check_error();
 }
 
+void set_uniform_vec2(GLhandleARB glsl_program_num, const std::string &prefix, const std::string &key, const float *values)
+{
+       std::string name = prefix + "_" + key;
+       GLint l = glGetUniformLocation(glsl_program_num, name.c_str());
+       if (l == -1) {
+               return;
+       }
+       check_error();
+       glUniform2fv(l, 1, values);
+       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;
@@ -51,6 +63,15 @@ bool Effect::set_float(const std::string &key, float value)
        return true;
 }
 
+bool Effect::set_vec2(const std::string &key, const float *values)
+{
+       if (params_vec2.count(key) == 0) {
+               return false;
+       }
+       memcpy(params_vec2[key], values, sizeof(float) * 2);
+       return true;
+}
+
 bool Effect::set_vec3(const std::string &key, const float *values)
 {
        if (params_vec3.count(key) == 0) {
@@ -72,6 +93,12 @@ void Effect::register_float(const std::string &key, float *value)
        params_float[key] = value;
 }
 
+void Effect::register_vec2(const std::string &key, float *values)
+{
+       assert(params_vec2.count(key) == 0);
+       params_vec2[key] = values;
+}
+
 void Effect::register_vec3(const std::string &key, float *values)
 {
        assert(params_vec3.count(key) == 0);
@@ -90,6 +117,13 @@ std::string Effect::output_convenience_uniforms()
                sprintf(buf, "uniform float PREFIX(%s);\n", it->first.c_str());
                output.append(buf);
        }
+       for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
+            it != params_vec2.end();
+            ++it) {
+               char buf[256];
+               sprintf(buf, "uniform vec2 PREFIX(%s);\n", it->first.c_str());
+               output.append(buf);
+       }
        for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
             it != params_vec3.end();
             ++it) {
@@ -107,6 +141,11 @@ void Effect::set_uniforms(GLhandleARB glsl_program_num, const std::string& prefi
             ++it) {
                set_uniform_float(glsl_program_num, prefix, it->first, *it->second);
        }
+       for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
+            it != params_vec2.end();
+            ++it) {
+               set_uniform_vec2(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) {
index 3585e2df9a168a0439d7da808471dd0871dee102..f92201e6b40d56ab6812dac7669ce09da13f6a69 100644 (file)
--- a/effect.h
+++ b/effect.h
@@ -6,6 +6,14 @@
 
 #include <GL/gl.h>
 
+// Can alias on a float[2].
+struct Point2D {
+       Point2D(float x, float y)
+               : x(x), y(y) {}
+
+       float x, y;
+};
+
 // Can alias on a float[3].
 struct RGBTriplet {
        RGBTriplet(float r, float g, float b)
@@ -14,8 +22,9 @@ struct RGBTriplet {
        float r, g, b;
 };
 
-// Convenience functions that deal with prepending the prefix..
+// 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_vec2(GLhandleARB glsl_program_num, const std::string &prefix, const std::string &key, const float *values);
 void set_uniform_vec3(GLhandleARB glsl_program_num, const std::string &prefix, const std::string &key, const float *values);
 
 class Effect {
@@ -33,17 +42,20 @@ public:
        // Neither of these take ownership.
        bool set_int(const std::string&, int value);
        bool set_float(const std::string &key, float value);
+       bool set_vec2(const std::string &key, const float *values);
        bool set_vec3(const std::string &key, const float *values);
 
 protected:
        // Neither of these take ownership.
        void register_int(const std::string &key, int *value);
        void register_float(const std::string &key, float *value);
+       void register_vec2(const std::string &key, float *values);
        void register_vec3(const std::string &key, float *values);
        
 private:
        std::map<std::string, int *> params_int;
        std::map<std::string, float *> params_float;
+       std::map<std::string, float *> params_vec2;
        std::map<std::string, float *> params_vec3;
 };