Importing mesh asset now functioning

- Added debug logging for all user actions regarding FileDialog
- Importing a Meshasset wil now Create a propery entity for the asset.
This commit is contained in:
Nigel Barink 2022-12-27 21:16:56 +01:00
parent 3fa5455b43
commit 2a5c7811e7
2 changed files with 26 additions and 11 deletions

View File

@ -41,7 +41,7 @@ void AssetManager::setAssetPath(std::filesystem::path path)
YoggieEngine::Mesh* AssetManager::LoadFromAssetFile(const std::filesystem::path assetPath)
{
YoggieEngine::Mesh imported;
YoggieEngine::Mesh* imported = nullptr;
std::ifstream AssetFile;
AssetFile.open(assetPath, std::ios::binary);
@ -65,14 +65,16 @@ YoggieEngine::Mesh* AssetManager::LoadFromAssetFile(const std::filesystem::path
std::cout << "Number of Elements: " << Enum << std::endl;
free(Header);
imported = new YoggieEngine::Mesh();
// Load Vertices (Vertex + UV )
imported.vertices = std::vector < YoggieEngine::Vertex>();
imported->vertices = std::vector < YoggieEngine::Vertex>();
for (int i = 0; i < Vnum; i++)
{
YoggieEngine::Vertex data = YoggieEngine::Vertex();
AssetFile.read((char*)&data, Vsize);
imported.vertices.push_back(data);
imported->vertices.push_back(data);
}
@ -80,11 +82,11 @@ YoggieEngine::Mesh* AssetManager::LoadFromAssetFile(const std::filesystem::path
AssetFile.ignore(sizeof(char) * 3);
// Load Elements
imported.elements = std::vector<unsigned int>();
imported->elements = std::vector<unsigned int>();
for (int i = 0; i < Enum; i++) {
unsigned int data = 0;
AssetFile.read((char*)&data, sizeof(unsigned int));
imported.elements.push_back(data);
imported->elements.push_back(data);
}
}
@ -92,8 +94,7 @@ YoggieEngine::Mesh* AssetManager::LoadFromAssetFile(const std::filesystem::path
std::cout << "Failed ot open mesh " << std::endl;
}
return nullptr;
return imported;
}
@ -107,7 +108,7 @@ YoggieEngine::Renderable* AssetManager::LoadFromSource(const std::filesystem::pa
std::cout << "Save path: " << MeshFileName << std::endl;
std::ofstream meshAsset;
meshAsset.open(MeshFileName.c_str(), std::ios::binary);
meshAsset.open(MeshFileName, std::ios::binary);
if (meshAsset.is_open()) {
// write a header
@ -123,7 +124,7 @@ YoggieEngine::Renderable* AssetManager::LoadFromSource(const std::filesystem::pa
meshAsset.write((char*)&Vnum, sizeof(uint32_t));
meshAsset.write((char*)&Enum, sizeof(uint32_t));
// write all vertices
for (auto vertice : exportMesh->vertices)
for (auto& vertice : exportMesh->vertices)
{
meshAsset.write((char*)&vertice, sizeof(vertice));
}

View File

@ -122,13 +122,27 @@ public:
if (ImGui::MenuItem("Import MeshAsset (temp)"))
{
auto result = NFD_OpenDialog("mesh", NULL, &path);
switch (result) {
case(NFD_OKAY):
AssetManager::LoadFromAssetFile(path);
{
YoggieEngine::Mesh* importedMesh = AssetManager::LoadFromAssetFile(path);
if (importedMesh != nullptr)
{
auto full_name = std::filesystem::path(path);
auto importedModel = scene.AddEntity(full_name.filename().u8string());
auto& rendererComponent = importedModel.AddComponent<Render3DComponent>();
rendererComponent.mesh = *importedMesh;
}
}
break;
case(NFD_CANCEL):
spdlog::debug("User cancelled action");
break;
case(NFD_ERROR):
spdlog::warn("Something went wrong!");
break;
}