SP3-237 Mesh compile and load successfully. Although now path to load/compile is hardcoded. Flow will be done after MS1

This commit is contained in:
Xiao Qi 2022-10-01 13:12:37 +08:00
parent 8a3d3c8d4e
commit 5069a42319
7 changed files with 65 additions and 59 deletions

BIN
Assets/Cube.003.shmesh Normal file

Binary file not shown.

BIN
Assets/Cube.012.shmesh Normal file

Binary file not shown.

View File

@ -81,7 +81,8 @@ namespace Sandbox
SHADE::SHSystemManager::RegisterRoutine<SHADE::SHInputManagerSystem, SHADE::SHInputManagerSystem::InputManagerRoutine>();
//TODO: REMOVE AFTER PRESENTATION
SHADE::SHAssetManager::LoadDataTemp("../../Assets/racoon.gltf");
//SHADE::SHAssetManager::LoadDataTemp("../../Assets/racoon.gltf");
SHADE::SHAssetManager::LoadDataTemp("../../Assets/Cube.012.shmesh");
//SHADE::SHAssetManager::LoadDataTemp("../../Assets/RaccoonBag_Color_Ver4.dds");
SHADE::SHAssetManager::LoadDataTemp("../../Assets/RaccoonPreTexturedVer1_Base9.dds");
//TODO: REMOVE AFTER PRESENTATION

View File

