]> git.localhorst.tv Git - blank.git/blob - src/graphics/VertexArray.inl
check for entities under crosshair
[blank.git] / src / graphics / VertexArray.inl
1 #include "../graphics/gl_traits.hpp"
2
3 namespace blank {
4
5 template<std::size_t N>
6 VertexArray<N>::VertexArray() noexcept
7 : idx_count(0)
8 , idx_type(GL_UNSIGNED_INT) {
9         glGenVertexArrays(1, &array_id);
10         glGenBuffers(N, attr_id);
11 }
12
13 template<std::size_t N>
14 VertexArray<N>::~VertexArray() noexcept {
15         if (array_id != 0) {
16                 glDeleteBuffers(N, attr_id);
17                 glDeleteVertexArrays(1, &array_id);
18         }
19 }
20
21 template<std::size_t N>
22 VertexArray<N>::VertexArray(VertexArray<N> &&other) noexcept
23 : array_id(other.array_id)
24 , idx_count(other.idx_count)
25 , idx_type(other.idx_type) {
26         other.array_id = 0;
27         for (std::size_t i = 0; i < N; ++i) {
28                 attr_id[i] = other.attr_id[i];
29                 other.attr_id[i] = 0;
30         }
31 }
32
33 template<std::size_t N>
34 VertexArray<N> &VertexArray<N>::operator =(VertexArray<N> &&other) noexcept {
35         std::swap(array_id, other.array_id);
36         for (std::size_t i = 0; i < N; ++i) {
37                 std::swap(attr_id[i], other.attr_id[i]);
38         }
39         idx_count = other.idx_count;
40         idx_type = other.idx_type;
41         return *this;
42 }
43
44 template<std::size_t N>
45 void VertexArray<N>::Bind() const noexcept {
46         glBindVertexArray(array_id);
47 }
48
49 template<std::size_t N>
50 template <class T>
51 void VertexArray<N>::PushAttribute(std::size_t which, const std::vector<T> &data) noexcept {
52         BindAttribute(which);
53         AttributeData(data);
54         EnableAttribute(which);
55         AttributePointer<T>(which);
56 }
57
58 template<std::size_t N>
59 void VertexArray<N>::BindAttribute(std::size_t i) const noexcept {
60         assert(i < NUM_ATTRS && "vertex attribute ID out of bounds");
61         glBindBuffer(GL_ARRAY_BUFFER, attr_id[i]);
62 }
63
64 template<std::size_t N>
65 void VertexArray<N>::EnableAttribute(std::size_t i) noexcept {
66         assert(i < NUM_ATTRS && "vertex attribute ID out of bounds");
67         glEnableVertexAttribArray(i);
68 }
69
70 template<std::size_t N>
71 template<class T>
72 void VertexArray<N>::AttributeData(const std::vector<T> &buf) noexcept {
73         glBufferData(GL_ARRAY_BUFFER, buf.size() * sizeof(T), buf.data(), GL_STATIC_DRAW);
74 }
75
76 template<std::size_t N>
77 template <class T>
78 void VertexArray<N>::AttributePointer(std::size_t which) noexcept {
79         glVertexAttribPointer(
80                 which,              // program location
81                 gl_traits<T>::size, // element size
82                 gl_traits<T>::type, // element type
83                 GL_FALSE,           // normalize to [-1,1] or [0,1] for unsigned types
84                 0,                  // stride
85                 nullptr             // offset
86         );
87 }
88
89 template<std::size_t N>
90 template <class T>
91 void VertexArray<N>::PushIndices(std::size_t which, const std::vector<T> &indices) noexcept {
92         BindIndex(which);
93         IndexData(indices);
94 }
95
96 template<std::size_t N>
97 void VertexArray<N>::BindIndex(std::size_t i) const noexcept {
98         assert(i < NUM_ATTRS && "element index ID out of bounds");
99         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, attr_id[i]);
100 }
101
102 template<std::size_t N>
103 template<class T>
104 void VertexArray<N>::IndexData(const std::vector<T> &buf) noexcept {
105         glBufferData(GL_ELEMENT_ARRAY_BUFFER, buf.size() * sizeof(T), buf.data(), GL_STATIC_DRAW);
106         idx_count = buf.size();
107         idx_type = gl_traits<T>::type;
108 }
109
110
111 template<std::size_t N>
112 void VertexArray<N>::DrawLineElements() const noexcept {
113         Bind();
114         glDrawElements(
115                 GL_LINES,  // how
116                 idx_count, // count
117                 idx_type,  // type
118                 nullptr    // offset
119         );
120 }
121
122 template<std::size_t N>
123 void VertexArray<N>::DrawTriangleElements() const noexcept {
124         Bind();
125         glDrawElements(
126                 GL_TRIANGLES, // how
127                 idx_count,    // count
128                 idx_type,     // type
129                 nullptr       // offset
130         );
131 }
132
133 }