From: Steinar H. Gunderson Date: Thu, 17 Jan 2013 00:49:54 +0000 (+0100) Subject: Add support for vec4 uniforms. X-Git-Tag: 1.0~168 X-Git-Url: https://git.sesse.net/?p=movit;a=commitdiff_plain;h=524148c91130cbbe1a506eb9c268750dba74cb95 Add support for vec4 uniforms. --- diff --git a/effect.cpp b/effect.cpp index 384e8c2..39e9490 100644 --- a/effect.cpp +++ b/effect.cpp @@ -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::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::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::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::iterator it = params_tex_1d.begin(); it != params_tex_1d.end(); diff --git a/effect.h b/effect.h index 39c6126..d7db299 100644 --- 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 params_float; std::map params_vec2; std::map params_vec3; + std::map params_vec4; std::map params_tex_1d; };