Brenta Engine 1.0
Loading...
Searching...
No Matches
shader.cpp
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#include "shader.hpp"
28
29using namespace Brenta;
30
31std::unordered_map<Types::ShaderName, unsigned int> Shader::shaders;
32
33unsigned int Shader::GetId(Types::ShaderName shader_name)
34{
35 if (Shader::shaders.find(shader_name) == Shader::shaders.end())
36 {
37 return 0;
38 }
39 return Shader::shaders.at(shader_name);
40}
41
42/* Use/activate the shader */
43void Shader::Use(Types::ShaderName shader_name)
44{
45 glUseProgram(Shader::GetId(shader_name));
46 GLenum err;
47 if ((err = glGetError()) != GL_NO_ERROR)
48 {
49 ERROR("Error using shader: ", shader_name);
50 std::cout << err << std::endl;
51 }
52}
53
54/* Utility uniform functions */
55void Shader::SetBool(Types::ShaderName shader_name, const std::string &name,
56 bool value)
57{
58 glUniform1i(glGetUniformLocation(Shader::GetId(shader_name), name.c_str()),
59 (int) value);
60 if (glGetError() != GL_NO_ERROR)
61 {
62 ERROR("Error setting bool value for shader: ", shader_name,
63 " and name: ", name);
64 }
65}
66
67void Shader::SetInt(Types::ShaderName shader_name, const std::string &name,
68 int value)
69{
70 glUniform1i(glGetUniformLocation(Shader::GetId(shader_name), name.c_str()),
71 value);
72 if (glGetError() != GL_NO_ERROR)
73 {
74 ERROR("Error setting int value for shader: ", shader_name,
75 " and name: ", name);
76 }
77}
78
79void Shader::SetFloat(Types::ShaderName shader_name, const std::string &name,
80 float value)
81{
82 glUniform1f(glGetUniformLocation(Shader::GetId(shader_name), name.c_str()),
83 value);
84 if (glGetError() != GL_NO_ERROR)
85 {
86 ERROR("Error setting float value for shader: ", shader_name,
87 " and name: ", name);
88 }
89}
90void Shader::SetMat4(Types::ShaderName shader_name, const GLchar *name,
91 glm::mat4 value)
92{
93 unsigned int matLoc =
94 glGetUniformLocation(Shader::GetId(shader_name), name);
95 glUniformMatrix4fv(matLoc, 1, GL_FALSE, glm::value_ptr(value));
96 if (glGetError() != GL_NO_ERROR)
97 {
98 ERROR("Error setting mat4 value for shader: ", shader_name,
99 " and name: ", name);
100 }
101}
102
103void Shader::SetVec3(Types::ShaderName shader_name, const GLchar *name, float x,
104 float y, float z)
105{
106 unsigned int vecLoc =
107 glGetUniformLocation(Shader::GetId(shader_name), name);
108 glUniform3f(vecLoc, x, y, z);
109 if (glGetError() != GL_NO_ERROR)
110 {
111 ERROR("Error setting vec3 value for shader: ", shader_name,
112 " and name: ", name);
113 }
114}
115
116void Shader::SetVec3(Types::ShaderName shader_name, const GLchar *name,
117 glm::vec3 value)
118{
119 unsigned int vecLoc =
120 glGetUniformLocation(Shader::GetId(shader_name), name);
121 glUniform3f(vecLoc, value.x, value.y, value.z);
122 if (glGetError() != GL_NO_ERROR)
123 {
124 ERROR("Error setting vec3 value for shader: ", shader_name,
125 " and name: ", name);
126 }
127}
128
129/* utility function for checking shader
130 * compilation/linking errors. */
131void Shader::CheckCompileErrors(unsigned int shader, std::string type)
132{
133 int success;
134 char infoLog[1024];
135 if (type != "PROGRAM")
136 {
137 glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
138 if (!success)
139 {
140 glGetShaderInfoLog(shader, 1024, NULL, infoLog);
141 std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type
142 << "\n"
143 << infoLog << std::endl;
144 }
145 }
146 else
147 {
148 glGetProgramiv(shader, GL_LINK_STATUS, &success);
149 if (!success)
150 {
151 glGetProgramInfoLog(shader, 1024, NULL, infoLog);
152 std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type
153 << "\n"
154 << infoLog << std::endl;
155 }
156 }
157}
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 std::unordered_map< Types::ShaderName, unsigned int > shaders
Map of shaders.
Definition shader.hpp:75
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