From f518c4e939a824a4174d1ce4ec0ddb53125cfab0 Mon Sep 17 00:00:00 2001 From: culacant Date: Tue, 6 Aug 2019 23:08:58 +0200 Subject: Fix loading multiple animations from .iqm file (#928) * Fix loading multiple animations from .iqm file * Fix memory leak in models_animation example * Added export instructions to the animation example * use raylib free * include to appease the travis CI gods * replace tabs with spaces --- src/models.c | 196 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 97 insertions(+), 99 deletions(-) (limited to 'src') diff --git a/src/models.c b/src/models.c index ee625906..f848e0ab 100644 --- a/src/models.c +++ b/src/models.c @@ -867,9 +867,6 @@ void SetModelMeshMaterial(Model *model, int meshId, int materialId) // Load model animations from file ModelAnimation *LoadModelAnimations(const char *filename, int *animCount) { - ModelAnimation *animations = (ModelAnimation *)RL_MALLOC(1*sizeof(ModelAnimation)); - int count = 1; - #define IQM_MAGIC "INTERQUAKEMODEL" // IQM file magic number #define IQM_VERSION 2 // only IQM version 2 supported @@ -904,8 +901,6 @@ ModelAnimation *LoadModelAnimations(const char *filename, int *animCount) unsigned int flags; } IQMAnim; - ModelAnimation animation = { 0 }; - FILE *iqmFile; IQMHeader iqm; @@ -931,153 +926,156 @@ ModelAnimation *LoadModelAnimations(const char *filename, int *animCount) fclose(iqmFile); } - // header - if (iqm.num_anims > 1) TraceLog(LOG_WARNING, "More than 1 animation in file, only the first one will be loaded"); - // bones IQMPose *poses; poses = RL_MALLOC(sizeof(IQMPose)*iqm.num_poses); fseek(iqmFile, iqm.ofs_poses, SEEK_SET); fread(poses, sizeof(IQMPose)*iqm.num_poses, 1, iqmFile); - animation.boneCount = iqm.num_poses; - animation.bones = RL_MALLOC(sizeof(BoneInfo)*iqm.num_poses); - - for (int j = 0; j < iqm.num_poses; j++) - { - strcpy(animation.bones[j].name, "ANIMJOINTNAME"); - animation.bones[j].parent = poses[j].parent; - } - // animations - IQMAnim anim = {0}; + *animCount = iqm.num_anims; + IQMAnim *anim = RL_MALLOC(iqm.num_anims*sizeof(IQMAnim)); fseek(iqmFile, iqm.ofs_anims, SEEK_SET); - fread(&anim, sizeof(IQMAnim), 1, iqmFile); + fread(anim, iqm.num_anims*sizeof(IQMAnim), 1, iqmFile); + ModelAnimation *animations = RL_MALLOC(iqm.num_anims*sizeof(ModelAnimation)); - animation.frameCount = anim.num_frames; - //animation.framerate = anim.framerate; // frameposes unsigned short *framedata = RL_MALLOC(sizeof(unsigned short)*iqm.num_frames*iqm.num_framechannels); fseek(iqmFile, iqm.ofs_frames, SEEK_SET); fread(framedata, sizeof(unsigned short)*iqm.num_frames*iqm.num_framechannels, 1, iqmFile); - animation.framePoses = RL_MALLOC(sizeof(Transform*)*anim.num_frames); - for (int j = 0; j < anim.num_frames; j++) animation.framePoses[j] = RL_MALLOC(sizeof(Transform)*iqm.num_poses); + for(int a=0;a= 0) + for (int i = 0; i < animations[a].boneCount; i++) { - animation.framePoses[frame][i].rotation = QuaternionMultiply(animation.framePoses[frame][animation.bones[i].parent].rotation, animation.framePoses[frame][i].rotation); - animation.framePoses[frame][i].translation = Vector3RotateByQuaternion(animation.framePoses[frame][i].translation, animation.framePoses[frame][animation.bones[i].parent].rotation); - animation.framePoses[frame][i].translation = Vector3Add(animation.framePoses[frame][i].translation, animation.framePoses[frame][animation.bones[i].parent].translation); - animation.framePoses[frame][i].scale = Vector3MultiplyV(animation.framePoses[frame][i].scale, animation.framePoses[frame][animation.bones[i].parent].scale); + if (animations[a].bones[i].parent >= 0) + { + animations[a].framePoses[frame][i].rotation = QuaternionMultiply(animations[a].framePoses[frame][animations[a].bones[i].parent].rotation, animations[a].framePoses[frame][i].rotation); + animations[a].framePoses[frame][i].translation = Vector3RotateByQuaternion(animations[a].framePoses[frame][i].translation, animations[a].framePoses[frame][animations[a].bones[i].parent].rotation); + animations[a].framePoses[frame][i].translation = Vector3Add(animations[a].framePoses[frame][i].translation, animations[a].framePoses[frame][animations[a].bones[i].parent].translation); + animations[a].framePoses[frame][i].scale = Vector3MultiplyV(animations[a].framePoses[frame][i].scale, animations[a].framePoses[frame][animations[a].bones[i].parent].scale); + } } } } RL_FREE(framedata); RL_FREE(poses); + RL_FREE(anim); fclose(iqmFile); - animations[0] = animation; - *animCount = count; return animations; } -- cgit v1.2.3