Skip to content

Commit

Permalink
character.h: Changed signature of cha_mesh_instance_calculate_bone_sp…
Browse files Browse the repository at this point in the history
…ace_pose_matrices_from_action(...). Added suitcase as a new optional mesh.
  • Loading branch information
Flix01 committed Mar 30, 2020
1 parent e34cb89 commit 0a7ffa8
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 21 deletions.
14 changes: 8 additions & 6 deletions Tests/test_character.c
Original file line number Diff line number Diff line change
Expand Up @@ -638,8 +638,8 @@ void CharacterGroupMoveAndAnimate(float totalTimeSeconds,float frameTimeSeconds)
const float animation_time=totalTimeSeconds*animation_speed; // good only for when 'animation_speed' is constant through all the animation time
//static float animation_time=0;animation_time+=frameTimeSeconds*animation_speed; // good always

cha_mesh_instance_calculate_bone_space_pose_matrices_from_action(mi,CHA_ARMATURE_ACTION_NAME_FALL_DOWN,animation_time,additional_time_to_get_to_first_frame);
//cha_mesh_instance_calculate_bone_space_pose_matrices_from_action(mi,CHA_ARMATURE_ACTION_NAME_RUN_CYCLE,animation_time,additional_time_to_get_to_first_frame);
cha_mesh_instance_calculate_bone_space_pose_matrices_from_action(mi,CHA_ARMATURE_ACTION_NAME_FALL_DOWN,animation_time,additional_time_to_get_to_first_frame,0);
//cha_mesh_instance_calculate_bone_space_pose_matrices_from_action(mi,CHA_ARMATURE_ACTION_NAME_RUN_CYCLE,animation_time,additional_time_to_get_to_first_frame,0);

}
# endif
Expand All @@ -650,7 +650,7 @@ void CharacterGroupMoveAndAnimate(float totalTimeSeconds,float frameTimeSeconds)
static float animation_time = 0.f;
animation_time+= (frameTimeSeconds)*character_animation_speed;
cha_mesh_instance_calculate_bone_space_pose_matrices_from_action_ex(mi,CHA_ARMATURE_ACTION_NAME_CYCLE_RUN,animation_time,additional_time_to_get_to_first_frame,character_animation_walk_run_mix_in_0_1,CHA_ARMATURE_ACTION_NAME_CYCLE_WALK,0,-1,0);//CHA_BONE_MASK_ARMS_AND_HANDS);
//cha_mesh_instance_calculate_bone_space_pose_matrices_from_action(mi,CHA_ARMATURE_ACTION_NAME_CYCLE_WALK,animation_time,additional_time_to_get_to_first_frame);
//cha_mesh_instance_calculate_bone_space_pose_matrices_from_action(mi,CHA_ARMATURE_ACTION_NAME_CYCLE_WALK,animation_time,additional_time_to_get_to_first_frame,0);
}
# endif

