migrate resourcepack to use with cereal + small fix for config serialization

This commit is contained in:
Lendemor 2018-05-30 00:39:15 +02:00
parent f2ecf2b9cc
commit f8ddb91c8a
10 changed files with 433 additions and 53 deletions

View File

@ -3,7 +3,7 @@
#include "engine.h" #include "engine.h"
#include "tools/scenepicker.h" #include "tools/scenepicker.h"
#include "scene/scenetree.h" #include "scene/scenetree.h"
#include "tools/resourcepack.h" #include "tools/newresourcepack.h"
#include "editor/objecteditor.h" #include "editor/objecteditor.h"
#include "sparrowshell/sparrowshell.h" #include "sparrowshell/sparrowshell.h"
@ -185,7 +185,7 @@ void Editor::toggleRenderingPipelineGui()
void Editor::toggleResourcePackGui() void Editor::toggleResourcePackGui()
{ {
if(m_editedResourcePack == nullptr) if(m_editedResourcePack == nullptr)
m_editedResourcePack = new ResourcePack(); m_editedResourcePack = new NewResourcePack();
else else
{ {
delete m_editedResourcePack; delete m_editedResourcePack;

View File

@ -5,7 +5,7 @@
class Engine; class Engine;
class ScenePicker; class ScenePicker;
class ResourcePack; class NewResourcePack;
class ObjectEditor; class ObjectEditor;
class Mesh; class Mesh;
@ -13,7 +13,7 @@ class Editor : public SceneNode
{ {
ScenePicker* m_pickerNode; ScenePicker* m_pickerNode;
Mesh* m_selectedMesh; Mesh* m_selectedMesh;
ResourcePack* m_editedResourcePack; NewResourcePack* m_editedResourcePack;
ObjectEditor* m_objectEditor; ObjectEditor* m_objectEditor;
bool m_materialEditorEnabled; bool m_materialEditorEnabled;
bool m_editorEnabled; bool m_editorEnabled;

View File

@ -5,6 +5,8 @@
#include "LinearMath/btMotionState.h" #include "LinearMath/btMotionState.h"
#include <SparrowRenderer/scene.h> #include <SparrowRenderer/scene.h>
#include <cereal/archives/binary.hpp>
class SceneTree; class SceneTree;
class Transform; class Transform;
class Engine; class Engine;
@ -159,6 +161,13 @@ public:
/// the scene node clones itself, with no parent /// the scene node clones itself, with no parent
virtual SceneNode* clone(); virtual SceneNode* clone();
//serialization methods
template <class Archive>
void serialize(Archive & archive)
{
archive(m_transform);
}
// global engine methods // global engine methods
Engine& getEngine(); Engine& getEngine();
}; };

View File

@ -173,17 +173,16 @@ struct Config
void save() void save()
{ {
std::fstream configFile; // std::fstream configFile;
configFile.open("config.ini", std::ios_base::out); // configFile.open("config.ini", std::ios_base::out);
std::ofstream configFile("config.ini");
cereal::JSONOutputArchive output(configFile); cereal::JSONOutputArchive output(configFile);
output(cereal::make_nvp("mode", mode));
output(cereal::make_nvp("scene", scene)); output(cereal::make_nvp("mode", mode),
output(cereal::make_nvp("vsync", vsync)); cereal::make_nvp("scene", scene),
output(cereal::make_nvp("width", width)); cereal::make_nvp("vsync", vsync),
output(cereal::make_nvp("height", height)); cereal::make_nvp("width", width),
output.finishNode(); cereal::make_nvp("height", height));
configFile << std::endl;
configFile.close();
} }
void load() void load()
@ -195,11 +194,11 @@ struct Config
try try
{ {
cereal::JSONInputArchive input(configFile); cereal::JSONInputArchive input(configFile);
input(cereal::make_nvp("mode", mode)); input(cereal::make_nvp("mode", mode),
input(cereal::make_nvp("scene", scene)); cereal::make_nvp("scene", scene),
input(cereal::make_nvp("vsync", vsync)); cereal::make_nvp("vsync", vsync),
input(cereal::make_nvp("width", width)); cereal::make_nvp("width", width),
input(cereal::make_nvp("height", height)); cereal::make_nvp("height", height));
} }
catch(cereal::Exception e) catch(cereal::Exception e)
{ {

View File

@ -6,7 +6,9 @@
#include <fstream> #include <fstream>
#include <cstdio> #include <cstdio>
#include <SparrowSerializer/serializationmanager.h> //#include <SparrowSerializer/serializationmanager.h>
#include <cereal/archives/json.hpp>
#include <cereal/types/array.hpp>
#include <SparrowRenderer/opengl.h> #include <SparrowRenderer/opengl.h>
@ -57,9 +59,9 @@ void LoadingThread::processTasks()
sf::sleep(sf::milliseconds(500)); // wait for half a second sf::sleep(sf::milliseconds(500)); // wait for half a second
else else
{ {
ResourceInterface* res = m_resourceQueue.front(); std::shared_ptr<ResourceInterface> res = m_resourceQueue.front();
m_resourceQueue.pop_front(); m_resourceQueue.pop_front();
res->load(m_taskProgress); res->loadResource(m_taskProgress);
glFlush(); glFlush();
if(m_taskProgress < 1.0) if(m_taskProgress < 1.0)
fprintf(stderr, "failed to process the following task : %s\n", res->m_name.c_str()); fprintf(stderr, "failed to process the following task : %s\n", res->m_name.c_str());
@ -80,13 +82,13 @@ const std::string& LoadingThread::getCurrentTaskName()
void LoadingThread::loadResourcePack(const std::string& name) void LoadingThread::loadResourcePack(const std::string& name)
{ {
std::fstream file; /* std::fstream file;
file.open("data/" + name + ".pack", std::ios_base::in); file.open("data/" + name + ".pack", std::ios_base::in);
ObjectLoader loader; ObjectLoader loader;
loader.loadAscii(file); loader.loadAscii(file);
file.close(); file.close();
const std::vector<ResourcePack*>& packVec = loader.getObjects<ResourcePack>(); const std::vector<NewResourcePack*>& packVec = loader.getObjects<NewResourcePack>();
ResourcePack* pack = packVec[0]; NewResourcePack* pack = packVec[0];
m_packs[name] = pack; m_packs[name] = pack;
for(ResourceInterface* res : pack->m_resources) for(ResourceInterface* res : pack->m_resources)
{ {
@ -94,18 +96,29 @@ void LoadingThread::loadResourcePack(const std::string& name)
m_resourceQueue.push_back(res); m_resourceQueue.push_back(res);
else else
delete res; delete res;
}*/
std::ifstream inputStream("data/" + name + ".pack");
cereal::JSONInputArchive input(inputStream);
NewResourcePack pack;
// input(pack);
for(std::shared_ptr<ResourceInterface> 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) void LoadingThread::freeResourcePack(const std::string& name)
{ {
ResourcePack* pack = m_packs[name]; NewResourcePack* pack = m_packs[name];
for(ResourceInterface* res : pack->m_resources) for(std::shared_ptr<ResourceInterface> res : pack->m_resources)
{ {
if((m_references[res->m_name] -= 1) == 0) if((m_references[res->m_name] -= 1) == 0)
{ {
m_references.erase(res->m_name); m_references.erase(res->m_name);
delete res; //delete res;
} }
} }
m_packs.erase(name); m_packs.erase(name);

View File

@ -5,15 +5,15 @@
#include <deque> #include <deque>
#include <thread> #include <thread>
#include "resourcepack.h" #include "newresourcepack.h"
class LoadingThread class LoadingThread
{ {
static LoadingThread* singleton; static LoadingThread* singleton;
static std::thread* thread; static std::thread* thread;
std::deque<ResourceInterface*> m_resourceQueue; std::deque<std::shared_ptr<ResourceInterface>> m_resourceQueue;
std::unordered_map<std::string, ResourcePack*> m_packs; std::unordered_map<std::string, NewResourcePack*> m_packs;
std::unordered_map<std::string, int> m_references; std::unordered_map<std::string, int> m_references;
ResourceInterface* m_currentTask; ResourceInterface* m_currentTask;
bool m_running; bool m_running;

View File

@ -0,0 +1,218 @@
#include "newresourcepack.h"
#include "loader.h"
#include "resourcemanager.h"
#include <fstream>
#include <SparrowRenderer/texture.h>
#include <SparrowRenderer/image.h>
#include <SparrowRenderer/pbrmaterial.h>
#include <imgui/imgui.h>
#include <cereal/archives/json.hpp>
#include <cereal/types/memory.hpp>
#include <cereal/types/vector.hpp>
void NewResourcePack::gui()
{
static std::shared_ptr<ResourceInterface> 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"))
{
// std::fstream file;
// ObjectSaver saver;
// saver.addObject(this);
// file.open("data/" + m_name + ".pack", std::ios_base::out);
// saver.saveAscii(file);
//file.close();
std::ofstream outputStream("data/"+m_name+".pack");
cereal::JSONOutputArchive output(outputStream);
output(std::shared_ptr<NewResourcePack>(this));
outputStream.close();
}
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<ResourcePack*>& packVec = loader.getObjects<ResourcePack>();
// 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);
NewResourcePack tmp = NewResourcePack();
tmp.m_name = this->m_name;
tmp.m_resources = this->m_resources;
input(tmp);
inputStream.close();
}
if(ImGui::Button("Add a texture"))
{
std::shared_ptr<ResourceInterface> resource(new TextureResource());
m_resources.push_back(resource);
}
if(ImGui::Button("Add a material"))
{
std::shared_ptr<ResourceInterface> resource(new MaterialResource());
// MaterialResource* resource = new MaterialResource();
m_resources.push_back(resource);
}
for(std::shared_ptr<ResourceInterface> 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; i<m_resources.size(); ++i)
{
if(m_resources[i] == currentResource)
{
m_resources.erase(m_resources.begin() + i);
//delete currentResource;
currentResource = nullptr;
}
}
}
ImGui::End();
if(!opened)
currentResource = nullptr;
}
}
TextureResource::TextureResource() :
m_texture(nullptr)
{
m_name = "new texture";
m_path = "myTexture.jpg";
m_bitsPerPixel = 24;
m_isVerticallyReversed = true;
m_needsMipMaps = true;
}
TextureResource::~TextureResource()
{
destroy();
}
void TextureResource::loadResource(float &progress)
{
progress = 0;
Image* img = Loader::loadImage(m_path,m_bitsPerPixel,m_isVerticallyReversed);
progress = 0.5;
if (img == nullptr)
return;
m_texture = new Texture(img,m_needsMipMaps);
RESOURCE_ADD(m_texture,Texture,m_name);
delete img;
progress = 1;
}
void TextureResource::destroy()
{
if(m_texture != nullptr)
{
delete m_texture;
m_texture = nullptr;
RESOURCE_REMOVE(Texture,m_name);
}
}
void TextureResource::gui()
{
char buf[1024] = {0};
strcpy(buf, m_path.c_str());
if(ImGui::InputText("Image file", buf, 1024))
m_path = buf;
ImGui::InputInt("bits per pixel", &m_bitsPerPixel);
ImGui::Checkbox("is vertically reversed", &m_isVerticallyReversed);
ImGui::Checkbox("needs mipmaps", &m_needsMipMaps);
ImGui::Text(m_texture == nullptr ? "Texture not loaded" : "Texture loaded");
if(ImGui::Button(m_texture == nullptr ? "Load" : "Destroy"))
{
float temp;
if(m_texture == nullptr)
loadResource(temp);
else
destroy();
}
}
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::loadResource(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");
}

139
src/tools/newresourcepack.h Normal file
View File

@ -0,0 +1,139 @@
#ifndef NEWRESOURCEPACK_H
#define NEWRESOURCEPACK_H
#include <string>
#include <glm/vec3.hpp>
#include <cereal/archives/json.hpp>
#include <cereal/types/array.hpp>
#include <cereal/types/memory.hpp>
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<class Archive>
void serialize(Archive & archive){
archive(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<std::shared_ptr<ResourceInterface>> m_resources;
NewResourcePack() {}
void gui();
template<class Archive>
void serialize(Archive & archive){
archive(m_name,CEREAL_NVP(m_resources));
}
};
/*template<class Archive>
void serialize(Archive & archive, std::shared_ptr<NewResourcePack> respack)
{
archive(respack);
}*/
/*
* 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 <class Archive>
void serialize(Archive & archive)
{
archive(cereal::base_class<ResourceInterface>(this),
m_path,
m_bitsPerPixel,
m_isVerticallyReversed,
m_needsMipMaps);
}
void loadResource(float & progress);
void destroy();
void gui();
};
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<class Archive>
void serialize(Archive & archive)
{
archive(cereal::base_class<ResourceInterface>(this),
m_albedo,
m_metallic,
m_roughness,
m_emission,
m_opacity,
m_albedoTexture,
m_metallicTexture,
m_roughnessTexture,
m_emissionTexture,
m_normalTexture,
m_alphaMaskTexture);
}
void loadResource(float & progress);
void destroy();
void gui();
};
//CEREAL_REGISTER_TYPE(TextureResource)
//CEREAL_REGISTER_TYPE(MaterialResource)
#endif // NEWRESOURCEPACK_H

View File

@ -13,9 +13,9 @@
#include <imgui/imgui.h> #include <imgui/imgui.h>
INIT_SERIALIZABLE(ResourcePack) //INIT_SERIALIZABLE(ResourcePack)
void ResourcePack::gui() /*void ResourcePack::gui()
{ {
static ResourceInterface* currentResource = nullptr; static ResourceInterface* currentResource = nullptr;
@ -94,11 +94,11 @@ void ResourcePack::gui()
if(!opened) if(!opened)
currentResource = nullptr; currentResource = nullptr;
} }
} }*/
INIT_SERIALIZABLE(TextureResource) //INIT_SERIALIZABLE(TextureResource)
TextureResource::TextureResource() : /*TextureResource::TextureResource() :
m_texture(nullptr) m_texture(nullptr)
{ {
m_name = "new texture"; m_name = "new texture";
@ -111,9 +111,9 @@ TextureResource::TextureResource() :
TextureResource::~TextureResource() TextureResource::~TextureResource()
{ {
destroy(); destroy();
} }*/
void TextureResource::load(float & progress) /* void TextureResource::load(float & progress)
{ {
progress = 0; progress = 0;
Image* img = Loader::loadImage(m_path, m_bitsPerPixel, m_isVerticallyReversed); Image* img = Loader::loadImage(m_path, m_bitsPerPixel, m_isVerticallyReversed);
@ -125,8 +125,9 @@ void TextureResource::load(float & progress)
delete img; delete img;
progress = 1; progress = 1;
} }
*/
void TextureResource::destroy() /* void TextureResource::destroy()
{ {
if(m_texture != nullptr) if(m_texture != nullptr)
{ {
@ -134,9 +135,9 @@ void TextureResource::destroy()
m_texture = nullptr; m_texture = nullptr;
RESOURCE_REMOVE(Texture, m_name); RESOURCE_REMOVE(Texture, m_name);
} }
} }*/
void TextureResource::gui() /*void TextureResource::gui()
{ {
char buf[1024] = {0}; char buf[1024] = {0};
strcpy(buf, m_path.c_str()); strcpy(buf, m_path.c_str());
@ -154,10 +155,11 @@ void TextureResource::gui()
else else
destroy(); destroy();
} }
} }*/
INIT_SERIALIZABLE(MaterialResource) //INIT_SERIALIZABLE(MaterialResource)
/*
MaterialResource::MaterialResource() : MaterialResource::MaterialResource() :
m_material(nullptr) m_material(nullptr)
{ {
@ -205,4 +207,4 @@ void MaterialResource::destroy()
void MaterialResource::gui() void MaterialResource::gui()
{ {
ImGui::Text("TODO"); ImGui::Text("TODO");
} }*/

View File

@ -8,8 +8,8 @@ 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
*/ **/
struct ResourceInterface : public Serializable /*struct ResourceInterface : public Serializable
{ {
P_STRING(m_name) P_STRING(m_name)
@ -18,12 +18,12 @@ struct ResourceInterface : public Serializable
virtual void destroy() = 0; virtual void destroy() = 0;
virtual void gui() {} virtual void gui() {}
}; };*/
/** /**
* @brief The ResourcePack class is just a container of LoadTask, it only exists for serialization purposes * @brief The ResourcePack class is just a container of LoadTask, it only exists for serialization purposes
*/ */
struct ResourcePack : public Serializable /*struct ResourcePack : public Serializable
{ {
P_STRING(m_name) P_STRING(m_name)
P_VECTOR_REFERENCE(ResourceInterface, m_resources) P_VECTOR_REFERENCE(ResourceInterface, m_resources)
@ -33,13 +33,13 @@ struct ResourcePack : public Serializable
void gui(); void gui();
SERIALIZABLE(ResourcePack, CAST(m_name), CAST(m_resources)) SERIALIZABLE(ResourcePack, CAST(m_name), CAST(m_resources))
}; };*/
// //
// Some LoadTask implementations // Some LoadTask implementations
// //
class TextureResource : public ResourceInterface /*class TextureResource : public ResourceInterface
{ {
P_STRING(m_path) P_STRING(m_path)
P_INT(m_bitsPerPixel) P_INT(m_bitsPerPixel)
@ -59,9 +59,9 @@ public:
void destroy(); void destroy();
void gui(); void gui();
}; };*/
class MaterialResource : public ResourceInterface /*class MaterialResource : public ResourceInterface
{ {
P_VEC3(m_albedo) P_VEC3(m_albedo)
P_FLOAT(m_metallic) P_FLOAT(m_metallic)
@ -100,6 +100,6 @@ public:
void destroy(); void destroy();
void gui(); void gui();
}; };*/
#endif // RESOURCEPACK_H #endif // RESOURCEPACK_H