1 #include "../graphics/gl_traits.hpp"
5 template<std::size_t N>
6 VertexArray<N>::VertexArray() noexcept
8 , idx_type(GL_UNSIGNED_INT) {
9 glGenVertexArrays(1, &array_id);
10 glGenBuffers(N, attr_id);
13 template<std::size_t N>
14 VertexArray<N>::~VertexArray() noexcept {
16 glDeleteBuffers(N, attr_id);
17 glDeleteVertexArrays(1, &array_id);
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) {
27 for (std::size_t i = 0; i < N; ++i) {
28 attr_id[i] = other.attr_id[i];
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]);
39 idx_count = other.idx_count;
40 idx_type = other.idx_type;
44 template<std::size_t N>
45 void VertexArray<N>::Bind() const noexcept {
46 glBindVertexArray(array_id);
49 template<std::size_t N>
51 void VertexArray<N>::PushAttribute(std::size_t which, const std::vector<T> &data) noexcept {
54 EnableAttribute(which);
55 AttributePointer<T>(which);
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]);
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);
70 template<std::size_t N>
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);
76 template<std::size_t N>
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
89 template<std::size_t N>
91 void VertexArray<N>::PushIndices(std::size_t which, const std::vector<T> &indices) noexcept {
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]);
102 template<std::size_t N>
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;
111 template<std::size_t N>
112 void VertexArray<N>::DrawLineElements() const noexcept {
122 template<std::size_t N>
123 void VertexArray<N>::DrawTriangleElements() const noexcept {