Loading...
Searching...
No Matches
Material.h
Go to the documentation of this file.
1#pragma once
2#include <iostream>
3#include <string>
4#include <unordered_map>
5#include <unordered_set>
6#include <variant>
7#include <optional>
8#include <glm/glm.hpp>
9#include <nlohmann/json.hpp>
10
12#include "hellfire/graphics/texture/Texture.h"
13
14namespace hellfire {
15 class MaterialInstance;
16 class Application;
17 class ShaderManager;
18}
19
20namespace hellfire {
22 public:
23 enum class PropertyType {
25 };
26
27 struct Property {
29 std::string name;
30 std::variant<float, glm::vec2, glm::vec3, glm::vec4, Texture *, bool, int, glm::mat3, glm::mat4> value;
31
32 std::string uniform_name;
33
35 }
36
37 Property(const std::string &name, float value, const std::string &uniform = "") : type(PropertyType::FLOAT),
38 name(name), value(value), uniform_name(uniform.empty() ? name : uniform) {
39 }
40
41 Property(const std::string &name, const glm::vec2 &value,
42 const std::string &uniform = "") : type(PropertyType::VEC2), value(value),
43 uniform_name(uniform.empty() ? name : uniform), name(name) {
44 }
45
46 Property(const std::string &name, const glm::vec3 &value,
47 const std::string &uniform = "") : type(PropertyType::VEC3), value(value),
48 uniform_name(uniform.empty() ? name : uniform), name(name) {
49 }
50
51 Property(const std::string &name, const glm::vec3 &value, PropertyType type,
52 const std::string &uniform = "")
53 : type(type), name(name), value(value),
54 uniform_name(uniform.empty() ? name : uniform) {
55 }
56
57 Property(const std::string &name, const glm::vec4 &value, PropertyType type,
58 const std::string &uniform = "")
59 : type(type), name(name), value(value),
60 uniform_name(uniform.empty() ? name : uniform) {
61 }
62
63 Property(const std::string &name, const glm::vec4 &value, const std::string &uniform = "") : name(name),
64 type(PropertyType::VEC4), value(value), uniform_name(uniform.empty() ? name : uniform) {
65 }
66
67 Property(const std::string &name, Texture *value, const std::string &uniform = "") : name(name),
68 type(PropertyType::TEXTURE), value(value), uniform_name(uniform.empty() ? name : uniform) {
69 }
70
71 Property(const std::string &name, bool value, const std::string &uniform = "") : name(name),
72 type(PropertyType::BOOL), value(value), uniform_name(uniform.empty() ? name : uniform) {
73 }
74
75 Property(const std::string &name, int value, const std::string &uniform = "") : name(name),
76 type(PropertyType::INT), value(value), uniform_name(uniform.empty() ? name : uniform) {
77 }
78
79 Property(const std::string &name, const glm::mat3 &value, const std::string &uniform = "") : name(name),
80 type(PropertyType::MAT3), value(value), uniform_name(uniform.empty() ? name : uniform) {
81 }
82
83 Property(const std::string &name, const glm::mat4 &value, const std::string &uniform = "") : name(name),
84 type(PropertyType::MAT4), value(value), uniform_name(uniform.empty() ? name : uniform) {
85 }
86 };
87
88 struct ShaderInfo {
89 std::string vertex_path;
90 std::string fragment_path;
91 std::optional<std::string> geometry_path;
93 std::unordered_map<std::string, std::string> uniform_mappings; // Property name -> uniform name
94
95 bool is_valid() const {
96 return !vertex_path.empty() && !fragment_path.empty();
97 }
98 };
99
100 private:
101 std::string name_;
105
106 // Instancing support
107 std::shared_ptr<Material> base_material_;
110
111 public:
112 explicit Material(const std::string &name) : name_(name) {}
113
114 // Generic Property Setters
115 template<typename T>
116 void set_property(const std::string &name, const T &value, PropertyType type,
117 const std::string &uniform_name = "") {
118 properties_[name] = Property(name, value, type, uniform_name);
119 }
120
121 template<typename T>
122 void set_property(const std::string &name, const T &value, const std::string &uniform_name = "") {
123 properties_[name] = Property(name, value, uniform_name);
124 }
125
126 // Generic Property Getter
127 template<typename T>
128 T get_property(const std::string &name, const T &default_value = T{}) const {
129 if (const auto it = properties_.find(name); it != properties_.end()) {
130 if (auto *val = std::get_if<T>(&it->second.value)) {
131 return *val;
132 }
133 }
134 return default_value;
135 }
136
137 // Texture Management
138 Material& set_texture(const std::string &path, TextureType type, int texture_slot = 0) {
139 auto* texture = new Texture(path, type);
140 return set_texture_internal(texture, type, texture_slot);
141 }
142
143 Material& set_texture(const std::shared_ptr<Texture> &texture, int texture_slot = 0) {
144 return set_texture_internal(texture.get(), texture->get_type(), texture_slot);
145 }
146
147 Material& set_texture(Texture* texture, int texture_slot = 0) {
148 return set_texture_internal(texture, texture->get_type(), texture_slot);
149 }
150
151 // Color Setters
152 void set_diffuse_color(const glm::vec3 &color) {
154 }
155
156 void set_ambient_color(const glm::vec3 &color) {
158 }
159
160 void set_specular_color(const glm::vec3 &color) {
162 }
163
164 void set_emissive_color(const glm::vec3 &color) {
166 }
167
168 // Material Property Setters
169 void set_shininess(float shininess) {
170 set_property(MaterialConstants::SHININESS, shininess);
171 }
172
173 void set_metallic(float metallic) {
174 set_property(MaterialConstants::METALLIC, metallic);
175 }
176
177 void set_roughness(float roughness) {
178 set_property(MaterialConstants::ROUGHNESS, roughness);
179 }
180
181 void set_opacity(float opacity) {
182 set_property(MaterialConstants::OPACITY, glm::clamp(opacity, 0.0f, 1.0f));
183 }
184
185 bool is_transparent() const {
186 return get_property<float>(MaterialConstants::OPACITY, 1.0f) < 1.0f;
187 }
188
189 // UV Transform
190 void set_uv_tiling(const glm::vec2 &tiling) {
191 set_property(MaterialConstants::UV_TILING, tiling);
192 }
193
194 void set_uv_tiling(float x, float y) {
195 set_uv_tiling(glm::vec2(x, y));
196 }
197
198 void set_uv_offset(const glm::vec2 &offset) {
199 set_property(MaterialConstants::UV_OFFSET, offset);
200 }
201
202 void set_uv_rotation(float rotation) {
203 set_property(MaterialConstants::UV_ROTATION, rotation);
204 }
205
206 // Custom Shader Support
207 void set_custom_shader(const std::string &vertex_path, const std::string &fragment_path) {
208 custom_shader_info_ = ShaderInfo{vertex_path, fragment_path};
209 }
210
211 void set_custom_shader(const ShaderInfo &shader_info) {
212 custom_shader_info_ = shader_info;
213 }
214
215 void add_shader_define(const std::string &define) {
216 if (custom_shader_info_) {
217 custom_shader_info_->defines.insert(define);
218 }
219 }
220
221 void set_uniform_mapping(const std::string &property_name, const std::string &uniform_name) {
222 if (custom_shader_info_) {
223 custom_shader_info_->uniform_mappings[property_name] = uniform_name;
224 }
225 }
226
227 bool has_custom_shader() const {
228 return custom_shader_info_ && custom_shader_info_->is_valid();
229 }
230
232 return custom_shader_info_ ? &*custom_shader_info_ : nullptr;
233 }
234
235 void set_compiled_shader_id(uint32_t shader_id) {
236 compiled_shader_id_ = shader_id;
237 }
238
239 uint32_t get_compiled_shader_id() const {
240 return compiled_shader_id_;
241 }
242
243 /// Used to bind a Material for rendering
244 void bind() const;
245 void unbind() const;
246
247 void unbind_all_textures() const;
248
249 // Getters
250 const auto &get_properties() const { return properties_; }
251 const std::string &get_name() const { return name_; }
252 void set_name(const std::string &name) { name_ = name; }
253 private:
255
256 Material& set_texture_internal(Texture* texture, TextureType type, int texture_slot) {
257 const char* uniform_name = MaterialConstants::get_texture_uniform_name(type);
258 const char* flag_name = MaterialConstants::get_texture_flag_name(type);
259
260 if (!uniform_name || !flag_name) {
261 std::cerr << "Warning: Unsupported texture type" << std::endl;
262 return *this;
263 }
264
265 if (texture_slot >= 0) {
266 texture->set_slot(texture_slot);
267 set_property(uniform_name + std::string("Slot"), texture_slot);
268 }
269
270 set_property(uniform_name, texture, uniform_name);
271 set_property(flag_name, texture != nullptr);
272
273 return *this;
274 }
275
276 void bind_all_properties(uint32_t shader_program, int &texture_unit) const;
277
278 bool has_property(const std::string &name) const {
279 return properties_.find(name) != properties_.end();
280 }
281
282 Property get_property_object(const std::string &name) const {
283 if (const auto it = properties_.find(name); it != properties_.end()) {
284 return it->second;
285 }
286 return Property{}; // Return default property
287 }
288 };
289
291 public:
292 static std::shared_ptr<Material> create(const std::string &name);
293 static std::shared_ptr<Material> create_custom(const std::string &name,
294 const std::string &vertex_path,
295 const std::string &fragment_path);
296 private:
297 static void compile_shader_from_material(Material &material);
298 };
299}
Definition IB.h:7
void pass_data(const T *data, size_t count) const
Definition IB.h:18
IB()
Definition IB.cpp:6
void pass_data(const std::vector< T > &data) const
Definition IB.h:24
uint32_t m_renderer_id_
Definition IB.h:8
void unbind()
Definition IB.cpp:21
~IB()
Definition IB.cpp:11
void bind()
Definition IB.cpp:16
Definition VA.h:7
void bind() const
Definition VA.cpp:16
uint32_t m_renderer_id_
Definition VA.h:8
~VA()
Definition VA.cpp:11
VA()
Definition VA.cpp:6
uint32_t get_id() const
Definition VA.cpp:25
void unbind()
Definition VA.cpp:20
Definition VB.h:7
void pass_data(const std::vector< T > &data) const
Definition VB.h:22
void unbind()
Definition VB.cpp:19
~VB()
Definition VB.cpp:9
void pass_dynamic_data(const std::vector< T > &data) const
Definition VB.h:28
uint32_t m_renderer_id_
Definition VB.h:8
void pass_data(const T *data, size_t count) const
Definition VB.h:16
void bind()
Definition VB.cpp:14
VB()
Definition VB.cpp:4
std::shared_ptr< Texture > get_texture(AssetID id)
size_t get_loaded_mesh_count() const
std::unordered_map< AssetID, std::shared_ptr< Texture > > texture_cache_
AssetManager(AssetRegistry &registry)
std::shared_ptr< Mesh > get_mesh(AssetID id)
size_t get_loaded_material_count() const
void unload(AssetID id)
AssetRegistry & registry_
std::unordered_map< AssetID, std::shared_ptr< Material > > material_cache_
size_t get_loaded_texture_count() const
std::shared_ptr< Material > get_material(AssetID id)
std::unordered_map< AssetID, std::shared_ptr< Mesh > > mesh_cache_
Registry for storing assets.
const std::filesystem::path & get_project_root()
std::optional< AssetID > get_uuid_by_path(const std::filesystem::path &filepath)
AssetID generate_uuid(const std::filesystem::path &filepath)
void unregister_asset(AssetID uuid)
bool has_asset_changed(AssetID uuid) const
void set_project_root(const std::filesystem::path &project_root)
std::filesystem::path get_absolute_path(AssetID uuid)
std::unordered_map< std::filesystem::path, AssetID > path_to_uuid_
std::filesystem::path registry_file_
AssetID register_asset(const std::filesystem::path &filepath, AssetType type)
std::unordered_map< AssetID, AssetMetadata > assets_
std::filesystem::path to_absolute_path(const std::filesystem::path &relative_path) const
static AssetType get_type_from_extension(const std::filesystem::path &filepath)
std::filesystem::path to_relative_path(const std::filesystem::path &absolute_path) const
AssetID register_asset(const std::filesystem::path &filepath)
std::vector< AssetID > register_directory(const std::filesystem::path &directory_path, bool recursive)
std::vector< AssetMetadata > get_assets_by_type(AssetType type)
std::filesystem::path get_relative_path(AssetID uuid)
bool asset_exists(AssetID uuid) const
size_t get_asset_count() const
std::vector< AssetID > get_modified_assets() const
AssetRegistry(const std::filesystem::path &registry_file, const std::filesystem::path &project_root)
std::optional< AssetMetadata > get_asset(AssetID uuid) const
std::filesystem::path project_root_
std::vector< AssetMetadata > get_all_assets() const
uint64_t get_file_last_modified(const std::filesystem::path &filepath) const
static void compile_shader_from_material(Material &material)
Definition Material.cpp:96
static std::optional< MaterialData > load(const std::filesystem::path &filepath)
void set_ambient_color(const glm::vec3 &color)
Definition Material.h:156
void bind_all_properties(uint32_t shader_program, int &texture_unit) const
Definition Material.cpp:40
Material & set_texture(Texture *texture, int texture_slot=0)
Definition Material.h:147
std::map< std::string, Property > properties_
Definition Material.h:102
std::shared_ptr< Material > base_material_
Definition Material.h:107
std::string name_
Definition Material.h:101
std::vector< int > bound_texture_units_
Definition Material.h:254
Material & set_texture_internal(Texture *texture, TextureType type, int texture_slot)
Definition Material.h:256
void set_uv_offset(const glm::vec2 &offset)
Definition Material.h:198
void bind() const
Used to bind a Material for rendering.
Definition Material.cpp:11
bool is_transparent() const
Definition Material.h:185
uint32_t get_compiled_shader_id() const
Definition Material.h:239
void unbind_all_textures() const
Definition Material.cpp:31
std::unordered_map< std::string, Property > overrides_
Definition Material.h:108
void set_uv_tiling(float x, float y)
Definition Material.h:194
const std::string & get_name() const
Definition Material.h:251
void set_roughness(float roughness)
Definition Material.h:177
const auto & get_properties() const
Definition Material.h:250
void set_specular_color(const glm::vec3 &color)
Definition Material.h:160
std::optional< ShaderInfo > custom_shader_info_
Definition Material.h:103
std::unordered_set< std::string > touched_uniforms_
Definition Material.h:109
void set_uv_rotation(float rotation)
Definition Material.h:202
void set_compiled_shader_id(uint32_t shader_id)
Definition Material.h:235
Material & set_texture(const std::shared_ptr< Texture > &texture, int texture_slot=0)
Definition Material.h:143
void set_metallic(float metallic)
Definition Material.h:173
void set_uv_tiling(const glm::vec2 &tiling)
Definition Material.h:190
void set_diffuse_color(const glm::vec3 &color)
Definition Material.h:152
void set_opacity(float opacity)
Definition Material.h:181
uint32_t compiled_shader_id_
Definition Material.h:104
bool has_custom_shader() const
Definition Material.h:227
void set_emissive_color(const glm::vec3 &color)
Definition Material.h:164
void set_custom_shader(const ShaderInfo &shader_info)
Definition Material.h:211
void unbind() const
Definition Material.cpp:24
const ShaderInfo * get_shader_info() const
Definition Material.h:231
void set_shininess(float shininess)
Definition Material.h:169
static std::shared_ptr< Mesh > load(const std::filesystem::path &filepath)
bool is_wireframe
Definition Mesh.h:32
Mesh(const std::vector< Vertex > &vertices, const std::vector< unsigned int > &indices)
Definition Mesh.cpp:11
int get_index_count() const
Definition Mesh.cpp:96
void bind() const
Definition Mesh.cpp:22
std::unique_ptr< VA > vao_
Definition Mesh.h:41
void build()
Definition Mesh.cpp:30
std::unique_ptr< IB > ibo_
Definition Mesh.h:43
void create_mesh()
Definition Mesh.cpp:34
std::vector< Vertex > vertices
Definition Mesh.h:29
bool is_built() const
Definition Mesh.h:26
void draw() const
Definition Mesh.cpp:83
void unbind() const
Definition Mesh.cpp:26
Mesh(const std::vector< Vertex > &vertices, const std::vector< unsigned int > &indices, bool defer_build)
Definition Mesh.cpp:16
std::unique_ptr< VB > vbo_
Definition Mesh.h:42
std::vector< unsigned int > indices
Definition Mesh.h:30
void draw_instanced(size_t amount) const
Definition Mesh.cpp:89
int index_count_
Definition Mesh.h:45
TextureType get_type() const
Definition Texture.h:113
void set_slot(int slot)
Definition Texture.h:118
TextureType
Definition Texture.h:13
constexpr AssetID INVALID_ASSET_ID
Definition Vertex.h:5
glm::vec3 position
Definition Vertex.h:6
glm::vec3 color
Definition Vertex.h:8
glm::vec3 normal
Definition Vertex.h:7
glm::vec3 tangent
Definition Vertex.h:10
glm::vec3 bitangent
Definition Vertex.h:11
glm::vec2 texCoords
Definition Vertex.h:9
std::filesystem::path filepath
static constexpr const char * EMISSIVE_COLOR
static constexpr const char * UV_ROTATION
static constexpr const char * SPECULAR_COLOR
static constexpr const char * METALLIC
static const char * get_texture_flag_name(TextureType type)
static constexpr const char * ROUGHNESS
static constexpr const char * SHININESS
static constexpr const char * DIFFUSE_COLOR
static constexpr const char * AMBIENT_COLOR
static constexpr const char * UV_TILING
static constexpr const char * UV_OFFSET
static const char * get_texture_uniform_name(TextureType type)
static constexpr const char * OPACITY
std::variant< float, glm::vec2, glm::vec3, glm::vec4, Texture *, bool, int, glm::mat3, glm::mat4 > value
Definition Material.h:30
std::unordered_map< std::string, std::string > uniform_mappings
Definition Material.h:93
std::unordered_set< std::string > defines
Definition Material.h:92
std::optional< std::string > geometry_path
Definition Material.h:91