-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshader.cpp
63 lines (52 loc) · 1.25 KB
/
shader.cpp
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
#include "shader.hpp"
namespace rematrix {
shader::shader(GLenum type, const char* src)
: id_ { glCreateShader(type) }
{
if (id_ == 0) {
throw runtime_error("couldn't create shader");
}
if (src) {
compile(src);
}
}
shader::shader(shader&& other) noexcept
: id_ { other.id_ }
{
other.id_ = 0;
}
shader::~shader()
{
glDeleteShader(id_);
}
shader& shader::operator=(shader&& other) noexcept
{
id_ = other.id_;
other.id_ = 0;
return *this;
}
void shader::compile(const char* src)
{
glShaderSource(id_, 1, &src, nullptr);
glCompileShader(id_);
GLint ok;
glGetShaderiv(id_, GL_COMPILE_STATUS, &ok);
if (!ok) {
GLint length;
glGetShaderiv(id_, GL_INFO_LOG_LENGTH, &length);
string log(length, '\0');
glGetShaderInfoLog(id_, length, nullptr, log.data());
throw runtime_error(log);
}
}
//----------------------------------------------------------------------------
vertex_shader::vertex_shader(const char* src)
: shader { GL_VERTEX_SHADER, src }
{
}
//----------------------------------------------------------------------------
fragment_shader::fragment_shader(const char* src)
: shader { GL_FRAGMENT_SHADER, src }
{
}
} // namespace rematrix