diff --git a/assets b/assets index 10a61165..4e2b71f1 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 10a611659ddaadfea6a560e707d395834855a979 +Subproject commit 4e2b71f13a3026d06ccde2337c41427e73400c80 diff --git a/include/Engine/Rendering/BlendTree.h b/include/Engine/Rendering/BlendTree.h index b19fbe4f..29ad3ca9 100644 --- a/include/Engine/Rendering/BlendTree.h +++ b/include/Engine/Rendering/BlendTree.h @@ -19,6 +19,7 @@ public: Animation, }; + struct Node { @@ -27,7 +28,7 @@ public: Node* Parent = nullptr; Node* Child[2] = { nullptr, nullptr }; NodeType Type; - std::map Pose; + std::map Pose; //std::vector Pose; double Weight = 0.0; @@ -85,7 +86,7 @@ private: BlendTree::Node* FillTreeByName(Node* parentNode, std::string name, EntityWrapper parentEntity); std::vector FindNodesByName(std::string name); - void Blend(std::map& pose); + void Blend(std::map& pose); }; #endif diff --git a/include/Engine/Rendering/Skeleton.h b/include/Engine/Rendering/Skeleton.h index e812bf12..08e348ab 100644 --- a/include/Engine/Rendering/Skeleton.h +++ b/include/Engine/Rendering/Skeleton.h @@ -50,11 +50,16 @@ public: std::map> JointAnimations; }; + struct PoseData { + glm::vec3 Translation; + glm::quat Orientation; + glm::vec3 Scale; + }; + Skeleton() { } ~Skeleton(); Bone* RootBone; - std::map Bones; std::unordered_map> BlendTrees; @@ -65,20 +70,20 @@ public: int GetBoneID(std::string name); const Animation* GetAnimation(std::string name); - std::map GetFrameBones(const Animation* animation, double time, bool additive, bool noRootMotion = false); - glm::mat4 GetBoneTransform(const Bone* bone, const Animation* animation, float time, glm::mat4 childMatrix); - std::map BlendPoses(const std::map& pose1, const std::map& pose2, double weight); - std::map OverridePose(const std::map& overridePose, const std::map& targetPose); - std::map BlendPoseAdditive(const std::map& additivePose, const std::map& targetPose); - void GetFinalPose(std::map& boneMatrices, std::vector& finalPose, std::map& boneTransforms); + std::map GetFrameBones(const Animation* animation, double time, bool additive, bool noRootMotion = false); + + std::map BlendPoses(const std::map& pose1, const std::map& pose2, double weight); + std::map OverridePose(const std::map& overridePose, const std::map& targetPose); + std::map BlendPoseAdditive(const std::map& additivePose, const std::map& targetPose); + void GetFinalPose(std::map& boneMatrices, std::vector& finalPose, std::map& boneTransforms); std::map Animations; private: - glm::mat4 GetAdditiveBonePose(const Bone* bone, const Animation* animation, double time); - glm::mat4 GetBonePose(const Bone* bone, const Animation* animation, double time, bool noRootMotion); - void AccumulateFinalPose(std::map& boneMatrices, std::map& boneTransforms, const Bone* bone, glm::mat4 parentMatrix); - void AdditiveBoneTransforms(const Animation* animation, double time, std::map& boneMatrices, const Bone* bone); - void AccumulateBoneTransforms(bool noRootMotion, const Animation* animation, double time, std::map& boneMatrices, const Bone* bone, glm::mat4 parentMatrix); + Skeleton::PoseData GetAdditiveBonePose(const Bone* bone, const Animation* animation, double time); + + void AccumulateFinalPose(std::map& boneMatrices, std::map& poseDatas, std::map& boneTransforms, const Bone* bone, glm::mat4 parentMatrix); + void AdditiveBoneTransforms(const Animation* animation, double time, std::map& boneMatrices, const Bone* bone); + void AccumulateBoneTransforms(bool noRootMotion, const Animation* animation, double time, std::map& boneMatrices, const Bone* bone); std::map m_BonesByName; diff --git a/resources/Schema/Entities/AnimationTests2.xml b/resources/Schema/Entities/AnimationTests2.xml index fa2a8463..aaa6de48 100644 --- a/resources/Schema/Entities/AnimationTests2.xml +++ b/resources/Schema/Entities/AnimationTests2.xml @@ -66,11 +66,12 @@ Models/Weapons/Blue/AssaultWeaponBlue.mesh + - + - + @@ -90,7 +91,7 @@ AimRifleA - + false true @@ -131,7 +132,7 @@ ShootFastRifleU - + 1 @@ -142,7 +143,7 @@ ShootRifleU - + 1 @@ -155,8 +156,8 @@ ReloadSwitchU - - false + + 1 @@ -169,7 +170,7 @@ StandCrouchBlend Jump - 0.48000049591064453 + 0 @@ -198,7 +199,7 @@ CrouchWalkF - + 1 @@ -219,7 +220,7 @@ CrouchStrafeLeftF - + 1 @@ -230,7 +231,7 @@ CrouchStrafeRightF - + 1 @@ -246,7 +247,7 @@ RunWalkBlend StrafeBlend - 0 + 1 @@ -265,7 +266,8 @@ RunF - + + 1 @@ -275,7 +277,7 @@ WalkF - + 1 @@ -289,7 +291,7 @@ Left Right - 0 + 1 @@ -298,7 +300,7 @@ StrafeLeftF - + 1 @@ -309,7 +311,7 @@ StrafeRightF - + 1 @@ -326,8 +328,8 @@ JumpF - - false + + 1 @@ -352,13 +354,13 @@ Models/Core/UnitCube.mesh - + true - + - + @@ -372,12 +374,12 @@ Models/Core/UnitCube.mesh - + - - - + + + @@ -391,12 +393,12 @@ Models/Core/UnitCube.mesh - + - - - + + + @@ -410,12 +412,12 @@ Models/Core/UnitCube.mesh - + - - - + + + @@ -429,12 +431,12 @@ Models/Core/UnitCube.mesh - + - - - + + + @@ -448,12 +450,12 @@ Models/Core/UnitCube.mesh - + - - - + + + @@ -467,12 +469,12 @@ Models/Core/UnitCube.mesh - + - - - + + + @@ -486,12 +488,12 @@ Models/Core/UnitCube.mesh - + - - - + + + @@ -505,12 +507,12 @@ Models/Core/UnitCube.mesh - + - - - + + + @@ -524,12 +526,12 @@ Models/Core/UnitCube.mesh - + - - - + + + @@ -543,12 +545,12 @@ Models/Core/UnitCube.mesh - + - - - + + + @@ -562,12 +564,12 @@ Models/Core/UnitCube.mesh - + - - - + + + @@ -581,12 +583,12 @@ Models/Core/UnitCube.mesh - + - - - + + + @@ -600,12 +602,12 @@ Models/Core/UnitCube.mesh - + - - - + + + @@ -619,12 +621,12 @@ Models/Core/UnitCube.mesh - + - - - + + + @@ -638,12 +640,12 @@ Models/Core/UnitCube.mesh - + - - - + + + @@ -657,12 +659,12 @@ Models/Core/UnitCube.mesh - + - - - + + + @@ -676,12 +678,12 @@ Models/Core/UnitCube.mesh - + - - - + + + @@ -695,12 +697,12 @@ Models/Core/UnitCube.mesh - + - - - + + + @@ -714,12 +716,12 @@ Models/Core/UnitCube.mesh - + - - - + + + @@ -733,12 +735,12 @@ Models/Core/UnitCube.mesh - + - - - + + + @@ -752,12 +754,12 @@ Models/Core/UnitCube.mesh - + - - - + + + @@ -771,12 +773,12 @@ Models/Core/UnitCube.mesh - + - - - + + + @@ -790,12 +792,12 @@ Models/Core/UnitCube.mesh - + - - - + + + @@ -809,12 +811,12 @@ Models/Core/UnitCube.mesh - + - - - + + + @@ -828,12 +830,12 @@ Models/Core/UnitCube.mesh - + - - - + + + @@ -847,12 +849,12 @@ Models/Core/UnitCube.mesh - + - - - + + + diff --git a/src/Engine/Rendering/BlendTree.cpp b/src/Engine/Rendering/BlendTree.cpp index 8ef67f0f..cb7ffa69 100644 --- a/src/Engine/Rendering/BlendTree.cpp +++ b/src/Engine/Rendering/BlendTree.cpp @@ -238,7 +238,7 @@ BlendTree::AutoBlendInfo BlendTree::AutoBlendStep(AutoBlendInfo blendInfo) return blendInfo; } -void BlendTree::Blend(std::map& pose) +void BlendTree::Blend(std::map& pose) { Node* currentNode; Node* start = m_Root; @@ -301,7 +301,7 @@ std::vector BlendTree::AccumulateFinalPose() return finalPose; } - std::map pose; + std::map pose; Blend(pose); m_Skeleton->GetFinalPose(pose, finalPose, m_FinalBoneTransforms); diff --git a/src/Engine/Rendering/BoneAttachmentSystem.cpp b/src/Engine/Rendering/BoneAttachmentSystem.cpp index 43dc8634..191fb3bd 100644 --- a/src/Engine/Rendering/BoneAttachmentSystem.cpp +++ b/src/Engine/Rendering/BoneAttachmentSystem.cpp @@ -50,6 +50,19 @@ void BoneAttachmentSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapp glm::vec4 perspective; glm::decompose(boneTransform, scale, rotation, translation, skew, perspective); + float lowRange = 0.98f; + float highRange = 1.02f; + if(scale.x < lowRange || scale.y < lowRange || scale.z < lowRange || + scale.x > highRange || scale.y > highRange || scale.z > highRange) { + if (entity.HasComponent("Model")) { + (glm::vec4&)entity["Model"]["Color"] = glm::vec4(1, 0, 0, 1); + } + } else { + if (entity.HasComponent("Model")) { + (glm::vec4&)entity["Model"]["Color"] = glm::vec4(0, 1, 0, 1); + } + } + glm::vec3 angles = glm::vec3(-glm::pitch(rotation), -glm::yaw(rotation), -glm::roll(rotation)); if ((bool)entity["BoneAttachment"]["InheritPosition"]) { diff --git a/src/Engine/Rendering/DrawBloomPass.cpp b/src/Engine/Rendering/DrawBloomPass.cpp index 12777941..73f73cc4 100644 --- a/src/Engine/Rendering/DrawBloomPass.cpp +++ b/src/Engine/Rendering/DrawBloomPass.cpp @@ -45,6 +45,7 @@ void DrawBloomPass::InitializeShaderPrograms() m_GaussianProgram_horiz->AddShader(std::shared_ptr(new VertexShader("Shaders/Gaussian_horiz.vert.glsl"))); m_GaussianProgram_horiz->AddShader(std::shared_ptr(new FragmentShader("Shaders/Gaussian_horiz.frag.glsl"))); m_GaussianProgram_horiz->Compile(); + m_GaussianProgram_horiz->BindFragDataLocation(0, "fragmentColor"); m_GaussianProgram_horiz->Link(); } @@ -53,6 +54,7 @@ void DrawBloomPass::InitializeShaderPrograms() m_GaussianProgram_vert->AddShader(std::shared_ptr(new VertexShader("Shaders/Gaussian_vert.vert.glsl"))); m_GaussianProgram_vert->AddShader(std::shared_ptr(new FragmentShader("Shaders/Gaussian_vert.frag.glsl"))); m_GaussianProgram_vert->Compile(); + m_GaussianProgram_vert->BindFragDataLocation(0, "fragmentColor"); m_GaussianProgram_vert->Link(); } } diff --git a/src/Engine/Rendering/SSAOPass.cpp b/src/Engine/Rendering/SSAOPass.cpp index 9992db70..3b11535f 100644 --- a/src/Engine/Rendering/SSAOPass.cpp +++ b/src/Engine/Rendering/SSAOPass.cpp @@ -56,6 +56,7 @@ void SSAOPass::InitializeShaderProgram() m_SSAOProgram->AddShader(std::shared_ptr(new VertexShader("Shaders/SSAO.vert.glsl"))); m_SSAOProgram->AddShader(std::shared_ptr(new FragmentShader("Shaders/SSAO.frag.glsl"))); m_SSAOProgram->Compile(); + m_SSAOProgram->BindFragDataLocation(0, "AO"); m_SSAOProgram->Link(); } @@ -64,6 +65,7 @@ void SSAOPass::InitializeShaderProgram() m_SSAOViewSpaceZProgram->AddShader(std::shared_ptr(new VertexShader("Shaders/SSAO.vert.glsl"))); m_SSAOViewSpaceZProgram->AddShader(std::shared_ptr(new FragmentShader("Shaders/SSAOViewSpaceZ.frag.glsl"))); m_SSAOViewSpaceZProgram->Compile(); + m_SSAOViewSpaceZProgram->BindFragDataLocation(0, "depthLinear"); m_SSAOViewSpaceZProgram->Link(); } @@ -72,6 +74,7 @@ void SSAOPass::InitializeShaderProgram() m_GaussianProgram_horiz->AddShader(std::shared_ptr(new VertexShader("Shaders/Gaussian_horiz.vert.glsl"))); m_GaussianProgram_horiz->AddShader(std::shared_ptr(new FragmentShader("Shaders/Gaussian_horiz.frag.glsl"))); m_GaussianProgram_horiz->Compile(); + m_GaussianProgram_horiz->BindFragDataLocation(0, "fragmentColor"); m_GaussianProgram_horiz->Link(); } @@ -80,6 +83,7 @@ void SSAOPass::InitializeShaderProgram() m_GaussianProgram_vert->AddShader(std::shared_ptr(new VertexShader("Shaders/Gaussian_vert.vert.glsl"))); m_GaussianProgram_vert->AddShader(std::shared_ptr(new FragmentShader("Shaders/Gaussian_vert.frag.glsl"))); m_GaussianProgram_vert->Compile(); + m_GaussianProgram_vert->BindFragDataLocation(0, "fragmentColor"); m_GaussianProgram_vert->Link(); } } diff --git a/src/Engine/Rendering/Skeleton.cpp b/src/Engine/Rendering/Skeleton.cpp index b6fea0bb..c9ae2732 100644 --- a/src/Engine/Rendering/Skeleton.cpp +++ b/src/Engine/Rendering/Skeleton.cpp @@ -1,20 +1,26 @@ #include "Rendering/Skeleton.h" -std::map Skeleton::GetFrameBones(const Animation* animation, double time, bool additive, bool noRootMotion /*= false*/) +std::map Skeleton::GetFrameBones(const Animation* animation, double time, bool additive, bool noRootMotion /*= false*/) { if (animation == nullptr) { - std::map finalMatrices; + std::map finalMatrices; for (auto& b : Bones) { - finalMatrices[b.second->ID] = glm::mat4(1); + PoseData poseData; + poseData.Translation = glm::vec3(0); + poseData.Orientation = glm::quat(); + poseData.Scale = glm::vec3(1); + + + finalMatrices[b.second->ID] = poseData; } return finalMatrices; } - std::map frameBones; + std::map frameBones; if(!additive) { - AccumulateBoneTransforms(true, animation, time, frameBones, RootBone, glm::mat4(1)); + AccumulateBoneTransforms(true, animation, time, frameBones, RootBone); } else { AdditiveBoneTransforms(animation, time, frameBones, RootBone); } @@ -22,11 +28,10 @@ std::map Skeleton::GetFrameBones(const Animation* animation, dou return frameBones; } -void Skeleton::AccumulateBoneTransforms(bool noRootMotion, const Animation* animation, double time, std::map& boneMatrices, const Bone* bone, glm::mat4 parentMatrix) +void Skeleton::AccumulateBoneTransforms(bool noRootMotion, const Animation* animation, double time, std::map& boneMatrices, const Bone* bone) { - glm::mat4 boneMatrix; + PoseData poseData; - if (animation->JointAnimations.find(bone->ID) != animation->JointAnimations.end()) { std::vector boneKeyFrames = animation->JointAnimations.at(bone->ID); @@ -66,37 +71,38 @@ void Skeleton::AccumulateBoneTransforms(bool noRootMotion, const Animation* anim position.z = 0; } - boneMatrix = (glm::translate(position) * glm::toMat4(rotation) * glm::scale(scale)); - boneMatrices[bone->ID] = boneMatrix;// *bone->OffsetMatrix; + poseData.Translation = position; + poseData.Orientation = rotation; + poseData.Scale = scale; + boneMatrices[bone->ID] = poseData; } else { // 1 keyframes for the current bone currentFrame = boneKeyFrames.at(0); - boneMatrix = (glm::translate(currentFrame.BoneProperties.Position) * glm::toMat4(glm::normalize(currentFrame.BoneProperties.Rotation)) * glm::scale(currentFrame.BoneProperties.Scale)); - boneMatrices[bone->ID] = boneMatrix;// *bone->OffsetMatrix; - } - } else { // 0 keyframes for the current bone - if (bone->Parent) { - //boneMatrix = parentMatrix * (glm::inverse(bone->OffsetMatrix) * bone->Parent->OffsetMatrix); - //boneMatrices[bone->ID] = boneMatrix *bone->OffsetMatrix; - } else { - //boneMatrix = glm::inverse(bone->OffsetMatrix); - //boneMatrices[bone->ID] = parentMatrix; + poseData.Translation = currentFrame.BoneProperties.Position; + poseData.Orientation = currentFrame.BoneProperties.Rotation; + poseData.Scale = currentFrame.BoneProperties.Scale; + boneMatrices[bone->ID] = poseData; } } for (auto &child : bone->Children) { - AccumulateBoneTransforms(noRootMotion, animation, time, boneMatrices, child, boneMatrix); + AccumulateBoneTransforms(noRootMotion, animation, time, boneMatrices, child); } } -void Skeleton::AdditiveBoneTransforms(const Animation* animation, double time, std::map& boneMatrices, const Bone* bone) +void Skeleton::AdditiveBoneTransforms(const Animation* animation, double time, std::map& boneMatrices, const Bone* bone) { if (animation->JointAnimations.find(bone->ID) != animation->JointAnimations.end()) { - glm::mat4 refPose = GetAdditiveBonePose(bone, animation, 0.0); - glm::mat4 srcPose = GetAdditiveBonePose(bone, animation, time + 1.0/60.0); - glm::mat4 boneMatrix = srcPose * glm::inverse(refPose); - boneMatrices[bone->ID] = boneMatrix; + PoseData refPose = GetAdditiveBonePose(bone, animation, 0.0); + PoseData srcPose = GetAdditiveBonePose(bone, animation, time + 1.0/60.0); + + PoseData finalPose; + finalPose.Translation = srcPose.Translation - refPose.Translation; + finalPose.Orientation = srcPose.Orientation * glm::inverse(refPose.Orientation); + finalPose.Scale = srcPose.Scale - refPose.Scale; + + boneMatrices[bone->ID] = finalPose; } for (auto &child : bone->Children) { @@ -104,7 +110,7 @@ void Skeleton::AdditiveBoneTransforms(const Animation* animation, double time, s } } -glm::mat4 Skeleton::GetAdditiveBonePose(const Bone* bone, const Animation* animation, double time) +Skeleton::PoseData Skeleton::GetAdditiveBonePose(const Bone* bone, const Animation* animation, double time) { glm::vec3 position = glm::vec3(0); glm::quat rotation = glm::quat(); @@ -152,141 +158,32 @@ glm::mat4 Skeleton::GetAdditiveBonePose(const Bone* bone, const Animation* anima } } - return (glm::translate(position) * glm::toMat4(rotation) * glm::scale(scale));; + PoseData finalPose; + finalPose.Translation = position; + finalPose.Orientation = rotation; + finalPose.Scale = scale; + + return finalPose; } - -glm::mat4 Skeleton::GetBonePose(const Bone* bone, const Animation* animation, double time, bool noRootMotion) +std::map Skeleton::BlendPoses(const std::map& pose1, const std::map& pose2, double weight) { - glm::mat4 boneMatrix; + std::map finalPose; - if (animation->JointAnimations.find(bone->ID) != animation->JointAnimations.end()) { - std::vector boneKeyFrames = animation->JointAnimations.at(bone->ID); - - Animation::Keyframe currentFrame; - Animation::Keyframe nextFrame; - - if (boneKeyFrames.size() > 1) { // 2+ keyframes for the current bone - for (int index = boneKeyFrames.size()-1; index >= 0; index--) { // find the bone keyframes that surrounds the current frame - if (time >= boneKeyFrames.at(index).Time) { - currentFrame = boneKeyFrames.at(index); - nextFrame = boneKeyFrames.at((index + 1) % boneKeyFrames.size()); - break; - } - } - - float progress; - - if (nextFrame.Index == 0) { - nextFrame = currentFrame; - progress = (time - currentFrame.Time) / (animation->Duration - currentFrame.Time); - } else { - progress = (time - currentFrame.Time) / (nextFrame.Time - currentFrame.Time); - - } - - - if (progress > 1.0f || progress < 0.0f) { - progress = glm::clamp(progress, 0.0f, 1.0f); - } - Animation::Keyframe::BoneProperty currentBoneProperty = currentFrame.BoneProperties; - Animation::Keyframe::BoneProperty nextBoneProperty = nextFrame.BoneProperties; - - glm::vec3 position = currentBoneProperty.Position * (1.f - progress) + nextBoneProperty.Position * progress; - glm::quat rotation = glm::slerp(currentBoneProperty.Rotation, nextBoneProperty.Rotation, progress); - glm::vec3 scale = currentBoneProperty.Scale * (1.f - progress) + nextBoneProperty.Scale * progress; - - // Flag for no root motion - if (bone == RootBone && noRootMotion) { - position.x = 0; - position.z = 0; - } - - boneMatrix = (glm::translate(position) * glm::toMat4(rotation) * glm::scale(scale)); - - } else { // 1 keyframes for the current bone - currentFrame = boneKeyFrames.at(0); - boneMatrix = (glm::translate(currentFrame.BoneProperties.Position) * glm::toMat4(currentFrame.BoneProperties.Rotation) * glm::scale(currentFrame.BoneProperties.Scale)); - } - } //else { // 0 keyframes for the current bone - - // } - - return boneMatrix; -} - -glm::mat4 Skeleton::GetBoneTransform(const Bone* bone, const Animation* animation, float time, glm::mat4 childMatrix) -{ - glm::mat4 boneMatrix; - - Animation::Keyframe currentFrame; - Animation::Keyframe nextFrame; - - if (animation->JointAnimations.find(bone->ID) != animation->JointAnimations.end()) { - std::vector boneKeyFrames = animation->JointAnimations.at(bone->ID); - - if (boneKeyFrames.size() > 1) { // 2+ keyframes for the current bone - for (int index = boneKeyFrames.size()-1; index >= 0; index--) { // find the bone keyframes that surrounds the current frame - if (time >= boneKeyFrames.at(index).Time) { - currentFrame = boneKeyFrames.at(index); - nextFrame = boneKeyFrames.at((index + 1) % boneKeyFrames.size()); - break; - } - } - - float progress; - - if (nextFrame.Index == 0) { - nextFrame = currentFrame; - progress = (time - currentFrame.Time) / (animation->Duration - currentFrame.Time); - } else { - progress = (time - currentFrame.Time) / (nextFrame.Time - currentFrame.Time); - } - - if (progress > 1.0f || progress < 0.0f) { - //LOG_INFO("Progress %f", progress); - progress = glm::clamp(progress, 0.0f, 1.0f); - } - Animation::Keyframe::BoneProperty currentBoneProperty = currentFrame.BoneProperties; - Animation::Keyframe::BoneProperty nextBoneProperty = nextFrame.BoneProperties; - - glm::vec3 positionInterp = currentBoneProperty.Position * (1.f - progress) + nextBoneProperty.Position * progress; - glm::quat rotationInterp = glm::slerp(currentBoneProperty.Rotation, nextBoneProperty.Rotation, progress); - glm::vec3 scaleInterp = currentBoneProperty.Scale * (1.f - progress) + nextBoneProperty.Scale * progress; - - boneMatrix = (glm::translate(positionInterp) * glm::toMat4(rotationInterp) * glm::scale(scaleInterp)) * childMatrix; - - } else { // 1 keyframes for the current bone - currentFrame = boneKeyFrames.at(0); - boneMatrix = (glm::translate(currentFrame.BoneProperties.Position) * glm::toMat4(currentFrame.BoneProperties.Rotation) * glm::scale(currentFrame.BoneProperties.Scale)) * childMatrix; - - } - } else { // 0 keyframes for the current bone - if (bone->Parent) { - boneMatrix = bone->Parent->OffsetMatrix * glm::inverse(bone->OffsetMatrix) * childMatrix; - } else { - boneMatrix = glm::inverse(bone->OffsetMatrix) * childMatrix; - } - } - - if (bone->Parent) { - return GetBoneTransform(bone->Parent, animation, time, boneMatrix); - } else { - return boneMatrix; - } -} - -std::map Skeleton::BlendPoses(const std::map& pose1, const std::map& pose2, double weight) -{ - std::map finalPose; + float weight1 = (float)(1.0 - weight); + float weight2 = (float)(weight); for (auto& b : Bones) { int boneID = b.second->ID; - glm::mat4 blendedPose = glm::mat4(0); + PoseData blendedPose; + blendedPose.Translation = glm::vec3(0); + blendedPose.Orientation = glm::quat(); + blendedPose.Scale = glm::vec3(1); if(pose1.find(boneID) != pose1.end() && pose2.find(boneID) != pose2.end()) { - blendedPose += pose1.at(boneID) * (float)(1.0 - weight); - blendedPose += pose2.at(boneID) * (float)weight; + blendedPose.Translation = pose1.at(boneID).Translation * weight1 + pose2.at(boneID).Translation * weight2; + blendedPose.Orientation = glm::slerp(pose1.at(boneID).Orientation, pose2.at(boneID).Orientation, weight2); + blendedPose.Scale = pose1.at(boneID).Scale * weight1 + pose2.at(boneID).Scale * weight2; finalPose[boneID] = blendedPose; } else if(pose1.find(boneID) != pose1.end()) { finalPose[boneID] = pose1.at(boneID); @@ -298,9 +195,9 @@ std::map Skeleton::BlendPoses(const std::map& po return finalPose; } -std::map Skeleton::OverridePose(const std::map& overridePose, const std::map& targetPose) +std::map Skeleton::OverridePose(const std::map& overridePose, const std::map& targetPose) { - std::map finalPose; + std::map finalPose; for (auto& b : Bones) { int boneID = b.second->ID; @@ -313,18 +210,22 @@ std::map Skeleton::OverridePose(const std::map& return finalPose; } -std::map Skeleton::BlendPoseAdditive(const std::map& additivePose, const std::map& targetPose) +std::map Skeleton::BlendPoseAdditive(const std::map& additivePose, const std::map& targetPose) { - std::map finalPose; + std::map finalPose; for (auto& b : Bones) { int boneID = b.second->ID; - glm::mat4 blendedPose = glm::mat4(1); + PoseData blendedPose; + blendedPose.Translation = glm::vec3(0); + blendedPose.Orientation = glm::quat(); + blendedPose.Scale = glm::vec3(1); if (additivePose.find(boneID) != additivePose.end() && targetPose.find(boneID) != targetPose.end()) { - blendedPose = additivePose.at(boneID) * targetPose.at(boneID); + blendedPose.Translation = additivePose.at(boneID).Translation + targetPose.at(boneID).Translation; + blendedPose.Orientation = additivePose.at(boneID).Orientation * targetPose.at(boneID).Orientation; + blendedPose.Scale = additivePose.at(boneID).Scale + targetPose.at(boneID).Scale; finalPose[boneID] = blendedPose; - } else if (additivePose.find(boneID) != additivePose.end()) { finalPose[boneID] = additivePose.at(boneID); } else if (targetPose.find(boneID) != targetPose.end()) { @@ -335,9 +236,12 @@ std::map Skeleton::BlendPoseAdditive(const std::map& boneMatrices, std::vector& finalPose, std::map& boneTransforms) +void Skeleton::GetFinalPose(std::map& poseDatas, std::vector& finalPose, std::map& boneTransforms) { - AccumulateFinalPose(boneMatrices, boneTransforms, RootBone, glm::mat4(1)); + + std::map boneMatrices; + + AccumulateFinalPose(boneMatrices, poseDatas, boneTransforms, RootBone, glm::mat4(1)); for(auto& b : boneMatrices) { finalPose.push_back(b.second); @@ -345,12 +249,12 @@ void Skeleton::GetFinalPose(std::map& boneMatrices, std::vector< } -void Skeleton::AccumulateFinalPose(std::map& boneMatrices, std::map& boneTransforms, const Bone* bone, glm::mat4 parentMatrix) +void Skeleton::AccumulateFinalPose(std::map& boneMatrices, std::map& poseDatas, std::map& boneTransforms, const Bone* bone, glm::mat4 parentMatrix) { glm::mat4 boneMatrix; - if (boneMatrices.find(bone->ID) != boneMatrices.end()) { - boneMatrix = parentMatrix * boneMatrices.at(bone->ID); + if (poseDatas.find(bone->ID) != poseDatas.end()) { + boneMatrix = parentMatrix * (glm::translate(poseDatas.at(bone->ID).Translation) * glm::mat4(poseDatas.at(bone->ID).Orientation) * glm::scale(poseDatas.at(bone->ID).Scale)); boneMatrices[bone->ID] = boneMatrix * bone->OffsetMatrix; } else { if (bone->Parent) { @@ -365,7 +269,7 @@ void Skeleton::AccumulateFinalPose(std::map& boneMatrices, std:: boneTransforms[bone->ID] = boneMatrix; for (auto &child : bone->Children) { - AccumulateFinalPose(boneMatrices, boneTransforms, child, boneMatrix); + AccumulateFinalPose(boneMatrices, poseDatas, boneTransforms, child, boneMatrix); } }