-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shader.cpp
61 lines (51 loc) · 1.61 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
#include "stdafx.h"
#include "Shader.h"
Shader::Shader(const std::string& name) {
std::ifstream vFile;
std::ifstream fFile;
vFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
fFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
vFile.open("assets/shaders/" + name + ".vert");
fFile.open("assets/shaders/" + name + ".frag");
std::stringstream vStream, fStream;
vStream << vFile.rdbuf();
fStream << fFile.rdbuf();
vFile.close();
fFile.close();
std::string vCode = vStream.str();
std::string fCode = fStream.str();
const char* vCodeC = vCode.c_str();
const char* fCodeC = fCode.c_str();
int success;
char infoLog[512];
GLuint vID = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vID, 1, &vCodeC, nullptr);
glCompileShader(vID);
glGetShaderiv(vID, GL_COMPILE_STATUS, &success);
if(!success) {
glGetShaderInfoLog(vID, 512, nullptr, infoLog);
SPDLOG_ERROR("Vertex Shader {} failed to compile: {}", name, infoLog);
}
GLuint fID = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fID, 1, &fCodeC, nullptr);
glCompileShader(fID);
glGetShaderiv(fID, GL_COMPILE_STATUS, &success);
if(!success) {
glGetShaderInfoLog(fID, 512, nullptr, infoLog);
SPDLOG_ERROR("Fragment Shader {} failed to compile: {}", name, infoLog);
}
ID = glCreateProgram();
glAttachShader(ID, vID);
glAttachShader(ID, fID);
glLinkProgram(ID);
glGetProgramiv(ID, GL_LINK_STATUS, &success);
if(!success) {
glGetShaderInfoLog(ID, 512, nullptr, infoLog);
SPDLOG_ERROR("Shader {} failed to link: {}", name, infoLog);
}
glDeleteShader(vID);
glDeleteShader(fID);
}
Shader::~Shader() {
glDeleteProgram(ID);
}