-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathshader.cpp
More file actions
56 lines (47 loc) · 1.3 KB
/
shader.cpp
File metadata and controls
56 lines (47 loc) · 1.3 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
#include "shader.h"
using namespace std;
Shader::Shader(const string & _name, GLuint _type) {
id = glCreateShader (_type);
name = _name;
type = _type;
filename = "";
source = "";
}
void Shader::loadFromFile (const string & _filename) {
filename = _filename;
ifstream in (filename.c_str());
if (!in)
cout << "Error loading shader source file" << endl;
string source;
char c[2];
c[1]='\0';
while (in.get (c[0])) {
source.append (c);
}
in.close ();
setSource (source);
}
void Shader::compile () {
const GLchar * tmp = source.c_str();
glShaderSource (id, 1, &tmp, NULL);
glCompileShader (id);
GLint shaderCompiled;
glGetShaderiv (id, GL_COMPILE_STATUS, &shaderCompiled);
if (!shaderCompiled) {
GLint maxLength = 0;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &maxLength);
// The maxLength includes the NULL character
std::vector<GLchar> errorLog(maxLength);
glGetShaderInfoLog(id, maxLength, &maxLength, &errorLog[0]);
for (auto c : errorLog)
cout << c;
cout << endl;
cout << "Error: shader not compiled." << name << endl;
}
}
void Shader::reload () {
if (filename != "") {
loadFromFile (std::string (filename));
compile ();
}
}