From c9928bf0a449672dc6264e8fced70633f9f442a9 Mon Sep 17 00:00:00 2001 From: Anselme Date: Tue, 29 Aug 2017 15:34:22 +0200 Subject: [PATCH] added material resource type --- src/resourcemanager.cpp | 12 +++++++- src/resourcemanager.h | 20 +++++++------ src/tools/resourcepack.cpp | 59 ++++++++++++++++++++++++++++++++++++++ src/tools/resourcepack.h | 42 +++++++++++++++++++++++++++ 4 files changed, 123 insertions(+), 10 deletions(-) diff --git a/src/resourcemanager.cpp b/src/resourcemanager.cpp index f1ed023..c56f26f 100644 --- a/src/resourcemanager.cpp +++ b/src/resourcemanager.cpp @@ -1,12 +1,22 @@ #include "resourcemanager.h" -std::unordered_map> ResourceManager::data; +std::unordered_map> data; + +std::unordered_map> ResourceManager::getResourceMap() +{ + return data; +} void ResourceManager::add(void* resource, const std::string &type, const std::string name) { data[type][name] = resource; } +void ResourceManager::remove(const std::string &type, const std::string name) +{ + data[type].erase(name); +} + void* ResourceManager::get(const std::string &type, const std::string name) { return check(type,name) ? data[type][name] : NULL; diff --git a/src/resourcemanager.h b/src/resourcemanager.h index 6092a68..2b310ea 100644 --- a/src/resourcemanager.h +++ b/src/resourcemanager.h @@ -8,16 +8,18 @@ #define RESOURCE_GET(type, name) ((type*)(ResourceManager::get(#type, name))) -#define RESOURCE_CHECK(type,name) ResourceManager::check(#type, name) +#define RESOURCE_CHECK(type, name) ResourceManager::check(#type, name) -class ResourceManager +#define RESOURCE_REMOVE(type, name) ResourceManager::remove(#type, name) + +namespace ResourceManager { -private: - static std::unordered_map> data; -public: - static void add(void* resource, const std::string &type, const std::string name); - static void* get(const std::string &type, const std::string name); - static bool check(const std::string &type, const std::string name); -}; + std::unordered_map> getResourceMap(); + + void add(void* resource, const std::string &type, const std::string name); + void remove(const std::string &type, const std::string name); + void* get(const std::string &type, const std::string name); + bool check(const std::string &type, const std::string name); +} #endif // RESOURCEMANAGER_H diff --git a/src/tools/resourcepack.cpp b/src/tools/resourcepack.cpp index 8aec89f..e9434c4 100644 --- a/src/tools/resourcepack.cpp +++ b/src/tools/resourcepack.cpp @@ -7,6 +7,7 @@ #include #include +#include #include @@ -53,6 +54,12 @@ void ResourcePack::gui() m_resources.push_back(resource); } + if(ImGui::Button("Add a material")) + { + MaterialResource* resource = new MaterialResource(); + m_resources.push_back(resource); + } + for(ResourceInterface* res : m_resources) { if(ImGui::Button(res->m_name.c_str())) @@ -125,6 +132,7 @@ void TextureResource::destroy() { delete m_texture; m_texture = nullptr; + RESOURCE_REMOVE(Texture, m_name); } } @@ -147,3 +155,54 @@ void TextureResource::gui() destroy(); } } + +INIT_SERIALIZABLE(MaterialResource) + +MaterialResource::MaterialResource() : + m_material(nullptr) +{ + m_name = "new material"; + m_albedo = glm::vec3(0.9f); + m_metallic = 0.2f; + m_roughness = 0.8f; + m_emission = glm::vec3(0); + m_opacity = 1.f; +} + +MaterialResource::~MaterialResource() +{ + destroy(); +} + +void MaterialResource::load(float & progress) +{ + m_material = new PBRMaterial(); + m_material->albedo = m_albedo; + m_material->metallic = m_metallic; + m_material->roughness = m_roughness; + m_material->emission = m_emission; + m_material->opacity = m_opacity; + m_material->setTexture(PBRMaterial::ALBEDO_SLOT, RESOURCE_GET(Texture, m_albedoTexture)); + m_material->setTexture(PBRMaterial::METALLIC_SLOT, RESOURCE_GET(Texture, m_metallicTexture)); + m_material->setTexture(PBRMaterial::ROUGHNESS_SLOT, RESOURCE_GET(Texture, m_roughnessTexture)); + m_material->setTexture(PBRMaterial::EMISSION_SLOT, RESOURCE_GET(Texture, m_emissionTexture)); + m_material->setTexture(PBRMaterial::NORMALS_SLOT, RESOURCE_GET(Texture, m_normalTexture)); + m_material->setTexture(PBRMaterial::ALPHA_SLOT, RESOURCE_GET(Texture, m_alphaMaskTexture)); + RESOURCE_ADD(m_material, PBRMaterial, m_name); + progress = 1; +} + +void MaterialResource::destroy() +{ + if(m_material != nullptr) + { + delete m_material; + m_material = nullptr; + RESOURCE_REMOVE(PBRMaterial, m_name); + } +} + +void MaterialResource::gui() +{ + ImGui::Text("TODO"); +} diff --git a/src/tools/resourcepack.h b/src/tools/resourcepack.h index 7c75e3a..8e4c83b 100644 --- a/src/tools/resourcepack.h +++ b/src/tools/resourcepack.h @@ -4,6 +4,7 @@ #include class Texture; +class PBRMaterial; /** * @brief The ResourceInterface struct holds a resource, it handles its loading and its destruction @@ -60,4 +61,45 @@ public: void gui(); }; +class MaterialResource : public ResourceInterface +{ + P_VEC3(m_albedo) + P_FLOAT(m_metallic) + P_FLOAT(m_roughness) + P_VEC3(m_emission) + P_FLOAT(m_opacity) + P_STRING(m_albedoTexture) + P_STRING(m_metallicTexture) + P_STRING(m_roughnessTexture) + P_STRING(m_emissionTexture) + P_STRING(m_normalTexture) + P_STRING(m_alphaMaskTexture) + + PBRMaterial* m_material; + +public: + MaterialResource(); + virtual ~MaterialResource(); + + SERIALIZABLE(MaterialResource, + CAST(m_name), + CAST(m_albedo), + CAST(m_metallic), + CAST(m_roughness), + CAST(m_emission), + CAST(m_opacity), + CAST(m_albedoTexture), + CAST(m_metallicTexture), + CAST(m_roughnessTexture), + CAST(m_emissionTexture), + CAST(m_normalTexture), + CAST(m_alphaMaskTexture)) + + void load(float & progress); + + void destroy(); + + void gui(); +}; + #endif // RESOURCEPACK_H