|
1 | 1 | #pragma once |
2 | 2 |
|
| 3 | +#include <functional> |
3 | 4 | #include <glad/glad.h> |
4 | 5 | #include <glm/glm.hpp> |
5 | 6 | #include <glm/gtc/type_ptr.hpp> |
@@ -46,6 +47,29 @@ struct Uniforms { |
46 | 47 | } light; |
47 | 48 | }; |
48 | 49 |
|
| 50 | +struct UniformLocations { |
| 51 | + // Vertex |
| 52 | + int model{-1}; |
| 53 | + int view{-1}; |
| 54 | + int projection{-1}; |
| 55 | + |
| 56 | + // Fragment |
| 57 | + int texture_diffuse{-1}; |
| 58 | + int texture_opacity{-1}; |
| 59 | + int light_direction{-1}; |
| 60 | + int light_color{-1}; |
| 61 | + int light_power{-1}; |
| 62 | + int camera_pos{-1}; |
| 63 | + int ambient_strength{-1}; |
| 64 | + int specularity_factor{-1}; |
| 65 | + int shininess{-1}; |
| 66 | + int gamma{-1}; |
| 67 | + |
| 68 | + int tex{-1}; |
| 69 | + int color{-1}; |
| 70 | + int id{-1}; |
| 71 | +}; |
| 72 | + |
49 | 73 | /** |
50 | 74 | * @brief Defines the types of shading techniques available in rendering. |
51 | 75 | * |
@@ -85,6 +109,7 @@ enum class ViewingMode { |
85 | 109 | struct ShaderSource { |
86 | 110 | char const* vertex_shader; |
87 | 111 | char const* fragment_shader; |
| 112 | + std::function<void(UniformLocations&, std::function<void(int&, char const*)>)> uniform_caching_function; |
88 | 113 | }; |
89 | 114 |
|
90 | 115 | /** |
@@ -121,24 +146,38 @@ class Shader { |
121 | 146 | static void init(); |
122 | 147 |
|
123 | 148 | Shader() = default; |
124 | | - Shader(char const* vertex_path, char const* fragment_path); |
125 | 149 | Shader(ShaderSource); |
126 | 150 | Shader(Shader const&) = delete; |
127 | 151 | Shader(Shader&&); |
128 | 152 | Shader& operator=(Shader&&); |
129 | 153 |
|
130 | 154 | unsigned int m_id; |
| 155 | + UniformLocations uniform_locations; |
131 | 156 | void use() const; |
132 | | - void set_bool(char const* name, bool value) const; |
133 | | - void set_int(char const* name, int value) const; |
134 | | - void set_uint(char const* name, unsigned int value) const; |
135 | | - void set_float(char const* name, float value) const; |
136 | | - void set_mat2(char const* name, glm::mat2 const& matrix) const; |
137 | | - void set_mat3(char const* name, glm::mat3 const& matrix) const; |
138 | | - void set_mat4(char const* name, glm::mat4 const& matrix) const; |
139 | | - void set_vec2(char const* name, glm::vec2 const& vector) const; |
140 | | - void set_vec3(char const* name, glm::vec3 const& vector) const; |
141 | | - void set_vec4(char const* name, glm::vec4 const& vector) const; |
| 157 | + |
| 158 | + template <typename T> |
| 159 | + void set_uniform(int location, T const& value) const |
| 160 | + { |
| 161 | + if (location < 0) { |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + if constexpr (std::is_same_v<T, bool>) { |
| 166 | + glUniform1i(location, static_cast<int>(value)); |
| 167 | + } else if constexpr (std::is_same_v<T, int>) { |
| 168 | + glUniform1i(location, value); |
| 169 | + } else if constexpr (std::is_same_v<T, unsigned int>) { |
| 170 | + glUniform1ui(location, value); |
| 171 | + } else if constexpr (std::is_same_v<T, float>) { |
| 172 | + glUniform1f(location, value); |
| 173 | + } else if constexpr (std::is_same_v<T, glm::mat4>) { |
| 174 | + glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(value)); |
| 175 | + } else if constexpr (std::is_same_v<T, glm::vec3>) { |
| 176 | + glUniform3fv(location, 1, glm::value_ptr(value)); |
| 177 | + } else if constexpr (std::is_same_v<T, glm::vec4>) { |
| 178 | + glUniform4fv(location, 1, glm::value_ptr(value)); |
| 179 | + } |
| 180 | + } |
142 | 181 |
|
143 | 182 | private: |
144 | 183 | void check_compile_errors(unsigned int shader, ShadingStage stage) const; |
|
0 commit comments