Loading...
Searching...
No Matches
MaterialManager.cpp
Go to the documentation of this file.
1//
2// Created by denzel on 13/08/2025.
3//
4#include "hellfire/graphics/managers/MaterialManager.h"
5
6#include <iostream>
7#include <glm/gtc/type_ptr.hpp>
8
9#include "hellfire/graphics/material/Material.h"
10#include "hellfire/graphics/shader/Shader.h"
11
12namespace hellfire {
13 void MaterialManager::bind_material(const Material &material) {
14 if (const uint32_t shader_program = material.get_compiled_shader_id(); shader_program == 0) {
15 std::cerr << "Warning: Material " << material.get_name() << " has no compiled shader!" << std::endl;
16 return;
17 }
18
19 material.bind();
20 }
21
22 void MaterialManager::bind_property_to_shader(const Material::Property &property, uint32_t shader_program,
23 int &texture_unit) {
24 bind_property(property, shader_program, texture_unit);
25 }
26
27 void MaterialManager::bind_property(const Material::Property &property, uint32_t shader_program,
28 int &texture_unit) {
29 const ShaderUniformBinder binder(shader_program);
30 const std::string &uniform_name = property.uniform_name;
31
32 switch (property.type) {
34 const float value = std::get<float>(property.value);
35 binder.set_float(uniform_name, value);
36 break;
37 }
38
40 const glm::vec2 value = std::get<glm::vec2>(property.value);
41 binder.set_vec2(uniform_name, value);
42 break;
43 }
46 const glm::vec3 value = std::get<glm::vec3>(property.value);
47 binder.set_vec3(uniform_name, value);
48 break;
49 }
50
53 const glm::vec4 value = std::get<glm::vec4>(property.value);
54 binder.set_vec4(uniform_name, value);
55 break;
56 }
57
59 if (const Texture *texture = std::get<Texture *>(property.value); texture && texture->is_valid()) {
60 texture->bind(texture_unit);
61 binder.set_int(uniform_name, texture_unit);
62
63 // Set the texture usage flag if it exists
64 if (const char* flag_name = get_texture_flag_for_uniform(uniform_name)) {
65 binder.set_bool(flag_name, true);
66 }
67
68 texture_unit++;
69 } else {
70 // Set texture usage flag to false for missing/invalid textures
71 if (const char* flag_name = get_texture_flag_for_uniform(uniform_name)) {
72 binder.set_bool(flag_name, false);
73 }
74
75 if (texture) {
76 std::cout << "Warning: Invalid texture for " << uniform_name << std::endl;
77 }
78 }
79 break;
80 }
81
83 const bool value = std::get<bool>(property.value);
84 binder.set_bool(uniform_name, value);
85 break;
86 }
87
89 const int value = std::get<int>(property.value);
90 binder.set_int(uniform_name, value);
91 break;
92 }
93
95 const glm::mat3 value = std::get<glm::mat3>(property.value);
96 binder.set_mat3(uniform_name, value);
97 break;
98 }
99
101 const glm::mat4 value = std::get<glm::mat4>(property.value);
102 binder.set_mat4(uniform_name, value);
103 break;
104 }
105 default: ;
106 }
107 }
108
109 const char* MaterialManager::get_texture_flag_for_uniform(const std::string& uniform_name) {
110 // Map texture uniform names to their corresponding flag names using constants
111 if (uniform_name == MaterialConstants::DIFFUSE_TEXTURE) {
113 } else if (uniform_name == MaterialConstants::NORMAL_TEXTURE) {
115 } else if (uniform_name == MaterialConstants::SPECULAR_TEXTURE) {
117 } else if (uniform_name == MaterialConstants::METALLIC_TEXTURE) {
119 } else if (uniform_name == MaterialConstants::ROUGHNESS_TEXTURE) {
121 } else if (uniform_name == MaterialConstants::AO_TEXTURE) {
123 } else if (uniform_name == MaterialConstants::EMISSIVE_TEXTURE) {
125 }
126
127 // For custom textures, return nullptr (no automatic flag)
128 return nullptr;
129 }
130
131}
static void bind_property(const Material::Property &property, uint32_t shader_program, int &texture_unit)
static void bind_property_to_shader(const Material::Property &property, uint32_t shader_program, int &texture_unit)
static void bind_material(const Material &material)
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
const std::string & get_name() const
Definition Material.h:251
ShaderUniformBinder(const uint32_t shader_program)
bool is_valid() const
Definition Texture.cpp:198
void bind(unsigned int slot=0) const
Definition Texture.cpp:202
static constexpr const char * ROUGHNESS_TEXTURE
static constexpr const char * DIFFUSE_TEXTURE
static constexpr const char * NORMAL_TEXTURE
static constexpr const char * USE_SPECULAR_TEXTURE
static constexpr const char * SPECULAR_TEXTURE
static constexpr const char * USE_ROUGHNESS_TEXTURE
static constexpr const char * AO_TEXTURE
static constexpr const char * USE_AO_TEXTURE
static constexpr const char * USE_EMISSIVE_TEXTURE
static constexpr const char * METALLIC_TEXTURE
static constexpr const char * EMISSIVE_TEXTURE
static constexpr const char * USE_NORMAL_TEXTURE
static constexpr const char * USE_DIFFUSE_TEXTURE
static constexpr const char * USE_METALLIC_TEXTURE