-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIndexBuffer.hpp
58 lines (46 loc) · 1.48 KB
/
IndexBuffer.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
#pragma once
#include <glbinding/gl/gl.h>
#include <array>
using namespace gl;
class IndexBuffer {
private:
GLuint id;
GLsizei count;
public:
IndexBuffer(const GLuint* data, GLsizei count) : count(count) {
glGenBuffers(1, &id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(GLuint), data, GL_STATIC_DRAW);
}
template<typename type, size_t size>
IndexBuffer(const std::array<type, size>& indices) : count(indices.size()) {
glGenBuffers(1, &id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices.data(), GL_STATIC_DRAW);
}
~IndexBuffer() {
glDeleteBuffers(1, &id);
}
void bind() const {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
}
static void unbind() {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
template<GLsizei size>
static constexpr std::array<GLuint, size> genIndices() {
std::array<GLuint, size> indices;
GLsizei offset = 0;
for (GLsizei i = 0; i < size; i += 6) {
indices[i + 0] = 0 + offset;
indices[i + 1] = 1 + offset;
indices[i + 2] = 2 + offset;
indices[i + 3] = 2 + offset;
indices[i + 4] = 3 + offset;
indices[i + 5] = 0 + offset;
offset += 4;
}
return indices;
}
inline GLsizei getCount() const { return count; };
};