-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shader.h
170 lines (142 loc) · 6.2 KB
/
Shader.h
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
#ifndef SLIMEMOLDSIMULATION_SHADER_H
#define SLIMEMOLDSIMULATION_SHADER_H
#include <glm/glm.hpp>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
class Shader {
const std::string enumValues[3] = {"VERTEX", "FRAGMENT", "PROGRAM"};
enum ShaderErrorType {
VERTEX, FRAGMENT, PROGRAM
};
public:
unsigned int ID;
Shader(const char *vertexPath, const char *fragmentPath) {
std::string vertexShader;
std::string fragmentShader;
std::ifstream vertexShaderFile;
std::ifstream fragmentShaderFile;
vertexShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
fragmentShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try {
vertexShaderFile.open(vertexPath);
fragmentShaderFile.open(fragmentPath);
std::stringstream vertexStream, fragmentStream;
vertexStream << vertexShaderFile.rdbuf();
fragmentStream << fragmentShaderFile.rdbuf();
vertexShaderFile.close();
fragmentShaderFile.close();
vertexShader = vertexStream.str();
fragmentShader = fragmentStream.str();
} catch (std::ifstream::failure &e) {
std::cout << "ERROR: Could not read shader file" << std::endl;
}
const char *vShader = vertexShader.c_str();
const char *fShader = fragmentShader.c_str();
uint32_t vertex, fragment;
// Vertex Shader
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShader, NULL);
glCompileShader(vertex);
checkCompileErrors(vertex, ShaderErrorType::VERTEX);
// Fragment Shader
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShader, NULL);
glCompileShader(fragment);
checkCompileErrors(fragment, ShaderErrorType::FRAGMENT);
// Shader Program
ID = glCreateProgram();
glAttachShader(ID, vertex);
glAttachShader(ID, fragment);
glLinkProgram(ID);
checkCompileErrors(ID, ShaderErrorType::PROGRAM);
glDeleteShader(vertex);
glDeleteShader(fragment);
}
void use() const {
glUseProgram(ID);
}
// utility uniform functions
// ------------------------------------------------------------------------
[[maybe_unused]]
void setBool(const std::string &name, bool value) const {
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int) value);
}
// ------------------------------------------------------------------------
[[maybe_unused]]
void setInt(const std::string &name, int value) const {
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
}
// ------------------------------------------------------------------------
[[maybe_unused]]
void setFloat(const std::string &name, float value) const {
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
}
// ------------------------------------------------------------------------
[[maybe_unused]]
void setVec2(const std::string &name, const glm::vec2 &value) const {
glUniform2fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
}
[[maybe_unused]]
void setVec2(const std::string &name, float x, float y) const {
glUniform2f(glGetUniformLocation(ID, name.c_str()), x, y);
}
// ------------------------------------------------------------------------
[[maybe_unused]]
void setVec3(const std::string &name, const glm::vec3 &value) const {
glUniform3fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
}
[[maybe_unused]]
void setVec3(const std::string &name, float x, float y, float z) const {
glUniform3f(glGetUniformLocation(ID, name.c_str()), x, y, z);
}
// ------------------------------------------------------------------------
[[maybe_unused]]
void setVec4(const std::string &name, const glm::vec4 &value) const {
glUniform4fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
}
[[maybe_unused]]
void setVec4(const std::string &name, float x, float y, float z, float w) const {
glUniform4f(glGetUniformLocation(ID, name.c_str()), x, y, z, w);
}
// ------------------------------------------------------------------------
[[maybe_unused]]
void setMat2(const std::string &name, const glm::mat2 &mat) const {
glUniformMatrix2fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
}
// ------------------------------------------------------------------------
[[maybe_unused]]
void setMat3(const std::string &name, const glm::mat3 &mat) const {
glUniformMatrix3fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
}
// ------------------------------------------------------------------------
[[maybe_unused]]
void setMat4(const std::string &name, const glm::mat4 &mat) const {
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
}
private:
// utility function for checking shader compilation/linking errors.
// ------------------------------------------------------------------------
void checkCompileErrors(GLuint shader, ShaderErrorType type) {
GLint success;
unsigned int logSize = 1024;
GLchar infoLog[logSize];
if (type != ShaderErrorType::PROGRAM) {
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << enumValues[type] << "\n"
<< infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
}
} else {
glGetProgramiv(shader, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shader, 1024, NULL, infoLog);
std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << enumValues[type] << "\n"
<< infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
}
}
}
};
#endif