Antkeeper  0.0.1
animation-system.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
26 #include <algorithm>
27 #include <execution>
28 
31 {}
32 
33 void animation_system::update(float t, float dt)
34 {
35 
36 }
37 
39 {
40  auto pose_group = registry.group<pose_component>(entt::get<scene_component>);
41  std::for_each
42  (
43  std::execution::par_unseq,
44  pose_group.begin(),
45  pose_group.end(),
46  [&](auto entity_id)
47  {
48  auto& pose = pose_group.get<pose_component>(entity_id);
49  auto& scene = pose_group.get<scene_component>(entity_id);
50 
51  auto& skeletal_mesh = static_cast<scene::skeletal_mesh&>(*scene.object);
52  for (std::size_t i = 0; i < skeletal_mesh.get_skeleton()->get_bone_count(); ++i)
53  {
54  const auto bone_index = static_cast<bone_index_type>(i);
55  const auto& previous_transform = pose.previous_pose.get_relative_transform(bone_index);
56  const auto& current_transform = pose.current_pose.get_relative_transform(bone_index);
57 
58  // Interpolate bone pose between previous and current states
59  bone_transform_type interpolated_transform;
60  interpolated_transform.translation = math::lerp(previous_transform.translation, current_transform.translation, alpha);
61  interpolated_transform.rotation = math::nlerp(previous_transform.rotation, current_transform.rotation, alpha);
62  interpolated_transform.scale = math::lerp(previous_transform.scale, current_transform.scale, alpha);
63 
64  // Update skeletal mesh bone pose with interpolated transform
65  skeletal_mesh.get_pose().set_relative_transform(static_cast<bone_index_type>(i), interpolated_transform);
66  }
67 
68  // Concatenate interpolated pose
69  skeletal_mesh.get_pose().update();
70  }
71  );
72 }
73 
animation_system(entity::registry &registry)
void update(float t, float dt) override
Perform's a system's update() function.
void interpolate(float alpha)
Abstract base class for updatable systems.
entity::registry & registry
Registry on which the system operate.
entt::registry registry
Component registry type.
Definition: registry.hpp:28