-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShaderProgram.cpp
More file actions
73 lines (63 loc) · 2.06 KB
/
ShaderProgram.cpp
File metadata and controls
73 lines (63 loc) · 2.06 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
#include "ShaderProgram.h"
#include "Util.h"
ShaderProgram::ShaderProgram(const std::string& vsFilePath, const std::string& fsFilePath) {
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
std::string vsSource = readFile(vsFilePath);
const char* vsSourceCstr = vsSource.c_str();
glShaderSource(vs, 1, &vsSourceCstr, nullptr);
glCompileShader(vs);
int res;
glGetShaderiv(vs, GL_COMPILE_STATUS, &res);
if (res == GL_FALSE) {
char buf[2048];
glGetShaderInfoLog(vs, 2048, nullptr, buf);
glDeleteShader(vs);
throw GraphicsException(std::string(buf));
}
std::string fsSource = readFile(fsFilePath);
const char* fsSourceCstr = fsSource.c_str();
glShaderSource(fs, 1, &fsSourceCstr, nullptr);
glCompileShader(fs);
glGetShaderiv(fs, GL_COMPILE_STATUS, &res);
if (res == GL_FALSE) {
char buf[2048];
glGetShaderInfoLog(fs, 2048, nullptr, buf);
glDeleteShader(vs);
glDeleteShader(fs);
throw GraphicsException(std::string(buf));
}
m_programId = glCreateProgram();
glAttachShader(m_programId, vs);
glAttachShader(m_programId, fs);
glLinkProgram(m_programId);
glGetProgramiv(m_programId, GL_LINK_STATUS, &res);
if (res == GL_FALSE) {
char buf[2048];
glGetProgramInfoLog(m_programId, 2048, nullptr, buf);
glDeleteShader(vs);
glDeleteShader(fs);
throw GraphicsException("link error: " + std::string(buf));
}
glDetachShader(m_programId, vs);
glDetachShader(m_programId, fs);
glDeleteShader(vs);
glDeleteShader(fs);
}
ShaderProgram::~ShaderProgram() {
glDeleteProgram(m_programId);
}
void ShaderProgram::use() {
glUseProgram(m_programId);
}
GLuint ShaderProgram::getId() const {
return m_programId;
}
GLint ShaderProgram::operator[](const std::string& s) const {
// auto it = m_uniforms.find(s);
// if (it == m_uniforms.end()) {
// return -1;
// }
// return it->second;
return glGetUniformLocation(m_programId, s.c_str());
}