From: Mikko Rasa Date: Tue, 21 Aug 2012 10:12:03 +0000 (+0300) Subject: Implement uniform arrays X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=0070eec93efbf27bcc70720141d8730b059eb964;p=libs%2Fgl.git Implement uniform arrays --- diff --git a/source/programdata.cpp b/source/programdata.cpp index 43767bf9..0a2ec605 100644 --- a/source/programdata.cpp +++ b/source/programdata.cpp @@ -120,6 +120,31 @@ void ProgramData::uniform_matrix4(const string &name, const Matrix &m) uniform_matrix4(name, v); } +void ProgramData::uniform1_array(const string &name, unsigned n, const float *v) +{ + uniform(name, new UniformArray(n, v)); +} + +void ProgramData::uniform2_array(const string &name, unsigned n, const float *v) +{ + uniform(name, new UniformArray(n, v)); +} + +void ProgramData::uniform3_array(const string &name, unsigned n, const float *v) +{ + uniform(name, new UniformArray(n, v)); +} + +void ProgramData::uniform4_array(const string &name, unsigned n, const float *v) +{ + uniform(name, new UniformArray(n, v)); +} + +void ProgramData::uniform_matrix4_array(const string &name, unsigned n, const float *v) +{ + uniform(name, new UniformArray(n, v)); +} + const UniformBlock &ProgramData::get_block(const Program &prog) const { if(modified) diff --git a/source/programdata.h b/source/programdata.h index 6d0ebf8a..d74017b5 100644 --- a/source/programdata.h +++ b/source/programdata.h @@ -71,6 +71,11 @@ public: void uniform4(const std::string &, const float *); void uniform_matrix4(const std::string &, const float *); void uniform_matrix4(const std::string &, const Matrix &); + void uniform1_array(const std::string &, unsigned, const float *); + void uniform2_array(const std::string &, unsigned, const float *); + void uniform3_array(const std::string &, unsigned, const float *); + void uniform4_array(const std::string &, unsigned, const float *); + void uniform_matrix4_array(const std::string &, unsigned, const float *); const UniformBlock &get_block(const Program &) const; diff --git a/source/uniform.cpp b/source/uniform.cpp index 688052ff..ac866cf5 100644 --- a/source/uniform.cpp +++ b/source/uniform.cpp @@ -5,41 +5,41 @@ namespace Msp { namespace GL { template<> -void UniformScalar::apply(int index) const +void UniformScalar::apply(int index, unsigned size, const int *value) { - glUniform1iARB(index, value); + glUniform1ivARB(index, size, value); } template<> -void UniformScalar::apply(int index) const +void UniformScalar::apply(int index, unsigned size, const float *value) { - glUniform1fARB(index, value); + glUniform1fvARB(index, size, value); } template<> -void UniformVector::apply(int index) const +void UniformVector::apply(int index, unsigned size, const float *value) { - glUniform2fvARB(index, 1, value); + glUniform2fvARB(index, size, value); } template<> -void UniformVector::apply(int index) const +void UniformVector::apply(int index, unsigned size, const float *value) { - glUniform3fvARB(index, 1, value); + glUniform3fvARB(index, size, value); } template<> -void UniformVector::apply(int index) const +void UniformVector::apply(int index, unsigned size, const float *value) { - glUniform4fvARB(index, 1, value); + glUniform4fvARB(index, size, value); } template<> -void UniformMatrix::apply(int index) const +void UniformMatrix::apply(int index, unsigned size, const float *value) { - glUniformMatrix4fvARB(index, 1, false, value); + glUniformMatrix4fvARB(index, size, false, value); } } // namespace GL diff --git a/source/uniform.h b/source/uniform.h index 32779ced..216d3be6 100644 --- a/source/uniform.h +++ b/source/uniform.h @@ -34,7 +34,10 @@ private: public: UniformScalar(Type v): value(v) { } - virtual void apply(int index) const; + virtual void apply(int index) const + { apply(index, 1, &value); } + + static void apply(int, unsigned, const T *); virtual UniformScalar *clone() const { return new UniformScalar(value); } @@ -58,7 +61,10 @@ public: UniformVector(const T *vp) { std::copy(vp, vp+vecsize, value); } - virtual void apply(int index) const; + virtual void apply(int index) const + { apply(index, 1, value); } + + static void apply(int index, unsigned size, const T *value); virtual UniformVector *clone() const { return new UniformVector(value); } @@ -83,7 +89,10 @@ public: UniformMatrix(const T *vp) { std::copy(vp, vp+rows*cols, value); } - virtual void apply(int index) const; + virtual void apply(int index) const + { apply(index, 1, value); } + + static void apply(int index, unsigned size, const T *value); virtual UniformMatrix *clone() const { return new UniformMatrix(value); } @@ -91,6 +100,32 @@ public: typedef UniformMatrix UniformMatrix4x4f; + +template +class UniformArray: public Uniform +{ +private: + typedef typename T::BaseType BaseType; + + BaseType *values; + unsigned size; + +public: + UniformArray(unsigned n, const BaseType *vp): + size(n) + { + unsigned elemsize = sizeof(typename T::Type)/sizeof(typename T::BaseType); + values = new BaseType[elemsize*size]; + std::copy(vp, vp+elemsize*size, values); + } + + virtual void apply(int index) const + { T::apply(index, size, values); } + + virtual UniformArray *clone() const + { return new UniformArray(size, values); } +}; + } // namespace GL } // namespace Msp