Expand Down Expand Up @@ -702,11 +702,13 @@ void CharacterGroupMoveAndAnimate(float totalTimeSeconds,float frameTimeSeconds)
const struct cha_armature* armature = peter_mi->armature; /* all characters share the same armature */
int anim_idx = 0;
CHA_ASSERT(armature);
if (anim_idx<armature->num_actions && armature->num_actions>2) {
if (anim_idx<armature->num_actions && armature->num_actions>4) {
for (i=0;i<group->num_instances;i++) {
struct cha_character_instance* inst = &group->instances[i];
if (inst!=peter && inst!=mary) {
while (anim_idx==CHA_ARMATURE_ACTION_NAME_CYCLE_RUN || anim_idx==CHA_ARMATURE_ACTION_NAME_CYCLE_WALK) anim_idx=(anim_idx+1)%armature->num_actions; // we skip these (already shown by peter)
while (anim_idx==CHA_ARMATURE_ACTION_NAME_CYCLE_RUN || anim_idx==CHA_ARMATURE_ACTION_NAME_CYCLE_WALK // we skip these (already shown by peter)
|| anim_idx==CHA_ARMATURE_ACTION_NAME_POSE_HANDS_OPEN || anim_idx==CHA_ARMATURE_ACTION_NAME_POSE_HANDS_CLOSED // we skip these (affect hands only, not very noticeable)
) anim_idx=(anim_idx+1)%armature->num_actions;
if (inst->active && !inst->culled) {
struct cha_mesh_instance* mi = &inst->mesh_instances[CHA_MESH_NAME_BODY];
const struct cha_armature_action* action = &armature->actions[anim_idx];
Expand All @@ -717,7 +719,7 @@ void CharacterGroupMoveAndAnimate(float totalTimeSeconds,float frameTimeSeconds)
//static float animation_time=0;animation_time+=frameTimeSeconds*animation_speed; // good always
if (!action->looping && animation_time>repeat_time) animation_time=fmodf(animation_time,repeat_time);

cha_mesh_instance_calculate_bone_space_pose_matrices_from_action(mi,anim_idx,animation_time,additional_time_to_get_to_first_frame);
cha_mesh_instance_calculate_bone_space_pose_matrices_from_action(mi,anim_idx,animation_time,additional_time_to_get_to_first_frame,0);
}
++anim_idx;anim_idx%=armature->num_actions;
}
Expand Down
16 changes: 9 additions & 7 deletions Tests/test_character_standalone.c
Original file line number Diff line number Diff line change
Expand Up @@ -958,8 +958,8 @@ void CharacterGroupMoveAndAnimate(float totalTimeSeconds,float frameTimeSeconds)
const float animation_time=totalTimeSeconds*animation_speed; // good only for when 'animation_speed' is constant through all the animation time
//static float animation_time=0;animation_time+=frameTimeSeconds*animation_speed; // good always

cha_mesh_instance_calculate_bone_space_pose_matrices_from_action(mi,CHA_ARMATURE_ACTION_NAME_FALL_DOWN,animation_time,additional_time_to_get_to_first_frame);
//cha_mesh_instance_calculate_bone_space_pose_matrices_from_action(mi,CHA_ARMATURE_ACTION_NAME_RUN_CYCLE,animation_time,additional_time_to_get_to_first_frame);
cha_mesh_instance_calculate_bone_space_pose_matrices_from_action(mi,CHA_ARMATURE_ACTION_NAME_FALL_DOWN,animation_time,additional_time_to_get_to_first_frame,0);
//cha_mesh_instance_calculate_bone_space_pose_matrices_from_action(mi,CHA_ARMATURE_ACTION_NAME_RUN_CYCLE,animation_time,additional_time_to_get_to_first_frame,0);

}
# endif
Expand All @@ -971,7 +971,7 @@ void CharacterGroupMoveAndAnimate(float totalTimeSeconds,float frameTimeSeconds)
static float animation_time = 0.f;
animation_time+= (frameTimeSeconds)*character_animation_speed;
cha_mesh_instance_calculate_bone_space_pose_matrices_from_action_ex(mi,CHA_ARMATURE_ACTION_NAME_CYCLE_RUN,animation_time,additional_time_to_get_to_first_frame,character_animation_walk_run_mix_in_0_1,CHA_ARMATURE_ACTION_NAME_CYCLE_WALK,0,-1,0);//CHA_BONE_MASK_ARMS_AND_HANDS);
//cha_mesh_instance_calculate_bone_space_pose_matrices_from_action(mi,CHA_ARMATURE_ACTION_NAME_CYCLE_WALK,animation_time,additional_time_to_get_to_first_frame);
//cha_mesh_instance_calculate_bone_space_pose_matrices_from_action(mi,CHA_ARMATURE_ACTION_NAME_CYCLE_WALK,animation_time,additional_time_to_get_to_first_frame,0);
}
# endif

Expand Down Expand Up @@ -1023,11 +1023,13 @@ void CharacterGroupMoveAndAnimate(float totalTimeSeconds,float frameTimeSeconds)
const struct cha_armature* armature = peter_mi->armature; /* all characters share the same armature */
int anim_idx = 0;
CHA_ASSERT(armature);
if (anim_idx<armature->num_actions && armature->num_actions>2) {
if (anim_idx<armature->num_actions && armature->num_actions>4) {
for (i=0;i<group->num_instances;i++) {
struct cha_character_instance* inst = &group->instances[i];
if (inst!=peter && inst!=mary) {
while (anim_idx==CHA_ARMATURE_ACTION_NAME_CYCLE_RUN || anim_idx==CHA_ARMATURE_ACTION_NAME_CYCLE_WALK) anim_idx=(anim_idx+1)%armature->num_actions; // we skip these (already shown by peter)
while (anim_idx==CHA_ARMATURE_ACTION_NAME_CYCLE_RUN || anim_idx==CHA_ARMATURE_ACTION_NAME_CYCLE_WALK // we skip these (already shown by peter)
|| anim_idx==CHA_ARMATURE_ACTION_NAME_POSE_HANDS_OPEN || anim_idx==CHA_ARMATURE_ACTION_NAME_POSE_HANDS_CLOSED // we skip these (affect hands only, not very noticeable)
) anim_idx=(anim_idx+1)%armature->num_actions;
if (inst->active && !inst->culled) {
struct cha_mesh_instance* mi = &inst->mesh_instances[CHA_MESH_NAME_BODY];
const struct cha_armature_action* action = &armature->actions[anim_idx];
Expand All @@ -1038,7 +1040,7 @@ void CharacterGroupMoveAndAnimate(float totalTimeSeconds,float frameTimeSeconds)
//static float animation_time=0;animation_time+=frameTimeSeconds*animation_speed; // good always
if (!action->looping && animation_time>repeat_time) animation_time=fmodf(animation_time,repeat_time);

cha_mesh_instance_calculate_bone_space_pose_matrices_from_action(mi,anim_idx,animation_time,additional_time_to_get_to_first_frame);
cha_mesh_instance_calculate_bone_space_pose_matrices_from_action(mi,anim_idx,animation_time,additional_time_to_get_to_first_frame,0);
}
++anim_idx;anim_idx%=armature->num_actions;
}
Expand Down Expand Up @@ -1066,7 +1068,7 @@ void CharacterGroupMoveAndAnimate(float totalTimeSeconds,float frameTimeSeconds)
while (anim_idx==CHA_ARMATURE_ACTION_NAME_CYCLE_RUN || anim_idx==CHA_ARMATURE_ACTION_NAME_CYCLE_WALK) anim_idx=(anim_idx+1)%armature->num_actions; // we skip these (already shown by peter)
if (inst->active && !inst->culled) {
struct cha_mesh_instance* mi = &inst->mesh_instances[CHA_MESH_NAME_BODY];
cha_mesh_instance_calculate_bone_space_pose_matrices_from_action(mi,anim_idx,animation_time,additional_time_to_get_to_first_frame);
cha_mesh_instance_calculate_bone_space_pose_matrices_from_action(mi,anim_idx,animation_time,additional_time_to_get_to_first_frame,0);
}
}
}
Expand Down
22 changes: 18 additions & 4 deletions character.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ A POSSIBLE ROADMAP:
#define CHARACTER_H_

#define CHA_VERSION "0.1 ALPHA"
#define CHA_VERSION_NUM 0003
#define CHA_VERSION_NUM 0004

#ifndef CHA_API_INL
# define CHA_API_INL __inline
Expand Down Expand Up @@ -2968,6 +2968,7 @@ struct cha_mesh_instance {
float* pose_matrices[4]; /* size = armature->num_bones*16. */
struct cha_mesh_instance_pose_data* pose_data; /* size = armature->num_bones */
unsigned pose_bone_mask; /* bone mask of the currently affected bones */
unsigned pose_bone_exclude_mask; /* user can set/reset it, so that all animations do not affect these bones (handy e.g. when hand is grabbing something). Ignored in manual animations. */

/* skeletal animation stuff (if 'armature'!=NULL) */
float *verts,*norms; /* size=mesh->num_verts; buffer for the animated verts/norms */
Expand Down Expand Up @@ -3389,6 +3390,7 @@ float cha_mesh_instance_calculate_bone_space_pose_matrices_from_action_ex(struct
float animation_time=0.f;
int i,j,looping=0,is_first_loop = 0;
int must_mix_with_another_action = 0;
bone_exclude_mask|=p->pose_bone_exclude_mask; // user can set/reset it
CHA_ASSERT(armature && action_index>=0 && armature->num_actions>action_index);
if (bone_start_idx<0) bone_start_idx=0;
if (num_bones_to_process<0) num_bones_to_process=armature->num_bones-bone_start_idx;
Expand Down Expand Up @@ -3541,8 +3543,8 @@ float cha_mesh_instance_calculate_bone_space_pose_matrices_from_action_ex(struct

return animation_time; /* returns fmod(global_animation_time,action->max_frame_time) for looping animations */
}
CHA_API_INL float cha_mesh_instance_calculate_bone_space_pose_matrices_from_action(struct cha_mesh_instance* p,int action_index,float global_animation_time,float additional_time_to_get_to_first_frame) {
return cha_mesh_instance_calculate_bone_space_pose_matrices_from_action_ex(p,action_index,global_animation_time,additional_time_to_get_to_first_frame,1.f,-1,0,-1,0);
CHA_API_INL float cha_mesh_instance_calculate_bone_space_pose_matrices_from_action(struct cha_mesh_instance* p,int action_index,float global_animation_time,float additional_time_to_get_to_first_frame,unsigned bone_exclude_mask) {
return cha_mesh_instance_calculate_bone_space_pose_matrices_from_action_ex(p,action_index,global_animation_time,additional_time_to_get_to_first_frame,1.f,-1,0,-1,bone_exclude_mask);
}
void cha_mesh_instance_update_bone_matrix(struct cha_mesh_instance* p,int bone_idx,int recursive) {
//p->pose_bone_mask = 0; /* well, this must be reset BEFORE calling this method (multiple times) */
Expand Down Expand Up @@ -4339,13 +4341,24 @@ void cha_character_group_init(struct cha_character_group* p,int num_men,int num_
}

if (add_some_optional_meshes) {
const int has_hat = (mtl_idx%4==2)?1:0, has_glasses= (mtl_idx%3==1)?1:0, has_covid_mask = (mtl_idx%5==3)?1:0;
const int has_hat = (mtl_idx%4==2)?1:0, has_glasses= (mtl_idx%3==1)?1:0, has_covid_mask = (mtl_idx%5==3)?1:0, has_suitcase_right = ((mtl_idx%4==3)?1:0), has_suitcase_left = ((mtl_idx%9==4)?1:0);
struct cha_mesh_instance* mi = &inst->mesh_instances[CHA_MESH_NAME_BODY];
if (has_hat) inst->mesh_instances[CHA_MESH_NAME_HAT].active=1;
if (has_glasses) inst->mesh_instances[CHA_MESH_NAME_GLASSES].active=1;
if (has_covid_mask) {
inst->mesh_instances[CHA_MESH_NAME_COVID_MASK].active=1;
inst->mesh_instances[CHA_MESH_NAME_MOUTH].active=0;
}
if (has_suitcase_right) {
inst->mesh_instances[CHA_MESH_NAME_SUITCASE_RIGHT].active=1;
cha_mesh_instance_calculate_bone_space_pose_matrices_from_action(mi,CHA_ARMATURE_ACTION_NAME_POSE_HANDS_CLOSED,10.f,0.f,CHA_BONE_MASK_HAND_L);
mi->pose_bone_exclude_mask|=CHA_BONE_MASK_HAND_R;
}
if (has_suitcase_left) {
inst->mesh_instances[CHA_MESH_NAME_SUITCASE_LEFT].active=1;
cha_mesh_instance_calculate_bone_space_pose_matrices_from_action(mi,CHA_ARMATURE_ACTION_NAME_POSE_HANDS_CLOSED,10.f,0.f,CHA_BONE_MASK_HAND_R);
mi->pose_bone_exclude_mask|=CHA_BONE_MASK_HAND_L;
}
}


Expand Down Expand Up @@ -4396,6 +4409,7 @@ CHA_API_DEF void Character_Init(void) {
cha_mesh_init_hat(&gCharacterMeshes[CHA_MESH_NAME_HAT]);
cha_mesh_init_glasses(&gCharacterMeshes[CHA_MESH_NAME_GLASSES]);
cha_mesh_init_covid_mask(&gCharacterMeshes[CHA_MESH_NAME_COVID_MASK]);
cha_mesh_init_suitcases(&gCharacterMeshes[CHA_MESH_NAME_SUITCASE_RIGHT],&gCharacterMeshes[CHA_MESH_NAME_SUITCASE_LEFT]);

cha_materials_init_names(&gMaterialNames); /* this doesn't need destruction */

Expand Down
Loading

0 comments on commit 0a7ffa8

Please sign in to comment.