Brenta Engine 1.1
Loading...
Searching...
No Matches
shader.hpp
1/*
2 * MIT License
3 *
4 * Copyright (c) 2024 Giovanni Santini
5
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 */
26
27#pragma once
28
29#include "engine_logger.hpp"
30
31#include <algorithm>
32#include <fstream>
33#include <glad/glad.h> /* OpenGL driver */
34#include <glm/glm.hpp>
35#include <glm/gtc/matrix_transform.hpp>
36#include <glm/gtc/type_ptr.hpp>
37#include <iostream>
38#include <sstream>
39#include <string>
40#include <unordered_map>
41#include <vector>
42
43namespace brenta
44{
45
46namespace types
47{
48
49typedef std::string shader_name_t;
50
51} // namespace types
52
65class shader
66{
67 public:
75 static std::unordered_map<types::shader_name_t, unsigned int> shaders;
76
92 template <typename... Args>
93 static void create(std::string shader_name, GLenum type, std::string path,
94 Args... args)
95 {
96 std::vector<unsigned int> compiled_shaders = {};
97 compile_shaders(compiled_shaders, type, path, args...);
98
99 /* shader Program */
100 unsigned int ID = glCreateProgram();
101 std::for_each(compiled_shaders.begin(), compiled_shaders.end(),
102 [&ID](auto shader) { glAttachShader(ID, shader); });
103
104 glLinkProgram(ID);
105 shader::check_compile_errors(ID, "PROGRAM");
106
107 shader::shaders.insert({shader_name, ID});
108 std::for_each(compiled_shaders.begin(), compiled_shaders.end(),
109 [](auto shader) { glDeleteShader(shader); });
110 }
111
127 template <typename... Args>
128 static void create(const GLchar **feedback_varyings, int num_varyings,
129 std::string shader_name, GLenum type, std::string path,
130 Args... args)
131 {
132 std::vector<unsigned int> compiled_shaders = {};
133 compile_shaders(compiled_shaders, type, path, args...);
134
135 /* shader Program */
136 unsigned int ID = glCreateProgram();
137 std::for_each(compiled_shaders.begin(), compiled_shaders.end(),
138 [&ID](auto shader) { glAttachShader(ID, shader); });
139
140 if (feedback_varyings != nullptr)
141 {
142 glTransformFeedbackVaryings(ID, num_varyings, feedback_varyings,
143 GL_INTERLEAVED_ATTRIBS);
144 }
145
146 glLinkProgram(ID);
147 shader::check_compile_errors(ID, "PROGRAM");
148
149 shader::shaders.insert({shader_name, ID});
150 std::for_each(compiled_shaders.begin(), compiled_shaders.end(),
151 [](auto shader) { glDeleteShader(shader); });
152 }
153
154 static void compile_shaders(std::vector<unsigned int> &compiled)
155 {
156 return;
157 }
158
159 template <typename... Args>
160 static void compile_shaders(std::vector<unsigned int> &compiled,
161 GLenum type, std::string path, Args... args)
162 {
163 std::string code;
164 std::ifstream file;
165 file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
166
167 try
168 {
169 file.open(path);
170 std::stringstream stream;
171 stream << file.rdbuf();
172 file.close();
173 code = stream.str();
174 }
175 catch (std::ifstream::failure &e)
176 {
177 ERROR("Error reading shader file: {}", path);
178 return;
179 }
180
181 const char *shader_code = code.c_str();
182 unsigned int shader = glCreateShader(type);
183 glShaderSource(shader, 1, &shader_code, NULL);
184 glCompileShader(shader);
185 shader::check_compile_errors(shader, "SHADER");
186
187 compiled.push_back(shader);
188 compile_shaders(compiled, args...);
189 }
190
197 static unsigned int get_id(types::shader_name_t shader_name);
198
206 static void use(types::shader_name_t shader_name);
207
208 /* Utility uniform functions */
209
217 static void set_bool(types::shader_name_t shader_name,
218 const std::string &name, bool value);
226 static void set_int(types::shader_name_t shader_name,
227 const std::string &name, int value);
235 static void set_float(types::shader_name_t shader_name,
236 const std::string &name, float value);
244 static void set_mat4(types::shader_name_t shader_name, const GLchar *name,
245 glm::mat4 value);
255 static void set_vec3(types::shader_name_t shader_name, const GLchar *name,
256 float x, float y, float z);
264 static void set_vec3(types::shader_name_t shader_name, const GLchar *name,
265 glm::vec3 value);
266
267 private:
268 static void check_compile_errors(unsigned int shader, std::string type);
269};
270
271} // namespace brenta
Shader class.
Definition shader.hpp:66
static void create(std::string shader_name, GLenum type, std::string path, Args... args)
Create a new shader.
Definition shader.hpp:93
static void set_bool(types::shader_name_t shader_name, const std::string &name, bool value)
Set a boolean in the shader.
Definition shader.cpp:55
static std::unordered_map< types::shader_name_t, unsigned int > shaders
Map of shaders.
Definition shader.hpp:75
static void use(types::shader_name_t shader_name)
Use the shader.
Definition shader.cpp:43
static void 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.
Definition shader.hpp:128
static void set_int(types::shader_name_t shader_name, const std::string &name, int value)
Set an integer in the shader.
Definition shader.cpp:66
static void set_vec3(types::shader_name_t shader_name, const GLchar *name, float x, float y, float z)
Set a 3D vector in the shader.
Definition shader.cpp:100
static unsigned int get_id(types::shader_name_t shader_name)
Get the ID of a shader.
Definition shader.cpp:33
static void set_mat4(types::shader_name_t shader_name, const GLchar *name, glm::mat4 value)
Set a 4x4 matrix in the shader.
Definition shader.cpp:88
static void set_float(types::shader_name_t shader_name, const std::string &name, float value)
Set a float in the shader.
Definition shader.cpp:77