From 03c17ca5d49ffb5fb26c454da686bf861801b7d8 Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Wed, 16 Nov 2022 10:41:27 +0800 Subject: [PATCH] Separated asset data class Parsing of animation clips, frames, channels --- src/Libraries/MeshCompiler.cpp | 103 +++++++++++++++++++++++++++------ src/Libraries/MeshCompiler.h | 6 +- src/PseudoMath.h | 25 ++++++++ src/Types/AnimationAsset.h | 20 ++++++- src/Types/MeshAsset.h | 55 +----------------- src/Types/ModelAsset.h | 47 +++++++++++++++ src/Types/RigAsset.h | 1 + 7 files changed, 181 insertions(+), 76 deletions(-) create mode 100644 src/PseudoMath.h create mode 100644 src/Types/ModelAsset.h create mode 100644 src/Types/RigAsset.h diff --git a/src/Libraries/MeshCompiler.cpp b/src/Libraries/MeshCompiler.cpp index af49fd1..9c233a2 100644 --- a/src/Libraries/MeshCompiler.cpp +++ b/src/Libraries/MeshCompiler.cpp @@ -46,26 +46,26 @@ namespace SH_COMP } } - void MeshCompiler::ExtractAnimations(aiScene const& scene, AnimVectorRef anims) noexcept - { - if (scene.HasAnimations()) - { - std::vector anims(scene.mNumAnimations); - for (auto i{ 0 }; i < scene.mNumAnimations; ++i) - { - auto const& anim{ *scene.mAnimations[i] }; + //void MeshCompiler::ExtractAnimations(aiScene const& scene, AnimVectorRef anims) noexcept + //{ + // if (scene.HasAnimations()) + // { + // std::vector anims(scene.mNumAnimations); + // for (auto i{ 0 }; i < scene.mNumAnimations; ++i) + // { + // auto const& anim{ *scene.mAnimations[i] }; - anims[i].name = anim.mName.C_Str(); + // anims[i].name = anim.mName.C_Str(); - anims[i].duration = anim.mDuration; - anims[i].ticksPerSecond = anim.mTicksPerSecond; + // anims[i].duration = anim.mDuration; + // anims[i].ticksPerSecond = anim.mTicksPerSecond; - std::copy_n(anim.mChannels, anim.mNumChannels, anims[i].nodeChannels.data()); - std::copy_n(anim.mMeshChannels, anim.mNumMeshChannels, anims[i].meshChannels.data()); - std::copy_n(anim.mMorphMeshChannels, anim.mNumMorphMeshChannels, anims[i].morphMeshChannels.data()); - } - } - } + // std::copy_n(anim.mChannels, anim.mNumChannels, anims[i].nodeChannels.data()); + // std::copy_n(anim.mMeshChannels, anim.mNumMeshChannels, anims[i].meshChannels.data()); + // std::copy_n(anim.mMorphMeshChannels, anim.mNumMorphMeshChannels, anims[i].morphMeshChannels.data()); + // } + // } + //} void MeshCompiler::GetMesh(aiMesh const& mesh, MeshData& meshData) noexcept { @@ -226,7 +226,9 @@ namespace SH_COMP return; } - //ExtractAnimations(*scene, anims); + std::vector anims; + + ParseAnimations(*scene, anims); ProcessNode(*scene->mRootNode, *scene, asset.meshes, asset.rig.root); @@ -285,6 +287,71 @@ namespace SH_COMP } } + void MeshCompiler::ParseAnimations(aiScene const& scene, std::vector& anims) noexcept + { + // Size and read for number of animation clips + anims.resize(scene.mNumAnimations); + for (auto i {0}; i < scene.mNumAnimations; ++i) + { + auto const& animData = *scene.mAnimations[i]; + auto& anim = anims[i]; + + anim.name = animData.mName.C_Str(); + anim.duration = animData.mDuration; + anim.ticksPerSecond = animData.mTicksPerSecond; + + // Size and read for number of animation frames + anim.nodeChannels.resize(animData.mNumChannels); + for (auto j {0}; j < animData.mNumChannels; ++j) + { + auto const& channelData = *animData.mChannels[j]; + auto& node = anim.nodeChannels[j]; + + node.name = channelData.mNodeName.C_Str(); + + // Position Keys + node.positionKeys.resize(channelData.mNumPositionKeys); + for (auto k{0}; k < channelData.mNumPositionKeys; ++k) + { + auto const& posKeyData = channelData.mPositionKeys[k]; + auto& posKey = node.positionKeys[k]; + + posKey.time = posKeyData.mTime; + posKey.value.x = posKeyData.mValue.x; + posKey.value.y = posKeyData.mValue.y; + posKey.value.z = posKeyData.mValue.z; + } + + // Rotation Keys + node.rotationKeys.resize(channelData.mNumRotationKeys); + for (auto k{0}; k < channelData.mNumRotationKeys; ++k) + { + auto const& posKeyData = channelData.mRotationKeys[k]; + auto& posKey = node.rotationKeys[k]; + + posKey.time = posKeyData.mTime; + posKey.value.x = posKeyData.mValue.x; + posKey.value.y = posKeyData.mValue.y; + posKey.value.z = posKeyData.mValue.z; + posKey.value.w = posKeyData.mValue.w; + } + + // Scale Keys + node.scaleKeys.resize(channelData.mNumScalingKeys); + for (auto k{0}; k < channelData.mNumScalingKeys; ++k) + { + auto const& posKeyData = channelData.mScalingKeys[k]; + auto& posKey = node.scaleKeys[k]; + + posKey.time = posKeyData.mTime; + posKey.value.x = posKeyData.mValue.x; + posKey.value.y = posKeyData.mValue.y; + posKey.value.z = posKeyData.mValue.z; + } + } + } + } + void MeshCompiler::LoadAndCompile(AssetPath path) noexcept { auto const asset = new ModelAsset(); diff --git a/src/Libraries/MeshCompiler.h b/src/Libraries/MeshCompiler.h index 757b3a2..13e3fa9 100644 --- a/src/Libraries/MeshCompiler.h +++ b/src/Libraries/MeshCompiler.h @@ -17,7 +17,7 @@ #include #include "Types/AnimationAsset.h" -#include "Types/MeshAsset.h" +#include "Types/ModelAsset.h" #include "AssetMacros.h" namespace SH_COMP @@ -31,7 +31,7 @@ namespace SH_COMP static Assimp::Importer aiImporter; 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 ExtractAnimations(aiScene const& scene, AnimVectorRef anims) noexcept; static void GetMesh(aiMesh const& mesh, MeshData& meshData) noexcept; static void BuildHeaders(ModelAsset& asset) noexcept; @@ -43,6 +43,8 @@ namespace SH_COMP static void BuildArmature(aiNode const& node, RigNode*& root) noexcept; static void CopyNode(aiNode const& source, RigNode* parent) noexcept; + + static void ParseAnimations(aiScene const& scene, std::vector& anims) noexcept; public: static void LoadAndCompile(AssetPath path) noexcept; }; diff --git a/src/PseudoMath.h b/src/PseudoMath.h new file mode 100644 index 0000000..5f88a59 --- /dev/null +++ b/src/PseudoMath.h @@ -0,0 +1,25 @@ +#pragma once + +namespace SH_COMP +{ + + struct SHVec2 + { + float x, y; + }; + + struct SHVec3 + { + float x, y, z; + }; + + struct SHVec4 + { + float x, y, z, w; + }; + + struct SHMat4 + { + float data[16]; + }; +} \ No newline at end of file diff --git a/src/Types/AnimationAsset.h b/src/Types/AnimationAsset.h index 773237f..a8ea4a1 100644 --- a/src/Types/AnimationAsset.h +++ b/src/Types/AnimationAsset.h @@ -10,18 +10,32 @@ *****************************************************************************/ #pragma once +#include "PseudoMath.h" #include #include namespace SH_COMP { + struct AnimationKey + { + float time; + SHVec4 value; + }; + struct AnimationNode + { + std::string name; + std::vector positionKeys; + std::vector rotationKeys; + std::vector scaleKeys; + }; + struct AnimationAsset { std::string name; - std::vector nodeChannels; - std::vector meshChannels; - std::vector morphMeshChannels; + std::vector nodeChannels; + //std::vector meshChannels; + //std::vector morphMeshChannels; double duration; double ticksPerSecond; diff --git a/src/Types/MeshAsset.h b/src/Types/MeshAsset.h index f5e1eb0..fbd4a6c 100644 --- a/src/Types/MeshAsset.h +++ b/src/Types/MeshAsset.h @@ -1,38 +1,12 @@ -/*************************************************************************//** - * \file SHMeshAsset.h - * \author Loh Xiao Qi - * \date 30 September 2022 - * \brief Struct to contain ready data for loading into GPU. Also used for - * compilation into binary files - * - * - * Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or - * disclosure of this file or its contents without the prior written consent - * of DigiPen Institute of Technology is prohibited. - *****************************************************************************/ #pragma once #include #include +#include "PseudoMath.h" + namespace SH_COMP { - - struct SHVec2 - { - float x, y; - }; - - struct SHVec3 - { - float x, y, z; - }; - - struct SHMat4 - { - float data[16]; - }; - struct MeshDataHeader { uint32_t vertexCount; @@ -71,29 +45,4 @@ namespace SH_COMP std::vector bonesInfo; std::vector bones; }; - - struct RigNode - { - std::string name; - uint32_t id; - SHMat4 transform; - std::vector children; - }; - - struct RigData - { - RigNode* root; - }; - - struct ModelAssetHeader - { - size_t meshCount; - }; - - struct ModelAsset - { - ModelAssetHeader header; - std::vector headers; - std::vector meshes; - }; } \ No newline at end of file diff --git a/src/Types/ModelAsset.h b/src/Types/ModelAsset.h new file mode 100644 index 0000000..9c09b66 --- /dev/null +++ b/src/Types/ModelAsset.h @@ -0,0 +1,47 @@ +/*************************************************************************//** + * \file SHMeshAsset.h + * \author Loh Xiao Qi + * \date 30 September 2022 + * \brief Struct to contain ready data for loading into GPU. Also used for + * compilation into binary files + * + * + * Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. + *****************************************************************************/ +#pragma once + +#include +#include + +#include "MeshAsset.h" + +namespace SH_COMP +{ + struct RigNode + { + std::string name; + uint32_t id; + SHMat4 transform; + std::vector children; + }; + + struct RigData + { + RigNode* root; + }; + + struct ModelAssetHeader + { + size_t meshCount; + }; + + struct ModelAsset + { + ModelAssetHeader header; + RigData rig; + std::vector headers; + std::vector meshes; + }; +} \ No newline at end of file diff --git a/src/Types/RigAsset.h b/src/Types/RigAsset.h new file mode 100644 index 0000000..6f70f09 --- /dev/null +++ b/src/Types/RigAsset.h @@ -0,0 +1 @@ +#pragma once