forked from lostjared/acidcamGL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gl_shader.cpp
136 lines (117 loc) · 4.05 KB
/
gl_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
#include"gl_shader.hpp"
#include<sstream>
#include<fstream>
namespace acidcam {
extern void updateError();
ShaderProgram::ShaderProgram() : shader_id{0} {
}
ShaderProgram::ShaderProgram(GLuint id) : shader_id{id} {
}
ShaderProgram &ShaderProgram::operator=(const ShaderProgram &p) {
shader_id = p.shader_id;
name_ = p.name_;
return *this;
}
int ShaderProgram::printShaderLog(GLuint shader) {
int len = 0;
int ch = 0;
char *log;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
if(len > 0) {
log = new char [len+1];
glGetShaderInfoLog(shader, len, &ch, log);
std::cout << "acidcam: Shader: " << log << "\n";
delete [] log;
}
return 0;
}
void ShaderProgram::printProgramLog(int p) {
int len = 0;
int ch = 0;
char *log;
glGetShaderiv(p, GL_INFO_LOG_LENGTH, &len);
if(len > 0) {
log = new char [len+1];
glGetProgramInfoLog(p, len, &ch, log);
std::cout << "acidcam: Program: " << log << "\n";
delete [] log;
}
}
bool ShaderProgram::checkError() {
bool e = false;
int glErr = glGetError();
while(glErr != GL_NO_ERROR) {
std::cout << "acidcam: GL Error: " << glErr << "\n";
std::cout << "acidcam: Error String: " << glewGetErrorString(glErr) << "\n";
e = true;
glErr = glGetError();
}
return e;
}
GLuint ShaderProgram::createProgram(const char *vshaderSource, const char *fshaderSource) {
GLint vertCompiled;
GLint fragCompiled;
GLint linked;
GLuint vShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vShader, 1, &vshaderSource, NULL);
glShaderSource(fShader, 1, &fshaderSource, NULL);
glCompileShader(vShader);
checkError();
glGetShaderiv(vShader, GL_COMPILE_STATUS, &vertCompiled);
if(vertCompiled != 1) {
std::cout << "acidcam: Error on Vertex compile\n";
printShaderLog(vShader);
return 0;
}
glCompileShader(fShader);
checkError();
glGetShaderiv(fShader, GL_COMPILE_STATUS, &fragCompiled);
if(fragCompiled != 1) {
std::cout << "acidcam: Error on Fragment compile\n";
printShaderLog(vShader);
return 0;
}
GLuint vfProgram = glCreateProgram();
glAttachShader(vfProgram, vShader);
glAttachShader(vfProgram, fShader);
glLinkProgram(vfProgram);
checkError();
glGetProgramiv(vfProgram, GL_LINK_STATUS, &linked);
if(linked != 1) {
std::cout << "acidcam: Linking failed...\n";
printProgramLog(vfProgram);
return 0;
}
return vfProgram;
}
GLuint ShaderProgram::createProgramFromFile(const std::string &vert, const std::string &frag) {
std::fstream v,f;
v.open(vert, std::ios::in);
if(!v.is_open()) {
std::cout << "acidcam: Error could not open file: " << vert << "\n";
acidcam::updateError();
}
f.open(frag, std::ios::in);
if(!f.is_open()) {
std::cout << "acidcam: Eror could not open file: " << frag << "\n";
acidcam::updateError();
}
std::ostringstream stream1, stream2;
stream1 << v.rdbuf();
stream2 << f.rdbuf();
return createProgram(stream1.str().c_str(), stream2.str().c_str());
}
bool ShaderProgram::loadProgram(const std::string &v, const std::string &f) {
shader_id = createProgramFromFile(v,f);
if(shader_id)
return true;
return false;
}
void ShaderProgram::setName(const std::string &n) {
name_ = n;
}
void ShaderProgram::useProgram() {
glUseProgram(shader_id);
}
}