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
|
#include "client/render/program.hh"
namespace client {
namespace render {
static void check_shader(const GLuint shader) {
int status = 0;
char info[512];
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (!status) {
glGetShaderInfoLog(shader, 512, nullptr, info);
throw std::runtime_error(std::string("failed to compile shader: ") +
info);
}
}
static void check_program(const GLuint program) {
int status = 0;
char info[512];
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (!status) {
glGetProgramInfoLog(program, 512, nullptr, info);
throw std::runtime_error(std::string("failed to link program: ") +
info);
}
}
program::program(const std::string_view vpath, const std::string_view fpath) {
const auto make_shader = [](const auto& path, const auto type) -> GLuint {
const std::string source = shared::read_file(path.data());
const char* const src_ptr = source.c_str();
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &src_ptr, nullptr);
glCompileShader(shader);
check_shader(shader);
return shader;
};
GLuint vertex = make_shader(vpath, GL_VERTEX_SHADER);
GLuint fragment = make_shader(fpath, GL_FRAGMENT_SHADER);
this->index = glCreateProgram();
glAttachShader(this->index, vertex);
glAttachShader(this->index, fragment);
glLinkProgram(this->index);
check_program(this->index);
glDeleteShader(vertex);
glDeleteShader(fragment);
}
} // namespace render
} // namespace client
|