-
Notifications
You must be signed in to change notification settings - Fork 0
/
gl.hpp
183 lines (139 loc) · 3.46 KB
/
gl.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#ifndef GL_HPP
#define GL_HPP
#include <GL/glew.h>
#include "finally.hpp"
#include <array>
#include <cassert>
#include <iostream>
namespace gl {
static void init() {
GLenum err = glewInit();
if(GLEW_OK != err) {
throw std::runtime_error((const char*)glewGetErrorString(err));
}
}
template<GLenum kind>
struct buffer {
GLuint id = 0;
buffer& bind() {
if(!id) {
glGenBuffers(1, &id);
assert(id);
}
glBindBuffer(kind, id);
return *this;
}
template<class T>
void data(const T* ptr, std::size_t size, GLenum mode) {
glBufferData(kind, sizeof(T) * size, ptr, mode);
}
// convenience
template<class Container>
void data(const Container& container, GLenum mode) {
data(container.data(), container.size(), mode);
}
};
template<class T>
struct value_traits;
template<std::size_t N>
struct value_traits<std::array<GLfloat, N>> {
static constexpr GLenum type = GL_FLOAT;
static constexpr GLuint size = N;
static_assert(N <= 4, "size error");
};
static bool error_check(std::ostream& out=std::cerr) {
GLenum err = glGetError();
if(err == GL_NO_ERROR) {
return false;
}
out << "error: " << err << "\n";
return error_check(out) || true;
}
// old-school vertex/color/normal pointers
template<GLenum kind>
struct attrib_traits;
template<>
struct attrib_traits<GL_VERTEX_ARRAY> {
template<class... Args>
static void enable(Args... args) {
return glVertexPointer(args...);
}
};
template<>
struct attrib_traits<GL_COLOR_ARRAY> {
template<class... Args>
static void enable(Args... args) {
return glColorPointer(args...);
}
};
template<>
struct attrib_traits<GL_NORMAL_ARRAY> {
template<class... Args>
static void enable(GLuint, Args... args) {
return glNormalPointer(args...);
}
};
template<>
struct attrib_traits<GL_TEXTURE_COORD_ARRAY> {
template<class... Args>
static void enable(Args... args) {
return glTexCoordPointer(args...);
}
};
template<GLenum kind>
struct attrib {
GLuint size;
GLuint type;
auto enable(GLenum stride = 0, std::size_t offset = 0) const {
glEnableClientState(kind);
attrib_traits<kind>::enable(size, type, stride, (void*)offset);
}
auto disable() const { glDisableClientState(kind); };
};
struct geometry {
template<GLenum kind>
struct attrib_buffer: attrib<kind> {
gl::buffer<GL_ARRAY_BUFFER> buffer;
template<class Container>
auto data(const Container& container, GLenum mode = GL_STATIC_DRAW) {
buffer.bind().data(container, mode);
using traits_type = gl::value_traits<typename Container::value_type>;
this->size = traits_type::size;
this->type = traits_type::type;
}
};
attrib_buffer<GL_VERTEX_ARRAY> vertex;
attrib_buffer<GL_NORMAL_ARRAY> normal;
attrib_buffer<GL_COLOR_ARRAY> color;
attrib_buffer<GL_TEXTURE_COORD_ARRAY> texcoord;
template<class Self, class Cont>
friend void foreach(Self&& self, Cont cont) {
if(self.vertex.buffer.id) {
cont(self.vertex);
}
if(self.normal.buffer.id) {
cont(self.normal);
}
if(self.color.buffer.id) {
cont(self.color);
}
if(self.texcoord.buffer.id) {
cont(self.texcoord);
}
}
auto enable() {
foreach(*this, [](auto& attrib) {
attrib.buffer.bind();
attrib.enable();
});
}
auto disable() const {
foreach(*this, [](auto& attrib) { attrib.disable(); });
}
auto lock() {
enable();
return finally([this] { disable(); });
}
};
} // namespace gl
#endif