]> git.tdb.fi Git - libs/gl.git/commitdiff
Rewrite the Uniform classes as templates
authorMikko Rasa <tdb@tdb.fi>
Tue, 21 Aug 2012 09:58:36 +0000 (12:58 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 21 Aug 2012 09:58:36 +0000 (12:58 +0300)
This makes adding new uniform types easier, especially arrays.

source/programdata.cpp
source/uniform.cpp
source/uniform.h
source/uniformblock.cpp

index 535e9e792f3b40826ab60d26bb1f72f2eca4e821..43767bf9526a4449cde0a6d5d3f076bba0460b4b 100644 (file)
@@ -62,17 +62,19 @@ void ProgramData::uniform(const string &name, float v)
 
 void ProgramData::uniform(const string &name, float v0, float v1)
 {
 
 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)
 {
 }
 
 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)
 {
 }
 
 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)
 }
 
 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)
 {
 
 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)
 {
 }
 
 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)
 }
 
 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)
 {
 
 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)
 }
 
 void ProgramData::uniform_matrix4(const string &name, const float *v)
index af49c047f71be82f5c635a66560a40d7f27d549d..688052ff16d0ae3b8fdebc5fafe99e1025c118d9 100644 (file)
-#include <algorithm>
 #include "arb_shader_objects.h"
 #include "uniform.h"
 
 namespace Msp {
 namespace GL {
 
 #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<int>::apply(int index) const
 {
 {
-       glUniform1fARB(index, v);
+       glUniform1iARB(index, value);
 }
 
 }
 
-Uniform1f *Uniform1f::clone() const
+template<>
+void UniformScalar<float>::apply(int index) const
 {
 {
-       return new Uniform1f(v);
+       glUniform1fARB(index, value);
 }
 
 
 }
 
 
-Uniform2f::Uniform2f(float v0, float v1)
+template<>
+void UniformVector<float, 2>::apply(int index) const
 {
 {
-       v[0] = v0;
-       v[1] = v1;
+       glUniform2fvARB(index, 1, value);
 }
 
 }
 
-void Uniform2f::apply(int index) const
+template<>
+void UniformVector<float, 3>::apply(int index) const
 {
 {
-       glUniform2fvARB(index, 1, v);
+       glUniform3fvARB(index, 1, value);
 }
 
 }
 
-Uniform2f *Uniform2f::clone() const
+template<>
+void UniformVector<float, 4>::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<float, 4, 4>::apply(int index) const
 {
 {
-       return new UniformMatrix4x4f(v);
+       glUniformMatrix4fvARB(index, 1, false, value);
 }
 
 } // namespace GL
 }
 
 } // namespace GL
index 70db91dfe0d93fbb8e19eff6a2494214258d37e9..32779ced0a06cc0206a20982ee57ac4d52024335 100644 (file)
@@ -1,11 +1,11 @@
 #ifndef MSP_GL_UNIFORM_H_
 #define MSP_GL_UNIFORM_H_
 
 #ifndef MSP_GL_UNIFORM_H_
 #define MSP_GL_UNIFORM_H_
 
+#include <algorithm>
+
 namespace Msp {
 namespace GL {
 
 namespace Msp {
 namespace GL {
 
-class Program;
-
 class Uniform
 {
 protected:
 class Uniform
 {
 protected:
@@ -21,83 +21,76 @@ public:
 };
 
 
 };
 
 
-class Uniform1i: public Uniform
+template<typename T>
+class UniformScalar: public Uniform
 {
 {
-private:
-       int v;
-
 public:
 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:
 private:
-       float v;
+       Type value;
 
 public:
 
 public:
-       Uniform1f(float v_);
+       UniformScalar(Type v): value(v) { }
 
        virtual void apply(int index) const;
 
        virtual void apply(int index) const;
-       virtual Uniform1f *clone() const;
+
+       virtual UniformScalar *clone() const
+       { return new UniformScalar(value); }
 };
 
 };
 
+typedef UniformScalar<int> Uniform1i;
+typedef UniformScalar<float> Uniform1f;
 
 
-class Uniform2f: public Uniform
-{
-private:
-       float v[2];
 
 
+template<typename T, unsigned vecsize>
+class UniformVector: public Uniform
+{
 public:
 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:
 private:
-       float v[3];
+       Type value;
 
 public:
 
 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 void apply(int index) const;
-       virtual Uniform3f *clone() const;
+
+       virtual UniformVector *clone() const
+       { return new UniformVector(value); }
 };
 
 };
 
+typedef UniformVector<float, 2> Uniform2f;
+typedef UniformVector<float, 3> Uniform3f;
+typedef UniformVector<float, 4> Uniform4f;
 
 
-class Uniform4f: public Uniform
-{
-private:
-       float v[4];
 
 
+template<typename T, unsigned rows, unsigned cols>
+class UniformMatrix: public Uniform
+{
 public:
 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:
 private:
-       float v[16];
+       Type value;
 
 public:
 
 public:
-       UniformMatrix4x4f(const float *);
+       UniformMatrix(const T *vp)
+       { std::copy(vp, vp+rows*cols, value); }
 
        virtual void apply(int index) const;
 
        virtual void apply(int index) const;
-       virtual UniformMatrix4x4f *clone() const;
+
+       virtual UniformMatrix *clone() const
+       { return new UniformMatrix(value); }
 };
 
 };
 
+typedef UniformMatrix<float, 4, 4> UniformMatrix4x4f;
+
 } // namespace GL
 } // namespace Msp
 
 } // namespace GL
 } // namespace Msp
 
index 95e1c2b368100a2aa920d92efdb2f0ddcb4fc843..fe044750d45e3d1cbcb06506843fd46e8beabea0 100644 (file)
@@ -45,17 +45,19 @@ void UniformBlock::uniform(int index, float v)
 
 void UniformBlock::uniform(int index, float v0, float v1)
 {
 
 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)
 {
 }
 
 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)
 {
 }
 
 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)
 }
 
 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)
 {
 
 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)
 {
 }
 
 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)
 }
 
 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)
 {
 
 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)
 }
 
 void UniformBlock::uniform_matrix4(int index, const float *v)