-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgl.hh
183 lines (150 loc) · 3.71 KB
/
gl.hh
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
#pragma once
#include "nullable.hh"
#include <memory>
#include <string>
#include <initializer_list>
#define GL_GLEXT_PROTOTYPES
#include <GL/glcorearb.h>
namespace gl
{
///////////////////////////////////////////////////////////////////////
inline namespace texture_detail
{
struct texture_deleter
{
using pointer = tue::nullable<GLuint>;
void operator () (GLuint tex) { glDeleteTextures(1, &tex); }
};
using texture = std::unique_ptr<void, texture_deleter>;
inline auto make_texture()
{
GLuint tex;
glGenTextures(1, &tex);
return texture{tex};
}
inline void bind(texture const& tex)
{
glBindTexture(GL_TEXTURE_2D, tex.get());
}
inline void upload(texture const& tex, int w, int h, float img[] = nullptr)
{
bind(tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, w, h, 0, GL_RED, GL_FLOAT, img);
}
inline void upload(texture const& tex, int w, int h, float img[], int x, int y)
{
bind(tex);
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_RED, GL_FLOAT, img);
}
inline auto make_texture(int w, int h)
{
auto tex = make_texture();
upload(tex, w, h);
return tex;
}
};
///////////////////////////////////////////////////////////////////////
inline namespace shader_detail
{
struct shader_deleter
{
using pointer = tue::nullable<GLuint>;
void operator () (GLuint s) { glDeleteShader(s); }
};
using shader = std::unique_ptr<void, shader_deleter>;
inline auto make_shader(GLenum type)
{
return shader{glCreateShader(type)};
}
inline auto upload(shader const& s, std::string const& src)
{
GLchar const* cstr = src.c_str();
GLint size = src.size();
glShaderSource(s.get(), 1, &cstr, &size);
}
inline auto compile(shader const& s)
{
glCompileShader(s.get());
}
inline auto make_shader(GLenum type, std::string const& src)
{
auto s = make_shader(type);
upload(s, src);
compile(s);
return s;
}
inline auto make_vertex_shader(std::string const& src)
{
return make_shader(GL_VERTEX_SHADER, src);
}
inline auto make_fragment_shader(std::string const& src)
{
return make_shader(GL_FRAGMENT_SHADER, src);
}
}
///////////////////////////////////////////////////////////////////////
inline namespace program_detail
{
struct program_deleter
{
using pointer = tue::nullable<GLuint>;
void operator () (GLuint p) { glDeleteProgram(p); }
};
using program = std::unique_ptr<void, program_deleter>;
inline auto make_program()
{
return program{glCreateProgram()};
}
template <class ...SHADERS>
inline auto attach(program const& p, SHADERS const& ... ss)
{
(void)(int[]){ 0, (glAttachShader(p.get(), ss.get()), 0)... };
}
inline auto link(program const& p)
{
glLinkProgram(p.get());
}
template <class ...SHADERS>
inline auto make_program_from(SHADERS const& ... ss)
{
auto p = make_program();
attach(p, ss...);
link(p);
return p;
}
inline auto make_program(std::string const& vs, std::string const& fs)
{
return make_program_from(
make_vertex_shader(vs),
make_fragment_shader(fs));
}
inline auto use(program const& p)
{
glUseProgram(p.get());
}
}
///////////////////////////////////////////////////////////////////////
inline namespace buffer_detail
{
struct buffer_deleter
{
using pointer = tue::nullable<GLuint>;
void operator () (GLuint buf) { glDeleteBuffers(1, &buf); }
};
using buffer = std::unique_ptr<void, buffer_deleter>;
inline auto make_buffer()
{
GLuint buf;
glGenBuffers(1, &buf);
return buffer{buf};
}
inline void bind(buffer const& buf)
{
glBindBuffer(GL_ARRAY_BUFFER, buf.get());
}
inline void upload(buffer const& buf)
{
glBindBuffer(GL_ARRAY_BUFFER, buf.get());
}
}
}