Untitled
Loading...
Searching...
No Matches
animation.c
Go to the documentation of this file.
1
2#include "core/engine_types.h"
3#include "load/init.h"
4#include "utilities/types.h"
5#include <string.h>
7#include "utilities/mathf.h"
8
9
11 if (!anim) return 0;
12 float max_time = 0.0f;
13 for (int i = 0; i < anim->sampler_count; i++) {
14 AnimationSampler *s = &anim->samplers[i];
15 if (s->count > 0) {
16 float last_time = s->times[s->count - 1];
17 if (last_time > max_time) max_time = last_time;
18 }
19 }
20 return max_time;
21}
22
23void sample_animation(const AnimationSampler *sampler, float t, float *out_value) {
24 if (sampler->count == 0) return;
25
26 float duration = sampler->times[sampler->count - 1];
27 if (duration <= 0.0f) {
28 memcpy(out_value, sampler->values, sizeof(float) * sampler->elem_count);
29 return;
30 }
31 t = fmodf(t, duration);
32
33 int i = 0;
34 while (i < sampler->count - 1 && t >= sampler->times[i + 1]) {
35 i++;
36 }
37
38 if (i == sampler->count - 1) {
39 memcpy(out_value, &sampler->values[i * sampler->elem_count],
40 sizeof(float) * sampler->elem_count);
41 return;
42 }
43
44 float t0 = sampler->times[i];
45 float t1 = sampler->times[i + 1];
46 float alpha = (t - t0) / (t1 - t0);
47
48 const float *v0 = &sampler->values[i * sampler->elem_count];
49 const float *v1 = &sampler->values[(i + 1) * sampler->elem_count];
50
51 switch (sampler->interpolation) {
52 case STEP:
53 memcpy(out_value, v0, sizeof(float) * sampler->elem_count);
54 break;
55
56 case LINEAR:
57 for (int j = 0; j < sampler->elem_count; j++) {
58 out_value[j] = lerp(v0[j], v1[j], alpha);
59 }
60 break;
61
62 // not handled
63 case CUBICSPLINE:
64 memcpy(out_value, v0, sizeof(float) * sampler->elem_count);
65 break;
66 }
67}
68void play_animation(Animation *anim, float time, int asset_instance) {
70 for (int i = 0; i < anim->channel_count; i++) {
71 GLTFAnimationChannel *ch = &anim->channels[i];
72 AnimationSampler *samp = &anim->samplers[ch->sampler];
73 float tmp[4];
74 sample_animation(samp, time, tmp);
75
76 AssetInstance * AI = &ASSET_INSTANCES[asset_instance];
77
78 MeshNode *node = &mns[AI->node_indices[ch->target_node]];
79 switch (ch->path) {
81 memcpy(&node->translation, tmp, sizeof(float)*3);
82 break;
83 case TARGET_ROTATION: memcpy(&node->rotation, tmp, sizeof(float)*4); break;
84 case TARGET_SCALE: memcpy(&node->scale, tmp, sizeof(float)*3); break;
85 case TARGET_WEIGHTS: break;
86 }
87 }
88}
89
90
91Animation * find_animation(const char * name, int asset_instance)
92{
93 AssetInstance * ai = &ASSET_INSTANCES[asset_instance];
94 if (!ai) return NULL;
96 Asset * a = &as[ai->asset_id];
98 for (int i = 0; i < ai->skin_count; i++)
99 {
100
101 GLTFSkin * skin = &skins[ai->skin_indices[i]];
102 for (int j = 0; j < skin->animation_indices_count; j++)
103 {
104 if (strcmp(a->animations[skin->animation_indices[j]].name, name) == 0)
105 {
106 return &a->animations[skin->animation_indices[j]];
107 }
108 }
109
110 }
111 return NULL;
112}
113
114
116{
117 for (int i = 0; i < MAX_ANIMATION_COMPONENTS; i++)
118 {
120 if (ac->free) continue;
121
122 for (int j = 0; j < ac->track_count; j++)
123 {
124
125
126 AnimationTrack * t = &ac->tracks[j];
127 if (!t->active) continue;
128 t->current_time += dt;
129 if (t->current_time >= t->duration)
130 {
131 if (t->loop)
132 {
133 t->current_time = fmodf(t->current_time, t->duration);
134
135 if (t->completion_data == NULL) break;
136 switch(t->completion_type)
137 {
139 break;
140 case ANIM_TARGET_VEC3:
141 break;
142 case ANIM_TARGET_INT:
143 break;
144 case ANIM_TARGET_BOOL:
145 *((bool*)t->completion_data) = !(*(bool*)t->completion_data);
146 break;
147 default:
148 break;
149 }
150 }
151 else
152 {
153 t->active = false;
154 }
155 }
156 for (int k = 0; k < t->kf_count; k++)
157 {
158 AnimationKeyframe * kf2 = &t->kf[k];
159 if (kf2->time < t->current_time) continue;
160
161 AnimationKeyframe * kf = &t->kf[k > 0 ? k-1 : t->kf_count-1];
162 float start_time = k > 0 ? kf->time : 0;
163 float current_norm = t->current_time - start_time;
164 float current_end = kf2->time - start_time;
165 float current_time = current_norm / current_end;
166 if (t->data == NULL) break;
167 switch(t->type)
168 {
170 *((float*)t->data) = lerp(kf->val.v, kf2->val.v, current_time);
171 break;
172 case ANIM_TARGET_VEC3:
173 *((vec3*)t->data) = vec3_lerp(kf->val.v3, kf2->val.v3, current_time);
174 break;
175 case ANIM_TARGET_INT:
176 *((int*)t->data) = (int)lerp(kf->val.i, kf2->val.i, current_time);
177 break;
178 case ANIM_TARGET_BOOL:
179 *((int*)t->data) = (int)lerp(kf->val.i, kf2->val.i, current_time);
180 break;
181 default:
182 break;
183 }
184
185 break;
186 }
187
188
189
190
191 }
192
193
194 }
195
196
197
198
199}
void play_animation(Animation *anim, float time, int asset_instance)
Plays an animation at a certain time value.
Definition animation.c:68
Animation * find_animation(const char *name, int asset_instance)
Finds an animation via name.
Definition animation.c:91
void update_animation_components(float dt)
Updates animation components (not skeletal) once a frame.
Definition animation.c:115
void sample_animation(const AnimationSampler *sampler, float t, float *out_value)
Samples a value for an animation sampler at a certain time value.
Definition animation.c:23
float animation_length(Animation *anim)
Finds the length of an animation.
Definition animation.c:10
Engine specific types and defines.
Vector mesh_nodes
MeshNodes.
AnimationInterpolation interpolation
AnimationKeyframe kf[64]
The AnimationKeyframes for this track.
bool loop
If the animation loops.
size_t skin_count
The amount of GLTFSkins in this instance.
int animation_indices_count
The amount of Animation indices.
int animation_indices[256]
Animation indices into Asset.
AnimationSampler * samplers
@ ANIM_TARGET_BOOL
@ ANIM_TARGET_VEC3
@ ANIM_TARGET_INT
@ ANIM_TARGET_FLOAT
AnimationTrack tracks[64]
The AnimationTracks with individual values that occur simultaneously.
int asset_id
The Asset ID in Globals.assets.
int kf_count
The amount of AnimationKeyframes.
Globals GLOBALS
Engine Globals.
Definition main.c:310
size_t * node_indices
The indices of MeshNodes spawned from this instance inside of Globals.mesh_nodes.
bool free
If the component is empty.
@ TARGET_ROTATION
@ TARGET_SCALE
@ TARGET_TRANSLATION
@ TARGET_WEIGHTS
AnimationTargetType completion_type
Type of data that is modified on completion.
float duration
The duration of all of the AnimationKeyframes.
void * data
Stored pointer to data to change during animation.
Vector assets
Assets.
int track_count
The amount of AnimationTracks.
GLTFAnimationChannel * channels
Animation * animations
Vector skins
GLTFSkins.
@ CUBICSPLINE
@ STEP
@ LINEAR
union AnimationKeyframe::@243377164052067242366020157201307241363031166101 val
char name[256]
void * completion_data
Pointer to the data that is modified on completion.
AnimationTargetType type
The type of the data stored on AnimationKeyframe.
bool active
If this animation is currently running.
float current_time
How long the animation has been running.
size_t * skin_indices
The indices of GLTFSkins spawned from this instance inside of Globals.skins.
The structure of an Skeletatal Animation loaded in from GLTFAnimation.
The animation component for interpolation between AnimationKeyframes.
Tagged union for the value stored in a keyframe.
Engine-side sampler version of GLTFAnimationSampler with the field allocated.
Tagged union for the value stored in a keyframe.
An Asset after it is loaded in from file into Globals.assets.
A instance of an Asset that retains its node structure but can be duplicated into multiple instances ...
The channels of an Animation.
The structure of an Skeletatal Animation loaded in from GLTFAnimation.
vec3 translation
MeshNodes that describe the position of Meshes, GLTF nodes for GLTFAnimations, or hold references to ...
Important globals for the entire engine and initialization functions that are mainly called on engine...
#define MAX_ANIMATION_COMPONENTS
Definition init.h:451
AnimationComponent ANIMATION_COMPONENTS[MAX_ANIMATION_COMPONENTS]
Definition main.c:315
float dt
The 1.0f/60 simulation timestep.
Definition main.c:319
float time
The frame time but wrapping at 1.0.
Definition main.c:320
AssetInstance ASSET_INSTANCES[MAX_ASSET_INSTANCES]
Definition main.c:313
vec3 vec3_lerp(vec3 a, vec3 b, float t)
Definition mathf.h:375
float lerp(float a, float b, float t)
Definition mathf.h:372
Definition types.h:12
#define CAST_VEC(vec, type)
Definition utilities.h:134