-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShader.cpp
More file actions
78 lines (64 loc) · 1.87 KB
/
Shader.cpp
File metadata and controls
78 lines (64 loc) · 1.87 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
#include "Shader.h"
#ifdef GL_API_GLAD_OPENGL_3
Shader::Shader(ShaderData shaders[], unsigned int numberOfShaders) {
ID = glCreateProgram();
for (unsigned int i = 0; i < numberOfShaders; i++) {
GLuint shader = glCreateShader(shaders[i].type);
glShaderSource(shader, 1, &shaders[i].shader, NULL);
glCompileShader(shader);
CompileErrors(shader, shaders[i].name);
glAttachShader(ID, shader);
glDeleteShader(shader);
}
glLinkProgram(ID);
LinkErrors();
Activate();
}
Shader::~Shader() {
Delete();
}
void Shader::Activate() {
Activate(ID);
}
void Shader::Activate(GLuint ID) {
glUseProgram(ID);
}
void Shader::Delete() {
Delete(ID);
}
void Shader::Delete(GLuint ID) {
glDeleteProgram(ID);
}
const char* Shader::GetShaderData(const char* filename) {
std::ifstream in(filename, std::ios::binary);
if (!in)
std::cout << "ERROR: There was an error getting the files contents of " << filename;
std::string contents;
in.seekg(0, std::ios::end);
contents.resize(in.tellg());
in.seekg(0, std::ios::beg);
in.read(&contents[0], contents.size());
const GLchar* buffer = new char[contents.length() + 1];
memcpy((void*)buffer, contents.c_str(), contents.length() + 1);
return buffer;
}
void Shader::CompileErrors(unsigned int shader, const char* type) {
GLint hasCompiled;
GLchar infoLog[logSize];
glGetShaderiv(shader, GL_COMPILE_STATUS, &hasCompiled);
if (hasCompiled == GL_TRUE) return;
glGetShaderInfoLog(shader, logSize, NULL, infoLog);
std::cout << "SHADER_COMPILATION_ERROR for:" << type << "\n" << infoLog << '\n';
}
void Shader::LinkErrors() {
LinkErrors(ID);
}
void Shader::LinkErrors(GLuint ID) {
GLint hasCompiled;
GLchar infoLog[logSize];
glGetProgramiv(ID, GL_LINK_STATUS, &hasCompiled);
if (hasCompiled == GL_TRUE) return;
glGetProgramInfoLog(ID, logSize, NULL, infoLog);
std::cout << "SHADER_LINKING_ERROR for: PROGRAM" << "\n" << infoLog << '\n';
}
#endif