UpdateModelAnimation() - Added security check
This commit is contained in:
parent
41732bebe8
commit
37bb8e9554
1 changed files with 63 additions and 65 deletions
30
src/models.c
30
src/models.c
|
@ -919,7 +919,7 @@ ModelAnimation *LoadModelAnimations(const char *filename, int *animCount)
|
||||||
TraceLog(LOG_ERROR, "[%s] Unable to open file", filename);
|
TraceLog(LOG_ERROR, "[%s] Unable to open file", filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
// header
|
// Read IQM header
|
||||||
fread(&iqm, sizeof(IQMHeader), 1, iqmFile);
|
fread(&iqm, sizeof(IQMHeader), 1, iqmFile);
|
||||||
|
|
||||||
if (strncmp(iqm.magic, IQM_MAGIC, sizeof(IQM_MAGIC)))
|
if (strncmp(iqm.magic, IQM_MAGIC, sizeof(IQM_MAGIC)))
|
||||||
|
@ -934,34 +934,30 @@ ModelAnimation *LoadModelAnimations(const char *filename, int *animCount)
|
||||||
fclose(iqmFile);
|
fclose(iqmFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
// bones
|
// Get bones data
|
||||||
IQMPose *poses;
|
IQMPose *poses = RL_MALLOC(iqm.num_poses*sizeof(IQMPose));
|
||||||
poses = RL_MALLOC(sizeof(IQMPose)*iqm.num_poses);
|
|
||||||
fseek(iqmFile, iqm.ofs_poses, SEEK_SET);
|
fseek(iqmFile, iqm.ofs_poses, SEEK_SET);
|
||||||
fread(poses, sizeof(IQMPose)*iqm.num_poses, 1, iqmFile);
|
fread(poses, iqm.num_poses*sizeof(IQMPose), 1, iqmFile);
|
||||||
|
|
||||||
// animations
|
// Get animations data
|
||||||
*animCount = iqm.num_anims;
|
*animCount = iqm.num_anims;
|
||||||
IQMAnim *anim = RL_MALLOC(iqm.num_anims*sizeof(IQMAnim));
|
IQMAnim *anim = RL_MALLOC(iqm.num_anims*sizeof(IQMAnim));
|
||||||
fseek(iqmFile, iqm.ofs_anims, SEEK_SET);
|
fseek(iqmFile, iqm.ofs_anims, SEEK_SET);
|
||||||
fread(anim, iqm.num_anims*sizeof(IQMAnim), 1, iqmFile);
|
fread(anim, iqm.num_anims*sizeof(IQMAnim), 1, iqmFile);
|
||||||
ModelAnimation *animations = RL_MALLOC(iqm.num_anims*sizeof(ModelAnimation));
|
ModelAnimation *animations = RL_MALLOC(iqm.num_anims*sizeof(ModelAnimation));
|
||||||
|
|
||||||
|
|
||||||
// frameposes
|
// frameposes
|
||||||
unsigned short *framedata = RL_MALLOC(sizeof(unsigned short)*iqm.num_frames*iqm.num_framechannels);
|
unsigned short *framedata = RL_MALLOC(iqm.num_frames*iqm.num_framechannels*sizeof(unsigned short));
|
||||||
fseek(iqmFile, iqm.ofs_frames, SEEK_SET);
|
fseek(iqmFile, iqm.ofs_frames, SEEK_SET);
|
||||||
fread(framedata, sizeof(unsigned short)*iqm.num_frames*iqm.num_framechannels, 1, iqmFile);
|
fread(framedata, iqm.num_frames*iqm.num_framechannels*sizeof(unsigned short), 1, iqmFile);
|
||||||
|
|
||||||
for (int a = 0; a < iqm.num_anims; a++)
|
for (int a = 0; a < iqm.num_anims; a++)
|
||||||
{
|
{
|
||||||
|
|
||||||
animations[a].frameCount = anim[a].num_frames;
|
animations[a].frameCount = anim[a].num_frames;
|
||||||
animations[a].boneCount = iqm.num_poses;
|
animations[a].boneCount = iqm.num_poses;
|
||||||
animations[a].bones = RL_MALLOC(sizeof(BoneInfo)*iqm.num_poses);
|
animations[a].bones = RL_MALLOC(iqm.num_poses*sizeof(BoneInfo));
|
||||||
animations[a].framePoses = RL_MALLOC(sizeof(Transform*)*anim[a].num_frames);
|
animations[a].framePoses = RL_MALLOC(anim[a].num_frames*sizeof(Transform *));
|
||||||
// unused for now
|
//animations[a].framerate = anim.framerate; // TODO: Use framerate?
|
||||||
//animations[a].framerate = anim.framerate;
|
|
||||||
|
|
||||||
for (int j = 0; j < iqm.num_poses; j++)
|
for (int j = 0; j < iqm.num_poses; j++)
|
||||||
{
|
{
|
||||||
|
@ -969,7 +965,7 @@ ModelAnimation *LoadModelAnimations(const char *filename, int *animCount)
|
||||||
animations[a].bones[j].parent = poses[j].parent;
|
animations[a].bones[j].parent = poses[j].parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int j = 0; j < anim[a].num_frames; j++) animations[a].framePoses[j] = RL_MALLOC(sizeof(Transform)*iqm.num_poses);
|
for (int j = 0; j < anim[a].num_frames; j++) animations[a].framePoses[j] = RL_MALLOC(iqm.num_poses*sizeof(Transform));
|
||||||
|
|
||||||
int dcounter = anim[a].first_frame*iqm.num_framechannels;
|
int dcounter = anim[a].first_frame*iqm.num_framechannels;
|
||||||
|
|
||||||
|
@ -1083,13 +1079,14 @@ ModelAnimation *LoadModelAnimations(const char *filename, int *animCount)
|
||||||
|
|
||||||
fclose(iqmFile);
|
fclose(iqmFile);
|
||||||
|
|
||||||
|
|
||||||
return animations;
|
return animations;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update model animated vertex data (positions and normals) for a given frame
|
// Update model animated vertex data (positions and normals) for a given frame
|
||||||
// NOTE: Updated data is uploaded to GPU
|
// NOTE: Updated data is uploaded to GPU
|
||||||
void UpdateModelAnimation(Model model, ModelAnimation anim, int frame)
|
void UpdateModelAnimation(Model model, ModelAnimation anim, int frame)
|
||||||
|
{
|
||||||
|
if ((anim.frameCount > 0) && (anim.bones != NULL) && (anim.framePoses != NULL))
|
||||||
{
|
{
|
||||||
if (frame >= anim.frameCount) frame = frame%anim.frameCount;
|
if (frame >= anim.frameCount) frame = frame%anim.frameCount;
|
||||||
|
|
||||||
|
@ -1148,6 +1145,7 @@ void UpdateModelAnimation(Model model, ModelAnimation anim, int frame)
|
||||||
rlUpdateBuffer(model.meshes[m].vboId[2], model.meshes[m].animVertices, model.meshes[m].vertexCount*3*sizeof(float)); // Update vertex normals
|
rlUpdateBuffer(model.meshes[m].vboId[2], model.meshes[m].animVertices, model.meshes[m].vertexCount*3*sizeof(float)); // Update vertex normals
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Unload animation data
|
// Unload animation data
|
||||||
void UnloadModelAnimation(ModelAnimation anim)
|
void UnloadModelAnimation(ModelAnimation anim)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue