Skip to content

Commit 5c7330a

Browse files
committed
Fix model to bone attachment.
1 parent 539582c commit 5c7330a

2 files changed

Lines changed: 33 additions & 44 deletions

File tree

src/engine_lib/src/game/model.c

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ prv_model_set_attribute_pointers_model_vertex_skinned(void) {
5858

5959
// Bone indices.
6060
glEnableVertexAttribArray(3);
61-
glVertexAttribIPointer( // <- note `I` here, passing array of integers
61+
glVertexAttribIPointer( // <- note `I` here, passing array of integers
6262
3, 4, GL_UNSIGNED_BYTE, sizeof(te_model_vertex_skinned),
6363
(void*)(sizeof(vec3) * 2 + sizeof(vec2)));
6464

@@ -249,7 +249,7 @@ model_destroy(te_model* model) {
249249
model->custom_on_before_destroyed(model);
250250
}
251251

252-
for (unsigned int i = 0; i < model->child_model_count; i++ ) {
252+
for (unsigned int i = 0; i < model->child_model_count; i++) {
253253
if (model->child_models[i]->world != NULL) {
254254
// We should have despawned it in our despawn callback.
255255
log_error("expected the child model to be despawned already");
@@ -349,7 +349,8 @@ prv_model_get_renderer(te_model* model) {
349349

350350
static void prv_model_calc_world_normal_matrices(te_model* model, mat4 world, mat3 normal);
351351

352-
static void prv_model_update_child_model_mats(te_model* model) {
352+
static void
353+
prv_model_update_child_model_mats(te_model* model) {
353354
for (unsigned int i = 0; i < model->child_model_count; i++) {
354355
te_model* child_model = model->child_models[i];
355356

@@ -372,62 +373,47 @@ prv_model_on_after_skeleton_updated(te_model* model) {
372373
}
373374

374375
static void
375-
prv_model_calc_world_normal_matrices(te_model* model, mat4 world, mat3 normal) {
376-
mat4 translate_mat;
377-
glm_translate_make(translate_mat, model->position);
378-
379-
mat4 rot_mat;
380-
math_make_rotation_mat(model->rotation, rot_mat);
376+
prv_model_calc_world_normal_matrices(te_model* model, mat4 out_world, mat3 out_normal) {
377+
mat4 mat2;
378+
math_make_rotation_mat(model->rotation, mat2);
381379

382-
mat4 scale_mat;
383-
glm_scale_make(scale_mat, model->scale);
380+
mat4 mat1;
381+
glm_scale_make(mat1, model->scale);
384382

385383
// Scale, rotate and then translate.
386-
glm_mat4_mul(rot_mat, scale_mat, world);
387-
glm_mat4_mul(translate_mat, world, world);
388-
389-
mat4 normal_mat;
390-
glm_mat4_inv(world, normal_mat);
391-
glm_mat4_transpose(normal_mat);
392-
393-
glm_mat4_pick3(normal_mat, normal);
384+
glm_mat4_mul(mat2, mat1, out_world);
385+
glm_translate_make(mat1, model->position);
386+
glm_mat4_mul(mat1, out_world, out_world);
394387

395388
if (model->parent_model != NULL && model->parent_model->render_data_handle != 0xffffffff) {
396389
if (model->parent_bone_idx != 0xFFFFFFFF) {
397-
te_skeleton* skeleton = model->skeleton;
390+
te_skeleton* skeleton = model->parent_model->skeleton;
398391
if (skeleton == NULL) {
399392
log_error("expected parent model to have a skeleton");
400393
abort();
401394
}
402-
glm_mat4_copy(
403-
skeleton_get_skinning_mats(skeleton)[model->parent_bone_idx], world);
404-
405-
// Ignore parent's scale.
406-
math_normalize_safely(world[0]);
407-
math_normalize_safely(world[1]);
408-
math_normalize_safely(world[2]);
409-
410-
mat4 normal_mat;
411-
glm_mat4_inv(world, normal_mat);
412-
glm_mat4_transpose(normal_mat);
413-
414-
glm_mat4_pick3(normal_mat, normal);
395+
glm_mat4_copy(skeleton_get_skinning_mats(skeleton)[model->parent_bone_idx], mat1);
415396
} else {
416397
te_model_renderer* renderer = prv_model_get_renderer(model->parent_model);
417398
te_model_render_data* data = model_renderer_get_render_data_tmp(
418399
renderer, model->parent_model->render_data_handle);
419400

420-
glm_mat4_copy(data->world_mat, world);
401+
glm_mat4_copy(data->world_mat, mat1);
402+
}
421403

422-
// Ignore parent's scale.
423-
math_normalize_safely(world[0]);
424-
math_normalize_safely(world[1]);
425-
math_normalize_safely(world[2]);
404+
// Ignore parent's scale.
405+
math_normalize_safely(mat1[0]);
406+
math_normalize_safely(mat1[1]);
407+
math_normalize_safely(mat1[2]);
426408

427-
glm_mat3_copy(data->normal_mat, normal);
428-
}
409+
glm_mat4_mul(mat1, out_world, out_world);
429410
}
430411

412+
// Calculate normal matrix.
413+
glm_mat4_inv(out_world, mat1);
414+
glm_mat4_transpose(mat1);
415+
glm_mat4_pick3(mat1, out_normal);
416+
431417
prv_model_update_child_model_mats(model);
432418

433419
if (model->attached_camera != NULL) {
@@ -947,7 +933,8 @@ prv_model_add_to_model_renderer(te_model* model) {
947933
te_game_manager* game_manager = world_get_game_manager(model->world);
948934

949935
if (model->skeleton_relative_path != NULL) {
950-
model->skeleton = prv_skeleton_create(model->skeleton_relative_path, model, game_manager);
936+
model->skeleton =
937+
prv_skeleton_create(model->skeleton_relative_path, model, game_manager);
951938
}
952939

953940
// Add to rendering.
@@ -972,8 +959,8 @@ prv_model_add_to_model_renderer(te_model* model) {
972959
data->aabb_world = aabb_shape_convert_to_world(&model->aabb_local, data->world_mat);
973960

974961
if (model->tex_relative_path != NULL) {
975-
te_texture_manager* texture_manager = renderer_get_texture_manager(
976-
game_manager_get_renderer(game_manager));
962+
te_texture_manager* texture_manager =
963+
renderer_get_texture_manager(game_manager_get_renderer(game_manager));
977964

978965
data->tex_id = texture_manager_request_texture(
979966
texture_manager, model->tex_relative_path, MODEL_TEX_LOAD_OPTION);
@@ -1274,7 +1261,8 @@ type_despawn(te_world* world, te_model* model) {
12741261
}
12751262

12761263
if (model->parent_model != NULL) {
1277-
model_set_parent(model, NULL, 0xFFFFFFFF); // make model to be in the array of root world objects
1264+
model_set_parent(
1265+
model, NULL, 0xFFFFFFFF); // make model to be in the array of root world objects
12781266
}
12791267
world_despawn_game_object(
12801268
model->world, model->game_object_info); // despawn root world object

src/engine_lib/src/game/skeleton.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,7 @@ skeleton_stop_animation(te_skeleton* skeleton) {
739739
prv_skeleton_update(skeleton, 0.0f);
740740

741741
game_manager_remove_tick_callback(skeleton->game_manager, skeleton->tick_callback_id);
742+
skeleton->tick_callback_id = 0xFFFFFFFF;
742743
}
743744

744745
void

0 commit comments

Comments
 (0)