-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshader.cpp
More file actions
214 lines (178 loc) · 5.6 KB
/
Copy pathshader.cpp
File metadata and controls
214 lines (178 loc) · 5.6 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "shader.h"
#include "stringhelper.h"
#include <iostream>
#include <fstream>
#include <sstream>
Shader* Shader::currShader = nullptr;
Shader::Shader() {}
void Shader::createAndCompileShaderProgram(string vertFilePath, string fragFilePath) {
// Read from file
string vertSource, fragSource;
ifstream vertShaderFile, fragShaderFile;
try {
stringstream vStream, fStream;
vertShaderFile.open(vertFilePath);
vStream << vertShaderFile.rdbuf();
vertSource = vStream.str();
vertShaderFile.close();
fragShaderFile.open(fragFilePath);
fStream << fragShaderFile.rdbuf();
fragSource = fStream.str();
fragShaderFile.close();
}
catch (std::ifstream::failure err) {
std::cout << "FAILED TO READ SHADER SOURCE >:(" << std::endl;
}
// Include files
pasteInShaderIncludes(&vertSource);
pasteInShaderIncludes(&fragSource);
// Compile
char infoLog[512];
int success;
GLuint vertHandle, fragHandle;
vertHandle = glCreateShader(GL_VERTEX_SHADER);
fragHandle = glCreateShader(GL_FRAGMENT_SHADER);
const char *vertSourceC = vertSource.c_str();
const char *fragSourceC = fragSource.c_str();
glShaderSource(vertHandle, 1, &vertSourceC, NULL);
glCompileShader(vertHandle);
glGetShaderiv(vertHandle, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(vertHandle, 512, NULL, infoLog);
cout << "VERT COMPILE FAIL:\n" << infoLog << endl;
}
glShaderSource(fragHandle, 1, &fragSourceC, NULL);
glCompileShader(fragHandle);
glGetShaderiv(fragHandle, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(fragHandle, 512, NULL, infoLog);
cout << "FRAG COMPILE FAIL:\n" << infoLog << endl;
}
// Link
programHandle = glCreateProgram();
glAttachShader(programHandle, vertHandle);
glAttachShader(programHandle, fragHandle);
glLinkProgram(programHandle);
glGetProgramiv(programHandle, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(programHandle, 512, NULL, infoLog);
cout << "FAIL TO LINK SHADER PROGRAM:\n" << infoLog << std::endl;
}
// Delete temp objects
glDeleteShader(vertHandle);
glDeleteShader(fragHandle);
//
parseShaderForVariables(vertSource, fragSource);
}
void Shader::pasteInShaderIncludes(string* source) {
vector<string> lines;
split(*source, "\n", &lines);
*source = "";
for (const string& line : lines) {
vector<string> sub;
split(line, " ", &sub);
if (sub.size() > 0 && sub[0] == "#include") {
string filePath = sub[1].substr(1, sub[1].length() - 2);
ifstream file;
file.open(filePath);
stringstream sstream;
sstream << file.rdbuf();
string includeSource = sstream.str();
pasteInShaderIncludes(&includeSource);
source->append(includeSource);
file.close();
}
else {
source->append(line);
}
source->append("\n");
}
cout << *source << endl;
}
void Shader::parseShaderForVariables(const string &vertSource, const string &fragSource) {
use();
vector<string> vertLines;
split(vertSource, "\n", &vertLines);
for (const string& line : vertLines) {
vector<string> sub;
split(line, " ", &sub);
if (sub.size() == 3 && sub[0] == "uniform") {
string name = sub[2].substr(0, sub[2].length() - 1);
cout << "Found uniform: " << name << endl;
uniforms[name] = glGetUniformLocation(programHandle, name.c_str());
}
if (sub.size() == 3 && sub[0] == "in") {
string name = sub[2].substr(0, sub[2].length() - 1);
cout << "Found in variable: " << name << endl;
attributes[name] = glGetAttribLocation(programHandle, name.c_str());
}
}
vector<string> fragLines;
split(fragSource, "\n", &fragLines);
for (const string& line : fragLines) {
vector<string> sub;
split(line, " ", &sub);
if (sub.size() == 3 && sub[0] == "uniform") {
string name = sub[2].substr(0, sub[2].length() - 1);
cout << "Found uniform: " << name << endl;
uniforms[name] = glGetUniformLocation(programHandle, name.c_str());
}
}
}
void Shader::use() {
if (currShader == this) return;
currShader = this;
glUseProgram(programHandle);
}
void Shader::draw(Drawable &d) {
if (d.indexCount <= 0) {
throw std::invalid_argument("Attempting to draw Drawable with indexCount <= 0");
}
if (attributes.count("vs_Pos") != 0) {
int attribHandle = attributes["vs_Pos"];
assert(d.hasBuffer(POSITION));
d.bindBuffer(POSITION);
glEnableVertexAttribArray(attribHandle);
glVertexAttribPointer(attribHandle, 4, GL_FLOAT, false, 0, nullptr);
}
d.bindBuffer(INDEX);
glDrawElements(d.drawMode(), d.indexCount, GL_UNSIGNED_INT, 0);
printGLErrorLog();
if (attributes.count("vs_Pos") != 0) glDisableVertexAttribArray(attributes["vs_Pos"]);
}
void Shader::printGLErrorLog() {
GLenum error = glGetError();
if (error != GL_NO_ERROR) {
cout << "OpenGL error " << error << endl;
}
}
void Shader::uniformFloat(const string& name, float f) {
assert(currShader == this);
assert(uniforms.count(name) != 0);
glUniform1f(uniforms[name], f);
}
void Shader::uniformVec2(const string& name, vec2 v) {
assert(currShader == this);
assert(uniforms.count(name) != 0);
glUniform2f(uniforms[name], v.x, v.y);
}
void Shader::uniformVec3(const string& name, vec3 v) {
assert(currShader == this);
assert(uniforms.count(name) != 0);
glUniform3f(uniforms[name], v.x, v.y, v.z);
}
void Shader::uniformVec4(const string& name, vec4 v) {
assert(currShader == this);
assert(uniforms.count(name) != 0);
glUniform4f(uniforms[name], v.x, v.y, v.z, v.w);
}
void Shader::uniformInt(const string& name, int i) {
assert(currShader == this);
assert(uniforms.count(name) != 0);
glUniform1i(uniforms[name], i);
}
void Shader::uniformVec2i(const string& name, ivec2 v) {
assert(currShader == this);
assert(uniforms.count(name) != 0);
glUniform2i(uniforms[name], v.x, v.y);
}