Loading...
Searching...
No Matches
Material.cpp
Go to the documentation of this file.
1//
2// Created by denzel on 04/04/2025.
3//
4#include "hellfire/graphics/managers/ShaderManager.h"
5#include "hellfire/graphics/material/Material.h"
6
7#include "hellfire/core/Application.h"
8#include "hellfire/utilities/ServiceLocator.h"
9
10namespace hellfire {
11 void Material::bind() const {
12 uint32_t shader_program = get_compiled_shader_id();
13 if (shader_program == 0) {
14 std::cerr << "Warning: Material " << get_name() << " has no compiled shader!" << std::endl;
15 return;
16 }
17
18 bound_texture_units_.clear();
19
20 int texture_unit = 0;
21 bind_all_properties(shader_program, texture_unit);
22 }
23
24 void Material::unbind() const {
25 uint32_t shader_program = get_compiled_shader_id();
26 if (shader_program == 0) return;
27
29 }
30
32 // Unbind all textures that were bound during the last bind() call
33 for (int texture_unit : bound_texture_units_) {
34 glActiveTexture(GL_TEXTURE0 + texture_unit);
35 glBindTexture(GL_TEXTURE_2D, 0);
36 }
37 bound_texture_units_.clear();
38 }
39
40 void Material::bind_all_properties(const uint32_t shader_program, int &texture_unit) const {
41 for (const auto &property: properties_ | std::views::values) {
42 // Track texture unit usage
43 int start_texture_unit = texture_unit;
44
45 MaterialManager::bind_property_to_shader(property, shader_program, texture_unit);
46
47 // If a texture was bound, track which unit it used
48 if (property.type == PropertyType::TEXTURE && texture_unit > start_texture_unit) {
49 bound_texture_units_.push_back(start_texture_unit);
50 }
51 }
52 }
53
54 std::shared_ptr<Material> MaterialBuilder::create(const std::string &name) {
55 auto material = std::make_shared<Material>(name);
56
57 // Set default colors
58 material->set_diffuse_color(glm::vec3(0.8f));
59 material->set_specular_color(glm::vec3(0.5f));
60
61 // Set default material properties
62 material->set_shininess(8.0f);
63 material->set_opacity(1.0f);
64 material->set_metallic(0.0f);
65 material->set_roughness(0.5f);
66
67 // Set default UV transforms
68 material->set_uv_tiling(1.0f, 1.0f);
69 material->set_uv_offset(glm::vec2(0.0f));
70 material->set_uv_rotation(0.0f);
71
72 // Initialize all texture usage flags to false
73 material->set_property(MaterialConstants::USE_DIFFUSE_TEXTURE, false);
74 material->set_property(MaterialConstants::USE_NORMAL_TEXTURE, false);
75 material->set_property(MaterialConstants::USE_SPECULAR_TEXTURE, false);
76 material->set_property(MaterialConstants::USE_METALLIC_TEXTURE, false);
77 material->set_property(MaterialConstants::USE_ROUGHNESS_TEXTURE, false);
78 material->set_property(MaterialConstants::USE_AO_TEXTURE, false);
79 material->set_property(MaterialConstants::USE_EMISSIVE_TEXTURE, false);
80
82
83 return material;
84 }
85
86 std::shared_ptr<Material> MaterialBuilder::create_custom(const std::string &name, const std::string &vertex_path,
87 const std::string &fragment_path) {
88 auto material = std::make_shared<Material>(name);
89 material->set_custom_shader(vertex_path, fragment_path);
90
92
93 return material;
94 }
95
97 auto shader_id = ServiceLocator::get_service<ShaderManager>()->get_shader_for_material(material);
98 material.set_compiled_shader_id(shader_id);
99 }
100}
static void compile_shader_from_material(Material &material)
Definition Material.cpp:96
void bind_all_properties(uint32_t shader_program, int &texture_unit) const
Definition Material.cpp:40
void bind() const
Used to bind a Material for rendering.
Definition Material.cpp:11
uint32_t get_compiled_shader_id() const
Definition Material.h:239
void unbind_all_textures() const
Definition Material.cpp:31
const std::string & get_name() const
Definition Material.h:251
void set_compiled_shader_id(uint32_t shader_id)
Definition Material.h:235
void unbind() const
Definition Material.cpp:24
static constexpr const char * USE_SPECULAR_TEXTURE
static constexpr const char * USE_ROUGHNESS_TEXTURE
static constexpr const char * USE_AO_TEXTURE
static constexpr const char * USE_EMISSIVE_TEXTURE
static constexpr const char * USE_NORMAL_TEXTURE
static constexpr const char * USE_DIFFUSE_TEXTURE
static constexpr const char * USE_METALLIC_TEXTURE