hnOsmium0001

GL attribute layout template + helpers

Jan 25th, 2020
193
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. template<int N, typename... Ts>
  2. using NthTypeOf = typename std::tuple_element<N, std::tuple<Ts...>>::type;
  3.  
  4. template <typename T>
  5. struct ToGL {
  6.   static constexpr GLenum value = 0;
  7.   using Type = void;
  8. };
  9.  
  10. template<> struct ToGL<float> {
  11.   static constexpr GLenum value = GL_FLOAT;
  12.   using Type = GLfloat;
  13. };
  14. template<> struct ToGL<double> {
  15.   static constexpr GLenum value = GL_DOUBLE;
  16.   using Type = GLdouble;
  17. };
  18. template<> struct ToGL<int32_t> {
  19.   static constexpr GLenum value = GL_INT;
  20.   using Type = GLint;
  21. };
  22. template<> struct ToGL<int16_t> {
  23.   static constexpr GLenum value = GL_SHORT;
  24.   using Type = GLshort;
  25. };
  26. template<> struct ToGL<int8_t> {
  27.   static constexpr GLenum value = GL_BYTE;
  28.   using Type = GLbyte;
  29. };
  30. template<> struct ToGL<uint32_t> {
  31.   static constexpr GLenum value = GL_UNSIGNED_INT;
  32.   using Type = GLuint;
  33. };
  34. template<> struct ToGL<uint16_t> {
  35.   static constexpr GLenum value = GL_UNSIGNED_SHORT;
  36.   using Type = GLushort;
  37. };
  38. template<> struct ToGL<uint8_t> {
  39.   static constexpr GLenum value = GL_UNSIGNED_BYTE;
  40.   using Type = GLubyte;
  41. };
  42.  
  43. template <typename... As>
  44. class AttributeLayout {
  45. public:
  46.   static constexpr size_t elements = sizeof...(As);
  47.   static constexpr size_t bytes = (sizeof(As) + ... + 0);
  48.  
  49.   static void SetupGL() {
  50.     SetupOne<0>(0);
  51.   }
  52.  
  53. private:
  54.   template <int32_t n>
  55.   inline static void SetupOne(size_t offset) {
  56.     static_assert(n < elements, "Attribute index out of bounds!");
  57.  
  58.     using Arr = NthTypeOf<n, As...>;
  59.     static_assert(std::is_array<Arr>::value, "AttributeLayout parameters must be arrays!");
  60.  
  61.     using Elm = typename std::remove_all_extents<Arr>::type;
  62.     constexpr size_t len = std::extent<Arr>::value;
  63.  
  64.     glEnableVertexAttribArray(n);
  65.     glVertexAttribPointer(n, len, ToGL<Elm>::value, GL_FALSE, bytes, (void*) offset);
  66.  
  67.     if constexpr (n < elements - 1) SetupOne<n + 1>(offset + sizeof(Arr));
  68.   }
  69. };
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×