206 lines
5.6 KiB
C++
206 lines
5.6 KiB
C++
#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"))
|
|
{
|
|
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<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};
|
|
// m_name = ResourceInterface::m_name;
|
|
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");
|
|
}
|