Some refactoring to make writing to file slight more efficient and organised

This commit is contained in:
Xiao Qi 2023-01-07 16:32:47 +08:00
parent d54222e2cf
commit 5d6cdc7fd3
3 changed files with 94 additions and 108 deletions

View File

@ -309,8 +309,6 @@ namespace SH_COMP
rig.header.charCounts.push_back(newPair.first->name.length()); rig.header.charCounts.push_back(newPair.first->name.length());
} }
} }
std::cout << "Done\n";
} }
void MeshCompiler::LoadAndCompile(AssetPath path) noexcept void MeshCompiler::LoadAndCompile(AssetPath path) noexcept

View File

@ -15,16 +15,14 @@
namespace SH_COMP namespace SH_COMP
{ {
void MeshWriter::WriteMeshHeader(std::ofstream& file, MeshDataHeader const& header) void MeshWriter::WriteMeshData(FileReference file, std::vector<MeshDataHeader> const& headers,
std::vector<MeshData> const& meshes)
{ {
file.write( for (auto i {0}; i < headers.size(); ++i)
reinterpret_cast<char const*>(&header), {
sizeof(MeshDataHeader) auto const& header = headers[i];
); auto const& asset = meshes[i];
}
void MeshWriter::WriteMeshData(std::ofstream& file, MeshDataHeader const& header, MeshData const& asset)
{
auto const vertexVec3Byte{ sizeof(SHVec3) * header.vertexCount }; auto const vertexVec3Byte{ sizeof(SHVec3) * header.vertexCount };
auto const vertexVec2Byte{ sizeof(SHVec2) * header.vertexCount }; auto const vertexVec2Byte{ sizeof(SHVec2) * header.vertexCount };
@ -58,29 +56,17 @@ namespace SH_COMP
sizeof(uint32_t) * header.indexCount sizeof(uint32_t) * header.indexCount
); );
} }
void MeshWriter::WriteAnimHeader(FileReference file, AnimDataHeader const& header)
{
auto constexpr intSize = sizeof(uint32_t);
file.write(
reinterpret_cast<char const*>(&header.charCount),
intSize
);
file.write(
reinterpret_cast<char const*>(&header.animNodeCount),
intSize
);
file.write(
reinterpret_cast<char const*>(header.nodeHeaders.data()),
sizeof(AnimNodeInfo) * header.nodeHeaders.size()
);
} }
void MeshWriter::WriteAnimData(FileReference file, AnimDataHeader const& header, AnimData const& data) void MeshWriter::WriteAnimData(FileReference file, std::vector<AnimDataHeader> const& headers,
std::vector<AnimData> const& anims)
{ {
for (auto i {0}; i < headers.size(); ++i)
{
auto const& header = headers[i];
auto const& data = anims[i];
file.write( file.write(
data.name.data(), data.name.data(),
header.charCount header.charCount
@ -101,6 +87,7 @@ namespace SH_COMP
WriteAnimNode(file, header.nodeHeaders[i], data.nodeChannels[i]); WriteAnimNode(file, header.nodeHeaders[i], data.nodeChannels[i]);
} }
} }
}
void MeshWriter::WriteAnimNode(FileReference file, AnimNodeInfo const& info, AnimNode const& node) void MeshWriter::WriteAnimNode(FileReference file, AnimNodeInfo const& info, AnimNode const& node)
{ {
@ -119,19 +106,26 @@ namespace SH_COMP
sizeof(AnimationBehaviour) sizeof(AnimationBehaviour)
); );
uint32_t const keySize = node.positionKeys.size();
file.write(
reinterpret_cast<char const*>(&keySize),
sizeof(uint32_t)
);
file.write( file.write(
reinterpret_cast<char const*>(node.positionKeys.data()), reinterpret_cast<char const*>(node.positionKeys.data()),
sizeof(PositionKey) * node.positionKeys.size() sizeof(PositionKey) * keySize
); );
file.write( file.write(
reinterpret_cast<char const*>(node.rotationKeys.data()), reinterpret_cast<char const*>(node.rotationKeys.data()),
sizeof(RotationKey) * node.rotationKeys.size() sizeof(RotationKey) * keySize
); );
file.write( file.write(
reinterpret_cast<char const*>(node.scaleKeys.data()), reinterpret_cast<char const*>(node.scaleKeys.data()),
sizeof(ScaleKey) * node.scaleKeys.size() sizeof(ScaleKey) * keySize
); );
} }
@ -232,27 +226,30 @@ namespace SH_COMP
void MeshWriter::WriteHeaders(FileReference file, ModelConstRef asset) void MeshWriter::WriteHeaders(FileReference file, ModelConstRef asset)
{ {
for (auto const& header : asset.meshHeaders) file.write(
{ reinterpret_cast<char const*>(&asset.header),
WriteMeshHeader(file, header); sizeof(asset.header)
} );
for (auto const& header : asset.animHeaders) file.write(
{ reinterpret_cast<char const*>(asset.meshHeaders.data()),
WriteAnimHeader(file, header); sizeof(MeshDataHeader) * asset.header.meshCount
} );
file.write(
reinterpret_cast<char const*>(asset.animHeaders.data()),
sizeof(AnimDataHeader) * asset.header.animCount
);
} }
void MeshWriter::WriteData(FileReference file, ModelConstRef asset) void MeshWriter::WriteData(FileReference file, ModelConstRef asset)
{ {
for (auto i {0}; i < asset.meshes.size(); ++i) WriteMeshData(file, asset.meshHeaders, asset.meshes);
{ WriteAnimData(file, asset.animHeaders, asset.anims);
WriteMeshData(file, asset.meshHeaders[i], asset.meshes[i]);
}
for (auto i {0}; i < asset.anims.size(); ++i) if (asset.rig.root)
{ {
WriteAnimData(file, asset.animHeaders[i], asset.anims[i]); WriteRig(file, asset.rig);
} }
} }
@ -268,14 +265,8 @@ namespace SH_COMP
return; return;
} }
file.write(
reinterpret_cast<char const*>(&asset.header),
sizeof(asset.header)
);
WriteHeaders(file, asset); WriteHeaders(file, asset);
WriteData(file, asset); WriteData(file, asset);
WriteRig(file, asset.rig);
file.close(); file.close();
} }

View File

@ -20,11 +20,8 @@ namespace SH_COMP
using FileReference = std::ofstream&; using FileReference = std::ofstream&;
using ModelConstRef = ModelAsset const&; using ModelConstRef = ModelAsset const&;
static void WriteMeshHeader(FileReference file, MeshDataHeader const& header); static void WriteMeshData(FileReference file, std::vector<MeshDataHeader> const& headers, std::vector<MeshData> const& meshes);
static void WriteMeshData(FileReference file, MeshDataHeader const& header, MeshData const& asset); static void WriteAnimData(FileReference file, std::vector<AnimDataHeader> const& headers, std::vector<AnimData> const& anims);
static void WriteAnimHeader(FileReference file, AnimDataHeader const& header);
static void WriteAnimData(FileReference file, AnimDataHeader const& header, AnimData const& data);
static void WriteAnimNode(FileReference file, AnimNodeInfo const& info, AnimNode const& node); static void WriteAnimNode(FileReference file, AnimNodeInfo const& info, AnimNode const& node);
static void WriteRig(FileReference file, RigData const& data); static void WriteRig(FileReference file, RigData const& data);