diff --git a/src/editor.cpp b/src/editor.cpp index 1589fa2..ebafbb0 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -3,7 +3,7 @@ #include "engine.h" #include "tools/scenepicker.h" #include "scene/scenetree.h" -#include "tools/newresourcepack.h" +#include "tools/resourcepack.h" #include "editor/objecteditor.h" #include "sparrowshell/sparrowshell.h" @@ -185,7 +185,7 @@ void Editor::toggleRenderingPipelineGui() void Editor::toggleResourcePackGui() { if(m_editedResourcePack == nullptr) - m_editedResourcePack = new NewResourcePack(); + m_editedResourcePack = new ResourcePack(); else { delete m_editedResourcePack; diff --git a/src/editor.h b/src/editor.h index 24099bf..8155968 100644 --- a/src/editor.h +++ b/src/editor.h @@ -5,7 +5,7 @@ class Engine; class ScenePicker; -class NewResourcePack; +class ResourcePack; class ObjectEditor; class Mesh; @@ -13,7 +13,7 @@ class Editor : public SceneNode { ScenePicker* m_pickerNode; Mesh* m_selectedMesh; - NewResourcePack* m_editedResourcePack; + ResourcePack* m_editedResourcePack; ObjectEditor* m_objectEditor; bool m_materialEditorEnabled; bool m_editorEnabled; diff --git a/src/tools/loadingthread.cpp b/src/tools/loadingthread.cpp index cc5c549..282216d 100644 --- a/src/tools/loadingthread.cpp +++ b/src/tools/loadingthread.cpp @@ -6,9 +6,8 @@ #include #include -//#include + #include -#include #include @@ -82,37 +81,20 @@ const std::string& LoadingThread::getCurrentTaskName() void LoadingThread::loadResourcePack(const std::string& name) { -/* std::fstream file; - file.open("data/" + name + ".pack", std::ios_base::in); - ObjectLoader loader; - loader.loadAscii(file); - file.close(); - const std::vector& packVec = loader.getObjects(); - NewResourcePack* pack = packVec[0]; - m_packs[name] = pack; - for(ResourceInterface* res : pack->m_resources) - { - if((m_references[res->m_name] += 1) == 1) - m_resourceQueue.push_back(res); - else - delete res; - }*/ std::ifstream inputStream("data/" + name + ".pack"); cereal::JSONInputArchive input(inputStream); - NewResourcePack pack; -// input(pack); + ResourcePack pack = ResourcePack(); + input(pack); for(std::shared_ptr res : pack.m_resources) { if((m_references[res->m_name] += 1) == 1) m_resourceQueue.push_back(res); - //else - //delete res; } } void LoadingThread::freeResourcePack(const std::string& name) { - NewResourcePack* pack = m_packs[name]; + ResourcePack* pack = m_packs[name]; for(std::shared_ptr res : pack->m_resources) { if((m_references[res->m_name] -= 1) == 0) diff --git a/src/tools/loadingthread.h b/src/tools/loadingthread.h index 70ed574..874b127 100644 --- a/src/tools/loadingthread.h +++ b/src/tools/loadingthread.h @@ -5,7 +5,7 @@ #include #include -#include "newresourcepack.h" +#include "resourcepack.h" class LoadingThread { @@ -13,7 +13,7 @@ class LoadingThread static std::thread* thread; std::deque> m_resourceQueue; - std::unordered_map m_packs; + std::unordered_map m_packs; std::unordered_map m_references; ResourceInterface* m_currentTask; bool m_running; diff --git a/src/tools/newresourcepack.cpp b/src/tools/newresourcepack.cpp deleted file mode 100644 index 6939bd5..0000000 --- a/src/tools/newresourcepack.cpp +++ /dev/null @@ -1,205 +0,0 @@ -#include "newresourcepack.h" - -#include "loader.h" -#include "resourcemanager.h" - -#include - -#include -#include -#include - -#include -#include -#include -#include - -void NewResourcePack::gui() -{ - static std::shared_ptr currentResource = nullptr; - - ImGui::Begin("ResourcePack"); - char buf[1024] = {0}; - strcpy(buf, m_name.c_str()); - if(ImGui::InputText("Pack name", buf, 1024)) - m_name = buf; - - if(ImGui::Button("Save resource pack")) - { - try{ - std::ofstream outputStream("data/"+m_name+".pack"); - cereal::JSONOutputArchive output(outputStream); - NewResourcePack tmp; - tmp.m_name = this->m_name; - tmp.m_resources = this->m_resources; - output(tmp); - }catch(cereal::Exception e){ - std::cerr << "Error while saving resource pack : " << e.what() << std::endl; - } - } - if(ImGui::Button("Load resource pack")) - { - std::ifstream inputStream("data/"+m_name+".pack"); - cereal::JSONInputArchive input(inputStream); - NewResourcePack tmp = NewResourcePack(); - input(tmp); - this->m_name = tmp.m_name; - this->m_resources = tmp.m_resources; - } - - if(ImGui::Button("Add a texture")) - { - std::shared_ptr resource(new TextureResource()); - m_resources.push_back(resource); - } - - if(ImGui::Button("Add a material")) - { - std::shared_ptr resource(new MaterialResource()); -// MaterialResource* resource = new MaterialResource(); - m_resources.push_back(resource); - } - - for(std::shared_ptr res : m_resources) - { - if(ImGui::Button(res->m_name.c_str())) - currentResource = res; - } - ImGui::End(); - - if(currentResource != nullptr) - { - bool opened = true; - ImGui::Begin("Resource Editor", &opened); - - char resBuf[1024] = {0}; - strcpy(resBuf, currentResource->m_name.c_str()); - if(ImGui::InputText("Name (should be unique)", resBuf, 1024)) - currentResource->m_name = resBuf; - - currentResource->gui(); - if(ImGui::Button("Delete resource")) - { - for(unsigned int i=0; ialbedo = 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/newresourcepack.h b/src/tools/newresourcepack.h deleted file mode 100644 index 71071d7..0000000 --- a/src/tools/newresourcepack.h +++ /dev/null @@ -1,153 +0,0 @@ -#ifndef NEWRESOURCEPACK_H -#define NEWRESOURCEPACK_H - -#include -#include - -#include -#include -#include - -class Texture; -class PBRMaterial; - -/** - * @brief The ResourceInterface struct holds a resource, it handles its loading and its destruction - */ -struct ResourceInterface -{ - std::string m_name; - - template - void serialize(Archive & archive){ - archive(CEREAL_NVP(m_name)); - } - - virtual void loadResource(float & progress) = 0; - - virtual void destroy() = 0; - - virtual void gui() = 0; -}; - -/** - * @brief The ResourcePack class is just a container of LoadTask, it only exists for serialization purposes - */ -struct NewResourcePack -{ - std::string m_name; - std::vector> m_resources; - - NewResourcePack() {} - - void gui(); - - template - void serialize(Archive & archive){ - archive(cereal::make_nvp("ResourcePack",m_name),CEREAL_NVP(m_resources)); - } -}; - -/* - * Some LoadTask implementations - */ - -class TextureResource : public ResourceInterface -{ -// std::string m_name; - std::string m_path; - int m_bitsPerPixel; - bool m_isVerticallyReversed; - bool m_needsMipMaps; - - Texture* m_texture; - -public: - TextureResource(); - virtual ~TextureResource(); - - template - void serialize(Archive & archive) - { - archive(CEREAL_NVP(m_name), - CEREAL_NVP(m_path), - CEREAL_NVP(m_bitsPerPixel), - CEREAL_NVP(m_isVerticallyReversed), - CEREAL_NVP(m_needsMipMaps)); - } - - void loadResource(float & progress); - - void destroy(); - void gui(); -}; - -CEREAL_REGISTER_TYPE(TextureResource) -CEREAL_REGISTER_POLYMORPHIC_RELATION(ResourceInterface,TextureResource) - -class MaterialResource : public ResourceInterface -{ - glm::vec3 m_albedo; - float m_metallic; - float m_roughness; - glm::vec3 m_emission; - float m_opacity; - std::string m_albedoTexture; - std::string m_metallicTexture; - std::string m_roughnessTexture; - std::string m_emissionTexture; - std::string m_normalTexture; - std::string m_alphaMaskTexture; - - PBRMaterial* m_material; - -public: - MaterialResource(); - virtual ~MaterialResource(); - - template - void serialize(Archive & archive) - { - archive(CEREAL_NVP(m_name), - CEREAL_NVP(m_albedo), - CEREAL_NVP(m_metallic), - CEREAL_NVP(m_roughness), - CEREAL_NVP(m_emission), - CEREAL_NVP(m_opacity), - CEREAL_NVP(m_albedoTexture), - CEREAL_NVP(m_metallicTexture), - CEREAL_NVP(m_roughnessTexture), - CEREAL_NVP(m_emissionTexture), - CEREAL_NVP(m_normalTexture), - CEREAL_NVP(m_alphaMaskTexture)); - } - - void loadResource(float & progress); - - void destroy(); - - void gui(); -}; - -template -void serialize(Archive & archive,MaterialResource mat) -{ - archive(CEREAL_NVP(mat.m_name), - CEREAL_NVP(mat.m_albedo), - CEREAL_NVP(mat.m_metallic), - CEREAL_NVP(mat.m_roughness), - CEREAL_NVP(mat.m_emission), - CEREAL_NVP(mat.m_opacity), - CEREAL_NVP(mat.m_albedoTexture), - CEREAL_NVP(mat.m_metallicTexture), - CEREAL_NVP(mat.m_roughnessTexture), - CEREAL_NVP(mat.m_emissionTexture), - CEREAL_NVP(mat.m_normalTexture), - CEREAL_NVP(mat.m_alphaMaskTexture)); -} - -CEREAL_REGISTER_TYPE(MaterialResource) -CEREAL_REGISTER_POLYMORPHIC_RELATION(ResourceInterface,MaterialResource) - - -#endif // NEWRESOURCEPACK_H diff --git a/src/tools/resourcepack.cpp b/src/tools/resourcepack.cpp index a5fcc9a..b62a69b 100644 --- a/src/tools/resourcepack.cpp +++ b/src/tools/resourcepack.cpp @@ -9,15 +9,11 @@ #include #include -#include - #include -//INIT_SERIALIZABLE(ResourcePack) - -/*void ResourcePack::gui() +void ResourcePack::gui() { - static ResourceInterface* currentResource = nullptr; + static std::shared_ptr currentResource = nullptr; ImGui::Begin("ResourcePack"); char buf[1024] = {0}; @@ -27,40 +23,41 @@ if(ImGui::Button("Save resource pack")) { - std::fstream file; - ObjectSaver saver; - saver.addObject(this); - file.open("data/" + m_name + ".pack", std::ios_base::out); - saver.saveAscii(file); - file.close(); + try{ + std::ofstream outputStream("data/"+m_name+".pack"); + cereal::JSONOutputArchive output(outputStream); + ResourcePack tmp; + tmp.m_name = this->m_name; + tmp.m_resources = this->m_resources; + output(tmp); + }catch(cereal::Exception e){ + std::cerr << "Error while saving resource pack : " << e.what() << std::endl; + } } if(ImGui::Button("Load resource pack")) { - std::fstream file; - file.open("data/" + m_name + ".pack", std::ios_base::in); - ObjectLoader loader; - loader.loadAscii(file); - file.close(); - const std::vector& packVec = loader.getObjects(); - for(ResourceInterface* res : m_resources) - delete res; - m_resources = packVec[0]->m_resources; - delete packVec[0]; + std::ifstream inputStream("data/"+m_name+".pack"); + cereal::JSONInputArchive input(inputStream); + ResourcePack tmp = ResourcePack(); + input(tmp); + this->m_name = tmp.m_name; + this->m_resources = tmp.m_resources; } if(ImGui::Button("Add a texture")) { - TextureResource* resource = new TextureResource(); + std::shared_ptr resource(new TextureResource()); m_resources.push_back(resource); } if(ImGui::Button("Add a material")) { - MaterialResource* resource = new MaterialResource(); + std::shared_ptr resource(new MaterialResource()); +// MaterialResource* resource = new MaterialResource(); m_resources.push_back(resource); } - for(ResourceInterface* res : m_resources) + for(std::shared_ptr res : m_resources) { if(ImGui::Button(res->m_name.c_str())) currentResource = res; @@ -85,7 +82,7 @@ if(m_resources[i] == currentResource) { m_resources.erase(m_resources.begin() + i); - delete currentResource; + //delete currentResource; currentResource = nullptr; } } @@ -94,11 +91,9 @@ if(!opened) currentResource = nullptr; } -}*/ +} -//INIT_SERIALIZABLE(TextureResource) - -/*TextureResource::TextureResource() : +TextureResource::TextureResource() : m_texture(nullptr) { m_name = "new texture"; @@ -111,35 +106,35 @@ TextureResource::~TextureResource() { destroy(); -}*/ +} -/* void TextureResource::load(float & progress) +void TextureResource::loadResource(float &progress) { progress = 0; - Image* img = Loader::loadImage(m_path, m_bitsPerPixel, m_isVerticallyReversed); + Image* img = Loader::loadImage(m_path,m_bitsPerPixel,m_isVerticallyReversed); progress = 0.5; - if(img == nullptr) + if (img == nullptr) return; - m_texture = new Texture(img, m_needsMipMaps); - RESOURCE_ADD(m_texture, Texture, m_name); + m_texture = new Texture(img,m_needsMipMaps); + RESOURCE_ADD(m_texture,Texture,m_name); delete img; progress = 1; } -*/ -/* void TextureResource::destroy() +void TextureResource::destroy() { if(m_texture != nullptr) { delete m_texture; m_texture = nullptr; - RESOURCE_REMOVE(Texture, m_name); + RESOURCE_REMOVE(Texture,m_name); } -}*/ +} -/*void TextureResource::gui() +void TextureResource::gui() { char buf[1024] = {0}; + // m_name = ResourceInterface::m_name; strcpy(buf, m_path.c_str()); if(ImGui::InputText("Image file", buf, 1024)) m_path = buf; @@ -151,15 +146,12 @@ TextureResource::~TextureResource() { float temp; if(m_texture == nullptr) - load(temp); + loadResource(temp); else destroy(); } -}*/ +} -//INIT_SERIALIZABLE(MaterialResource) - -/* MaterialResource::MaterialResource() : m_material(nullptr) { @@ -176,7 +168,7 @@ MaterialResource::~MaterialResource() destroy(); } -void MaterialResource::load(float & progress) +void MaterialResource::loadResource(float &progress) { m_material = new PBRMaterial(); m_material->albedo = m_albedo; @@ -200,11 +192,11 @@ void MaterialResource::destroy() { delete m_material; m_material = nullptr; - RESOURCE_REMOVE(PBRMaterial, m_name); + RESOURCE_REMOVE(PBRMaterial,m_name); } } void MaterialResource::gui() { ImGui::Text("TODO"); -}*/ +} diff --git a/src/tools/resourcepack.h b/src/tools/resourcepack.h index dd8f625..8d2c1ba 100644 --- a/src/tools/resourcepack.h +++ b/src/tools/resourcepack.h @@ -1,50 +1,66 @@ #ifndef RESOURCEPACK_H #define RESOURCEPACK_H -#include +#include +#include + +#include +#include +#include +#include class Texture; class PBRMaterial; +class Mesh; /** * @brief The ResourceInterface struct holds a resource, it handles its loading and its destruction - **/ -/*struct ResourceInterface : public Serializable + */ +struct ResourceInterface { - P_STRING(m_name) + std::string m_name; - virtual void load(float & progress) = 0; + template + void serialize(Archive & archive){ + archive(CEREAL_NVP(m_name)); + } + + virtual void loadResource(float & progress) = 0; virtual void destroy() = 0; - virtual void gui() {} -};*/ + virtual void gui() = 0; +}; /** * @brief The ResourcePack class is just a container of LoadTask, it only exists for serialization purposes */ -/*struct ResourcePack : public Serializable +struct ResourcePack { - P_STRING(m_name) - P_VECTOR_REFERENCE(ResourceInterface, m_resources) + std::string m_name; + std::vector> m_resources; ResourcePack() {} void gui(); - SERIALIZABLE(ResourcePack, CAST(m_name), CAST(m_resources)) -};*/ + template + void serialize(Archive & archive){ + archive(cereal::make_nvp("ResourcePack",m_name),CEREAL_NVP(m_resources)); + } +}; -// -// Some LoadTask implementations -// +/* + * Some LoadTask implementations + */ -/*class TextureResource : public ResourceInterface +class TextureResource : public ResourceInterface { - P_STRING(m_path) - P_INT(m_bitsPerPixel) - P_BOOL(m_isVerticallyReversed) - P_BOOL(m_needsMipMaps) + // std::string m_name; + std::string m_path; + int m_bitsPerPixel; + bool m_isVerticallyReversed; + bool m_needsMipMaps; Texture* m_texture; @@ -52,28 +68,38 @@ public: TextureResource(); virtual ~TextureResource(); - SERIALIZABLE(TextureResource, CAST(m_name), CAST(m_path), CAST(m_bitsPerPixel), CAST(m_isVerticallyReversed), CAST(m_needsMipMaps)) + template + void serialize(Archive & archive) + { + archive(CEREAL_NVP(m_name), + CEREAL_NVP(m_path), + CEREAL_NVP(m_bitsPerPixel), + CEREAL_NVP(m_isVerticallyReversed), + CEREAL_NVP(m_needsMipMaps)); + } - void load(float & progress); + void loadResource(float & progress); void destroy(); - void gui(); -};*/ +}; -/*class MaterialResource : public ResourceInterface +CEREAL_REGISTER_TYPE(TextureResource) +CEREAL_REGISTER_POLYMORPHIC_RELATION(ResourceInterface,TextureResource) + +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) + glm::vec3 m_albedo; + float m_metallic; + float m_roughness; + glm::vec3 m_emission; + float m_opacity; + std::string m_albedoTexture; + std::string m_metallicTexture; + std::string m_roughnessTexture; + std::string m_emissionTexture; + std::string m_normalTexture; + std::string m_alphaMaskTexture; PBRMaterial* m_material; @@ -81,25 +107,72 @@ 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)) + template + void serialize(Archive & archive) + { + archive(CEREAL_NVP(m_name), + CEREAL_NVP(m_albedo), + CEREAL_NVP(m_metallic), + CEREAL_NVP(m_roughness), + CEREAL_NVP(m_emission), + CEREAL_NVP(m_opacity), + CEREAL_NVP(m_albedoTexture), + CEREAL_NVP(m_metallicTexture), + CEREAL_NVP(m_roughnessTexture), + CEREAL_NVP(m_emissionTexture), + CEREAL_NVP(m_normalTexture), + CEREAL_NVP(m_alphaMaskTexture)); + } - void load(float & progress); + void loadResource(float & progress); void destroy(); void gui(); -};*/ +}; + +template +void serialize(Archive & archive,MaterialResource mat) +{ + archive(CEREAL_NVP(mat.m_name), + CEREAL_NVP(mat.m_albedo), + CEREAL_NVP(mat.m_metallic), + CEREAL_NVP(mat.m_roughness), + CEREAL_NVP(mat.m_emission), + CEREAL_NVP(mat.m_opacity), + CEREAL_NVP(mat.m_albedoTexture), + CEREAL_NVP(mat.m_metallicTexture), + CEREAL_NVP(mat.m_roughnessTexture), + CEREAL_NVP(mat.m_emissionTexture), + CEREAL_NVP(mat.m_normalTexture), + CEREAL_NVP(mat.m_alphaMaskTexture)); +} + +CEREAL_REGISTER_TYPE(MaterialResource) +CEREAL_REGISTER_POLYMORPHIC_RELATION(ResourceInterface,MaterialResource) + +class MeshResource : public ResourceInterface +{ + //add metadata for mesh + + Mesh* m_mesh; +public: + MeshResource(); + virtual ~MeshResource(); + + + // differentiate load and save ? + template + void serialize(Archive & archive) + { + + } + + void loadResource(float & progress); + + void destroy(); + + void gui(); +}; #endif // RESOURCEPACK_H