52 static std::unordered_map<types::shader_name_t, unsigned int>
shaders;
70 template <
typename... Args>
71 static bool create(std::string shader_name, GLenum type, std::string path,
74 std::vector<unsigned int> compiled_shaders = {};
75 if (!compile_shaders(compiled_shaders, type, path, args...))
77 ERROR(
"shader: error compiling shader {}", path);
82 unsigned int ID = glCreateProgram();
83 std::for_each(compiled_shaders.begin(), compiled_shaders.end(),
84 [&ID](
auto shader) { glAttachShader(ID, shader); });
87 if (!shader::check_compile_errors(ID,
"PROGRAM"))
93 std::for_each(compiled_shaders.begin(), compiled_shaders.end(),
94 [](
auto shader) { glDeleteShader(shader); });
114 template <
typename... Args>
115 static bool create(
const GLchar **feedback_varyings,
int num_varyings,
116 std::string shader_name, GLenum type, std::string path,
119 std::vector<unsigned int> compiled_shaders = {};
120 if (!compile_shaders(compiled_shaders, type, path, args...))
122 ERROR(
"shader: error compiling shader {}", path)
127 unsigned int ID = glCreateProgram();
128 std::for_each(compiled_shaders.begin(), compiled_shaders.end(),
129 [&ID](
auto shader) { glAttachShader(ID, shader); });
131 if (feedback_varyings !=
nullptr)
133 glTransformFeedbackVaryings(ID, num_varyings, feedback_varyings,
134 GL_INTERLEAVED_ATTRIBS);
138 if (!shader::check_compile_errors(ID,
"PROGRAM"))
144 std::for_each(compiled_shaders.begin(), compiled_shaders.end(),
145 [](
auto shader) { glDeleteShader(shader); });
151 compile_shaders([[maybe_unused]] std::vector<unsigned int> &compiled)
156 template <
typename... Args>
157 static bool compile_shaders(std::vector<unsigned int> &compiled, GLenum type,
158 std::string path, Args... args)
162 file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
167 if (!file.is_open())
throw "Cannot open file";
168 std::stringstream stream;
169 stream << file.rdbuf();
173 catch (std::ifstream::failure &e)
175 ERROR(
"shader: error reading shader file: {}", path);
181 ERROR(
"shader: file is empty: {}", path);
185 const char *shader_code = code.c_str();
186 unsigned int shader = glCreateShader(type);
187 glShaderSource(shader, 1, &shader_code, NULL);
188 glCompileShader(shader);
189 if (!shader::check_compile_errors(shader,
"SHADER"))
194 compiled.push_back(shader);
195 return compile_shaders(compiled, args...);
204 static unsigned int get_id(types::shader_name_t shader_name);
216 static bool use(types::shader_name_t shader_name);
228 static bool set_bool(types::shader_name_t shader_name,
229 const GLchar *name,
bool value);
238 static bool set_int(types::shader_name_t shader_name,
const GLchar *name,
248 static bool set_float(types::shader_name_t shader_name,
249 const GLchar *name,
float value);
258 static bool set_mat4(types::shader_name_t shader_name,
const GLchar *name,
270 static bool set_vec3(types::shader_name_t shader_name,
const GLchar *name,
271 float x,
float y,
float z);
280 static bool set_vec3(types::shader_name_t shader_name,
const GLchar *name,
285 static bool check_compile_errors(
unsigned int shader, std::string type);
static bool create(const GLchar **feedback_varyings, int num_varyings, std::string shader_name, GLenum type, std::string path, Args... args)
Create a new shader with feedback varyings.
static bool set_vec3(types::shader_name_t shader_name, const GLchar *name, float x, float y, float z)
Set a 3D vector in the shader.