diff options
Diffstat (limited to 'src/animation_impl.h')
| -rw-r--r-- | src/animation_impl.h | 96 | 
1 files changed, 96 insertions, 0 deletions
| diff --git a/src/animation_impl.h b/src/animation_impl.h new file mode 100644 index 0000000..4929b97 --- /dev/null +++ b/src/animation_impl.h | |||
| @@ -0,0 +1,96 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <gfx/animation.h> | ||
| 4 | #include <gfx/sizes.h> | ||
| 5 | |||
| 6 | #include "types.h" | ||
| 7 | |||
| 8 | #include <cstring.h> | ||
| 9 | #include <math/defs.h> | ||
| 10 | #include <math/mat4.h> | ||
| 11 | #include <math/quat.h> | ||
| 12 | #include <math/vec3.h> | ||
| 13 | |||
| 14 | #include <stddef.h> | ||
| 15 | |||
| 16 | typedef struct Buffer Buffer; | ||
| 17 | |||
| 18 | // Currently ignoring scale in skinning and animation. | ||
| 19 | // | ||
| 20 | // TODO: Simultaneous animation of disjoint animations. | ||
| 21 | |||
| 22 | /// Skeleton joint. | ||
| 23 | /// Joints are mutable and store the transform and joint matrices that result | ||
| 24 | /// from animation, aside from the inverse bind matrix. | ||
| 25 | typedef struct Joint { | ||
| 26 | joint_idx child; // First child Joint; index into Anima's joints. | ||
| 27 | joint_idx next; // Next sibling Joint; index into Anima's joints. | ||
| 28 | mat4 transform; // Local transform relative to parent. | ||
| 29 | mat4 inv_bind_matrix; // Transforms the mesh into the joint's local space. | ||
| 30 | mat4 joint_matrix; // inv(global) * global joint transform * inv(bind). | ||
| 31 | aabb3 box; // Bounding box of vertices affected by joint. | ||
| 32 | } Joint; | ||
| 33 | |||
| 34 | /// Animation skeleton. | ||
| 35 | typedef struct Skeleton { | ||
| 36 | skeleton_idx next; | ||
| 37 | size_t num_joints; | ||
| 38 | joint_idx joints[GFX_MAX_NUM_JOINTS]; // Indices into Anima's joints array. | ||
| 39 | } Skeleton; | ||
| 40 | |||
| 41 | /// A keyframe of animation. | ||
| 42 | typedef struct Keyframe { | ||
| 43 | R time; // Start time in [0, end animation time] | ||
| 44 | union { | ||
| 45 | vec3 translation; | ||
| 46 | quat rotation; | ||
| 47 | }; | ||
| 48 | } Keyframe; | ||
| 49 | |||
| 50 | /// Animation channel. | ||
| 51 | typedef struct Channel { | ||
| 52 | joint_idx target; // Index into Anima's joints array. | ||
| 53 | ChannelType type; | ||
| 54 | AnimationInterpolation interpolation; | ||
| 55 | size_t num_keyframes; | ||
| 56 | Keyframe keyframes[GFX_MAX_NUM_KEYFRAMES]; | ||
| 57 | } Channel; | ||
| 58 | |||
| 59 | /// A skeletal animation. | ||
| 60 | typedef struct Animation { | ||
| 61 | animation_idx next; | ||
| 62 | sstring name; | ||
| 63 | R duration; | ||
| 64 | size_t num_channels; | ||
| 65 | Channel channels[GFX_MAX_NUM_CHANNELS]; | ||
| 66 | } Animation; | ||
| 67 | |||
| 68 | /// Animation state. | ||
| 69 | /// | ||
| 70 | /// This represents the current state of an animation. | ||
| 71 | typedef struct AnimationState { | ||
| 72 | R start_time; // Time when the current animation started playing. -1 means the | ||
| 73 | // animation playback has not yet been initialized. | ||
| 74 | animation_idx animation; // Current animation. 0 = no animation. | ||
| 75 | bool loop; | ||
| 76 | } AnimationState; | ||
| 77 | |||
| 78 | /// Animation object. | ||
| 79 | /// | ||
| 80 | /// This is the top-level animation object that encapsulates everything | ||
| 81 | /// necessary for animation. | ||
| 82 | /// | ||
| 83 | /// For lack of a better name, this is called an Anima. It is short and the | ||
| 84 | /// Latin root of animation. | ||
| 85 | /// | ||
| 86 | /// The last joint of the joints array at index 'num_joints - 1' is the root of | ||
| 87 | /// all skeletons; specifically, the root of all joints that otherwise would | ||
| 88 | /// have no parent (a skeleton need not have its own root and can be a set of | ||
| 89 | /// disjoint node hierarchies). | ||
| 90 | typedef struct Anima { | ||
| 91 | skeleton_idx skeleton; // Index of first skeleton. | ||
| 92 | animation_idx animation; // Index of first animation. | ||
| 93 | AnimationState state; // Current animation state. | ||
| 94 | size_t num_joints; // Number of actual joints in the array. | ||
| 95 | Joint joints[GFX_MAX_NUM_JOINTS]; // Shared by all skeletons. | ||
| 96 | } Anima; | ||
