This makes adding new uniform types easier, especially arrays.
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)
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)
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)
-#include <algorithm>
#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
#ifndef MSP_GL_UNIFORM_H_
#define MSP_GL_UNIFORM_H_
+#include <algorithm>
+
namespace Msp {
namespace GL {
-class Program;
-
class Uniform
{
protected:
};
-class Uniform1i: public Uniform
+template<typename T>
+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<int> Uniform1i;
+typedef UniformScalar<float> Uniform1f;
-class Uniform2f: public Uniform
-{
-private:
- float v[2];
+template<typename T, unsigned vecsize>
+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<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:
- 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<float, 4, 4> UniformMatrix4x4f;
+
} // namespace GL
} // namespace Msp
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)
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)
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)