Gave up on trying to build node collection while copying ai node tree.

Tree copy is done, finish writing to file after building node data collection
This commit is contained in:
Xiao Qi 2023-01-07 00:16:28 +08:00
parent e6f6f9b0c5
commit abd3f2dd53
5 changed files with 73 additions and 51 deletions

View File

@ -27,8 +27,8 @@ namespace SH_COMP
SHMat4 result; SHMat4 result;
std::memcpy( std::memcpy(
reinterpret_cast<char*>(&result), &result,
reinterpret_cast<char const*>(&inMatrx), &inMatrx,
sizeof(result) sizeof(result)
); );
@ -38,22 +38,22 @@ namespace SH_COMP
Assimp::Importer MeshCompiler::aiImporter; Assimp::Importer MeshCompiler::aiImporter;
uint32_t MeshCompiler::rigNodeIDCounter { 0 }; uint32_t MeshCompiler::rigNodeIDCounter { 0 };
void MeshCompiler::ProcessNode(aiNode const& node, aiScene const& scene, MeshVectorRef meshes, RigData& rig) noexcept void MeshCompiler::ProcessNode(AiNodeConstPtr node, aiScene const& scene, MeshVectorRef meshes, RigData& rig) noexcept
{ {
for (auto i{0}; i < node.mNumChildren; ++i) for (auto i{ 0 }; i < node->mNumChildren; ++i)
{ {
auto const& child {*node.mChildren[i]}; auto child{ node->mChildren[i] };
if (child.mNumMeshes > 0) if (child->mNumMeshes > 0)
{ {
aiMesh* mesh = scene.mMeshes[child.mMeshes[0]]; aiMesh* mesh = scene.mMeshes[child->mMeshes[0]];
meshes.emplace_back(); meshes.emplace_back();
GetMesh(*mesh, meshes.back()); GetMesh(*mesh, meshes.back());
meshes.back().name = child.mName.C_Str(); meshes.back().name = child->mName.C_Str();
} }
else else
{ {
BuildArmature(child, rig); BuildArmature(child, rig);
} }
} }
} }
@ -68,7 +68,7 @@ namespace SH_COMP
meshData.bonesInfo.resize(mesh.mNumBones); meshData.bonesInfo.resize(mesh.mNumBones);
meshData.bones.resize(mesh.mNumBones); meshData.bones.resize(mesh.mNumBones);
for (auto i {0}; i < mesh.mNumBones; ++i) for (auto i{ 0 }; i < mesh.mNumBones; ++i)
{ {
auto const& bone = *mesh.mBones[i]; auto const& bone = *mesh.mBones[i];
auto& newBone = meshData.bones[i]; auto& newBone = meshData.bones[i];
@ -80,7 +80,7 @@ namespace SH_COMP
std::memcpy(&newBone.offset, &bone.mOffsetMatrix, sizeof(SHMat4)); std::memcpy(&newBone.offset, &bone.mOffsetMatrix, sizeof(SHMat4));
newBone.weights.resize(bone.mNumWeights); newBone.weights.resize(bone.mNumWeights);
for (auto j {0}; j < bone.mNumWeights; ++j) for (auto j{ 0 }; j < bone.mNumWeights; ++j)
{ {
newBone.weights[j].index = bone.mWeights[j].mVertexId; newBone.weights[j].index = bone.mWeights[j].mVertexId;
newBone.weights[j].weight = bone.mWeights[j].mWeight; newBone.weights[j].weight = bone.mWeights[j].mWeight;
@ -142,7 +142,7 @@ namespace SH_COMP
// Mesh Headers // Mesh Headers
asset.meshHeaders.resize(asset.meshes.size()); asset.meshHeaders.resize(asset.meshes.size());
asset.header.meshCount = asset.meshes.size(); asset.header.meshCount = asset.meshes.size();
for (auto i{0}; i < asset.header.meshCount; ++i) for (auto i{ 0 }; i < asset.header.meshCount; ++i)
{ {
auto const& mesh = asset.meshes[i]; auto const& mesh = asset.meshes[i];
auto& head = asset.meshHeaders[i]; auto& head = asset.meshHeaders[i];
@ -156,7 +156,7 @@ namespace SH_COMP
// Anim Headers // Anim Headers
asset.animHeaders.resize(asset.anims.size()); asset.animHeaders.resize(asset.anims.size());
asset.header.animCount = asset.anims.size(); asset.header.animCount = asset.anims.size();
for (auto i{0}; i < asset.header.animCount; ++i) for (auto i{ 0 }; i < asset.header.animCount; ++i)
{ {
auto const& anim = asset.anims[i]; auto const& anim = asset.anims[i];
auto& head = asset.animHeaders[i]; auto& head = asset.animHeaders[i];
@ -165,9 +165,9 @@ namespace SH_COMP
head.animNodeCount = anim.nodeChannels.size(); head.animNodeCount = anim.nodeChannels.size();
head.nodeHeaders.resize(head.animNodeCount); head.nodeHeaders.resize(head.animNodeCount);
for (auto j{0}; j < head.animNodeCount; ++j) for (auto j{ 0 }; j < head.animNodeCount; ++j)
{ {
auto const& animNode = anim.nodeChannels[j]; auto const& animNode = anim.nodeChannels[j];
auto& nodeHeader = head.nodeHeaders[j]; auto& nodeHeader = head.nodeHeaders[j];
nodeHeader.charCount = animNode.name.size(); nodeHeader.charCount = animNode.name.size();
@ -182,9 +182,9 @@ namespace SH_COMP
{ {
// Size and read for number of animation clips // Size and read for number of animation clips
anims.resize(scene.mNumAnimations); anims.resize(scene.mNumAnimations);
for (auto i {0}; i < scene.mNumAnimations; ++i) for (auto i{ 0 }; i < scene.mNumAnimations; ++i)
{ {
auto const& animData = *scene.mAnimations[i]; auto const& animData = *scene.mAnimations[i];
auto& anim = anims[i]; auto& anim = anims[i];
anim.name = animData.mName.C_Str(); anim.name = animData.mName.C_Str();
@ -193,7 +193,7 @@ namespace SH_COMP
// Size and read for number of animation frames // Size and read for number of animation frames
anim.nodeChannels.resize(animData.mNumChannels); anim.nodeChannels.resize(animData.mNumChannels);
for (auto j {0}; j < animData.mNumChannels; ++j) for (auto j{ 0 }; j < animData.mNumChannels; ++j)
{ {
auto const& channelData = *animData.mChannels[j]; auto const& channelData = *animData.mChannels[j];
auto& node = anim.nodeChannels[j]; auto& node = anim.nodeChannels[j];
@ -202,9 +202,9 @@ namespace SH_COMP
// Position Keys // Position Keys
node.positionKeys.resize(channelData.mNumPositionKeys); node.positionKeys.resize(channelData.mNumPositionKeys);
for (auto k{0}; k < channelData.mNumPositionKeys; ++k) for (auto k{ 0 }; k < channelData.mNumPositionKeys; ++k)
{ {
auto const& posKeyData = channelData.mPositionKeys[k]; auto const& posKeyData = channelData.mPositionKeys[k];
auto& posKey = node.positionKeys[k]; auto& posKey = node.positionKeys[k];
posKey.time = posKeyData.mTime; posKey.time = posKeyData.mTime;
@ -215,9 +215,9 @@ namespace SH_COMP
// Rotation Keys // Rotation Keys
node.rotationKeys.resize(channelData.mNumRotationKeys); node.rotationKeys.resize(channelData.mNumRotationKeys);
for (auto k{0}; k < channelData.mNumRotationKeys; ++k) for (auto k{ 0 }; k < channelData.mNumRotationKeys; ++k)
{ {
auto const& rotKeyData = channelData.mRotationKeys[k]; auto const& rotKeyData = channelData.mRotationKeys[k];
auto& rotKey = node.rotationKeys[k]; auto& rotKey = node.rotationKeys[k];
rotKey.time = rotKeyData.mTime; rotKey.time = rotKeyData.mTime;
@ -229,9 +229,9 @@ namespace SH_COMP
// Scale Keys // Scale Keys
node.scaleKeys.resize(channelData.mNumScalingKeys); node.scaleKeys.resize(channelData.mNumScalingKeys);
for (auto k{0}; k < channelData.mNumScalingKeys; ++k) for (auto k{ 0 }; k < channelData.mNumScalingKeys; ++k)
{ {
auto const& scaKeyData = channelData.mScalingKeys[k]; auto const& scaKeyData = channelData.mScalingKeys[k];
auto& scaKey = node.scaleKeys[k]; auto& scaKey = node.scaleKeys[k];
scaKey.time = scaKeyData.mTime; scaKey.time = scaKeyData.mTime;
@ -243,9 +243,19 @@ namespace SH_COMP
} }
} }
std::pair<RigNodeData*, aiNode const*> MeshCompiler::PairHelper(AiNodeConstPtr node)
{
return std::make_pair(NewNode(node), node);
}
RigNodeData* MeshCompiler::NewNode(AiNodeConstPtr inNode)
{
return new RigNodeData(inNode->mName.C_Str(), aiTransformToMat4(inNode->mTransformation));
}
void MeshCompiler::LoadFromFile(AssetPath path, ModelAsset& asset) noexcept void MeshCompiler::LoadFromFile(AssetPath path, ModelAsset& asset) noexcept
{ {
const aiScene* scene = aiImporter.ReadFile(path.string().c_str(),0 const aiScene* scene = aiImporter.ReadFile(path.string().c_str(), 0
//aiProcess_Triangulate // Make sure we get triangles rather than nvert polygons //aiProcess_Triangulate // Make sure we get triangles rather than nvert polygons
//| aiProcess_GenUVCoords // Convert any type of mapping to uv mapping //| aiProcess_GenUVCoords // Convert any type of mapping to uv mapping
//| aiProcess_TransformUVCoords // preprocess UV transformations (scaling, translation ...) //| aiProcess_TransformUVCoords // preprocess UV transformations (scaling, translation ...)
@ -264,26 +274,38 @@ namespace SH_COMP
return; return;
} }
ProcessNode(*scene->mRootNode, *scene, asset.meshes, asset.rig); ProcessNode(scene->mRootNode, *scene, asset.meshes, asset.rig);
ParseAnimations(*scene, asset.anims); ParseAnimations(*scene, asset.anims);
aiImporter.FreeScene(); aiImporter.FreeScene();
} }
void MeshCompiler::BuildArmature(AiNodeConstPtr baseNode, RigData& rig) noexcept
void MeshCompiler::BuildArmature(aiNode const& baseNode, RigData& rig) noexcept
{ {
std::queue<std::pair<RigNodeData*, aiNode const&>> nodeQueue; // Build implementation copy of armature tree
auto& collection = rig.nodeDataCollection; // node collection write done later when writing to file
collection.clear(); std::queue<std::pair<RigNodeData*, AiNodeConstPtr>> nodeQueue;
//TODO FINISH BFS COPY nodeQueue.emplace(PairHelper(baseNode));
rig.root = nodeQueue.front().first;
while(!nodeQueue.empty()) while(!nodeQueue.empty())
{ {
auto currPair = nodeQueue.front();
nodeQueue.pop();
auto currNode = currPair.first;
auto const& currAiNode = currPair.second;
for (auto i {0}; i < currAiNode->mNumChildren; ++i)
{
auto newPair = PairHelper(currAiNode->mChildren[i]);
currNode->children.push_back(newPair.first);
nodeQueue.push(newPair);
}
} }
std::cout << "Done\n";
} }
void MeshCompiler::LoadAndCompile(AssetPath path) noexcept void MeshCompiler::LoadAndCompile(AssetPath path) noexcept

View File

@ -30,16 +30,21 @@ namespace SH_COMP
using ModelRef = ModelAsset&; using ModelRef = ModelAsset&;
using AiNodeConstPtr = aiNode const*;
static Assimp::Importer aiImporter; static Assimp::Importer aiImporter;
static uint32_t rigNodeIDCounter; static uint32_t rigNodeIDCounter;
static void ProcessNode(aiNode const& node, aiScene const& scene, MeshVectorRef meshes, RigData& rig) noexcept; static void ProcessNode(AiNodeConstPtr node, aiScene const& scene, MeshVectorRef meshes, RigData& rig) noexcept;
static void GetMesh(aiMesh const& mesh, MeshData& meshData) noexcept; static void GetMesh(aiMesh const& mesh, MeshData& meshData) noexcept;
static void BuildHeaders(ModelRef asset) noexcept; static void BuildHeaders(ModelRef asset) noexcept;
static void BuildArmature(aiNode const& node, RigData& rig) noexcept; static void BuildArmature(AiNodeConstPtr node, RigData& rig) noexcept;
static void ParseAnimations(aiScene const& scene, std::vector<AnimData>& anims) noexcept; static void ParseAnimations(aiScene const& scene, std::vector<AnimData>& anims) noexcept;
static std::pair<RigNodeData*, AiNodeConstPtr> PairHelper(AiNodeConstPtr node);
static RigNodeData* NewNode(AiNodeConstPtr inNode);
static void LoadFromFile(AssetPath path, ModelRef asset) noexcept; static void LoadFromFile(AssetPath path, ModelRef asset) noexcept;
public: public:

View File

@ -149,7 +149,7 @@ namespace SH_COMP
} }
void MeshWriter::WriteRigData(FileReference file, RigDataHeader const& header, void MeshWriter::WriteRigData(FileReference file, RigDataHeader const& header,
std::map<uint32_t, RigNodeData> const& data) RigData const& data)
{ {
for (auto i {0}; i < header.nodeCount; ++i) for (auto i {0}; i < header.nodeCount; ++i)
{ {

View File

@ -28,8 +28,8 @@ namespace SH_COMP
static void WriteAnimNode(FileReference file, AnimNodeInfo const& info, AnimNode const& node); static void WriteAnimNode(FileReference file, AnimNodeInfo const& info, AnimNode const& node);
static void WriteRigHeader(FileReference file, RigDataHeader const& header); static void WriteRigHeader(FileReference file, RigDataHeader const& header);
static void WriteRigData(FileReference file, RigDataHeader const& header, std::map<uint32_t, RigNodeData> const& data); static void WriteRigData(FileReference file, RigDataHeader const& header, RigData const& data);
static void WriteRigNodes(FileReference file, RigDataHeader const& header, RigNode const* root); //static void WriteRigNodes(FileReference file, RigDataHeader const& header, RigNode const* root);
static void WriteHeaders(FileReference file, ModelConstRef asset); static void WriteHeaders(FileReference file, ModelConstRef asset);
static void WriteData(FileReference file, ModelConstRef asset); static void WriteData(FileReference file, ModelConstRef asset);

View File

@ -21,18 +21,13 @@ namespace SH_COMP
std::string name; std::string name;
SHMat4 transform; SHMat4 transform;
}; std::vector<RigNodeData*> children;
struct RigNode
{
uint32_t idRef;
std::vector<RigNode*> children;
}; };
struct RigData struct RigData
{ {
RigDataHeader header; RigDataHeader header;
std::map<uint32_t, RigNodeData> nodeDataCollection; // std::map<uint32_t, RigNodeData> nodeDataCollection;
RigNode* root; RigNodeData* root;
}; };
} }