summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2024-01-14 11:38:09 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2024-01-14 11:38:09 +0000
commit08128c14ec49a2b357e830c832ccd0a8e63c175d (patch)
tree6634dea4e5d8bac32bdcf1c2ea68000874c61a5a /lib
parentWe only need 3x3 matrix for model rotation spec (diff)
downloadilt-08128c14ec49a2b357e830c832ccd0a8e63c175d.tar.bz2
ilt-08128c14ec49a2b357e830c832ccd0a8e63c175d.tar.xz
ilt-08128c14ec49a2b357e830c832ccd0a8e63c175d.zip
Add traits wrapper for setting uniforms
Diffstat (limited to 'lib')
-rw-r--r--lib/gl_traits.h56
1 files changed, 56 insertions, 0 deletions
diff --git a/lib/gl_traits.h b/lib/gl_traits.h
index 934b505..2d3d85d 100644
--- a/lib/gl_traits.h
+++ b/lib/gl_traits.h
@@ -4,6 +4,8 @@
#include <glad/gl.h>
#include <glm/common.hpp>
#include <glm/fwd.hpp>
+#include <glm/gtc/type_ptr.hpp>
+#include <span>
template<typename T> struct gl_traits;
@@ -37,6 +39,9 @@ struct gl_traits_integer : public gl_traits_base {
template<> struct gl_traits<glm::f32> : public gl_traits_float {
static constexpr GLenum type {GL_FLOAT};
+ static constexpr auto glUniformFunc {&glUniform1f};
+ static constexpr std::array glUniformvFunc {&glUniform1fv, &glUniform2fv, &glUniform3fv, &glUniform4fv};
+ static constexpr std::array glUniformmFunc {&glUniformMatrix2fv, &glUniformMatrix3fv, &glUniformMatrix4fv};
};
template<> struct gl_traits<glm::f64> : public gl_traits_longfloat {
@@ -53,6 +58,8 @@ template<> struct gl_traits<glm::int16> : public gl_traits_integer {
template<> struct gl_traits<glm::int32> : public gl_traits_integer {
static constexpr GLenum type {GL_INT};
+ static constexpr auto glUniformFunc {&glUniform1i};
+ static constexpr std::array glUniformvFunc {&glUniform1iv, &glUniform2iv, &glUniform3iv, &glUniform4iv};
};
template<> struct gl_traits<glm::uint8> : public gl_traits_integer {
@@ -65,6 +72,9 @@ template<> struct gl_traits<glm::uint16> : public gl_traits_integer {
template<> struct gl_traits<glm::uint32> : public gl_traits_integer {
static constexpr GLenum type {GL_UNSIGNED_INT};
+ static constexpr auto glUniformFunc {&glUniform1ui};
+ static constexpr std::array<decltype(&glUniform1uiv), 5> glUniformvFunc {
+ &glUniform1uiv, &glUniform2uiv, &glUniform3uiv, &glUniform4uiv};
};
template<typename T, std::size_t S> struct gl_traits<std::array<T, S>> : public gl_traits<T> {
@@ -87,3 +97,49 @@ struct gl_traits<glm::mat<C, R, T, Q>> : public gl_traits<T> {
return R;
}};
};
+
+template<typename T>
+void
+glUniform(GLint location, T v)
+{
+ static_assert(
+ requires { gl_traits<T>::glUniformFunc; }, "Has glUnform1T");
+ (*gl_traits<T>::glUniformFunc)(location, v);
+}
+
+template<glm::length_t L, typename T, glm::qualifier Q>
+void
+glUniform(GLint location, const glm::vec<L, T, Q> & v)
+{
+ static_assert(
+ requires { gl_traits<T>::glUniformFunc; }, "Has glUnformNTv");
+ (*gl_traits<T>::glUniformvFunc[L - 1])(location, 1, glm::value_ptr(v));
+}
+
+template<glm::length_t L, typename T, glm::qualifier Q>
+void
+glUniform(GLint location, std::span<const glm::vec<L, T, Q>> v)
+{
+ static_assert(
+ requires { gl_traits<T>::glUniformvFunc; }, "Has glUnformNTv");
+ (*gl_traits<T>::glUniformvFunc[L - 1])(location, static_cast<GLsizei>(v.size()), glm::value_ptr(v.front()));
+}
+
+template<glm::length_t L, typename T, glm::qualifier Q>
+void
+glUniform(GLint location, const glm::mat<L, L, T, Q> & v)
+{
+ static_assert(
+ requires { gl_traits<T>::glUniformmFunc; }, "Has glUnformMatrixNTv");
+ (*gl_traits<T>::glUniformmFunc[L - 2])(location, 1, GL_FALSE, glm::value_ptr(v));
+}
+
+template<glm::length_t L, typename T, glm::qualifier Q>
+void
+glUniform(GLint location, std::span<const glm::mat<L, L, T, Q>> v)
+{
+ static_assert(
+ requires { gl_traits<T>::glUniformmFunc; }, "Has glUnformMatrixNTv");
+ (*gl_traits<T>::glUniformmFunc[L - 2])(
+ location, static_cast<GLsizei>(v.size()), GL_FALSE, glm::value_ptr(v.front()));
+}