From f118c1c2cabb4309b6e3d747cd51c97dfe9a28e8 Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Wed, 1 Mar 2023 17:56:37 +0800 Subject: [PATCH] WIP Load new model asset structure --- .../Asset Types/Models/SHAnimationAsset.h | 78 +++++++------------ .../Assets/Asset Types/Models/SHMeshAsset.h | 42 ++++------ .../Assets/Asset Types/Models/SHRigAsset.h | 40 ++++++---- .../Libraries/Loaders/SHModelLoader.cpp | 62 ++------------- .../Assets/Libraries/Loaders/SHModelLoader.h | 4 +- 5 files changed, 77 insertions(+), 149 deletions(-) diff --git a/SHADE_Engine/src/Assets/Asset Types/Models/SHAnimationAsset.h b/SHADE_Engine/src/Assets/Asset Types/Models/SHAnimationAsset.h index d7128977..424f8cf3 100644 --- a/SHADE_Engine/src/Assets/Asset Types/Models/SHAnimationAsset.h +++ b/SHADE_Engine/src/Assets/Asset Types/Models/SHAnimationAsset.h @@ -18,71 +18,53 @@ namespace SHADE { - enum class SHAnimationBehaviour : uint8_t - { - DEFAULT = 0x0, - CONSTANT = 0x1, - LINEAR = 0x2, - REPEAT = 0x3 - }; + enum class AnimationInterpolation : uint8_t + { + DEFAULT = 0x1, + LINEAR = 0x1, + STEP = 0x2, + CUBICSPLINE = 0x3 + }; - // Smallest data containers - struct PositionKey - { - float time; - SHVec3 value; - }; - - struct RotationKey - { - float time; - SHVec4 value; - }; - - struct ScaleKey - { - float time; - SHVec3 value; - }; + // Base + struct KeyBase + { + float time; + SHVec3 value; + }; + + // Smallest data containers + struct PositionKey :KeyBase {}; + + struct RotationKey : KeyBase {}; + + struct ScaleKey :KeyBase {}; // Headers for read/write - struct SHAnimNodeInfo - { - uint32_t charCount; - uint32_t posKeyCount; - uint32_t rotKeyCount; - uint32_t scaKeyCount; - }; - struct SHAnimDataHeader { uint32_t charCount; - uint32_t animNodeCount; - std::vector nodeHeaders; + uint32_t animNodeCount; + uint32_t frameCount; }; // Main data containers - struct SHAnimData + struct SHAnimNode { std::string name; - SHAnimationBehaviour pre; - SHAnimationBehaviour post; - - std::vector positionKeys; - std::vector rotationKeys; - std::vector scaleKeys; + std::vector positionKeys; + std::vector rotationKeys; + std::vector scaleKeys; }; - struct SH_API SHAnimAsset : SHAssetData + struct SH_API SHAnimAsset final : SHAssetData { std::string name; - double duration; - double ticksPerSecond; + double duration{}; + double ticksPerSecond{}; - std::vector nodeChannels; - //std::vector meshChannels; - //std::vector morphMeshChannels; + std::vector nodeChannels{}; }; } \ No newline at end of file diff --git a/SHADE_Engine/src/Assets/Asset Types/Models/SHMeshAsset.h b/SHADE_Engine/src/Assets/Asset Types/Models/SHMeshAsset.h index 14744c4d..37172070 100644 --- a/SHADE_Engine/src/Assets/Asset Types/Models/SHMeshAsset.h +++ b/SHADE_Engine/src/Assets/Asset Types/Models/SHMeshAsset.h @@ -19,45 +19,31 @@ namespace SHADE { - constexpr int BONE_INDEX_ALIGHTMENT = 4; struct SHMeshDataHeader { uint32_t vertexCount; uint32_t indexCount; uint32_t charCount; - uint32_t boneCount; + bool hasWeights; }; - struct MeshBoneInfo - { - uint32_t charCount; - uint32_t weightCount; // Size should be same as boneCount - }; - - struct BoneWeight - { - uint32_t index; - float weight; - }; - - struct MeshBone - { - std::string name; - SHMatrix offset; - std::vector weights; - }; + struct VertexWeight + { + SHVec4 weights; + SHVec4U joints; + }; struct SH_API SHMeshAsset : SHAssetData { std::string name; - std::vector VertexPositions; - std::vector VertexTangents; - std::vector VertexNormals; - std::vector VertexTexCoords; - std::vector Indices; - std::vector VertexBoneIndices; - std::vector VertexBoneWeights; - uint32_t BoneCount; + std::vector VertexPositions{}; + std::vector VertexTangents{}; + std::vector VertexNormals{}; + std::vector VertexTexCoords{}; + std::vector Indices{}; + + //Variables + std::vector VertexWeights{}; }; } diff --git a/SHADE_Engine/src/Assets/Asset Types/Models/SHRigAsset.h b/SHADE_Engine/src/Assets/Asset Types/Models/SHRigAsset.h index 59c9b51e..85f0c540 100644 --- a/SHADE_Engine/src/Assets/Asset Types/Models/SHRigAsset.h +++ b/SHADE_Engine/src/Assets/Asset Types/Models/SHRigAsset.h @@ -17,31 +17,41 @@ namespace SHADE { + using NodeDataFlag = unsigned char; + + constexpr NodeDataFlag NODE_DATA_ROTATION = 0b0001; + constexpr NodeDataFlag NODE_DATA_SCALE = 0b0010; + constexpr NodeDataFlag NODE_DATA_TRANSLATION = 0b0100; + constexpr NodeDataFlag NODE_DATA_MATRIX = 0b1000; + + constexpr size_t NODE_COMPONENT_COUNT_ROTATION{ 4 }; + constexpr size_t NODE_COMPONENT_COUNT_SCALE{ 3 }; + constexpr size_t NODE_COMPONENT_COUNT_TRANSLATION{ 3 }; + constexpr size_t NODE_COMPONENT_COUNT_MATRIX{ 16 }; + struct SHRigDataHeader { - uint32_t nodeCount; - std::vector charCounts; + uint32_t nodeCount{}; + uint32_t startNode{}; + std::vector charCounts{}; + std::vector childCount{}; + std::vector dataFlags{}; }; struct SHRigNodeData { std::string name; - SHMatrix transform; - SHMatrix offset; + std::vector + rotation, + scale, + translation, + matrix; + SHMatrix inverseBindMatrix; }; - struct SHRigNodeAsset + struct SH_API SHRigAsset final : SHAssetData { - uint32_t idRef; - std::vector children; - }; - - struct SH_API SHRigAsset : SHAssetData - { - ~SHRigAsset(); - SHRigDataHeader header; - std::vector nodeDataCollection; - SHRigNodeAsset* root; + std::vector nodeDataCollection{}; }; } diff --git a/SHADE_Engine/src/Assets/Libraries/Loaders/SHModelLoader.cpp b/SHADE_Engine/src/Assets/Libraries/Loaders/SHModelLoader.cpp index 52a3a925..6f0e5fc5 100644 --- a/SHADE_Engine/src/Assets/Libraries/Loaders/SHModelLoader.cpp +++ b/SHADE_Engine/src/Assets/Libraries/Loaders/SHModelLoader.cpp @@ -36,29 +36,12 @@ namespace SHADE if (asset.header.animCount > 0) { asset.animHeaders.resize(asset.header.animCount); - for (auto i {0}; i < asset.header.animCount; ++i) + for (auto& animHeader : asset.animHeaders) { - auto& animHeader = asset.animHeaders[i]; file.read( - reinterpret_cast(&animHeader.charCount), - sizeof(uint32_t) + reinterpret_cast(&animHeader), + sizeof(animHeader) ); - - file.read( - reinterpret_cast(&animHeader.animNodeCount), - sizeof(uint32_t) - ); - - animHeader.nodeHeaders.resize(animHeader.animNodeCount); - for (auto j {0}; j < animHeader.animNodeCount; ++j) - { - auto& nodeHeader = animHeader.nodeHeaders[j]; - - file.read( - reinterpret_cast(&nodeHeader), - sizeof(SHAnimNodeInfo) - ); - } } } } @@ -73,11 +56,10 @@ namespace SHADE { ReadRigHeader(file, asset.rig.header); ReadRigData(file, asset.rig.header, asset.rig.nodeDataCollection); - ReadRigTree(file, asset.rig.header, asset.rig.root); } } - void SHModelLoader::ReadAnimNode(FileReference file, SHAnimNodeInfo const& info, SHAnimData& data) + void SHModelLoader::ReadAnimNode(FileReference file, SHAnimDataHeader const& info, SHAnimNode& data) { data.name.resize(info.charCount); file.read( @@ -85,40 +67,8 @@ namespace SHADE info.charCount ); - file.read( - reinterpret_cast(&data.pre), - sizeof(SHAnimationBehaviour) - ); - - file.read( - reinterpret_cast(&data.post), - sizeof(SHAnimationBehaviour) - ); - - uint32_t keySize {0}; - file.read( - reinterpret_cast(&keySize), - sizeof(uint32_t) - ); - - data.positionKeys.resize(keySize); - data.rotationKeys.resize(keySize); - data.scaleKeys.resize(keySize); - - file.read( - reinterpret_cast(data.positionKeys.data()), - sizeof(PositionKey) * keySize - ); - - file.read( - reinterpret_cast(data.rotationKeys.data()), - sizeof(RotationKey) * keySize - ); - - file.read( - reinterpret_cast(data.scaleKeys.data()), - sizeof(ScaleKey) * keySize - ); + //TODO read and parse data flags + //TODO read channels } void SHModelLoader::ReadRigHeader(FileReference file, SHRigDataHeader& header) diff --git a/SHADE_Engine/src/Assets/Libraries/Loaders/SHModelLoader.h b/SHADE_Engine/src/Assets/Libraries/Loaders/SHModelLoader.h index 4320727f..82fe43b4 100644 --- a/SHADE_Engine/src/Assets/Libraries/Loaders/SHModelLoader.h +++ b/SHADE_Engine/src/Assets/Libraries/Loaders/SHModelLoader.h @@ -20,11 +20,11 @@ namespace SHADE { using FileReference = std::ifstream&; - void ReadAnimNode(FileReference file, SHAnimNodeInfo const& info, SHAnimData& data); void ReadRigHeader(FileReference file, SHRigDataHeader& header); void ReadRigData(FileReference file, SHRigDataHeader const& header, std::vector& data); - void ReadRigTree(FileReference file, SHRigDataHeader const& header, SHRigNodeAsset*& root); + void ReadAnimNode(FileReference file, SHAnimDataHeader const& info, SHAnimNode& data); + void ReadAnimNode(FileReference file, SHAnimNode& data); void ReadMeshData(FileReference file, std::vector const& headers, std::vector& meshes); void ReadAnimData(FileReference file, std::vector const& headers, std::vector& anims);