]> git.sesse.net Git - movit/commitdiff
Add support for vec4 uniforms.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 17 Jan 2013 00:49:54 +0000 (01:49 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 17 Jan 2013 00:49:54 +0000 (01:49 +0100)
effect.cpp
effect.h

index 384e8c2944e741ce53bade017d4e7b9064148228..39e949078390e1bb21210e7ac586f3abad053b98 100644 (file)
@@ -57,6 +57,17 @@ void set_uniform_vec3(GLuint glsl_program_num, const std::string &prefix, const
        check_error();
 }
 
+void set_uniform_vec4(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values)
+{
+       GLint location = get_uniform_location(glsl_program_num, prefix, key);
+       if (location == -1) {
+               return;
+       }
+       check_error();
+       glUniform4fv(location, 1, values);
+       check_error();
+}
+
 void set_uniform_vec4_array(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values, size_t num_values)
 {
        GLint location = get_uniform_location(glsl_program_num, prefix, key);
@@ -124,6 +135,15 @@ bool Effect::set_vec3(const std::string &key, const float *values)
        return true;
 }
 
+bool Effect::set_vec4(const std::string &key, const float *values)
+{
+       if (params_vec4.count(key) == 0) {
+               return false;
+       }
+       memcpy(params_vec4[key], values, sizeof(float) * 4);
+       return true;
+}
+
 void Effect::register_int(const std::string &key, int *value)
 {
        assert(params_int.count(key) == 0);
@@ -148,6 +168,12 @@ void Effect::register_vec3(const std::string &key, float *values)
        params_vec3[key] = values;
 }
 
+void Effect::register_vec4(const std::string &key, float *values)
+{
+       assert(params_vec4.count(key) == 0);
+       params_vec4[key] = values;
+}
+
 void Effect::register_1d_texture(const std::string &key, float *values, size_t size)
 {
        assert(params_tex_1d.count(key) == 0);
@@ -202,6 +228,13 @@ std::string Effect::output_convenience_uniforms() const
                sprintf(buf, "uniform vec3 PREFIX(%s);\n", it->first.c_str());
                output.append(buf);
        }
+       for (std::map<std::string, float*>::const_iterator it = params_vec4.begin();
+            it != params_vec4.end();
+            ++it) {
+               char buf[256];
+               sprintf(buf, "uniform vec4 PREFIX(%s);\n", it->first.c_str());
+               output.append(buf);
+       }
        for (std::map<std::string, Texture1D>::const_iterator it = params_tex_1d.begin();
             it != params_tex_1d.end();
             ++it) {
@@ -229,6 +262,11 @@ void Effect::set_gl_state(GLuint glsl_program_num, const std::string& prefix, un
             ++it) {
                set_uniform_vec3(glsl_program_num, prefix, it->first, it->second);
        }
+       for (std::map<std::string, float*>::const_iterator it = params_vec4.begin();
+            it != params_vec4.end();
+            ++it) {
+               set_uniform_vec4(glsl_program_num, prefix, it->first, it->second);
+       }
 
        for (std::map<std::string, Texture1D>::iterator it = params_tex_1d.begin();
             it != params_tex_1d.end();
index 39c612601a36a4e4a9ed9d755c11233d298d2479..d7db29907dbdb4d070efdbfd00a80e5c04c1d85b 100644 (file)
--- a/effect.h
+++ b/effect.h
@@ -40,12 +40,21 @@ struct RGBTriplet {
        float r, g, b;
 };
 
+// Can alias on a float[4].
+struct RGBATriplet {
+       RGBATriplet(float r, float g, float b, float a)
+               : r(r), g(g), b(b), a(a) {}
+
+       float r, g, b, a;
+};
+
 // Convenience functions that deal with prepending the prefix.
 GLint get_uniform_location(GLuint glsl_program_num, const std::string &prefix, const std::string &key);
 void set_uniform_int(GLuint glsl_program_num, const std::string &prefix, const std::string &key, int value);
 void set_uniform_float(GLuint glsl_program_num, const std::string &prefix, const std::string &key, float value);
 void set_uniform_vec2(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values);
 void set_uniform_vec3(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values);
+void set_uniform_vec4(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values);
 void set_uniform_vec4_array(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values, size_t num_values);
 void set_uniform_mat3(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const Eigen::Matrix3d &matrix);
 
@@ -226,6 +235,7 @@ public:
        virtual bool set_float(const std::string &key, float value) MUST_CHECK_RESULT;
        virtual bool set_vec2(const std::string &key, const float *values) MUST_CHECK_RESULT;
        virtual bool set_vec3(const std::string &key, const float *values) MUST_CHECK_RESULT;
+       virtual bool set_vec4(const std::string &key, const float *values) MUST_CHECK_RESULT;
 
 protected:
        // Register a parameter. Whenever set_*() is called with the same key,
@@ -238,10 +248,11 @@ protected:
        // Thus, ints that you register will _not_ be converted to GLSL uniforms.
        void register_int(const std::string &key, int *value);
 
-       // These correspond directly to float/vec2/vec3 in GLSL.
+       // These correspond directly to float/vec2/vec3/vec4 in GLSL.
        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);
+       void register_vec4(const std::string &key, float *values);
 
        // This will register a 1D texture, which will be bound to a sampler
        // when your GLSL code runs (so it corresponds 1:1 to a sampler2D uniform
@@ -266,6 +277,7 @@ private:
        std::map<std::string, float *> params_float;
        std::map<std::string, float *> params_vec2;
        std::map<std::string, float *> params_vec3;
+       std::map<std::string, float *> params_vec4;
        std::map<std::string, Texture1D> params_tex_1d;
 };