added material resource type
This commit is contained in:
parent
925faa7717
commit
c9928bf0a4
@ -1,12 +1,22 @@
|
|||||||
#include "resourcemanager.h"
|
#include "resourcemanager.h"
|
||||||
|
|
||||||
std::unordered_map<std::string, std::unordered_map<std::string, void*>> ResourceManager::data;
|
std::unordered_map<std::string, std::unordered_map<std::string, void*>> data;
|
||||||
|
|
||||||
|
std::unordered_map<std::string, std::unordered_map<std::string, void*>> ResourceManager::getResourceMap()
|
||||||
|
{
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
void ResourceManager::add(void* resource, const std::string &type, const std::string name)
|
void ResourceManager::add(void* resource, const std::string &type, const std::string name)
|
||||||
{
|
{
|
||||||
data[type][name] = resource;
|
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)
|
void* ResourceManager::get(const std::string &type, const std::string name)
|
||||||
{
|
{
|
||||||
return check(type,name) ? data[type][name] : NULL;
|
return check(type,name) ? data[type][name] : NULL;
|
||||||
|
@ -10,14 +10,16 @@
|
|||||||
|
|
||||||
#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:
|
std::unordered_map<std::string, std::unordered_map<std::string, void*>> getResourceMap();
|
||||||
static std::unordered_map<std::string, std::unordered_map<std::string, void*>> data;
|
|
||||||
public:
|
void add(void* resource, const std::string &type, const std::string name);
|
||||||
static void add(void* resource, const std::string &type, const std::string name);
|
void remove(const std::string &type, const std::string name);
|
||||||
static void* get(const std::string &type, const std::string name);
|
void* get(const std::string &type, const std::string name);
|
||||||
static bool check(const std::string &type, const std::string name);
|
bool check(const std::string &type, const std::string name);
|
||||||
};
|
}
|
||||||
|
|
||||||
#endif // RESOURCEMANAGER_H
|
#endif // RESOURCEMANAGER_H
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include <SparrowRenderer/texture.h>
|
#include <SparrowRenderer/texture.h>
|
||||||
#include <SparrowRenderer/image.h>
|
#include <SparrowRenderer/image.h>
|
||||||
|
#include <SparrowRenderer/pbrmaterial.h>
|
||||||
|
|
||||||
#include <SparrowSerializer/serializationmanager.h>
|
#include <SparrowSerializer/serializationmanager.h>
|
||||||
|
|
||||||
@ -53,6 +54,12 @@ void ResourcePack::gui()
|
|||||||
m_resources.push_back(resource);
|
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)
|
for(ResourceInterface* res : m_resources)
|
||||||
{
|
{
|
||||||
if(ImGui::Button(res->m_name.c_str()))
|
if(ImGui::Button(res->m_name.c_str()))
|
||||||
@ -125,6 +132,7 @@ void TextureResource::destroy()
|
|||||||
{
|
{
|
||||||
delete m_texture;
|
delete m_texture;
|
||||||
m_texture = nullptr;
|
m_texture = nullptr;
|
||||||
|
RESOURCE_REMOVE(Texture, m_name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,3 +155,54 @@ void TextureResource::gui()
|
|||||||
destroy();
|
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");
|
||||||
|
}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include <SparrowSerializer/serializable.h>
|
#include <SparrowSerializer/serializable.h>
|
||||||
|
|
||||||
class Texture;
|
class Texture;
|
||||||
|
class PBRMaterial;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The ResourceInterface struct holds a resource, it handles its loading and its destruction
|
* @brief The ResourceInterface struct holds a resource, it handles its loading and its destruction
|
||||||
@ -60,4 +61,45 @@ public:
|
|||||||
void gui();
|
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
|
#endif // RESOURCEPACK_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user