1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
#pragma once
#include "collections.hpp"
#include "gl_traits.hpp"
#include "special_members.hpp"
#include <GL/glew.h>
class VertexArrayObject {
public:
template<typename T> [[nodiscard]] VertexArrayObject(const T & arrayObject)
{
glBindVertexArray(arrayObject);
}
~VertexArrayObject()
{
glBindVertexArray(0);
}
NO_MOVE(VertexArrayObject);
NO_COPY(VertexArrayObject);
template<typename m, typename T> struct MP {
constexpr MP(m T::*p) : P {p} { }
operator void *() const
{
return &(static_cast<T *>(nullptr)->*P);
}
m T::*P;
using value_type = m;
};
template<typename m, typename T> MP(m T::*) -> MP<m, T>;
template<typename VertexT, MP... attribs>
VertexArrayObject &
addAttribs(const GLuint arrayBuffer, const SequentialCollection<VertexT> auto & vertices)
{
addAttribs<VertexT, attribs...>(arrayBuffer);
data(vertices, arrayBuffer, GL_ARRAY_BUFFER);
return *this;
}
template<typename VertexT, MP... attribs>
VertexArrayObject &
addAttribs(const GLuint arrayBuffer)
{
configure_attribs<VertexT, attribs...>(arrayBuffer);
return *this;
}
template<typename Indices>
VertexArrayObject &
addIndices(const GLuint arrayBuffer, const Indices & indices)
{
data(indices, arrayBuffer, GL_ELEMENT_ARRAY_BUFFER);
return *this;
}
VertexArrayObject &
addIndices(const GLuint arrayBuffer)
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, arrayBuffer);
return *this;
}
template<typename Data>
static void
data(const Data & data, const GLuint arrayBuffer, GLenum target)
{
using Value = typename Data::value_type;
glBindBuffer(target, arrayBuffer);
glBufferData(target, static_cast<GLsizeiptr>(sizeof(Value) * data.size()), data.data(), GL_STATIC_DRAW);
}
private:
template<typename VertexT, typename T>
static void
set_pointer(const GLuint vertexArrayId, const void * ptr)
{
glEnableVertexAttribArray(vertexArrayId);
using traits = gl_traits<T>;
traits::vertexAttribFunc(vertexArrayId, traits::size, traits::type, sizeof(VertexT), ptr);
}
template<typename VertexT, MP attrib>
static void
set_pointer(const GLuint vertexArrayId)
{
set_pointer<VertexT, typename decltype(attrib)::value_type>(vertexArrayId, attrib);
}
template<typename VertexT, MP... attribs>
static void
configure_attribs(const GLuint arrayBuffer)
{
glBindBuffer(GL_ARRAY_BUFFER, arrayBuffer);
if constexpr (sizeof...(attribs) == 0) {
set_pointer<VertexT, VertexT>(0, nullptr);
}
else {
GLuint vertexArrayId {};
(set_pointer<VertexT, attribs>(vertexArrayId++), ...);
}
}
};
|