From: Mikko Rasa Date: Tue, 21 Aug 2012 09:58:36 +0000 (+0300) Subject: Rewrite the Uniform classes as templates X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;ds=inline;h=f9c15dc04462b2f1eea1d6bdd71e3ba967b1bd8c;p=libs%2Fgl.git Rewrite the Uniform classes as templates This makes adding new uniform types easier, especially arrays. --- diff --git a/source/programdata.cpp b/source/programdata.cpp index 535e9e79..43767bf9 100644 --- a/source/programdata.cpp +++ b/source/programdata.cpp @@ -62,17 +62,19 @@ void ProgramData::uniform(const string &name, float v) void ProgramData::uniform(const string &name, float v0, float v1) { - uniform(name, new Uniform2f(v0, v1)); + float va[2] = { v0, v1 }; + uniform2(name, va); } void ProgramData::uniform2(const string &name, const float *v) { - uniform(name, v[0], v[1]); + uniform(name, new Uniform2f(v)); } void ProgramData::uniform(const string &name, float v0, float v1, float v2) { - uniform(name, new Uniform3f(v0, v1, v2)); + float va[3] = { v0, v1, v2 }; + uniform3(name, va); } void ProgramData::uniform(const string &name, const Vector3 &v) @@ -82,12 +84,13 @@ void ProgramData::uniform(const string &name, const Vector3 &v) void ProgramData::uniform3(const string &name, const float *v) { - uniform(name, v[0], v[1], v[2]); + uniform(name, new Uniform3f(v)); } void ProgramData::uniform(const string &name, float v0, float v1, float v2, float v3) { - uniform(name, new Uniform4f(v0, v1, v2, v3)); + float va[4] = { v0, v1, v2, v3 }; + uniform4(name, va); } void ProgramData::uniform(const string &name, const Vector4 &v) @@ -102,7 +105,7 @@ void ProgramData::uniform(const string &name, const Color &c) void ProgramData::uniform4(const string &name, const float *v) { - uniform(name, v[0], v[1], v[2], v[3]); + uniform(name, new Uniform4f(v)); } void ProgramData::uniform_matrix4(const string &name, const float *v) diff --git a/source/uniform.cpp b/source/uniform.cpp index af49c047..688052ff 100644 --- a/source/uniform.cpp +++ b/source/uniform.cpp @@ -1,107 +1,45 @@ -#include #include "arb_shader_objects.h" #include "uniform.h" namespace Msp { namespace GL { -Uniform1i::Uniform1i(int v_): - v(v_) -{ } - -void Uniform1i::apply(int index) const -{ - glUniform1iARB(index, v); -} - -Uniform1i *Uniform1i::clone() const -{ - return new Uniform1i(v); -} - - -Uniform1f::Uniform1f(float v_): - v(v_) -{ } - -void Uniform1f::apply(int index) const +template<> +void UniformScalar::apply(int index) const { - glUniform1fARB(index, v); + glUniform1iARB(index, value); } -Uniform1f *Uniform1f::clone() const +template<> +void UniformScalar::apply(int index) const { - return new Uniform1f(v); + glUniform1fARB(index, value); } -Uniform2f::Uniform2f(float v0, float v1) +template<> +void UniformVector::apply(int index) const { - v[0] = v0; - v[1] = v1; + glUniform2fvARB(index, 1, value); } -void Uniform2f::apply(int index) const +template<> +void UniformVector::apply(int index) const { - glUniform2fvARB(index, 1, v); + glUniform3fvARB(index, 1, value); } -Uniform2f *Uniform2f::clone() const +template<> +void UniformVector::apply(int index) const { - return new Uniform2f(v[0], v[1]); + glUniform4fvARB(index, 1, value); } -Uniform3f::Uniform3f(float v0, float v1, float v2) -{ - v[0] = v0; - v[1] = v1; - v[2] = v2; -} - -void Uniform3f::apply(int index) const -{ - glUniform3fvARB(index, 1, v); -} - -Uniform3f *Uniform3f::clone() const -{ - return new Uniform3f(v[0], v[1], v[2]); -} - - -Uniform4f::Uniform4f(float v0, float v1, float v2, float v3) -{ - v[0] = v0; - v[1] = v1; - v[2] = v2; - v[3] = v3; -} - -void Uniform4f::apply(int index) const -{ - glUniform4fvARB(index, 1, v); -} - -Uniform4f *Uniform4f::clone() const -{ - return new Uniform4f(v[0], v[1], v[2], v[3]); -} - - -UniformMatrix4x4f::UniformMatrix4x4f(const float *vp) -{ - std::copy(vp, vp+16, v); -} - -void UniformMatrix4x4f::apply(int index) const -{ - glUniformMatrix4fvARB(index, 1, false, v); -} - -UniformMatrix4x4f *UniformMatrix4x4f::clone() const +template<> +void UniformMatrix::apply(int index) const { - return new UniformMatrix4x4f(v); + glUniformMatrix4fvARB(index, 1, false, value); } } // namespace GL diff --git a/source/uniform.h b/source/uniform.h index 70db91df..32779ced 100644 --- a/source/uniform.h +++ b/source/uniform.h @@ -1,11 +1,11 @@ #ifndef MSP_GL_UNIFORM_H_ #define MSP_GL_UNIFORM_H_ +#include + namespace Msp { namespace GL { -class Program; - class Uniform { protected: @@ -21,83 +21,76 @@ public: }; -class Uniform1i: public Uniform +template +class UniformScalar: public Uniform { -private: - int v; - public: - Uniform1i(int v_); - - virtual void apply(int index) const; - virtual Uniform1i *clone() const; -}; - + typedef T BaseType; + typedef T Type; -class Uniform1f: public Uniform -{ private: - float v; + Type value; public: - Uniform1f(float v_); + UniformScalar(Type v): value(v) { } virtual void apply(int index) const; - virtual Uniform1f *clone() const; + + virtual UniformScalar *clone() const + { return new UniformScalar(value); } }; +typedef UniformScalar Uniform1i; +typedef UniformScalar Uniform1f; -class Uniform2f: public Uniform -{ -private: - float v[2]; +template +class UniformVector: public Uniform +{ public: - Uniform2f(float v0, float v1); + typedef T BaseType; + typedef T Type[vecsize]; - virtual void apply(int index) const; - virtual Uniform2f *clone() const; -}; - - -class Uniform3f: public Uniform -{ private: - float v[3]; + Type value; public: - Uniform3f(float v0, float v1, float v2); + UniformVector(const T *vp) + { std::copy(vp, vp+vecsize, value); } virtual void apply(int index) const; - virtual Uniform3f *clone() const; + + virtual UniformVector *clone() const + { return new UniformVector(value); } }; +typedef UniformVector Uniform2f; +typedef UniformVector Uniform3f; +typedef UniformVector Uniform4f; -class Uniform4f: public Uniform -{ -private: - float v[4]; +template +class UniformMatrix: public Uniform +{ public: - Uniform4f(float v0, float v1, float v2, float v3); + typedef T BaseType; + typedef T Type[rows*cols]; - virtual void apply(int index) const; - virtual Uniform4f *clone() const; -}; - - -class UniformMatrix4x4f: public Uniform -{ private: - float v[16]; + Type value; public: - UniformMatrix4x4f(const float *); + UniformMatrix(const T *vp) + { std::copy(vp, vp+rows*cols, value); } virtual void apply(int index) const; - virtual UniformMatrix4x4f *clone() const; + + virtual UniformMatrix *clone() const + { return new UniformMatrix(value); } }; +typedef UniformMatrix UniformMatrix4x4f; + } // namespace GL } // namespace Msp diff --git a/source/uniformblock.cpp b/source/uniformblock.cpp index 95e1c2b3..fe044750 100644 --- a/source/uniformblock.cpp +++ b/source/uniformblock.cpp @@ -45,17 +45,19 @@ void UniformBlock::uniform(int index, float v) void UniformBlock::uniform(int index, float v0, float v1) { - uniform(index, new Uniform2f(v0, v1)); + float va[2] = { v0, v1 }; + uniform2(index, va); } void UniformBlock::uniform2(int index, const float *v) { - uniform(index, v[0], v[1]); + uniform(index, new Uniform2f(v)); } void UniformBlock::uniform(int index, float v0, float v1, float v2) { - uniform(index, new Uniform3f(v0, v1, v2)); + float va[3] = { v0, v1, v2 }; + uniform3(index, va); } void UniformBlock::uniform(int index, const Vector3 &v) @@ -65,12 +67,13 @@ void UniformBlock::uniform(int index, const Vector3 &v) void UniformBlock::uniform3(int index, const float *v) { - uniform(index, v[0], v[1], v[2]); + uniform(index, new Uniform3f(v)); } void UniformBlock::uniform(int index, float v0, float v1, float v2, float v3) { - uniform(index, new Uniform4f(v0, v1, v2, v3)); + float va[4] = { v0, v1, v2, v3 }; + uniform4(index, va); } void UniformBlock::uniform(int index, const Vector4 &v) @@ -85,7 +88,7 @@ void UniformBlock::uniform(int index, const Color &c) void UniformBlock::uniform4(int index, const float *v) { - uniform(index, v[0], v[1], v[2], v[3]); + uniform(index, new Uniform4f(v)); } void UniformBlock::uniform_matrix4(int index, const float *v)