Implement bone matrix computation
This commit is contained in:
1 parent
e81061ad22
commit
998d5e88e1
6 files changed
+132
No files matched your search
+50
@@ -0,0 +1,50 @@
|
||||
#include "animation.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static mat4_t bone_transform(bone_t bone)
|
||||
{
|
||||
return mat4_mul(
|
||||
mat4_translate_vec3(bone.translation),
|
||||
mat4_mul(
|
||||
mat4_rotate_quaternion(bone.rotation),
|
||||
mat4_scale_vec3(bone.scale)));
|
||||
}
|
||||
|
||||
static void compute_bone_matrices(
|
||||
size_t bone_count, const bone_t *bones,
|
||||
const mat4_t *inverse_bind_matrices, mat4_t *matrices)
|
||||
{
|
||||
matrices[0] = bone_transform(bones[0]);
|
||||
for (unsigned i = 1; i < bone_count; ++i) {
|
||||
const mat4_t transform = bone_transform(bones[i]);
|
||||
assert(bones[i].parent < bone_count);
|
||||
matrices[i] = mat4_mul(matrices[bones[i].parent], transform);
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < bone_count; ++i)
|
||||
matrices[i] = mat4_mul(matrices[i], inverse_bind_matrices[i]);
|
||||
}
|
||||
|
||||
animation_state_t animation_init(const model_t *model)
|
||||
{
|
||||
mat4_t *bone_matrices = calloc(model->bone_count, sizeof(mat4_t));
|
||||
assert(bone_matrices != nullptr);
|
||||
return (animation_state_t) { .bone_matrices = bone_matrices };
|
||||
}
|
||||
|
||||
void animation_cleanup(animation_state_t *state)
|
||||
{
|
||||
if (state->bone_matrices != nullptr) {
|
||||
free(state->bone_matrices);
|
||||
state->bone_matrices = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void animation_update(animation_state_t state, const model_t *model)
|
||||
{
|
||||
compute_bone_matrices(
|
||||
model->bone_count, model->bones, model->inverse_bind_matrices,
|
||||
state.bone_matrices);
|
||||
}
|
||||
Reference in new issue
Block a user