diff --git a/include/Engine/Rendering/AnimationSystem.h b/include/Engine/Rendering/AnimationSystem.h index dbe4b3fc..d05dc765 100644 --- a/include/Engine/Rendering/AnimationSystem.h +++ b/include/Engine/Rendering/AnimationSystem.h @@ -9,20 +9,17 @@ #include "Rendering/Model.h" #include "Rendering/EAnimationComplete.h" #include "Rendering/Skeleton.h" -#include +#include "Rendering/BlendTree.h" -class AnimationSystem : public PureSystem +class AnimationSystem : public ImpureSystem { public: - AnimationSystem(SystemParams params) - : System(params) - , PureSystem("Animation") - { - - } + AnimationSystem(SystemParams params); ~AnimationSystem() { } - virtual void UpdateComponent(EntityWrapper& entity, ComponentWrapper& animationComponent, double dt) override; + virtual void Update(double dt) override; private: + void CreateBlendTrees(); + void UpdateAnimations(double dt); }; diff --git a/include/Engine/Rendering/BlendTree.h b/include/Engine/Rendering/BlendTree.h index 8d192cfe..a472465b 100644 --- a/include/Engine/Rendering/BlendTree.h +++ b/include/Engine/Rendering/BlendTree.h @@ -62,7 +62,7 @@ public: std::vector GetFinalPose() { return m_FinalPose; } - + glm::mat4 GetBoneTransform(int boneID); void PrintTree(); @@ -72,6 +72,8 @@ private: Node* m_Root = nullptr; std::vector m_FinalPose; + std::map m_FinalBoneTransforms; + std::vector AccumulateFinalPose(); BlendTree::Node* FillTreeByName(Node* parentNode, std::string name, EntityWrapper parentEntity); diff --git a/include/Engine/Rendering/BoneAttachmentSystem.h b/include/Engine/Rendering/BoneAttachmentSystem.h index 55c2a1c8..2791d877 100644 --- a/include/Engine/Rendering/BoneAttachmentSystem.h +++ b/include/Engine/Rendering/BoneAttachmentSystem.h @@ -8,6 +8,7 @@ #include "Core/ResourceManager.h" #include "Rendering/Model.h" #include "Rendering/Skeleton.h" +#include "Rendering/BlendTree.h" //Needs to be a higher orderlevel than AnimationSystem class BoneAttachmentSystem : public PureSystem diff --git a/include/Engine/Rendering/ModelJob.h b/include/Engine/Rendering/ModelJob.h index 4ea7a1f6..adc21c6f 100644 --- a/include/Engine/Rendering/ModelJob.h +++ b/include/Engine/Rendering/ModelJob.h @@ -125,7 +125,10 @@ struct ModelJob : RenderJob if (Skeleton != nullptr) { EntityWrapper entityWrapper = EntityWrapper(world, modelComponent.EntityID); - BlendTree = std::shared_ptr<::BlendTree>(new ::BlendTree(entityWrapper, Skeleton)); + + if(Skeleton->BlendTrees.find(entityWrapper) != Skeleton->BlendTrees.end()) { + BlendTree = Skeleton->BlendTrees.at(entityWrapper); + } } } }; diff --git a/include/Engine/Rendering/Skeleton.h b/include/Engine/Rendering/Skeleton.h index fa5a329d..ffa08452 100644 --- a/include/Engine/Rendering/Skeleton.h +++ b/include/Engine/Rendering/Skeleton.h @@ -6,27 +6,9 @@ #include "../GLM.h" #include #include +#include "../Core/EntityWrapper.h" -//struct Bone -//{ -// Bone(std::string name, glm::mat4 offsetMatrix) -// : Name(name) -// , OffsetMatrix(offsetMatrix) -// { } -// -// ~Bone() -// { -// for (auto kv : Children) { -// delete kv.second; -// } -// } -// -// std::string Name; -// glm::mat4 OffsetMatrix; -// glm::mat4 LocalMatrix; -// -// std::map Children; -//}; +class BlendTree; class Skeleton { @@ -75,6 +57,8 @@ public: std::map Bones; + std::unordered_map> BlendTrees; + // Attach a new bone to the skeleton // Returns: New bone index int CreateBone(int ID, int parentID, std::string name, glm::mat4 offsetMatrix); @@ -86,13 +70,13 @@ public: std::map BlendPoses(const std::map& pose1, const std::map& pose2, float weight); std::map OverridePose(const std::map& overridePose, const std::map& targetPose); std::map BlendPoseAdditive(const std::map& additivePose, const std::map& targetPose); - std::vector GetFinalPose(std::map& boneMatrices); + 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, const Bone* bone, glm::mat4 parentMatrix); + 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); diff --git a/include/Game/Systems/LifetimeSystem.h b/include/Game/Systems/LifetimeSystem.h index 6dee644d..d19308d5 100644 --- a/include/Game/Systems/LifetimeSystem.h +++ b/include/Game/Systems/LifetimeSystem.h @@ -10,7 +10,7 @@ public: : System(params) , PureSystem("Lifetime") { - LOG_INFO("ASDASDASSA"); + } virtual void Update(double dt) override; diff --git a/resources/Schema/Entities/AnimationTests2.xml b/resources/Schema/Entities/AnimationTests2.xml index 4779bbaf..ca7c705d 100644 --- a/resources/Schema/Entities/AnimationTests2.xml +++ b/resources/Schema/Entities/AnimationTests2.xml @@ -9,19 +9,22 @@ - + + - + + 0.10000047832727432 + Models/Widgets/Lights/DirectionalLightWidget.mesh - + @@ -30,36 +33,22 @@ - 10 + 8 + 0.20000000298023224 - + - - - - - - 10 - - - - - - - - + Models/Core/UnitPlane.mesh - - - + @@ -85,7 +74,9 @@ Models/Weapons/Blue/AssaultWeaponBlue.mesh + + @@ -94,7 +85,6 @@ AimRifleA - true @@ -114,7 +104,7 @@ ShootFastRifleU - + 1 @@ -123,63 +113,28 @@ - - BlendWalkRun - StrafeAnimation - 1 - + + RunF + + 1 + - - - - - StrafeRightF - - 1 - - - - - - - - - RunAnimtaion - WalkAnimation - 0.43000054359436035 - - - - - - - - RunF - - 1 - - - - - - - - - WalkF - - 1 - - - - - - - - + + + + + 3 + + + + + + + @@ -206,7 +161,9 @@ Models/Weapons/Blue/AssaultWeaponBlue.mesh + + @@ -215,7 +172,8 @@ AimRifleA - + + 0.5 true @@ -235,7 +193,7 @@ ShootFastRifleU - + 1 @@ -247,7 +205,7 @@ BlendWalkRun StrafeAnimation - 1 + 0 @@ -255,8 +213,8 @@ - StrafeRightF - + StrafeLeftF + 1 @@ -268,7 +226,7 @@ RunAnimtaion WalkAnimation - 0.43000054359436035 + 1 @@ -277,7 +235,7 @@ RunF - + 1 @@ -288,7 +246,7 @@ WalkF - + 1 @@ -301,6 +259,20 @@ + + + + + 3 + 0.80000001192092896 + 0.30000001192092896 + + + + + + + @@ -327,7 +299,9 @@ Models/Weapons/Blue/AssaultWeaponBlue.mesh + + @@ -336,7 +310,7 @@ AimRifleA - + true @@ -355,8 +329,8 @@ - ShootFastRifleU - + ShootRifleU + 1 @@ -368,7 +342,7 @@ BlendWalkRun StrafeAnimation - 1 + 0 @@ -377,7 +351,7 @@ StrafeRightF - + 1 @@ -398,7 +372,7 @@ RunF - + 1 @@ -409,7 +383,7 @@ WalkF - + 1 @@ -422,6 +396,19 @@ + + + + + 3 + 0.30000001192092896 + + + + + + + @@ -448,7 +435,9 @@ Models/Weapons/Blue/AssaultWeaponBlue.mesh + + @@ -457,7 +446,7 @@ AimRifleA - + true @@ -473,17 +462,6 @@ - - - - ShootFastRifleU - - 1 - - - - - @@ -498,7 +476,7 @@ StrafeRightF - + 1 @@ -510,7 +488,7 @@ RunAnimtaion WalkAnimation - 0.43000054359436035 + 1 @@ -518,8 +496,8 @@ - RunF - + CrouchWalkF + 1 @@ -530,7 +508,7 @@ WalkF - + 1 @@ -541,10 +519,50 @@ + + + + ReloadSwitchU + + 1 + + + + + + + + + + 3 + + + + + + + + + + + Models/Core/UnitSphere.mesh + + + + + 3 + + + + + + + + diff --git a/src/Engine/Rendering/AnimationSystem.cpp b/src/Engine/Rendering/AnimationSystem.cpp index 1b260b08..9a74da29 100644 --- a/src/Engine/Rendering/AnimationSystem.cpp +++ b/src/Engine/Rendering/AnimationSystem.cpp @@ -1,66 +1,109 @@ #include "Rendering/AnimationSystem.h" -void AnimationSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& animationComponent, double dt) +AnimationSystem::AnimationSystem(SystemParams params) + : System(params) { - - EntityWrapper parent = entity.FirstParentWithComponent("Model"); - Model* model; - try { - model = ResourceManager::Load<::Model, true>(parent["Model"]["Resource"]); - } catch (const std::exception&) { +} + +void AnimationSystem::Update(double dt) +{ + UpdateAnimations(dt); + CreateBlendTrees(); +} + +void AnimationSystem::CreateBlendTrees() +{ + auto modelComponents = m_World->GetComponents("Model"); + if (modelComponents == nullptr) { return; } - Skeleton* skeleton = model->m_RawModel->m_Skeleton; - if (skeleton == nullptr) { + for (auto& modelC : *modelComponents) { + EntityWrapper entity = EntityWrapper(m_World, modelC.EntityID); + + Model* model; + try { + model = ResourceManager::Load<::Model, true>(entity["Model"]["Resource"]); + } catch (const std::exception&) { + continue;; + } + + Skeleton* skeleton = model->m_RawModel->m_Skeleton; + if (skeleton == nullptr) { + continue; + } + + skeleton->BlendTrees[entity] = std::shared_ptr(new BlendTree(entity, skeleton)); + } +} + +void AnimationSystem::UpdateAnimations(double dt) +{ + auto animationComponents = m_World->GetComponents("Animation"); + if(animationComponents == nullptr) { return; } - for (int i = 1; i <= 1; i++) { - const Skeleton::Animation* animation = skeleton->GetAnimation(animationComponent["AnimationName"]); + for (auto& animationC : *animationComponents) { + EntityWrapper entity = EntityWrapper(m_World, animationC.EntityID); + EntityWrapper parent = entity.FirstParentWithComponent("Model"); + + Model* model; + try { + model = ResourceManager::Load<::Model, true>(parent["Model"]["Resource"]); + } catch (const std::exception&) { + return; + } + + Skeleton* skeleton = model->m_RawModel->m_Skeleton; + if (skeleton == nullptr) { + return; + } + + const Skeleton::Animation* animation = skeleton->GetAnimation(animationC["AnimationName"]); if (animation == nullptr) { continue;; } - double animationSpeed = (double)animationComponent["Speed"]; + double animationSpeed = (double)animationC["Speed"]; if (animationSpeed != 0.0) { - double nextTime = (double)animationComponent["Time"] + animationSpeed * dt; + double nextTime = (double)animationC["Time"] + animationSpeed * dt; - if (!(bool)animationComponent["Loop"]) { + if (!(bool)animationC["Loop"]) { if (nextTime > animation->Duration) { nextTime = animation->Duration; Events::AnimationComplete e; e.Entity = entity; - e.Name = (std::string)animationComponent["AnimationName"]; + e.Name = (std::string)animationC["AnimationName"]; m_EventBroker->Publish(e); } else if (nextTime < 0) { Events::AnimationComplete e; e.Entity = entity; - e.Name = (std::string)animationComponent["AnimationName"]; + e.Name = (std::string)animationC["AnimationName"]; m_EventBroker->Publish(e); nextTime = 0; } - (double&)animationComponent["Speed"] = 0.0; - + (double&)animationC["Speed"] = 0.0; + } else { if (nextTime > animation->Duration) { Events::AnimationComplete e; e.Entity = entity; - e.Name = (std::string)animationComponent["AnimationName"]; + e.Name = (std::string)animationC["AnimationName"]; m_EventBroker->Publish(e); - while(nextTime > animation->Duration) { + while (nextTime > animation->Duration) { nextTime -= animation->Duration; } } else if (nextTime < 0) { Events::AnimationComplete e; e.Entity = entity; - e.Name = (std::string)animationComponent["AnimationName"]; + e.Name = (std::string)animationC["AnimationName"]; m_EventBroker->Publish(e); while (nextTime < 0) { @@ -69,8 +112,8 @@ void AnimationSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& a } } - (double&)animationComponent["Time"] = nextTime; + (double&)animationC["Time"] = nextTime; } - } + } } diff --git a/src/Engine/Rendering/BlendTree.cpp b/src/Engine/Rendering/BlendTree.cpp index 1d628a06..0d87665f 100644 --- a/src/Engine/Rendering/BlendTree.cpp +++ b/src/Engine/Rendering/BlendTree.cpp @@ -76,6 +76,17 @@ BlendTree::~BlendTree() } } + +glm::mat4 BlendTree::GetBoneTransform(int boneID) +{ + if(m_FinalBoneTransforms.find(boneID) != m_FinalBoneTransforms.end()) { + return m_FinalBoneTransforms.at(boneID); + } else { + return glm::mat4(1); + } + +} + void BlendTree::PrintTree() { Node* currentNode = m_Root; @@ -213,7 +224,7 @@ std::vector BlendTree::AccumulateFinalPose() std::map pose; Blend(pose); - finalPose = m_Skeleton->GetFinalPose(pose); + m_Skeleton->GetFinalPose(pose, finalPose, m_FinalBoneTransforms); return finalPose; } diff --git a/src/Engine/Rendering/BoneAttachmentSystem.cpp b/src/Engine/Rendering/BoneAttachmentSystem.cpp index 0e259f4b..43dc8634 100644 --- a/src/Engine/Rendering/BoneAttachmentSystem.cpp +++ b/src/Engine/Rendering/BoneAttachmentSystem.cpp @@ -8,10 +8,13 @@ void BoneAttachmentSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapp return; } - auto parent = entity.FirstParentWithComponent("Animation"); - if (!parent.HasComponent("Model")) { + auto parent = entity.FirstParentWithComponent("Model"); + + if(!parent.Valid()) { return; } + + Model* model; try { model = ResourceManager::Load<::Model, true>(parent["Model"]["Resource"]); @@ -35,61 +38,29 @@ void BoneAttachmentSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapp return; } - /* std::vector<::Skeleton::AnimationData> Animations; - ::Skeleton::AnimationOffset AnimationOffset; - glm::mat4 boneTransform; + if (skeleton->BlendTrees.find(parent) != skeleton->BlendTrees.end()) { - if (parent.HasComponent("Animation")) { - ::Skeleton::AnimationData animationData; - animationData.animation = model->m_RawModel->m_Skeleton->GetAnimation(parent["Animation"]["AnimationName"]); - if (animationData.animation != nullptr) { - animationData.time = (double)parent["Animation"]["Time"]; - Animations.push_back(animationData); - } - } - if (parent.HasComponent("AnimationOffset")) { - AnimationOffset.animation = model->m_RawModel->m_Skeleton->GetAnimation(parent["AnimationOffset"]["AnimationName"]); - AnimationOffset.time = (double)parent["AnimationOffset"]["Time"]; + glm::mat4 boneTransform = skeleton->BlendTrees.at(parent)->GetBoneTransform(id); - if(AnimationOffset.animation != nullptr) { - boneTransform = skeleton->GetBoneTransform(false, skeleton->Bones.at(id), Animations, AnimationOffset, glm::mat4(1)); - } else { - boneTransform = skeleton->GetBoneTransform(false, skeleton->Bones.at(id), Animations, glm::mat4(1)); - } + glm::vec3 scale; + glm::quat rotation; + glm::vec3 translation; + glm::vec3 skew; + glm::vec4 perspective; + glm::decompose(boneTransform, scale, rotation, translation, skew, perspective); + + glm::vec3 angles = glm::vec3(-glm::pitch(rotation), -glm::yaw(rotation), -glm::roll(rotation)); - } else { - boneTransform = skeleton->GetBoneTransform(false, skeleton->Bones.at(id), Animations, glm::mat4(1)); + if ((bool)entity["BoneAttachment"]["InheritPosition"]) { + (glm::vec3&)entity["Transform"]["Position"] = translation + (glm::vec3)entity["BoneAttachment"]["PositionOffset"]; + } + if ((bool)entity["BoneAttachment"]["InheritOrientation"]) { + (glm::vec3&)entity["Transform"]["Orientation"] = angles + (glm::vec3)entity["BoneAttachment"]["OrientationOffset"]; + } + if ((bool)entity["BoneAttachment"]["InheritScale"]) { + (glm::vec3&)entity["Transform"]["Scale"] = scale * (glm::vec3)entity["BoneAttachment"]["ScaleOffset"]; + } } - - - glm::vec3 scale; - glm::quat rotation; - glm::vec3 translation; - glm::vec3 skew; - glm::vec4 perspective; - glm::decompose(boneTransform, scale, rotation, translation, skew, perspective); - - glm::vec3 angles = glm::vec3(-glm::pitch(rotation), -glm::yaw(rotation), -glm::roll(rotation)); -/ * - - angles.y = asin(-boneTransform[0][2]); - if (cos(angles.y) != 0) { - angles.x = atan2(boneTransform[1][2], boneTransform[2][2]); - angles.z = atan2(boneTransform[0][1], boneTransform[0][0]); - } else { - angles.x = atan2(-boneTransform[2][0], boneTransform[1][1]); - angles.z = 0; - }* / - - if ((bool)entity["BoneAttachment"]["InheritPosition"]) { - (glm::vec3&)entity["Transform"]["Position"] = translation + (glm::vec3)entity["BoneAttachment"]["PositionOffset"]; - } - if ((bool)entity["BoneAttachment"]["InheritOrientation"]) { - (glm::vec3&)entity["Transform"]["Orientation"] = angles + (glm::vec3)entity["BoneAttachment"]["OrientationOffset"]; - } - if ((bool)entity["BoneAttachment"]["InheritScale"]) { - (glm::vec3&)entity["Transform"]["Scale"] = scale * (glm::vec3)entity["BoneAttachment"]["ScaleOffset"]; - }*/ } diff --git a/src/Engine/Rendering/DrawFinalPass.cpp b/src/Engine/Rendering/DrawFinalPass.cpp index b139f2ba..7360d064 100644 --- a/src/Engine/Rendering/DrawFinalPass.cpp +++ b/src/Engine/Rendering/DrawFinalPass.cpp @@ -337,9 +337,11 @@ void DrawFinalPass::DrawModelRenderQueues(std::list>& BindExplosionUniforms(explosionSkinnedHandle, explosionEffectJob, scene); //bind textures BindExplosionTextures(explosionSkinnedHandle, explosionEffectJob); - std::vector frameBones; - frameBones = explosionEffectJob->BlendTree->GetFinalPose(); - glUniformMatrix4fv(glGetUniformLocation(explosionSkinnedHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); + if (explosionEffectJob->BlendTree != nullptr) { + std::vector frameBones; + frameBones = explosionEffectJob->BlendTree->GetFinalPose(); + glUniformMatrix4fv(glGetUniformLocation(explosionSkinnedHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); + } } else { m_ExplosionEffectProgram->Bind(); GLERROR("Bind ExplosionEffect program"); @@ -360,10 +362,11 @@ void DrawFinalPass::DrawModelRenderQueues(std::list>& //bind textures BindExplosionTextures(explosionSplatMapSkinnedHandle, explosionEffectJob); GLERROR("asdasd"); - std::vector frameBones; - frameBones = explosionEffectJob->BlendTree->GetFinalPose(); - glUniformMatrix4fv(glGetUniformLocation(explosionSplatMapSkinnedHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); - + if (explosionEffectJob->BlendTree != nullptr) { + std::vector frameBones; + frameBones = explosionEffectJob->BlendTree->GetFinalPose(); + glUniformMatrix4fv(glGetUniformLocation(explosionSplatMapSkinnedHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); + } } else { m_ExplosionEffectSplatMapProgram->Bind(); GLERROR("Bind ExplosionEffectSplatMap program"); @@ -401,10 +404,12 @@ void DrawFinalPass::DrawModelRenderQueues(std::list>& BindModelUniforms(forwardSkinnedHandle, modelJob, scene); //bind textures BindModelTextures(forwardSkinnedHandle, modelJob); - std::vector frameBones; - frameBones = modelJob->BlendTree->GetFinalPose(); - glUniformMatrix4fv(glGetUniformLocation(forwardSkinnedHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); + if (modelJob->BlendTree != nullptr) { + std::vector frameBones; + frameBones = modelJob->BlendTree->GetFinalPose(); + glUniformMatrix4fv(glGetUniformLocation(forwardSkinnedHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); + } } else { m_ForwardPlusProgram->Bind(); GLERROR("Bind ForwardPlusProgram"); @@ -425,10 +430,11 @@ void DrawFinalPass::DrawModelRenderQueues(std::list>& //bind textures BindModelTextures(forwardSplatMapSkinnedHandle, modelJob); GLERROR("asdasd"); - std::vector frameBones; - frameBones = modelJob->BlendTree->GetFinalPose(); - glUniformMatrix4fv(glGetUniformLocation(forwardSplatMapSkinnedHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); - + if (modelJob->BlendTree != nullptr) { + std::vector frameBones; + frameBones = modelJob->BlendTree->GetFinalPose(); + glUniformMatrix4fv(glGetUniformLocation(forwardSplatMapSkinnedHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); + } } else { m_ForwardPlusSplatMapProgram->Bind(); GLERROR("Bind SplatMap program"); @@ -469,9 +475,11 @@ void DrawFinalPass::DrawShieldToStencilBuffer(std::listMatrix)); glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "V"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ViewMatrix())); glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "P"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ProjectionMatrix())); - std::vector frameBones; - frameBones = modelJob->BlendTree->GetFinalPose(); - glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); + if (modelJob->BlendTree != nullptr) { + std::vector frameBones; + frameBones = modelJob->BlendTree->GetFinalPose(); + glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); + } } else { m_ShieldToStencilProgram->Bind(); GLuint shaderHandle = m_ShieldToStencilProgram->GetHandle(); @@ -523,10 +531,11 @@ void DrawFinalPass::DrawShieldedModelRenderQueue(std::list frameBones; - frameBones = explosionEffectJob->BlendTree->GetFinalPose(); - glUniformMatrix4fv(glGetUniformLocation(explosionHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); - + if (explosionEffectJob->BlendTree != nullptr) { + std::vector frameBones; + frameBones = explosionEffectJob->BlendTree->GetFinalPose(); + glUniformMatrix4fv(glGetUniformLocation(explosionHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); + } if (GLERROR("Animation")) { continue; } @@ -558,10 +567,11 @@ void DrawFinalPass::DrawShieldedModelRenderQueue(std::list frameBones; - frameBones = modelJob->BlendTree->GetFinalPose(); - glUniformMatrix4fv(glGetUniformLocation(forwardHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); - + if (modelJob->BlendTree != nullptr) { + std::vector frameBones; + frameBones = modelJob->BlendTree->GetFinalPose(); + glUniformMatrix4fv(glGetUniformLocation(forwardHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); + } //draw glBindVertexArray(modelJob->Model->VAO); @@ -590,10 +600,11 @@ void DrawFinalPass::DrawToDepthBuffer(std::list>& job glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "P"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ProjectionMatrix())); glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix)); - std::vector frameBones; - frameBones = modelJob->BlendTree->GetFinalPose(); - glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); - + if (modelJob->BlendTree != nullptr) { + std::vector frameBones; + frameBones = modelJob->BlendTree->GetFinalPose(); + glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); + } } else { m_FillDepthBufferProgram->Bind(); GLuint shaderHandle = m_FillDepthBufferProgram->GetHandle(); diff --git a/src/Engine/Rendering/PickingPass.cpp b/src/Engine/Rendering/PickingPass.cpp index 64483755..4d156e46 100644 --- a/src/Engine/Rendering/PickingPass.cpp +++ b/src/Engine/Rendering/PickingPass.cpp @@ -100,12 +100,10 @@ void PickingPass::Draw(RenderScene& scene) glUniformMatrix4fv(glGetUniformLocation(shaderSkinnedHandle, "P"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ProjectionMatrix())); glUniform2fv(glGetUniformLocation(shaderSkinnedHandle, "PickingColor"), 1, glm::value_ptr(glm::vec2(pickColor[0], pickColor[1]))); - if (modelJob->Model->m_RawModel->m_Skeleton != nullptr) { - + if (modelJob->BlendTree != nullptr) { std::vector frameBones; frameBones = modelJob->BlendTree->GetFinalPose(); glUniformMatrix4fv(glGetUniformLocation(shaderSkinnedHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); - } } else { m_PickingProgram->Bind(); @@ -155,9 +153,11 @@ void PickingPass::Draw(RenderScene& scene) glUniformMatrix4fv(glGetUniformLocation(shaderSkinnedHandle, "P"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ProjectionMatrix())); glUniform2fv(glGetUniformLocation(shaderSkinnedHandle, "PickingColor"), 1, glm::value_ptr(glm::vec2(pickColor[0], pickColor[1]))); - std::vector frameBones; - frameBones = modelJob->BlendTree->GetFinalPose(); - glUniformMatrix4fv(glGetUniformLocation(shaderSkinnedHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); + if (modelJob->BlendTree != nullptr) { + std::vector frameBones; + frameBones = modelJob->BlendTree->GetFinalPose(); + glUniformMatrix4fv(glGetUniformLocation(shaderSkinnedHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); + } } else { m_PickingProgram->Bind(); glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix)); @@ -206,9 +206,11 @@ void PickingPass::Draw(RenderScene& scene) glUniformMatrix4fv(glGetUniformLocation(shaderSkinnedHandle, "P"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ProjectionMatrix())); glUniform2fv(glGetUniformLocation(shaderSkinnedHandle, "PickingColor"), 1, glm::value_ptr(glm::vec2(pickColor[0], pickColor[1]))); - std::vector frameBones; - frameBones = modelJob->BlendTree->GetFinalPose(); - glUniformMatrix4fv(glGetUniformLocation(shaderSkinnedHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); + if (modelJob->BlendTree != nullptr) { + std::vector frameBones; + frameBones = modelJob->BlendTree->GetFinalPose(); + glUniformMatrix4fv(glGetUniformLocation(shaderSkinnedHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); + } } else { m_PickingProgram->Bind(); @@ -261,12 +263,10 @@ void PickingPass::Draw(RenderScene& scene) glUniformMatrix4fv(glGetUniformLocation(shaderSkinnedHandle, "P"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ProjectionMatrix())); glUniform2fv(glGetUniformLocation(shaderSkinnedHandle, "PickingColor"), 1, glm::value_ptr(glm::vec2(pickColor[0], pickColor[1]))); - if (modelJob->Model->m_RawModel->m_Skeleton != nullptr) { - + if (modelJob->BlendTree != nullptr) { std::vector frameBones; frameBones = modelJob->BlendTree->GetFinalPose(); glUniformMatrix4fv(glGetUniformLocation(shaderSkinnedHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); - } } else { m_PickingProgram->Bind(); diff --git a/src/Engine/Rendering/Skeleton.cpp b/src/Engine/Rendering/Skeleton.cpp index 077e9d02..4583de92 100644 --- a/src/Engine/Rendering/Skeleton.cpp +++ b/src/Engine/Rendering/Skeleton.cpp @@ -94,7 +94,7 @@ void Skeleton::AdditiveBoneTransforms(const Animation* animation, double time, s { if (animation->JointAnimations.find(bone->ID) != animation->JointAnimations.end()) { glm::mat4 refPose = GetAdditiveBonePose(bone, animation, 0.0); - glm::mat4 srcPose = GetAdditiveBonePose(bone, animation, time); + glm::mat4 srcPose = GetAdditiveBonePose(bone, animation, time + 1.0/60.0); glm::mat4 boneMatrix = srcPose * glm::inverse(refPose); boneMatrices[bone->ID] = boneMatrix; } @@ -335,21 +335,17 @@ std::map Skeleton::BlendPoseAdditive(const std::map Skeleton::GetFinalPose(std::map& boneMatrices) +void Skeleton::GetFinalPose(std::map& boneMatrices, std::vector& finalPose, std::map& boneTransforms) { - std::vector finalPose; - - AccumulateFinalPose(boneMatrices, RootBone, glm::mat4(1)); - + AccumulateFinalPose(boneMatrices, boneTransforms, RootBone, glm::mat4(1)); for(auto& b : boneMatrices) { finalPose.push_back(b.second); } - return finalPose; } -void Skeleton::AccumulateFinalPose(std::map& boneMatrices, const Bone* bone, glm::mat4 parentMatrix) +void Skeleton::AccumulateFinalPose(std::map& boneMatrices, std::map& boneTransforms, const Bone* bone, glm::mat4 parentMatrix) { glm::mat4 boneMatrix; @@ -370,8 +366,10 @@ void Skeleton::AccumulateFinalPose(std::map& boneMatrices, const } } + boneTransforms[bone->ID] = boneMatrix; + for (auto &child : bone->Children) { - AccumulateFinalPose(boneMatrices, child, boneMatrix); + AccumulateFinalPose(boneMatrices, boneTransforms, child, boneMatrix); } }