Elevate Engine 1
Loading...
Searching...
No Matches
Material.cpp
Go to the documentation of this file.
1#include "Material.h"
2
10#include <algorithm>
11#include <format>
12#include <utility>
13
14
15namespace Elevate
16{
17 MaterialID Material::s_nextId = EE_DEFAULT_MATERIAL + 1; // Keep 0 for a default material
18
19 Material::Material()
20 {
21 m_id = s_nextId++;
22 }
23
24 Material::Material(const std::shared_ptr<Shader>& shader)
25 : Material()
26 {
27 m_shader = shader;
28
29 if (shader)
30 {
31 m_buffer.resize(m_shader->GetLayout().GetStride());
32 m_definedUniforms.resize(m_shader->GetLayout().GetElements().size());
33 }
34 else
35 {
36 EE_CORE_ERROR("A Material can only be created from a valid Shader instance.");
37 }
38 }
39
40 void Material::SetTexture(const std::string& name, TexturePtr texture)
41 {
42 m_textures[name] = texture;
43 }
44
45 void Material::Apply()
46 {
47 if (m_shader)
48 {
49 Renderer::BindShader(m_shader);
50
51 for (const auto& uniform : m_shader->GetLayout())
52 {
53 // ignore system managed uniforms
54 if (uniform.Type == ShaderDataType::Sampler2D) continue;
55
56 if (uniform.Name == EE_SHADER_VIEWPROJ ||
57 uniform.Name == EE_SHADER_CAMPOS ||
58 uniform.Name == EE_SHADER_MODEL)
59 {
60 continue;
61 }
62
63 void* data = m_buffer.data() + uniform.Offset;
64 // Only apply the uniform if it has been defined
65 if (m_definedUniforms[uniform.Index])
66 {
67 m_shader->SetUniform(uniform.Name, uniform.Type, data);
68 }
69 }
70
71 // Reset all texture bindings
72 m_shader->SetUniform1i("has_diffuseTex", 0);
73 m_shader->SetUniform1i("has_specularTex", 0);
74 m_shader->SetUniform1i("has_ambientTex", 0);
75
76 uint32_t slot = 0;
77 for (auto& tex : m_textures)
78 {
79 if (tex.second && tex.second->IsTextureLoaded())
80 {
81 Renderer::BindTexture(tex.second, slot);
82 m_shader->SetUniform1i(tex.first, slot);
83 m_shader->SetUniform1i("has_" + tex.first, 1);
84 slot++;
85 }
86 }
87 }
88 else
89 {
90 EE_CORE_ERROR("Material::Apply() : Cannot apply uniforms to a shader that is nullptr.");
91 }
92 }
93
94 std::shared_ptr<Shader> Material::GetShader()
95 {
96 return m_shader;
97 }
98
99 MaterialPtr MaterialFactory::Create(const std::shared_ptr<Shader>& shader)
100 {
101 if (!shader)
102 {
103 EE_CORE_ERROR("(MaterialFactory) : Cannot create a material from a null shader.");
104 return nullptr;
105 }
106 return std::shared_ptr<Material>(new Material(shader));
107 }
108
109 MaterialPtr MaterialRegistry::LoadMaterial(const std::shared_ptr<Shader>& shader)
110 {
111 EE_TRACE("(MaterialRegistry) : Creating shader for shader : {}", shader->GetRendererID());
112 MaterialPtr material = MaterialFactory::Create(shader);
113 instance().m_materials[material->GetID()] = material;
114 return material;
115 }
116
117 MaterialPtr MaterialRegistry::GetMaterial(MaterialID id)
118 {
119 if (instance().m_materials.contains(id))
120 {
121 return instance().m_materials.at(id);
122 }
123 return nullptr;
124 }
125
126 MaterialRegistry::MaterialRegistry()
127 {
128 EE_CORE_INFO("Initializing MaterialRegistry.");
129 // Create a default material for the default shader
130 EE_CORE_TRACE("(MaterialRegistry) : Creating default material from default shader.");
131 m_materials[EE_DEFAULT_MATERIAL] = MaterialFactory::Create(ShaderManager::GetShader(EE_DEFAULT_SHADER));
132 EE_CORE_INFO("Initialized MaterialRegistry.");
133 }
134
135 MaterialRegistry& MaterialRegistry::instance()
136 {
137 static MaterialRegistry instance;
138 return instance;
139 }
140}
#define EE_TRACE(...)
Definition Log.h:66
#define EE_DEFAULT_MATERIAL
Definition Material.h:17
#define EE_DEFAULT_SHADER
#define EE_SHADER_MODEL
Definition Shader.h:11
#define EE_SHADER_VIEWPROJ
Definition Shader.h:12
#define EE_SHADER_CAMPOS
Definition Shader.h:13
uint32_t MaterialID
Definition Material.h:26
std::shared_ptr< Material > MaterialPtr
Definition Material.h:24
std::shared_ptr< Texture > TexturePtr
Definition Texture.h:13