-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShaderProgram.cpp
222 lines (181 loc) · 6.51 KB
/
ShaderProgram.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "ShaderProgram.h"
ShaderProgram::ShaderProgram(const std::unordered_map<GLenum, std::string> &inputShaders)
{
shaderProgram = glCreateProgram();
if (inputShaders.find(GL_VERTEX_SHADER) != inputShaders.end())
{
shaderObjects[GL_VERTEX_SHADER] = LoadShaderObject(GL_VERTEX_SHADER, inputShaders.at(GL_VERTEX_SHADER));
glAttachShader(shaderProgram, shaderObjects[GL_VERTEX_SHADER]);
}
if (inputShaders.find(GL_FRAGMENT_SHADER) != inputShaders.end())
{
shaderObjects[GL_FRAGMENT_SHADER] = LoadShaderObject(GL_FRAGMENT_SHADER, inputShaders.at(GL_FRAGMENT_SHADER));
glAttachShader(shaderProgram, shaderObjects[GL_FRAGMENT_SHADER]);
}
if (inputShaders.find(GL_GEOMETRY_SHADER) != inputShaders.end())
{
shaderObjects[GL_GEOMETRY_SHADER] = LoadShaderObject(GL_GEOMETRY_SHADER, inputShaders.at(GL_GEOMETRY_SHADER));
glAttachShader(shaderProgram, shaderObjects[GL_GEOMETRY_SHADER]);
}
if (inputShaders.find(GL_TESS_CONTROL_SHADER) != inputShaders.end())
{
shaderObjects[GL_TESS_CONTROL_SHADER] = LoadShaderObject(GL_TESS_CONTROL_SHADER,
inputShaders.at(GL_TESS_CONTROL_SHADER));
glAttachShader(shaderProgram, shaderObjects[GL_TESS_CONTROL_SHADER]);
}
if (inputShaders.find(GL_TESS_EVALUATION_SHADER) != inputShaders.end())
{
shaderObjects[GL_TESS_EVALUATION_SHADER] = LoadShaderObject(GL_TESS_EVALUATION_SHADER,
inputShaders.at(GL_TESS_EVALUATION_SHADER));
glAttachShader(shaderProgram, shaderObjects[GL_TESS_EVALUATION_SHADER]);
}
if (inputShaders.find(GL_COMPUTE_SHADER) != inputShaders.end())
{
shaderObjects[GL_COMPUTE_SHADER] = LoadShaderObject(GL_COMPUTE_SHADER, inputShaders.at(GL_COMPUTE_SHADER));
glAttachShader(shaderProgram, shaderObjects[GL_COMPUTE_SHADER]);
}
glLinkProgram(shaderProgram);
GLint linkStatus;
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &linkStatus);
if (linkStatus != GL_TRUE)
{
GLchar infoLog[512];
glGetProgramInfoLog(shaderProgram, 512, nullptr, infoLog);
std::cerr << "Shader program linking failed\n" << infoLog << std::endl;
shaderProgram = 0;
}
}
void ShaderProgram::Release()
{
if (shaderObjects.find(GL_VERTEX_SHADER) != shaderObjects.end())
{
glDetachShader(shaderProgram, shaderObjects[GL_VERTEX_SHADER]);
glDeleteShader(shaderObjects[GL_VERTEX_SHADER]);
}
if (shaderObjects.find(GL_FRAGMENT_SHADER) != shaderObjects.end())
{
glDetachShader(shaderProgram, shaderObjects[GL_FRAGMENT_SHADER]);
glDeleteShader(shaderObjects[GL_FRAGMENT_SHADER]);
}
if (shaderObjects.find(GL_GEOMETRY_SHADER) != shaderObjects.end())
{
glDetachShader(shaderProgram, shaderObjects[GL_GEOMETRY_SHADER]);
glDeleteShader(shaderObjects[GL_GEOMETRY_SHADER]);
}
if (shaderObjects.find(GL_TESS_CONTROL_SHADER) != shaderObjects.end())
{
glDetachShader(shaderProgram, shaderObjects[GL_TESS_CONTROL_SHADER]);
glDeleteShader(shaderObjects[GL_TESS_CONTROL_SHADER]);
}
if (shaderObjects.find(GL_TESS_EVALUATION_SHADER) != shaderObjects.end())
{
glDetachShader(shaderProgram, shaderObjects[GL_TESS_EVALUATION_SHADER]);
glDeleteShader(shaderObjects[GL_TESS_EVALUATION_SHADER]);
}
if (shaderObjects.find(GL_COMPUTE_SHADER) != shaderObjects.end())
{
glDetachShader(shaderProgram, shaderObjects[GL_COMPUTE_SHADER]);
glDeleteShader(shaderObjects[GL_COMPUTE_SHADER]);
}
glDeleteProgram(shaderProgram);
}
bool ShaderProgram::reLink()
{
GLint linked;
glLinkProgram(shaderProgram);
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &linked);
if (!linked)
{
GLint logLength, charsWritten;
glGetProgramiv(this->shaderProgram, GL_INFO_LOG_LENGTH, &logLength);
auto log = new char[logLength];
glGetProgramInfoLog(this->shaderProgram, logLength, &charsWritten, log);
std::cerr << "Shader program link error: " << std::endl << log << std::endl;
delete[] log;
shaderProgram = 0;
return false;
}
return true;
}
GLuint ShaderProgram::LoadShaderObject(GLenum type, const std::string &filename)
{
std::ifstream fs(filename);
if (!fs.is_open())
{
std::cerr << "ERROR: Could not read shader from " << filename << std::endl;
return 0;
}
std::string shaderText((std::istreambuf_iterator<char>(fs)), std::istreambuf_iterator<char>());
GLuint newShaderObject = glCreateShader(type);
const char *shaderSrc = shaderText.c_str();
glShaderSource(newShaderObject, 1, &shaderSrc, nullptr);
glCompileShader(newShaderObject);
GLint compileStatus;
glGetShaderiv(newShaderObject, GL_COMPILE_STATUS, &compileStatus);
if (compileStatus != GL_TRUE)
{
GLchar infoLog[512];
glGetShaderInfoLog(newShaderObject, 512, nullptr, infoLog);
std::cerr << "Shader compilation failed : " << std::endl << infoLog << std::endl;
return 0;
}
return newShaderObject;
}
void ShaderProgram::StartUseShader() const
{
glUseProgram(shaderProgram);
}
void ShaderProgram::StopUseShader() const
{
glUseProgram(0);
}
void ShaderProgram::SetUniform(const std::string &location, int value) const
{
GLint uniformLocation = glGetUniformLocation(shaderProgram, location.c_str());
if (uniformLocation == -1)
{
std::cerr << "Uniform " << location << " not found" << std::endl;
return;
}
glUniform1i(uniformLocation, value);
}
void ShaderProgram::SetUniform(const std::string &location, unsigned int value) const
{
GLint uniformLocation = glGetUniformLocation(shaderProgram, location.c_str());
if (uniformLocation == -1)
{
std::cerr << "Uniform " << location << " not found" << std::endl;
return;
}
glUniform1ui(uniformLocation, value);
}
void ShaderProgram::SetUniform(const std::string &location, float value) const
{
GLint uniformLocation = glGetUniformLocation(shaderProgram, location.c_str());
if (uniformLocation == -1)
{
std::cerr << "Uniform " << location << " not found" << std::endl;
return;
}
glUniform1f(uniformLocation, value);
}
void ShaderProgram::SetUniform(const std::string &location, LiteMath::float4x4 a_mat) const
{
GLint uniformLocation = glGetUniformLocation(shaderProgram, location.c_str());
if (uniformLocation == -1)
{
std::cerr << "Uniform " << location << " not found" << std::endl;
return;
}
glUniformMatrix4fv(uniformLocation, 1, true, a_mat.L());
}
void ShaderProgram::SetUniform(const std::string &location, double value) const
{
GLint uniformLocation = glGetUniformLocation(shaderProgram, location.c_str());
if (uniformLocation == -1)
{
std::cerr << "Uniform " << location << " not found" << std::endl;
return;
}
glUniform1d(uniformLocation, value);
}