Toast Engine
Loading...
Searching...
No Matches
animation.hpp
Go to the documentation of this file.
1
12
13#pragma once
14#include "core_types.hpp"
15
16#include <cmath>
17#include <cstdint>
18#include <glm/glm.hpp>
19#include <glm/gtc/quaternion.hpp>
20#include <span>
21#include <string>
22#include <toast/export.hpp>
23#include <unordered_map>
24#include <vector>
25
26namespace assets {
27
28namespace _detail {
30inline constexpr uint32_t animation_format_version = 3;
31}
32
34enum class Interpolation : uint8_t {
35 linear,
36 step,
40};
41
43enum class TrackTarget : uint8_t {
44 translation,
45 rotation,
46 scale,
47};
48
55struct TOAST_API AnimationTrack {
56 std::string target_node;
57 TrackTarget target = TrackTarget::translation;
58 Interpolation interpolation = Interpolation::linear;
59
60 std::vector<float> times;
61 std::vector<glm::vec3> vec3_values;
62 std::vector<glm::quat> quat_values;
63
65 [[nodiscard]]
66 auto keyframeCount() const -> size_t {
67 return times.size();
68 }
69
76 [[nodiscard]]
77 auto sampleVec3(float time) const -> glm::vec3;
78
79 [[nodiscard]]
80 auto sampleQuat(float time) const -> glm::quat;
81};
82
87struct NodePose {
88 glm::vec3 translation {0.0f};
89 glm::quat rotation {1.0f, 0.0f, 0.0f, 0.0f};
90 glm::vec3 scale {1.0f};
91 bool has_translation = false;
92 bool has_rotation = false;
93 bool has_scale = false;
94};
95
97struct TOAST_API AnimationClip {
98 std::string name;
99 float duration = 0.0f;
100 std::vector<AnimationTrack> tracks;
101
107 [[nodiscard]]
108 auto sample(float time) const -> std::unordered_map<std::string, NodePose>;
109
111 [[nodiscard]]
112 auto wrap(float time) const -> float {
113 if (duration <= 0.0f) {
114 return 0.0f;
115 }
116 const float wrapped = std::fmod(time, duration);
117 return wrapped < 0.0f ? wrapped + duration : wrapped;
118 }
119};
120
127struct TOAST_API Skin {
128 std::string name;
129 std::string skeleton_root;
130 std::vector<std::string> joints;
131 std::vector<glm::mat4> inverse_bind_matrices;
132
145 [[nodiscard]]
146 auto jointMatrices(std::span<const glm::mat4> joint_world_transforms) const -> std::vector<glm::mat4>;
147};
148
157class TOAST_API Animation : public Asset {
158public:
160 explicit Animation(std::span<const uint8_t> data);
161
162 [[nodiscard]]
163 auto type() const -> std::string_view override {
164 return "animation";
165 }
166
167 [[nodiscard]]
168 auto clips() const noexcept -> const std::vector<AnimationClip>& {
169 return m_clips;
170 }
171
172 [[nodiscard]]
173 auto skins() const noexcept -> const std::vector<Skin>& {
174 return m_skins;
175 }
176
178 [[nodiscard]]
179 auto findClip(std::string_view name) const -> const AnimationClip*;
180
182 [[nodiscard]]
183 static auto toBinary(const std::vector<AnimationClip>& clips, const std::vector<Skin>& skins) -> std::vector<uint8_t>;
184
185private:
186 std::vector<AnimationClip> m_clips;
187 std::vector<Skin> m_skins;
188};
189
190}
constexpr uint32_t animation_format_version
Binary layout version for .tanim; readers reject anything they don't recognise.
Definition animation.hpp:30
Interpolation
How a sampler interpolates between two keyframes, matching glTF's three modes.
Definition animation.hpp:34
@ cubic_spline
Definition animation.hpp:39
TrackTarget
Which part of a node's transform a track drives.
Definition animation.hpp:43
Animation(std::span< const uint8_t > data)
Parses the binary .tanim representation.
Definition animation.cpp:255
auto findClip(std::string_view name) const -> const AnimationClip *
Definition animation.cpp:316
static auto toBinary(const std::vector< AnimationClip > &clips, const std::vector< Skin > &skins) -> std::vector< uint8_t >
Serialises clips + skins into the binary .tanim layout the constructor reads back.
Definition animation.cpp:325
Base types for the asset system.
A named set of tracks that play together.
Definition animation.hpp:97
float duration
seconds; the largest time across all tracks
Definition animation.hpp:99
auto wrap(float time) const -> float
Wraps time into [0, duration], for looping playback.
Definition animation.hpp:112
auto sample(float time) const -> std::unordered_map< std::string, NodePose >
Samples every track at time, grouped per target node.
Definition animation.cpp:226
One animated channel: a keyframe curve driving one component of one node's transform.
Definition animation.hpp:55
auto keyframeCount() const -> size_t
Definition animation.hpp:66
std::string target_node
node name, resolved against the instantiated tree at playback time
Definition animation.hpp:56
One node's sampled transform at a point in a clip.
Definition animation.hpp:87
A skeleton: the joint nodes a skinned mesh binds to.
Definition animation.hpp:127
std::vector< std::string > joints
node names, in the order joint indices refer to
Definition animation.hpp:130
std::string skeleton_root
node name, empty when glTF didn't specify one
Definition animation.hpp:129
std::vector< glm::mat4 > inverse_bind_matrices
parallel to joints; empty means identity for all
Definition animation.hpp:131
auto jointMatrices(std::span< const glm::mat4 > joint_world_transforms) const -> std::vector< glm::mat4 >
Builds the per-joint matrices a skinning shader multiplies vertices by.
Definition animation.cpp:212