WIP Ingestion of rig nodes from model file

This commit is contained in:
Xiao Qi 2022-11-11 17:33:42 +08:00
parent 457094dc50
commit 7777e5642b
5 changed files with 68 additions and 16 deletions

BIN
racoon.fbx Normal file

Binary file not shown.

View File

@ -16,12 +16,14 @@
#include <fstream>
#include <iostream>
#include <queue>
namespace SH_COMP
{
Assimp::Importer MeshCompiler::aiImporter;
void MeshCompiler::ProcessNode(aiNode const& node, aiScene const& scene, MeshVectorRef meshes) noexcept
void MeshCompiler::ProcessNode(aiNode const& node, aiScene const& scene, MeshVectorRef meshes, RigNode*& root) noexcept
{
for (size_t i{ 0 }; i < node.mNumMeshes; ++i)
{
@ -31,9 +33,16 @@ namespace SH_COMP
meshes.back().name = node.mName.C_Str();
}
for (size_t i{ 0 }; i < node.mNumChildren; ++i)
if (std::strcmp(node.mName.C_Str(), "Armature") == 0)
{
ProcessNode(*node.mChildren[i], scene, meshes);
BuildArmature(&node, root);
}
else
{
for (size_t i{ 0 }; i < node.mNumChildren; ++i)
{
ProcessNode(*node.mChildren[i], scene, meshes, root);
}
}
}
@ -65,6 +74,12 @@ namespace SH_COMP
meshData.vertexTangent.reserve(mesh.mNumVertices);
meshData.texCoords.reserve(mesh.mNumVertices);
for (auto i {0}; i < mesh.mNumBones; ++i)
{
auto& bone = *mesh.mBones[i];
std::cout << "bone" << std::endl;
}
for (size_t i{ 0 }; i < mesh.mNumVertices; ++i)
{
// Vertex position
@ -175,16 +190,16 @@ namespace SH_COMP
void MeshCompiler::LoadFromFile(AssetPath path, MeshAsset& asset) noexcept
{
const aiScene* scene = aiImporter.ReadFile(path.string().c_str(),
aiProcess_Triangulate // Make sure we get triangles rather than nvert polygons
| aiProcess_GenUVCoords // Convert any type of mapping to uv mapping
| aiProcess_TransformUVCoords // preprocess UV transformations (scaling, translation ...)
| aiProcess_FindInstances // search for instanced meshes and remove them by references to one master
| aiProcess_CalcTangentSpace // calculate tangents and bitangents if possible
| aiProcess_JoinIdenticalVertices // join identical vertices/ optimize indexing
| aiProcess_FindInvalidData // detect invalid model data, such as invalid normal vectors
| aiProcess_FlipUVs // flip the V to match the Vulkans way of doing UVs
| aiProcess_ValidateDataStructure
const aiScene* scene = aiImporter.ReadFile(path.string().c_str(),0
//aiProcess_Triangulate // Make sure we get triangles rather than nvert polygons
//| aiProcess_GenUVCoords // Convert any type of mapping to uv mapping
//| aiProcess_TransformUVCoords // preprocess UV transformations (scaling, translation ...)
//| aiProcess_FindInstances // search for instanced meshes and remove them by references to one master
//| aiProcess_CalcTangentSpace // calculate tangents and bitangents if possible
//| aiProcess_JoinIdenticalVertices // join identical vertices/ optimize indexing
//| aiProcess_FindInvalidData // detect invalid model data, such as invalid normal vectors
//| aiProcess_FlipUVs // flip the V to match the Vulkans way of doing UVs
//| aiProcess_ValidateDataStructure
);
if (!scene || !scene->HasMeshes())
@ -195,7 +210,7 @@ namespace SH_COMP
//ExtractAnimations(*scene, anims);
ProcessNode(*scene->mRootNode, *scene, asset.meshes);
ProcessNode(*scene->mRootNode, *scene, asset.meshes, asset.rig.root);
aiImporter.FreeScene();
}
@ -226,10 +241,28 @@ namespace SH_COMP
file.close();
}
void MeshCompiler::BuildArmature(aiNode const* node, RigNode*& root) noexcept
{
std::queue<aiNode const*> nodes;
nodes.push(node);
root = new RigNode();
auto current = root;
while(!nodes.empty())
{
auto node = nodes.front();
nodes.pop();
current->name = node->mName.C_Str();
}
}
void MeshCompiler::LoadAndCompile(AssetPath path) noexcept
{
auto const asset = new MeshAsset();
asset->rig.root = nullptr;
LoadFromFile(path, *asset);
BuildHeaders(*asset);
CompileMeshBinary(path, *asset);

View File

@ -30,7 +30,7 @@ namespace SH_COMP
static Assimp::Importer aiImporter;
static void ProcessNode(aiNode const& node, aiScene const& scene, MeshVectorRef meshes) noexcept;
static void ProcessNode(aiNode const& node, aiScene const& scene, MeshVectorRef meshes, RigNode*& root) noexcept;
static void ExtractAnimations(aiScene const& scene, AnimVectorRef anims) noexcept;
static void GetMesh(aiMesh const& mesh, MeshData& meshData) noexcept;
static void BuildHeaders(MeshAsset& asset) noexcept;
@ -41,6 +41,7 @@ namespace SH_COMP
static void LoadFromFile(AssetPath path, MeshAsset& asset) noexcept;
static void CompileMeshBinary(AssetPath path, MeshAsset const& asset) noexcept;
static void BuildArmature(aiNode const* node, RigNode*& root) noexcept;
public:
static void LoadAndCompile(AssetPath path) noexcept;
};

View File

@ -28,6 +28,11 @@ namespace SH_COMP
float x, y, z;
};
struct SHMat4
{
float data[16];
};
struct MeshDataHeader
{
uint32_t vertexCount;
@ -45,6 +50,18 @@ namespace SH_COMP
std::vector<uint32_t> indices;
};
struct RigNode
{
std::string name;
SHMat4 transform;
std::vector<RigNode*> children;
};
struct RigData
{
RigNode* root;
};
struct MeshAssetHeader
{
size_t meshCount;
@ -53,6 +70,7 @@ namespace SH_COMP
struct MeshAsset
{
MeshAssetHeader header;
RigData rig;
std::vector<MeshDataHeader> headers;
std::vector<MeshData> meshes;
};

View File

@ -53,7 +53,7 @@ int main(int argc, char* argv[])
// SH_COMP::MeshCompiler::LoadAndCompile(path);
//}
SH_COMP::MeshCompiler::LoadAndCompile("Racoon.gltf");
SH_COMP::MeshCompiler::LoadAndCompile("Racoon.fbx");
return 0;
}