-
Notifications
You must be signed in to change notification settings - Fork 0
/
VertexArray.hpp
150 lines (116 loc) · 3.42 KB
/
VertexArray.hpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#pragma once
#include <Tuple.hpp>
#include <Debug.hpp>
#include <Logger.hpp>
#include <ShaderAttrib.hpp>
namespace gl {
template <typename... ShaderTs> class ShaderProgram;
template<typename... AttribTs>
struct VertexArray {
using self_t = VertexArray<AttribTs...>;
GLuint vaoId = 0;
std::tuple<AttribTs &...> attributes;
explicit VertexArray(AttribTs &... attribs):
attributes(std::tie(attribs...))
{}
static void init(GLuint &vao) {
glGenVertexArrays(1, &vao); GLERROR
}
static void init(VertexArray &va) {
va.init();
}
void init() {
init(vaoId);
}
GLuint id() const {
return vaoId;
}
static void bind(GLuint vao) {
glBindVertexArray(vao); GLERROR
}
static void bind(self_t &vao) {
vao.bind();
}
void bind() {
bind(vaoId);
}
template <typename AttribT>
void enable(const AttribT &attrib) {
this->bind();
int index = Tuple::lfind(attributes, attrib);
ASSERT(index != -1);
glEnableVertexAttribArray(index); GLERROR
this->unbind();
}
template <typename AttribT>
void set_access(const AttribT &attrib, size_t stride=0, void *ptr=nullptr) {
this->bind();
attrib.bind();
using atype = a_cast_type<AttribT::element_type>;
glVertexAttribPointer(Tuple::lfind(attributes, attrib),
sizeof(typename atype::type) / sizeof(typename atype::vtype),
gl_scalar_enum<typename atype::vtype>::value,
GL_FALSE,
stride,
ptr); GLERROR
attrib.unbind();
this->unbind();
}
template <typename AttribT>
void set_divisor(const AttribT &attrib, size_t divisor=0) {
this->bind();
attrib.bind();
glVertexAttribDivisor(Tuple::lfind(attributes, attrib), divisor); GLERROR
this->unbind();
}
template <typename AttribT>
void disable(const AttribT &attrib) {
this->bind();
glDisableVertexAttribArray(Tuple::lfind(attributes, attrib)); GLERROR
this->unbind();
}
template <GLenum PRIMITIVES>
void draw(size_t start=0, size_t no_primitives=SIZE_MAX) {
this->bind();
if(no_primitives == SIZE_MAX) {
size_t maxprimitives = 0;
Tuple::for_each(attributes, [&](const auto &attr) mutable -> void {
ASSERT(attr.buf != nullptr);
if(attr.buf->numberOfElements > maxprimitives) {
maxprimitives = attr.buf->numberOfElements;
}
});
no_primitives = maxprimitives - start;
}
ASSERT(no_primitives != SIZE_MAX);
glDrawArrays(PRIMITIVES, start, no_primitives); GLERROR
this->unbind();
}
template <GLenum PRIMITIVES, typename... ShaderTs>
void draw(gl::ShaderProgram<ShaderTs...> &prog, size_t start=0, size_t no_primitives=SIZE_MAX) {
gl::ShaderProgram<ShaderTs...>::use(prog);
this->draw<PRIMITIVES>(start, no_primitives);
gl::ShaderProgram<ShaderTs...>::unuse();
}
template <GLenum PRIMITIVES>
static void draw(self_t &vao, size_t start=0, size_t no_primitives=SIZE_MAX) {
vao.draw<PRIMITIVES>(start, no_primitives);
}
template <GLenum PRIMITIVES, typename... ShaderTs>
static void draw(gl::ShaderProgram<ShaderTs...> &prog, self_t &vao, size_t start=0, size_t no_primitives=SIZE_MAX) {
vao.draw<PRIMITIVES>(prog, start, no_primitives);
}
static void unbind() {
glBindVertexArray(0); GLERROR
}
static void clear(GLuint &vao) {
glDeleteVertexArrays(1, &vao); GLERROR
}
static void clear(VertexArray &va) {
va.clear();
}
void clear() {
clear(vaoId);
}
};
} // namespace gl