1 #ifndef MSP_GL_UNIFORM_H_
2 #define MSP_GL_UNIFORM_H_
15 Uniform(const Uniform &);
16 Uniform &operator=(const Uniform &);
18 virtual ~Uniform() { }
20 virtual void apply(int) const = 0;
21 virtual void store(const Program::UniformInfo &, void *) const = 0;
22 virtual Uniform *clone() const = 0;
27 class UniformScalar: public Uniform
37 UniformScalar(Type v): value(v) { }
39 Type get() const { return value; }
41 virtual void apply(int index) const
42 { apply(index, 1, &value); }
44 static void apply(int, unsigned, const T *);
46 virtual void store(const Program::UniformInfo &info, void *buffer) const
47 { store(info, buffer, &value); }
49 static void store(const Program::UniformInfo &, void *buffer, const T *value)
50 { *reinterpret_cast<T *>(buffer) = *value; }
52 virtual UniformScalar *clone() const
53 { return new UniformScalar(value); }
56 typedef UniformScalar<int> Uniform1i;
57 typedef UniformScalar<float> Uniform1f;
60 template<typename T, unsigned vecsize>
61 class UniformVector: public Uniform
65 typedef T Type[vecsize];
71 UniformVector(const T *vp)
72 { std::copy(vp, vp+vecsize, value); }
74 BaseType get(unsigned i) const { return value[i]; }
76 virtual void apply(int index) const
77 { apply(index, 1, value); }
79 static void apply(int index, unsigned size, const T *value);
81 virtual void store(const Program::UniformInfo &info, void *buffer) const
82 { store(info, buffer, value); }
84 static void store(const Program::UniformInfo &, void *buffer, const T *value)
85 { std::copy(value, value+vecsize, reinterpret_cast<T *>(buffer)); }
87 virtual UniformVector *clone() const
88 { return new UniformVector(value); }
91 typedef UniformVector<int, 2> Uniform2i;
92 typedef UniformVector<float, 2> Uniform2f;
93 typedef UniformVector<int, 3> Uniform3i;
94 typedef UniformVector<float, 3> Uniform3f;
95 typedef UniformVector<int, 4> Uniform4i;
96 typedef UniformVector<float, 4> Uniform4f;
99 template<typename T, unsigned rows, unsigned cols>
100 class UniformMatrix: public Uniform
104 typedef T Type[rows*cols];
110 UniformMatrix(const T *vp)
111 { std::copy(vp, vp+rows*cols, value); }
113 virtual void apply(int index) const
114 { apply(index, 1, value); }
116 static void apply(int index, unsigned size, const T *value);
118 virtual void store(const Program::UniformInfo &info, void *buffer) const
119 { store(info, buffer, value); }
121 static void store(const Program::UniformInfo &info, void *buffer, const T *value)
123 for(unsigned i=0; i<cols; ++i)
124 UniformVector<T, rows>::store(info, reinterpret_cast<char *>(buffer)+i*info.matrix_stride, value+i*rows);
127 virtual UniformMatrix *clone() const
128 { return new UniformMatrix(value); }
131 // The naming of these types follows the OpenGL convention of columns x rows
132 typedef UniformMatrix<float, 2, 2> UniformMatrix2x2f;
133 typedef UniformMatrix<float, 2, 3> UniformMatrix3x2f;
134 typedef UniformMatrix<float, 2, 4> UniformMatrix4x2f;
135 typedef UniformMatrix<float, 3, 2> UniformMatrix2x3f;
136 typedef UniformMatrix<float, 3, 3> UniformMatrix3x3f;
137 typedef UniformMatrix<float, 3, 4> UniformMatrix4x3f;
138 typedef UniformMatrix<float, 4, 2> UniformMatrix2x4f;
139 typedef UniformMatrix<float, 4, 3> UniformMatrix3x4f;
140 typedef UniformMatrix<float, 4, 4> UniformMatrix4x4f;
144 class UniformArray: public Uniform
147 typedef typename T::BaseType BaseType;
148 enum { elemsize = sizeof(typename T::Type)/sizeof(typename T::BaseType) };
154 UniformArray(unsigned n, const BaseType *vp):
157 values = new BaseType[elemsize*size];
158 std::copy(vp, vp+elemsize*size, values);
166 virtual void apply(int index) const
167 { T::apply(index, size, values); }
169 virtual void store(const Program::UniformInfo &info, void *buffer) const
171 for(unsigned i=0; i<size; ++i)
172 T::store(info, reinterpret_cast<char *>(buffer)+i*info.array_stride, values+i*elemsize);
175 virtual UniformArray *clone() const
176 { return new UniformArray(size, values); }