From 6984653b6484f2f658ffbf1917957f9f45ea457d Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Fri, 4 Nov 2022 17:18:40 +0800 Subject: [PATCH] Renamed and restructured model asset members Added mesh asset vector for model asset to contain --- .../src/Assets/Asset Types/SHModelAsset.h | 25 ++++++++-------- .../Libraries/Loaders/SHModelLoader.cpp | 30 ++++++++++--------- .../Assets/Libraries/Loaders/SHModelLoader.h | 12 ++++++-- .../src/Resource/SHResourceManager.hpp | 12 ++++---- 4 files changed, 44 insertions(+), 35 deletions(-) diff --git a/SHADE_Engine/src/Assets/Asset Types/SHModelAsset.h b/SHADE_Engine/src/Assets/Asset Types/SHModelAsset.h index 45b06198..88a0ab9e 100644 --- a/SHADE_Engine/src/Assets/Asset Types/SHModelAsset.h +++ b/SHADE_Engine/src/Assets/Asset Types/SHModelAsset.h @@ -18,21 +18,11 @@ namespace SHADE { - struct SHMeshDataHeader + struct SHMeshAssetHeader { uint32_t vertexCount; uint32_t indexCount; - uint32_t charCount; - }; - - struct SHMeshData - { std::string name; - std::vector vertexPositions; - std::vector vertexTangents; - std::vector vertexNormals; - std::vector vertexTexCoords; - std::vector indices; }; struct SHModelAssetHeader @@ -40,10 +30,19 @@ namespace SHADE size_t meshCount; }; + struct SH_API SHMeshData : SHAssetData + { + SHMeshAssetHeader header; + std::vector vertexPositions; + std::vector vertexTangents; + std::vector vertexNormals; + std::vector vertexTexCoords; + std::vector indices; + }; + struct SH_API SHModelAsset : SHAssetData { SHModelAssetHeader header; - std::vector headers; - std::vector meshes; + std::vector subMeshes; }; } \ No newline at end of file diff --git a/SHADE_Engine/src/Assets/Libraries/Loaders/SHModelLoader.cpp b/SHADE_Engine/src/Assets/Libraries/Loaders/SHModelLoader.cpp index 57d92b57..4a63a715 100644 --- a/SHADE_Engine/src/Assets/Libraries/Loaders/SHModelLoader.cpp +++ b/SHADE_Engine/src/Assets/Libraries/Loaders/SHModelLoader.cpp @@ -16,38 +16,39 @@ namespace SHADE { - void SHModelLoader::ReadHeader(std::ifstream& file, SHMeshDataHeader& header) noexcept + void SHModelLoader::ReadHeader(std::ifstream& file, SHMeshLoaderHeader& header) noexcept { file.read( reinterpret_cast(&header), - sizeof(SHMeshDataHeader) + sizeof(SHMeshLoaderHeader) ); } - void SHModelLoader::ReadData(std::ifstream& file, SHMeshDataHeader const& header, SHMeshData& data) noexcept + void SHModelLoader::ReadData(std::ifstream& file, SHMeshLoaderHeader const& header, SHMeshData& data) noexcept { - auto const vertexVec3Byte{ sizeof(SHVec3) * header.vertexCount }; - auto const vertexVec2Byte{ sizeof(SHVec2) * header.indexCount }; + auto const vertexVec2Byte{ sizeof(SHVec2) * header.vertexCount }; data.vertexPositions.resize(header.vertexCount); data.vertexTangents.resize(header.vertexCount); data.vertexNormals.resize(header.vertexCount); data.vertexTexCoords.resize(header.vertexCount); data.indices.resize(header.indexCount); + data.header.name.resize(header.charCount); - file.read(data.name.data(), header.charCount); + file.read(data.header.name.data(), header.charCount); file.read(reinterpret_cast(data.vertexPositions.data()), vertexVec3Byte); file.read(reinterpret_cast(data.vertexTangents.data()), vertexVec3Byte); file.read(reinterpret_cast(data.vertexNormals.data()), vertexVec3Byte); file.read(reinterpret_cast(data.vertexTexCoords.data()), vertexVec2Byte); file.read(reinterpret_cast(data.indices.data()), sizeof(uint32_t) * header.indexCount); - + data.header.vertexCount = header.vertexCount; + data.header.indexCount = header.indexCount; } - void SHModelLoader::LoadSHMesh(AssetPath path, SHModelAsset& model) noexcept - { + void SHModelLoader::LoadSHMesh(AssetPath path, SHModelAsset& model) noexcept + { std::ifstream file{ path.string(), std::ios::in | std::ios::binary }; if (!file.is_open()) { @@ -58,17 +59,18 @@ namespace SHADE file.seekg(0); file.read( - reinterpret_cast(&model.header), + reinterpret_cast(&model.header), sizeof(SHModelAssetHeader) ); - model.headers.resize(model.header.meshCount); - model.meshes.resize(model.header.meshCount); + std::vector headers(model.header.meshCount); + model.subMeshes.resize(model.header.meshCount); for (auto i{ 0 }; i < model.header.meshCount; ++i) { - ReadHeader(file, model.headers[i]); - ReadData(file, model.headers[i], model.meshes[i]); + model.subMeshes[i] = new SHMeshData(); + ReadHeader(file, headers[i]); + ReadData(file, headers[i], *model.subMeshes[i]); } file.close(); } diff --git a/SHADE_Engine/src/Assets/Libraries/Loaders/SHModelLoader.h b/SHADE_Engine/src/Assets/Libraries/Loaders/SHModelLoader.h index 688e628e..35dc4514 100644 --- a/SHADE_Engine/src/Assets/Libraries/Loaders/SHModelLoader.h +++ b/SHADE_Engine/src/Assets/Libraries/Loaders/SHModelLoader.h @@ -18,8 +18,16 @@ namespace SHADE { class SHModelLoader : public SHAssetLoader { - void ReadHeader(std::ifstream& file, SHMeshDataHeader& header) noexcept; - void ReadData(std::ifstream& file, SHMeshDataHeader const& header, SHMeshData& data) noexcept; + struct SHMeshLoaderHeader + { + uint32_t vertexCount; + uint32_t indexCount; + uint32_t charCount; + }; + + + void ReadHeader(std::ifstream& file, SHMeshLoaderHeader& header) noexcept; + void ReadData(std::ifstream& file, SHMeshLoaderHeader const& header, SHMeshData& data) noexcept; public: void LoadSHMesh(AssetPath path, SHModelAsset& model) noexcept; diff --git a/SHADE_Engine/src/Resource/SHResourceManager.hpp b/SHADE_Engine/src/Resource/SHResourceManager.hpp index d078ce48..1fba71b9 100644 --- a/SHADE_Engine/src/Resource/SHResourceManager.hpp +++ b/SHADE_Engine/src/Resource/SHResourceManager.hpp @@ -49,7 +49,7 @@ namespace SHADE throw std::runtime_error("[SHResourceManager] Attempted to load graphics resource without a SHGraphicsSystem installed."); // Load - const SHModelAsset* assetData = SHAssetManager::GetData(assetId); + const SHMeshData* assetData = SHAssetManager::GetData(assetId); if (assetData == nullptr) { SHLog::Warning("[SHResourceManager] Attempted to load an asset with an invalid Asset ID."); @@ -59,11 +59,11 @@ namespace SHADE Handle meshHandle = gfxSystem->AddMesh ( - assetData->vertexPosition.size(), - assetData->vertexPosition.data(), - assetData->texCoords.data(), - assetData->vertexTangent.data(), - assetData->vertexNormal.data(), + assetData->vertexPositions.size(), + assetData->vertexPositions.data(), + assetData->vertexTexCoords.data(), + assetData->vertexTangents.data(), + assetData->vertexNormals.data(), assetData->indices.size(), assetData->indices.data() );