summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoromnisci3nce <17525998+omnisci3nce@users.noreply.github.com>2024-04-07 21:46:51 +1000
committeromnisci3nce <17525998+omnisci3nce@users.noreply.github.com>2024-04-07 21:46:51 +1000
commit61d96cf09e2e125f36a94a4c64ed5682fda0df1c (patch)
treeaa79fe48e1a0800bec0157460a4adf7d19d88077 /src
parent42924fe7b32e93bf55ce034467ceb52e0436c303 (diff)
its bending but not deforming as expected, looks like rotating around model origin
Diffstat (limited to 'src')
-rw-r--r--src/animation.c10
-rw-r--r--src/maths/maths.h8
-rw-r--r--src/renderer/render.c14
-rw-r--r--src/resources/gltf.c9
4 files changed, 28 insertions, 13 deletions
diff --git a/src/animation.c b/src/animation.c
index de7e9a2..7a79529 100644
--- a/src/animation.c
+++ b/src/animation.c
@@ -6,14 +6,14 @@ keyframe animation_sample(animation_sampler *sampler, f32 t) {
size_t previous_index = 0;
f32 previous_time = 0.0;
// look forwards
- DEBUG("%d\n", sampler->animation.values.kind);
- TRACE("Here %d", sampler->animation.n_timestamps);
- for (u32 i = 1; i < sampler->animation.n_timestamps; i++) {
+ // DEBUG("%d\n", sampler->animation.values.kind);
+ TRACE("Total timestamps %d", sampler->animation.n_timestamps);
+ for (u32 i = 0; i < sampler->animation.n_timestamps; i++) {
f32 current_time = sampler->animation.timestamps[i];
if (current_time > t) {
break;
}
- previous_time = current_time;
+ previous_time = sampler->animation.timestamps[i];
previous_index = i;
}
@@ -28,7 +28,7 @@ keyframe animation_sample(animation_sampler *sampler, f32 t) {
f32 time_diff =
sampler->animation.timestamps[next_index] - sampler->animation.timestamps[previous_index];
- f32 percent = (t - sampler->animation.timestamps[next_index]) / time_diff;
+ f32 percent = (t - previous_time) / time_diff;
quat interpolated_rot =
quat_slerp(sampler->animation.values.values[previous_index].rotation,
diff --git a/src/maths/maths.h b/src/maths/maths.h
index 76531f2..911b9b7 100644
--- a/src/maths/maths.h
+++ b/src/maths/maths.h
@@ -302,7 +302,7 @@ static inline mat4 mat4_look_at(vec3 position, vec3 target, vec3 up) {
#define TRANSFORM_DEFAULT \
((transform){ .position = VEC3_ZERO, \
- .rotation = (quat){ .x = 0., .y = 0., .z = 0., .w = 0. }, \
+ .rotation = (quat){ .x = 0., .y = 0., .z = 0., .w = 1. }, \
.scale = 1.0, \
.is_dirty = false })
@@ -311,8 +311,10 @@ static transform transform_create(vec3 pos, quat rot, f32 scale) {
}
static inline mat4 transform_to_mat(transform *tf) {
- // TODO: rotation
- return mat4_mult(mat4_translation(tf->position), mat4_scale(tf->scale));
+ mat4 scale = mat4_scale(tf->scale);
+ mat4 rotation = mat4_rotation(tf->rotation);
+ mat4 translation = mat4_translation(tf->position);
+ return mat4_mult(translation, mat4_mult(rotation, scale));
}
// --- Sizing asserts
diff --git a/src/renderer/render.c b/src/renderer/render.c
index 42f6ee4..d7e2d48 100644
--- a/src/renderer/render.c
+++ b/src/renderer/render.c
@@ -1,6 +1,7 @@
#include <assert.h>
#include <stdlib.h>
#include <string.h>
+#include "animation.h"
#include "maths_types.h"
#include "mem.h"
#define STB_IMAGE_IMPLEMENTATION
@@ -192,8 +193,19 @@ void draw_skinned_mesh(renderer* ren, mesh* mesh, transform tf, material* mat, m
// for now assume correct ordering
mat4* bone_transforms = malloc(n_bones * sizeof(mat4));
+ mat4 parent = mat4_ident();
for (int bone_i = 0; bone_i < n_bones; bone_i++) {
- bone_transforms[bone_i] = mat4_ident();
+ transform tf = mesh->bones->data[bone_i].transform_components;
+ mat4 local = transform_to_mat(&mesh->bones->data[bone_i].transform_components);
+ bone_transforms[bone_i] = mat4_mult(parent, local);
+ parent = bone_transforms[bone_i];
+ }
+
+ // premultiply the inverses
+ for (int bone_i = 0; bone_i < n_bones; bone_i++) {
+ joint j = mesh->bones->data[bone_i];
+ // bone_transforms[bone_i] = mat4_mult(bone_transforms[bone_i], j.inverse_bind_matrix);
+ bone_transforms[bone_i] = mat4_mult(bone_transforms[bone_i], j.inverse_bind_matrix);
}
glUniformMatrix4fv(glGetUniformLocation(lighting_shader.program_id, "boneMatrices"), n_bones,
diff --git a/src/resources/gltf.c b/src/resources/gltf.c
index 7efd2bb..7668a49 100644
--- a/src/resources/gltf.c
+++ b/src/resources/gltf.c
@@ -89,7 +89,7 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel
vec4u_darray *tmp_joint_indices = vec4u_darray_new(1000);
vec4_darray *tmp_weights = vec4_darray_new(1000);
joint_darray *tmp_joints = joint_darray_new(256);
- vertex_bone_data_darray* tmp_vertex_bone_data = vertex_bone_data_darray_new(1000);
+ vertex_bone_data_darray *tmp_vertex_bone_data = vertex_bone_data_darray_new(1000);
cgltf_options options = { 0 };
cgltf_data *data = NULL;
@@ -276,7 +276,7 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel
mesh mesh = { 0 };
mesh.vertices = vertex_darray_new(10);
- mesh.vertex_bone_data =vertex_bone_data_darray_new(1);
+ mesh.vertex_bone_data = vertex_bone_data_darray_new(1);
if (primitive.material != NULL) {
for (int i = 0; i < material_darray_len(out_model->materials); i++) {
@@ -297,7 +297,8 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel
vertex_bone_data data;
data.joints = tmp_joint_indices->data[i];
data.weights = tmp_weights->data[i];
- vertex_bone_data_darray_push(tmp_vertex_bone_data, data); // Push the temp data that aligns with raw vertices
+ vertex_bone_data_darray_push(tmp_vertex_bone_data,
+ data); // Push the temp data that aligns with raw vertices
}
for (int i = 0; i < tmp_joints->len; i++) {
joint data = tmp_joints->data[i];
@@ -330,7 +331,7 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel
vertex_darray_push(mesh.vertices, vert);
if (is_skinned) {
- vertex_bone_data vbd = tmp_vertex_bone_data->data[index]; // create a copy
+ vertex_bone_data vbd = tmp_vertex_bone_data->data[index]; // create a copy
vertex_bone_data_darray_push(mesh.vertex_bone_data, vbd);
}
// for each vertex do the bone data