-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader.cpp
306 lines (258 loc) · 7.71 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <shader.hpp>
#include <mesh/object.hpp>
#include <map>
#include <string>
#include <iostream>
#include <fstream>
#include <cerrno>
#include <stdexcept>
#include <GL/glew.h>
#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
static std::string textFileRead(std::string filename);
static void printLinkInfoLog(int prog);
static void printShaderInfoLog(int shader);
Shader* Shader::inUse = NULL;
Shader::Shader() : hasVert(false), hasFrag(false), hasProgram(false) {}
Shader::~Shader() {
if(hasVert)
glDeleteShader(shadVert);
if(hasFrag)
glDeleteShader(shadFrag);
if(hasProgram)
glDeleteProgram(program);
}
Shader::Shader(const ShaderConf& conf) : hasVert(false), hasFrag(false), hasProgram(false) {
setVertShader(conf.vertFile);
setFragShader(conf.fragFile);
recompile();
}
Shader::Shader(std::string vSrc, std::string fSrc) : hasVert(false), hasFrag(false), hasProgram(false) {
setVertShader(vSrc + ".vert.glsl");
setFragShader(fSrc + ".frag.glsl");
recompile();
}
Shader::Shader(std::string src) : hasVert(false), hasFrag(false), hasProgram(false) {
setVertShader(src + ".vert.glsl");
setFragShader(src + ".frag.glsl");
recompile();
}
void Shader::setVertShader(std::string src) {
std::string vertSourceS = textFileRead(src);
const char *vertSource = vertSourceS.c_str();
if(hasVert)
glDeleteShader(shadVert);
shadVert = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(shadVert, 1, &vertSource, NULL);
glCompileShader(shadVert);
printShaderInfoLog(shadVert);
hasVert = true;
}
void Shader::setFragShader(std::string src) {
std::string fragSourceS = textFileRead(src);
const char *fragSource = fragSourceS.c_str();
if(hasFrag)
glDeleteShader(shadFrag);
shadFrag = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(shadFrag, 1, &fragSource, NULL);
glCompileShader(shadFrag);
printShaderInfoLog(shadFrag);
hasFrag = true;
}
void Shader::setUniform(std::string name, const glm::vec3& v) {
int loc = resolveUniform(name);
if(loc < 0)
return;
glUniform3fv(loc, 1, glm::value_ptr(v));
}
void Shader::setUniform(std::string name, float x, float y, float z) {
int loc = resolveUniform(name);
if(loc < 0)
return;
glUniform3f(loc, x, y, z);
}
void Shader::setUniform(std::string name, bool v) {
int loc = resolveUniform(name);
if(loc < 0)
return;
glUniform1i(loc, v);
}
void Shader::setUniform(std::string name, int v) {
int loc = resolveUniform(name);
if(loc < 0)
return;
glUniform1i(loc, v);
}
void Shader::setUniform(std::string name, const glm::mat4& m) {
int loc = resolveUniform(name);
if(loc < 0)
return;
glUniformMatrix4fv(loc, 1, GL_FALSE, &m[0][0]);
}
void Shader::bindVertexData(std::string name, unsigned int vbo) {
int loc = resolveAttribute(name);
if(loc < 0)
return;
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(loc, 3, GL_FLOAT, false, 0, NULL);
glEnableVertexAttribArray(loc);
}
void Shader::bindVertexData(std::string name, unsigned int vbo, int sso) {
int loc = resolveAttribute(name);
if(loc < 0)
return;
glBindBuffer(GL_ARRAY_BUFFER, vbo);
int size = SHADER_SSO_SIZE(sso);
int stride = SHADER_SSO_STRIDE(sso) * sizeof(float);
void *offset = (void*)(SHADER_SSO_OFFSET(sso) * sizeof(float));
glVertexAttribPointer(loc, size, GL_FLOAT, false, stride, offset);
glEnableVertexAttribArray(loc);
}
void Shader::bindIndexData(unsigned int vbo) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo);
}
void Shader::unbindVertexData(std::string name) {
int loc = resolveAttribute(name);
if(loc < 0)
return;
glDisableVertexAttribArray(loc);
}
void Shader::recompile() {
if(hasProgram)
glDeleteProgram(program);
program = glCreateProgram();
if(hasVert)
glAttachShader(program, shadVert);
if(hasFrag)
glAttachShader(program, shadFrag);
glLinkProgram(program);
printLinkInfoLog(program);
if(hasVert)
glDetachShader(program, shadVert);
if(hasFrag)
glDetachShader(program, shadFrag);
hasProgram = true;
}
int Shader::resolveUniform(std::string name) {
use();
std::map<std::string,unsigned int>::iterator itr = uMap.find(name);
int loc;
if(itr != uMap.end()) {
loc = itr->second;
} else {
loc = glGetUniformLocation(program, name.c_str());
if(loc < 0)
std::cerr << "warning: couldn't resolve uniform '" << name << "', not binding...\n";
uMap[name] = loc;
}
return loc;
}
int Shader::resolveAttribute(std::string name) {
use();
std::map<std::string,unsigned int>::iterator itr = aMap.find(name);
int loc;
if(itr != aMap.end()) {
loc = itr->second;
} else {
loc = glGetAttribLocation(program, name.c_str());
if(loc < 0)
std::cerr << "warning: couldn't resolve attribute '" << name << "', not binding...\n";
aMap[name] = loc;
}
return loc;
}
static std::string textFileRead(std::string filename)
{
// http://insanecoding.blogspot.com/2011/11/how-to-read-in-file-in-c.html
std::ifstream in(filename.c_str());
if (!in) {
std::cerr << "Error reading file " << filename << std::endl;
throw (errno);
}
return std::string(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>());
}
static void printLinkInfoLog(int prog)
{
GLint linked;
glGetProgramiv(prog, GL_LINK_STATUS, &linked);
if (linked == GL_TRUE) {
return;
}
std::cerr << "GLSL LINK ERROR" << std::endl;
int infoLogLen = 0;
int charsWritten = 0;
GLchar *infoLog;
glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &infoLogLen);
if (infoLogLen > 0) {
infoLog = new GLchar[infoLogLen];
// error check for fail to allocate memory omitted
glGetProgramInfoLog(prog, infoLogLen, &charsWritten, infoLog);
std::cerr << "InfoLog:" << std::endl << infoLog << std::endl;
delete[] infoLog;
}
// Throwing here allows us to use the debugger to track down the error.
throw;
}
static void printShaderInfoLog(int shader)
{
GLint compiled;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (compiled == GL_TRUE) {
return;
}
std::cerr << "GLSL COMPILE ERROR" << std::endl;
int infoLogLen = 0;
int charsWritten = 0;
GLchar *infoLog;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLen);
if (infoLogLen > 0) {
infoLog = new GLchar[infoLogLen];
// error check for fail to allocate memory omitted
glGetShaderInfoLog(shader, infoLogLen, &charsWritten, infoLog);
std::cerr << "InfoLog:" << std::endl << infoLog << std::endl;
delete[] infoLog;
}
// Throwing here allows us to use the debugger to track down the error.
throw;
}
void Shader::use() {
if(inUse != this && hasProgram) {
glUseProgram(program);
inUse = this;
}
}
void Shader::requireAttr(std::string name) {
int l = resolveAttribute(name);
if(l < 0) {
std::string w = "shader missing required attribute '" + name + "'";
throw std::runtime_error(w.c_str());
}
}
void Shader::requireAttr(std::string name, int loc) {
int l = resolveAttribute(name);
if(l < 0 || l != loc) {
std::string w = "shader missing required attribute '" + name + "'";
throw std::runtime_error(w.c_str());
}
}
void Shader::requireUniform(std::string name) {
int l = resolveUniform(name);
if(l < 0) {
std::string w = "shader missing required uniform '" + name + "'";
throw std::runtime_error(w.c_str());
}
}
void Shader::requireUniform(std::string name, int loc) {
int l = resolveUniform(name);
if(l < 0 || l != loc) {
std::string w = "shader missing required uniform '" + name + "'";
throw std::runtime_error(w.c_str());
}
}
void Shader::bind(GLEntity *entity) {
entity->bind(this);
}
void Shader::draw(Object *obj) {
obj->draw(this);
}