From 600a4f7586a31990cfdb02ab811768ca6afc909e Mon Sep 17 00:00:00 2001 From: Frederico Linhares Date: Wed, 19 Mar 2025 16:20:37 -0300 Subject: fixt Use bone from mesh when animating model * src/blu_cat/gra/skeletal_model.cpp (SkeletalModel::tick): Use bone position from mesh. Not using it was a bug. --- src/blu_cat/com/binary_writer.cpp | 2 - src/blu_cat/gra/skeletal_model.cpp | 75 +++++++++++++++++++------------------ test/meshes/cuboid.cgmesh | Bin 2800 -> 2800 bytes 3 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/blu_cat/com/binary_writer.cpp b/src/blu_cat/com/binary_writer.cpp index 0c4cace..7a987d4 100644 --- a/src/blu_cat/com/binary_writer.cpp +++ b/src/blu_cat/com/binary_writer.cpp @@ -16,8 +16,6 @@ #include "binary_writer.hpp" -#include - namespace { diff --git a/src/blu_cat/gra/skeletal_model.cpp b/src/blu_cat/gra/skeletal_model.cpp index b58982d..4b44910 100644 --- a/src/blu_cat/gra/skeletal_model.cpp +++ b/src/blu_cat/gra/skeletal_model.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 Frederico de Oliveira Linhares * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -192,47 +192,50 @@ SkeletalModel::tick(float delta) for(int i{0}; i < current_animation->bone_transforms.size(); i++) { - BluCat::GRA::BoneTransform *bone_transform = ¤t_animation->bone_transforms[i]; + BluCat::GRA::BoneTransform *bone_transform = + ¤t_animation->bone_transforms[i]; auto position{bone_transform->positions.interpolate( - this->animation_time, - [](glm::vec3 frame) - { - return glm::translate(glm::mat4(1.0f), frame); - }, - [](glm::vec3 previous_frame, glm::vec3 next_frame, float scale_factor) - { - glm::vec3 final_position{glm::mix( - previous_frame, next_frame, scale_factor)}; - return glm::translate(glm::mat4(1.0f), final_position); - })}; + this->animation_time, + [](glm::vec3 frame) + { + return glm::translate(glm::mat4(1.0f), frame); + }, + [](glm::vec3 previous_frame, glm::vec3 next_frame, float scale_factor) + { + glm::vec3 final_position{glm::mix( + previous_frame, next_frame, scale_factor)}; + return glm::translate(glm::mat4(1.0f), final_position); + })}; auto rotation{bone_transform->rotations.interpolate( - this->animation_time, - [](glm::quat frame) - { - return glm::toMat4(glm::normalize(frame)); - }, - [](glm::quat previous_frame, glm::quat next_frame, float scale_factor) - { - return glm::toMat4(glm::slerp( - previous_frame, next_frame, scale_factor)); - })}; + this->animation_time, + [](glm::quat frame) + { + return glm::toMat4(glm::normalize(frame)); + }, + [](glm::quat previous_frame, glm::quat next_frame, float scale_factor) + { + return glm::toMat4(glm::slerp( + previous_frame, next_frame, scale_factor)); + })}; auto scale{bone_transform->scales.interpolate( - this->animation_time, - [](glm::vec3 frame) - { - return glm::scale(glm::mat4(1.0f), frame); - }, - [](glm::vec3 previous_frame, glm::vec3 next_frame, float scale_factor) - { - glm::vec3 scale{glm::mix( - previous_frame, next_frame, scale_factor)}; - return glm::scale(glm::mat4(1.0f), scale); - })}; - - this->bone_transforms[i] = position * rotation * scale; + this->animation_time, + [](glm::vec3 frame) + { + return glm::scale(glm::mat4(1.0f), frame); + }, + [](glm::vec3 previous_frame, glm::vec3 next_frame, float scale_factor) + { + glm::vec3 scale{glm::mix( + previous_frame, next_frame, scale_factor)}; + return glm::scale(glm::mat4(1.0f), scale); + })}; + + this->bone_transforms[i] = + this->skeletal_mesh->bones[i].offset_matrix * + (position * rotation * scale); } } diff --git a/test/meshes/cuboid.cgmesh b/test/meshes/cuboid.cgmesh index aa95118..79a643a 100644 Binary files a/test/meshes/cuboid.cgmesh and b/test/meshes/cuboid.cgmesh differ -- cgit v1.2.3