-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShaderProgram.cpp
More file actions
99 lines (78 loc) · 2.27 KB
/
Copy pathShaderProgram.cpp
File metadata and controls
99 lines (78 loc) · 2.27 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
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
#include "ShaderProgram.h"
#include <vector>
ShaderProgram::ShaderProgram(GLuint programId)
: programIdHolder(new ShaderProgramIdHolder(programId))
{
}
ShaderProgram::ShaderProgram(const Recipe & recipe)
: programIdHolder(new ShaderProgramIdHolder(recipe.link()))
{
}
ShaderProgram::~ShaderProgram() {
}
GLuint ShaderProgram::getProgramId() const {
return this->programIdHolder->programId;
}
void ShaderProgram::onPreDraw(const Model & model) const {
// do nothing - this function is just for inherited classes
}
void ShaderProgram::onPostDraw(const Model & model) const {
// empty
}
ShaderProgram::ShaderProgramIdHolder::ShaderProgramIdHolder(GLuint programId) {
this->programId = programId;
}
ShaderProgram::ShaderProgramIdHolder::~ShaderProgramIdHolder() {
glDeleteProgram(this->programId);
}
GLuint ShaderProgram::Recipe::link() const {
GLuint programId = glCreateProgram();
try {
// attach
for (const Shader & shader : this->shaders) {
glAttachShader(programId, shader.getShaderId());
}
// link
glLinkProgram(programId);
// detach
for (const Shader & shader : this->shaders) {
glDetachShader(programId, shader.getShaderId());
}
// check the link status
GLint glResult = GL_FALSE;
glGetProgramiv(programId, GL_LINK_STATUS, &glResult);
if (GL_TRUE != glResult) {
int infoLogLen = 0;
glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &infoLogLen);
std::vector<char> infoLog(infoLogLen + 1);
glGetProgramInfoLog(programId, infoLogLen, nullptr, &infoLog[0]);
std::string errorMessage = "Shader Link Error:\n";
errorMessage += &infoLog[0];
throw Exception(errorMessage);
}
}
catch (Exception) {
if (0 != programId) {
glDeleteProgram(programId);
programId = 0;
}
throw;
}
return programId;
}
ShaderProgram::Recipe & ShaderProgram::Recipe::addShader(const Shader & shader) {
this->shaders.push_back(shader);
return *this;
}
ShaderProgram::Recipe::Iterator ShaderProgram::Recipe::begin() {
return this->shaders.begin();
}
ShaderProgram::Recipe::Iterator ShaderProgram::Recipe::end() {
return this->shaders.end();
}
ShaderProgram::Recipe::ConstIterator ShaderProgram::Recipe::begin() const {
return this->shaders.end();
}
ShaderProgram::Recipe::ConstIterator ShaderProgram::Recipe::end() const {
return this->shaders.end();
}