-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuffer.h
More file actions
122 lines (99 loc) · 1.96 KB
/
Buffer.h
File metadata and controls
122 lines (99 loc) · 1.96 KB
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
#ifndef BUFFER_H
#define BUFFER_H
#include <GL/glew.h>
#include <vector>
class GlBuffer
{
public:
GlBuffer()
: index(0)
{
}
explicit GlBuffer(GLuint index)
: index(index)
{
}
GlBuffer(const GlBuffer& other) = delete;
GlBuffer(GlBuffer&& other)
{
index = other.index;
other.index = 0;
}
GlBuffer& operator=(const GlBuffer& other) = delete;
GlBuffer& operator=(GlBuffer&& other)
{
index = other.index;
other.index = 0;
return *this;
}
~GlBuffer()
{
glDeleteBuffers(1, &index);
}
protected:
GLuint index;
};
class VertexBufferObject : public GlBuffer
{
public:
using GlBuffer::GlBuffer;
static VertexBufferObject generate()
{
GLuint buffer;
glGenBuffers(1, &buffer);
return VertexBufferObject(buffer);
}
template <typename PodType, int N>
static VertexBufferObject generateWithData(PodType (&data)[N], GLenum usage)
{
VertexBufferObject vbo = generate();
vbo.setData(data, usage);
return vbo;
}
void bind() const
{
glBindBuffer(GL_ARRAY_BUFFER, index);
}
static void unbind()
{
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
template <typename PodType, int N>
void setData(PodType (&data)[N], GLenum usage) const
{
bind();
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, usage);
}
template <typename PodType>
void setData(const std::vector<PodType> &data, GLenum usage) const
{
bind();
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(PodType), &data[0], usage);
}
};
class ElementArrayBuffer : public GlBuffer
{
public:
using GlBuffer::GlBuffer;
static ElementArrayBuffer generate()
{
GLuint buffer;
glGenBuffers(1, &buffer);
return ElementArrayBuffer(buffer);
}
void bind() const
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index);
}
static void unbind()
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
template <typename PodType, int N>
void setData(PodType (&data)[N], GLenum usage) const
{
bind();
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(data), data, usage);
}
};
#endif // BUFFER_H