Brenta Engine 1.0
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 ShaderName;
50
51} // namespace Types
52
65class Shader
66{
67 public:
75 static std::unordered_map<Types::ShaderName, unsigned int> shaders;
76
92 template <typename... Args>
93 static void New(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::CheckCompileErrors(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 New(const GLchar **feedbackVaryings, int numVaryings,
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 (feedbackVaryings != nullptr)
141 {
142 glTransformFeedbackVaryings(ID, numVaryings, feedbackVaryings,
143 GL_INTERLEAVED_ATTRIBS);
144 }
145
146 glLinkProgram(ID);
147 Shader::CheckCompileErrors(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::CheckCompileErrors(shader, "SHADER");
186
187 compiled.push_back(shader);
188 compile_shaders(compiled, args...);
189 }
190
197 static unsigned int GetId(Types::ShaderName shader_name);
198
206 static void Use(Types::ShaderName shader_name);
207
208 /* Utility uniform functions */
209
217 static void SetBool(Types::ShaderName shader_name, const std::string &name,
218 bool value);
226 static void SetInt(Types::ShaderName shader_name, const std::string &name,
227 int value);
235 static void SetFloat(Types::ShaderName shader_name, const std::string &name,
236 float value);
244 static void SetMat4(Types::ShaderName shader_name, const GLchar *name,
245 glm::mat4 value);
255 static void SetVec3(Types::ShaderName shader_name, const GLchar *name,
256 float x, float y, float z);
264 static void SetVec3(Types::ShaderName shader_name, const GLchar *name,
265 glm::vec3 value);
266
267 private:
268 static void CheckCompileErrors(unsigned int shader, std::string type);
269};
270
271} // namespace Brenta
Shader class.
Definition shader.hpp:66
static void SetMat4(Types::ShaderName shader_name, const GLchar *name, glm::mat4 value)
Set a 4x4 matrix in the shader.
Definition shader.cpp:90
static void SetVec3(Types::ShaderName shader_name, const GLchar *name, float x, float y, float z)
Set a 3D vector in the shader.
Definition shader.cpp:103
static void SetBool(Types::ShaderName shader_name, const std::string &name, bool value)
Set a boolean in the shader.
Definition shader.cpp:55
static unsigned int GetId(Types::ShaderName shader_name)
Get the ID of a shader.
Definition shader.cpp:33
static void SetInt(Types::ShaderName shader_name, const std::string &name, int value)
Set an integer in the shader.
Definition shader.cpp:67
static void New(const GLchar **feedbackVaryings, int numVaryings, std::string shader_name, GLenum type, std::string path, Args... args)
Create a new shader with feedback varyings.
Definition shader.hpp:128
static std::unordered_map< Types::ShaderName, unsigned int > shaders
Map of shaders.
Definition shader.hpp:75
static void New(std::string shader_name, GLenum type, std::string path, Args... args)
Create a new shader.
Definition shader.hpp:93
static void SetFloat(Types::ShaderName shader_name, const std::string &name, float value)
Set a float in the shader.
Definition shader.cpp:79
static void Use(Types::ShaderName shader_name)
Use the shader.
Definition shader.cpp:43