All Classes Files Functions Variables Typedefs Pages
VertexLayout.hpp
1 #pragma once
2 
3 #include <GL/glew.h>
4 
5 #include <cstdint>
6 #include <type_traits>
7 #include <iostream>
8 
9 namespace lumina {
10 
11 namespace internal {
12 
13 template <int Index,
14  int Stride,
15  int Offset,
16  int Size,
17  int... Tail>
18 typename std::enable_if<sizeof...(Tail) == 0>::type applyLayoutImpl() {
19  // std::cout << Index << ", " << Stride << ", " << Offset << ", " << Size
20  // << std::endl;
21  static_assert(Size >= 4, "Incompatible type for vertex layout (to small!)");
22  static_assert(Size % 4 == 0,
23  "Incompatible type for vertex layout (size not "
24  "divisible by 4!)");
25  glVertexAttribPointer(Index,
26  Size/4,
27  GL_FLOAT,
28  GL_FALSE,
29  Stride,
30  reinterpret_cast<void*>(Offset));
31  glEnableVertexAttribArray(Index);
32 }
33 
34 template <int Index,
35  int Stride,
36  int Offset,
37  int Size,
38  int... Tail>
39 typename std::enable_if<sizeof...(Tail) != 0>::type applyLayoutImpl() {
40  static_assert(Size >= 4, "Incompatible type for vertex layout (to small!)");
41  static_assert(Size % 4 == 0,
42  "Incompatible type for vertex layout (size not "
43  "divisible by 4!)");
44  // std::cout << Index << ", " << Stride << ", " << Offset << ", " << Size
45  // << std::endl;
46  glVertexAttribPointer(Index,
47  Size/4,
48  GL_FLOAT,
49  GL_FALSE,
50  Stride,
51  reinterpret_cast<void*>(Offset));
52  glEnableVertexAttribArray(Index);
53  applyLayoutImpl<Index + 1, Stride, Offset + Size, Tail...>();
54 }
55 
56 template <typename T, typename... Ts>
57 struct LayoutTypes {
58  static constexpr int stride = LayoutTypes<Ts...>::stride + sizeof(T);
59 };
60 template <typename T>
61 struct LayoutTypes<T> {
62  static constexpr int stride = sizeof(T);
63 };
64 
65 }
66 
67 } // namespace lumina
Definition: VertexLayout.hpp:57