1 #include "../graphics/gl_traits.hpp"
6 template<std::size_t N>
7 VertexArray<N>::VertexArray() noexcept
9 , idx_type(GL_UNSIGNED_INT) {
10 glGenVertexArrays(1, &array_id);
11 glGenBuffers(N, attr_id);
14 template<std::size_t N>
15 VertexArray<N>::~VertexArray() noexcept {
17 glDeleteBuffers(N, attr_id);
18 glDeleteVertexArrays(1, &array_id);
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) {
28 for (std::size_t i = 0; i < N; ++i) {
29 attr_id[i] = other.attr_id[i];
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]);
40 idx_count = other.idx_count;
41 idx_type = other.idx_type;
45 template<std::size_t N>
46 void VertexArray<N>::Bind() const noexcept {
47 glBindVertexArray(array_id);
50 template<std::size_t N>
52 void VertexArray<N>::PushAttribute(std::size_t which, const std::vector<T> &data, bool normalized) noexcept {
55 EnableAttribute(which);
56 AttributePointer<T>(which, normalized);
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]);
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);
71 template<std::size_t N>
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);
77 template<std::size_t N>
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
90 template<std::size_t N>
92 void VertexArray<N>::PushIndices(std::size_t which, const std::vector<T> &indices) noexcept {
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]);
103 template<std::size_t N>
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;
112 template<std::size_t N>
113 void VertexArray<N>::DrawLineElements() const noexcept {
123 template<std::size_t N>
124 void VertexArray<N>::DrawTriangleElements() const noexcept {