From 6deda4129825b4b77fb7179f43d4bb7a755c4813 Mon Sep 17 00:00:00 2001 From: Frederico Linhares Date: Fri, 5 Sep 2025 09:55:25 -0300 Subject: feat Add hierarchy to skeletal mesh --- src/blu_cat/gra/skeletal_model.cpp | 40 ++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 17 deletions(-) (limited to 'src/blu_cat/gra/skeletal_model.cpp') diff --git a/src/blu_cat/gra/skeletal_model.cpp b/src/blu_cat/gra/skeletal_model.cpp index 4b44910..726b97e 100644 --- a/src/blu_cat/gra/skeletal_model.cpp +++ b/src/blu_cat/gra/skeletal_model.cpp @@ -172,30 +172,30 @@ SkeletalModel::~SkeletalModel() void SkeletalModel::tick(float delta) { - BluCat::GRA::Animation *current_animation = - &this->skeletal_mesh->animations[this->animation_index]; + BluCat::GRA::Animation ¤t_animation = + this->skeletal_mesh->animations[this->animation_index]; { // update time this->animation_time += delta; - if(this->animation_time > current_animation->final_time) + if(this->animation_time > current_animation.final_time) { - this->animation_time -= current_animation->final_time; + this->animation_time -= current_animation.final_time; for(BluCat::GRA::BoneTransform &bone_transform: - current_animation->bone_transforms) + current_animation.bone_transforms) { - bone_transform.positions.current_index = 0; - bone_transform.rotations.current_index = 0; - bone_transform.scales.current_index = 0; + bone_transform.positions.current_index = 0; + bone_transform.rotations.current_index = 0; + bone_transform.scales.current_index = 0; } } } - for(int i{0}; i < current_animation->bone_transforms.size(); i++) + for(UI16 i{0}; i < current_animation.bone_transforms.size(); i++) { - BluCat::GRA::BoneTransform *bone_transform = - ¤t_animation->bone_transforms[i]; + BluCat::GRA::BoneTransform &bone_transform = + current_animation.bone_transforms[i]; - auto position{bone_transform->positions.interpolate( + auto position{bone_transform.positions.interpolate( this->animation_time, [](glm::vec3 frame) { @@ -208,7 +208,7 @@ SkeletalModel::tick(float delta) return glm::translate(glm::mat4(1.0f), final_position); })}; - auto rotation{bone_transform->rotations.interpolate( + auto rotation{bone_transform.rotations.interpolate( this->animation_time, [](glm::quat frame) { @@ -220,7 +220,7 @@ SkeletalModel::tick(float delta) previous_frame, next_frame, scale_factor)); })}; - auto scale{bone_transform->scales.interpolate( + auto scale{bone_transform.scales.interpolate( this->animation_time, [](glm::vec3 frame) { @@ -233,9 +233,15 @@ SkeletalModel::tick(float delta) return glm::scale(glm::mat4(1.0f), scale); })}; - this->bone_transforms[i] = - this->skeletal_mesh->bones[i].offset_matrix * - (position * rotation * scale); + auto parent{this->skeletal_mesh->bones[i].parent}; + auto node_transform{(this->skeletal_mesh->bones[i].offset_matrix * + (position * rotation * scale))}; + + if(parent == i) + this->bone_transforms[i] = node_transform; + else + this->bone_transforms[i] = + this->bone_transforms[parent] * node_transform; } } -- cgit v1.2.3