@ -42,7 +42,7 @@ namespace Sandbox
std::vector<Handle<SHMesh>> handles;
for (auto const& mesh : meshes)
{
if (mesh.meshName == "Cube.012")
if (mesh.header.meshName == "Cube.012")
{
handles.push_back(graphicsSystem->AddMesh(
mesh.header.vertexCount,

View File

@ -18,7 +18,11 @@
void SHADE::SHMeshCompiler::CompileMeshBinary(SHMeshAsset const& asset, AssetPath path) noexcept
{
std::ofstream file{path, std::ios::out | std::ios::binary};
std::string newPath{ path.string() };
newPath = newPath.substr(0, newPath.find_last_of('/') + 1);
newPath += asset.header.meshName + MESH_EXTENSION;
std::ofstream file{ newPath, std::ios::out | std::ios::binary | std::ios::trunc };
if (!file.is_open())
{
SHLOG_ERROR("Unable to open file for writing mesh file: {}", path.string());
@ -34,17 +38,6 @@ void SHADE::SHMeshCompiler::CompileMeshBinary(SHMeshAsset const& asset, AssetPat
sizeof(uint32_t)
);
uint32_t charCount{static_cast<uint32_t>(asset.header.meshName.size())};
file.write(
reinterpret_cast<char const*>(&charCount),
sizeof(uint32_t)
);
file.write(
asset.header.meshName.c_str(),
asset.header.meshName.size()
);
auto const vertexVec3Byte {sizeof(SHVec3) * asset.header.vertexCount};
auto const vertexVec2Byte {sizeof(SHVec2) * asset.header.vertexCount};
@ -70,7 +63,7 @@ void SHADE::SHMeshCompiler::CompileMeshBinary(SHMeshAsset const& asset, AssetPat
file.write(
reinterpret_cast<char const*>(asset.indices.data()),
sizeof(uint32_t)
sizeof(uint32_t) * asset.header.indexCount
);
file.close();

View File

@ -19,7 +19,7 @@ namespace SHADE
{
Assimp::Importer SHMeshLoader::aiImporter;
void SHMeshLoader::ProcessNode(aiNode const& node, aiScene const& scene, std::vector<SHMeshAsset>& meshes)
void SHMeshLoader::ProcessNode(aiNode const& node, aiScene const& scene, std::vector<SHMeshAsset>& meshes) noexcept
{
for (size_t i {0}; i < node.mNumMeshes; ++i)
{
@ -33,7 +33,7 @@ namespace SHADE
}
}
SHMeshAsset SHMeshLoader::ProcessMesh(aiMesh const& mesh, aiScene const& scene)
SHMeshAsset SHMeshLoader::ProcessMesh(aiMesh const& mesh, aiScene const& scene) noexcept
{
(void)scene;
@ -43,8 +43,6 @@ namespace SHADE
.changed { false }
};
result.header.meshName = mesh.mName.C_Str();
for (size_t i{0}; i < mesh.mNumVertices; ++i)
{
// Vertex position
@ -95,6 +93,7 @@ namespace SHADE
result.header.vertexCount = result.vertexPosition.size();
result.header.indexCount = result.indices.size();
result.header.meshName = mesh.mName.C_Str();
return result;
}
@ -143,16 +142,21 @@ namespace SHADE
SHLOG_ERROR("Unable to open SHMesh File: {}", path.string());
}
uint32_t vertCount, indexCount, charCount;
std::string name{ path.filename().string() };
name = name.substr(0, name.find_last_of('.'));
file.seekg(0);
uint32_t vertCount, indexCount;
std::vector<SHVec3> vertPos, vertTan, vertNorm;
std::vector<SHVec2> texCoord;
std::vector<uint32_t> indices;
std::string name;
file.read(reinterpret_cast<char*>(&vertCount), sizeof(uint32_t));
file.read(reinterpret_cast<char*>(&indexCount), sizeof(uint32_t));
file >> vertCount;
file >> indexCount;
file >> charCount;
auto const vertexVec3Byte{ sizeof(SHVec3) * vertCount };
auto const vertexVec2Byte{ sizeof(SHVec2) * vertCount };
vertPos.resize(vertCount);
vertTan.resize(vertCount);
@ -160,43 +164,43 @@ namespace SHADE
texCoord.resize(vertCount);
indices.resize(indexCount);
name.reserve(charCount);
for (auto i{0}; i < charCount; ++i)
{
file >> name[i];
}
file.read(reinterpret_cast<char *>(vertPos.data()), vertexVec3Byte);
file.read(reinterpret_cast<char *>(vertTan.data()), vertexVec3Byte);
file.read(reinterpret_cast<char *>(vertNorm.data()), vertexVec3Byte);
file.read(reinterpret_cast<char *>(texCoord.data()), vertexVec2Byte);
file.read(reinterpret_cast<char *>(indices.data()), sizeof(uint32_t) * indexCount);
for (auto i{ 0 }; i < vertCount; ++i)
{
file >> vertPos[i].x;
file >> vertPos[i].y;
file >> vertPos[i].z;
}
//for (auto i{ 0 }; i < vertCount; ++i)
//{
// file >> vertPos[i].x;
// file >> vertPos[i].y;
// file >> vertPos[i].z;
//}
//
//for (auto i{ 0 }; i < vertCount; ++i)
//{
// file >> vertTan[i].x;
// file >> vertTan[i].y;
// file >> vertTan[i].z;
//}
for (auto i{ 0 }; i < vertCount; ++i)
{
file >> vertTan[i].x;
file >> vertTan[i].y;
file >> vertTan[i].z;
}
//for (auto i{ 0 }; i < vertCount; ++i)
//{
// file >> vertNorm[i].x;
// file >> vertNorm[i].y;
// file >> vertNorm[i].z;
//}
for (auto i{ 0 }; i < vertCount; ++i)
{
file >> vertNorm[i].x;
file >> vertNorm[i].y;
file >> vertNorm[i].z;
}
//for (auto i{ 0 }; i < vertCount; ++i)
//{
// file >> texCoord[i].x;
// file >> texCoord[i].y;
//}
for (auto i{ 0 }; i < vertCount; ++i)
{
file >> texCoord[i].x;
file >> texCoord[i].y;
}
for (auto i{ 0 }; i < indexCount; ++i)
{
file >> indices[i];
}
//for (auto i{ 0 }; i < indexCount; ++i)
//{
// file >> indices[i];
//}
mesh.compiled = true;
mesh.changed = false;
@ -214,7 +218,7 @@ namespace SHADE
file.close();
}
void SHMeshLoader::LoadMesh(std::vector<SHMeshAsset>& meshes, AssetPath path)
void SHMeshLoader::LoadMesh(std::vector<SHMeshAsset>& meshes, AssetPath path) noexcept
{
if (path.extension().string() == GLTF_EXTENSION)
{

View File

@ -17,6 +17,8 @@
#include "Libraries/SHMeshLoader.h"
#include "Libraries/SHTextureLoader.h"
#include "Libraries/SHMeshCompiler.h"
namespace SHADE
{
FMOD::System* SHAssetManager::audioSystem;
@ -200,7 +202,8 @@ namespace SHADE
AssetPath path{ p };
if (path.extension().string() == FBX_EXTENSION
|| path.extension().string() == GLTF_EXTENSION)
|| path.extension().string() == GLTF_EXTENSION
|| path.extension().string() == MESH_EXTENSION)
{
LoadGLTF(
{
@ -301,6 +304,11 @@ namespace SHADE
for (auto const& mesh : meshes)
{
meshCollection.emplace(GenerateAssetID(AssetType::MESH), mesh);
if (!mesh.compiled)
{
SHMeshCompiler::CompileMeshBinary(mesh, asset.path);
}
}
}