Renamed and restructured model asset members

Added mesh asset vector for model asset to contain
This commit is contained in:
Xiao Qi 2022-11-04 17:18:40 +08:00
parent 608b204f88
commit 6984653b64
4 changed files with 44 additions and 35 deletions

View File

@ -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<SHVec3> vertexPositions;
std::vector<SHVec3> vertexTangents;
std::vector<SHVec3> vertexNormals;
std::vector<SHVec2> vertexTexCoords;
std::vector<uint32_t> indices;
};
struct SHModelAssetHeader
@ -40,10 +30,19 @@ namespace SHADE
size_t meshCount;
};
struct SH_API SHMeshData : SHAssetData
{
SHMeshAssetHeader header;
std::vector<SHVec3> vertexPositions;
std::vector<SHVec3> vertexTangents;
std::vector<SHVec3> vertexNormals;
std::vector<SHVec2> vertexTexCoords;
std::vector<uint32_t> indices;
};
struct SH_API SHModelAsset : SHAssetData
{
SHModelAssetHeader header;
std::vector<SHMeshDataHeader> headers;
std::vector<SHMeshData> meshes;
std::vector<SHMeshData*> subMeshes;
};
}

View File

@ -16,34 +16,35 @@
namespace SHADE
{
void SHModelLoader::ReadHeader(std::ifstream& file, SHMeshDataHeader& header) noexcept
void SHModelLoader::ReadHeader(std::ifstream& file, SHMeshLoaderHeader& header) noexcept
{
file.read(
reinterpret_cast<char*>(&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<char*>(data.vertexPositions.data()), vertexVec3Byte);
file.read(reinterpret_cast<char*>(data.vertexTangents.data()), vertexVec3Byte);
file.read(reinterpret_cast<char*>(data.vertexNormals.data()), vertexVec3Byte);
file.read(reinterpret_cast<char*>(data.vertexTexCoords.data()), vertexVec2Byte);
file.read(reinterpret_cast<char*>(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
@ -58,17 +59,18 @@ namespace SHADE
file.seekg(0);
file.read(
reinterpret_cast<char *>(&model.header),
reinterpret_cast<char*>(&model.header),
sizeof(SHModelAssetHeader)
);
model.headers.resize(model.header.meshCount);
model.meshes.resize(model.header.meshCount);
std::vector<SHMeshLoaderHeader> 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();
}

View File

@ -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;

View File

@ -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<SHModelAsset>(assetId);
const SHMeshData* assetData = SHAssetManager::GetData<SHMeshData>(assetId);
if (assetData == nullptr)
{
SHLog::Warning("[SHResourceManager] Attempted to load an asset with an invalid Asset ID.");
@ -59,11 +59,11 @@ namespace SHADE
Handle<SHMesh> 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()
);