-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShaderLoader.cpp
More file actions
139 lines (120 loc) · 4.4 KB
/
ShaderLoader.cpp
File metadata and controls
139 lines (120 loc) · 4.4 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
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
#include "ShaderLoader.h"
#include <iostream>
#include <sstream>
#include <fstream>
static char infoLog[512];
GLuint compileVertexShader(const std::string& shaderCode) {
GLuint id = glCreateShader(GL_VERTEX_SHADER);
const char* code = shaderCode.c_str();
glShaderSource(id, 1, &code, nullptr);
glCompileShader(id);
GLint success;
glGetShaderiv(id, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(id, 512, nullptr, infoLog);
std::cerr << infoLog << std::endl;
throw std::exception("Failed to compile vertex shader");
}
return id;
}
GLuint compileGeometryShader(const std::string& shaderCode) {
GLuint id = glCreateShader(GL_GEOMETRY_SHADER);
const char* code = shaderCode.c_str();
glShaderSource(id, 1, &code, nullptr);
glCompileShader(id);
GLint success;
glGetShaderiv(id, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(id, 512, nullptr, infoLog);
std::cerr << infoLog << std::endl;
throw std::exception("Failed to compile vertex shader");
}
return id;
}
GLuint compileFragmentShader(const std::string& shaderCode) {
GLuint id = glCreateShader(GL_FRAGMENT_SHADER);
const char* code = shaderCode.c_str();
glShaderSource(id, 1, &code, nullptr);
glCompileShader(id);
GLint success;
glGetShaderiv(id, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(id, 512, nullptr, infoLog);
std::cerr << infoLog << std::endl;
throw std::exception("Failed to compile fragment shader");
}
return id;
}
GLuint loadVertexFragmantShader(const std::string& vertexPath, const std::string& fragmentPath) {
std::ifstream vertexFile, fragmentFile;
vertexFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
fragmentFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
std::string vertexCode, fragmentCode;
try {
vertexFile.open(vertexPath);
fragmentFile.open(fragmentPath);
std::stringstream vSS, fSS;
vSS << vertexFile.rdbuf();
fSS << fragmentFile.rdbuf();
vertexFile.close();
fragmentFile.close();
vertexCode = vSS.str(), fragmentCode = fSS.str();
}
catch (std::ifstream::failure e) {
std::cerr << "Failed to load shaders" << std::endl;
throw std::exception(e.what());
}
int vertex = compileVertexShader(vertexCode);
int fragment = compileFragmentShader(fragmentCode);
GLuint id = glCreateProgram();
glAttachShader(id, vertex);
glAttachShader(id, fragment);
glLinkProgram(id);
int success;
glGetProgramiv(id, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(id, 512, nullptr, infoLog);
std::cerr << infoLog << std::endl;
throw std::exception("Failed to link the shader program");
}
glDeleteShader(vertex);
glDeleteShader(fragment);
return id;
}
GLuint loadVertexGeometryShader(const std::string& vertexPath, const std::string& geometryPath, const char* const* feedBackValues, int numValues) {
std::ifstream vertexFile, geometryFile;
vertexFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
geometryFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
std::string vertexCode, geometryCode;
try {
vertexFile.open(vertexPath);
geometryFile.open(geometryPath);
std::stringstream vSS, fSS, gSS;
vSS << vertexFile.rdbuf();
gSS << geometryFile.rdbuf();
vertexFile.close();
geometryFile.close();
vertexCode = vSS.str(), geometryCode = gSS.str();
}
catch (std::ifstream::failure e) {
std::cerr << "Failed to load shaders" << std::endl;
throw std::exception(e.what());
}
int vertex = compileVertexShader(vertexCode);
int geometry = compileGeometryShader(geometryCode);
GLuint id = glCreateProgram();
glTransformFeedbackVaryings(id, numValues, feedBackValues, GL_INTERLEAVED_ATTRIBS);
glAttachShader(id, vertex);
glAttachShader(id, geometry);
glLinkProgram(id);
int success;
glGetProgramiv(id, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(id, 512, nullptr, infoLog);
std::cerr << infoLog << std::endl;
throw std::exception("Failed to link the shader program");
}
glDeleteShader(vertex);
glDeleteShader(geometry);
return id;
}