From dadeddcafa1e0c292bb4093529eff4d353b1d700 Mon Sep 17 00:00:00 2001 From: Joshua Moerman Date: Sat, 3 Sep 2011 16:38:28 +0200 Subject: [PATCH] added support for arrays (via std::array) --- J/interpolator.h | 101 +++++++++++++++++++++++++++++++++++++++++------ J/shader.h | 8 +++- 2 files changed, 96 insertions(+), 13 deletions(-) diff --git a/J/interpolator.h b/J/interpolator.h index 071cde9..f6a6003 100644 --- a/J/interpolator.h +++ b/J/interpolator.h @@ -11,6 +11,9 @@ #include +// NOTE: there is a specialisation for std::array +// But i'm not glad with how this is done... + namespace J { namespace interpolators { @@ -58,16 +61,17 @@ class interpolator { public: - interpolator(T begin_value_ = T(0), size_t length_ = 100) : + template + interpolator(S begin_value_, size_t length_ = 100) : ease_function(interpolators::cubic_in_out()) , length(length_) - , steps(0) + , steps(length_) , value(begin_value_) , begin_value(begin_value_) , end_value(begin_value_) {} - template - interpolator(T begin_value_, size_t length_, F ease_function_) : + template + interpolator(S begin_value_, size_t length_, F ease_function_) : ease_function(ease_function_) , length(length_) , steps(0) @@ -75,13 +79,13 @@ public: , begin_value(begin_value_) , end_value(begin_value_) {} - operator T&(){ - return value; - } - - operator T const () const{ + operator T const & () const{ return value; } + + T const & get_value() const{ + return value; + } void set_value(T new_value){ begin_value = value; @@ -110,9 +114,82 @@ public: }; - struct foo{ - foo(int){} - }; +template + class interpolator > { + + std::tr1::function ease_function; + + size_t length; + size_t steps; + + std::array value; + std::array begin_value; + std::array end_value; + +public: + + template + interpolator(S begin_value_, size_t length_ = 100) : + ease_function(interpolators::cubic_in_out()) + , length(length_) + , steps(length_) + , value(begin_value_) + , begin_value(begin_value_) + , end_value(begin_value_) {} + + template + interpolator(S const & begin_value_, size_t length_, F const & ease_function_) : + ease_function(ease_function_) + , length(length_) + , steps(0) + , value(begin_value_) + , begin_value(begin_value_) + , end_value(begin_value_) {} + + std::array const & get_value() const{ + return value; + } + + operator std::array const & () const{ + return value; + } + + void set_value(std::array const & new_value){ + begin_value = value; + end_value = new_value; + steps = 0; + } + + void set_value(T const (& new_value)[N]){ + begin_value = value; + for (unsigned int i = 0; i < N; ++i) { + end_value[i] = new_value[i]; + } + steps = 0; + } + + void set_length(size_t new_length){ + T ratio = (T) steps / (T) length; + length = new_length; + steps = ratio*length; + } + + template + void set_ease_function(F new_ease_function){ + ease_function = new_ease_function; + } + + void interpolate(){ + if(steps >= length) return; + ++steps; + T ratio = (T) steps / (T) length; + ratio = ease_function(ratio); + for (unsigned int i = 0; i < N; ++i) { + value[i] = (T(1) - ratio)*begin_value[i] + ratio*end_value[i]; + } + } + +}; } // namespace J diff --git a/J/shader.h b/J/shader.h index 78eee00..f4b19c4 100644 --- a/J/shader.h +++ b/J/shader.h @@ -16,13 +16,15 @@ #include #include -#import "basic.h" +#include "basic.h" +#include "array.h" // TODO: do glValidateProgram, like in the OpenGL template (see bottom) // TODO: add error checking at set_uniforms? // TODO: use an uniform map? (benchmark first!) // TODO: allow arrays is uniforms (now all counts are 1) // TODO: matrix attributes are not handled well (they take up 4 indices in the attributes) +// NOTE: there is only 1 set_uniform for std::array... namespace J { @@ -175,6 +177,10 @@ public: void set_uniform(char const* name, const GLfloat (&value)[16], GLboolean trans) const { glUniformMatrix4fv(glGetUniformLocation(program, name), 1, trans, value); } + + void set_uniform(char const* name, const std::array & value, GLboolean trans) const { + glUniformMatrix4fv(glGetUniformLocation(program, name), 1, trans, &value[0]); + } void set_uniform(char const* name, const GLfloat (&value)[9], GLboolean trans) const { glUniformMatrix3fv(glGetUniformLocation(program, name), 1, trans, value);