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