Browse Source

added support for arrays (via std::array)

master
Joshua Moerman 14 years ago
parent
commit
dadeddcafa
  1. 93
      J/interpolator.h
  2. 8
      J/shader.h

93
J/interpolator.h

@ -11,6 +11,9 @@
#include <tr1/functional> #include <tr1/functional>
// NOTE: there is a specialisation for std::array
// But i'm not glad with how this is done...
namespace J { namespace J {
namespace interpolators { namespace interpolators {
@ -58,16 +61,17 @@ class interpolator {
public: public:
interpolator(T begin_value_ = T(0), size_t length_ = 100) : template <typename S>
interpolator(S begin_value_, size_t length_ = 100) :
ease_function(interpolators::cubic_in_out()) ease_function(interpolators::cubic_in_out())
, length(length_) , length(length_)
, steps(0) , steps(length_)
, value(begin_value_) , value(begin_value_)
, begin_value(begin_value_) , begin_value(begin_value_)
, end_value(begin_value_) {} , end_value(begin_value_) {}
template <typename F> template <typename S, typename F>
interpolator(T begin_value_, size_t length_, F ease_function_) : interpolator(S begin_value_, size_t length_, F ease_function_) :
ease_function(ease_function_) ease_function(ease_function_)
, length(length_) , length(length_)
, steps(0) , steps(0)
@ -75,11 +79,11 @@ public:
, begin_value(begin_value_) , begin_value(begin_value_)
, end_value(begin_value_) {} , end_value(begin_value_) {}
operator T&(){ operator T const & () const{
return value; return value;
} }
operator T const () const{ T const & get_value() const{
return value; return value;
} }
@ -110,8 +114,81 @@ public:
}; };
struct foo{ template <typename T, size_t N>
foo(int){} class interpolator<std::array<T, N> > {
std::tr1::function<T (T)> ease_function;
size_t length;
size_t steps;
std::array<T, N> value;
std::array<T, N> begin_value;
std::array<T, N> end_value;
public:
template <typename S>
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 <typename S, typename F>
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<T, N> const & get_value() const{
return value;
}
operator std::array<T, N> const & () const{
return value;
}
void set_value(std::array<T, N> 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 <typename F>
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 } // namespace J

8
J/shader.h

@ -16,13 +16,15 @@
#include <vector> #include <vector>
#include <iterator> #include <iterator>
#import "basic.h" #include "basic.h"
#include "array.h"
// TODO: do glValidateProgram, like in the OpenGL template (see bottom) // TODO: do glValidateProgram, like in the OpenGL template (see bottom)
// TODO: add error checking at set_uniforms? // TODO: add error checking at set_uniforms?
// TODO: use an uniform map? (benchmark first!) // TODO: use an uniform map? (benchmark first!)
// TODO: allow arrays is uniforms (now all counts are 1) // 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) // 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 { namespace J {
@ -176,6 +178,10 @@ public:
glUniformMatrix4fv(glGetUniformLocation(program, name), 1, trans, value); glUniformMatrix4fv(glGetUniformLocation(program, name), 1, trans, value);
} }
void set_uniform(char const* name, const std::array<GLfloat, 16> & 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 { void set_uniform(char const* name, const GLfloat (&value)[9], GLboolean trans) const {
glUniformMatrix3fv(glGetUniformLocation(program, name), 1, trans, value); glUniformMatrix3fv(glGetUniformLocation(program, name), 1, trans, value);
} }