Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<int N, typename... Ts>
- using NthTypeOf = typename std::tuple_element<N, std::tuple<Ts...>>::type;
- template <typename T>
- struct ToGL {
- static constexpr GLenum value = 0;
- using Type = void;
- };
- template<> struct ToGL<float> {
- static constexpr GLenum value = GL_FLOAT;
- using Type = GLfloat;
- };
- template<> struct ToGL<double> {
- static constexpr GLenum value = GL_DOUBLE;
- using Type = GLdouble;
- };
- template<> struct ToGL<int32_t> {
- static constexpr GLenum value = GL_INT;
- using Type = GLint;
- };
- template<> struct ToGL<int16_t> {
- static constexpr GLenum value = GL_SHORT;
- using Type = GLshort;
- };
- template<> struct ToGL<int8_t> {
- static constexpr GLenum value = GL_BYTE;
- using Type = GLbyte;
- };
- template<> struct ToGL<uint32_t> {
- static constexpr GLenum value = GL_UNSIGNED_INT;
- using Type = GLuint;
- };
- template<> struct ToGL<uint16_t> {
- static constexpr GLenum value = GL_UNSIGNED_SHORT;
- using Type = GLushort;
- };
- template<> struct ToGL<uint8_t> {
- static constexpr GLenum value = GL_UNSIGNED_BYTE;
- using Type = GLubyte;
- };
- template <typename... As>
- class AttributeLayout {
- public:
- static constexpr size_t elements = sizeof...(As);
- static constexpr size_t bytes = (sizeof(As) + ... + 0);
- static void SetupGL() {
- SetupOne<0>(0);
- }
- private:
- template <int32_t n>
- inline static void SetupOne(size_t offset) {
- static_assert(n < elements, "Attribute index out of bounds!");
- using Arr = NthTypeOf<n, As...>;
- static_assert(std::is_array<Arr>::value, "AttributeLayout parameters must be arrays!");
- using Elm = typename std::remove_all_extents<Arr>::type;
- constexpr size_t len = std::extent<Arr>::value;
- glEnableVertexAttribArray(n);
- glVertexAttribPointer(n, len, ToGL<Elm>::value, GL_FALSE, bytes, (void*) offset);
- if constexpr (n < elements - 1) SetupOne<n + 1>(offset + sizeof(Arr));
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement