Loading info about vertex to joint connection but animation is still not working

This commit is contained in:
Hristo Stamenov 2021-01-26 22:12:07 +02:00
parent 8e0e6e7776
commit 822d2cf865
2 changed files with 53 additions and 42 deletions

View File

@ -84,7 +84,7 @@ int main(void)
for (int i = 0; i < model.boneCount; i++)
{
DrawCube(anims[0].framePoses[animFrameCounter][i].translation, 0.2f, 0.2f, 0.2f, RED);
DrawSphere(anims[0].framePoses[animFrameCounter][i].translation, 0.01f, RED);
}
DrawGrid(10, 1.0f); // Draw a grid

View File

@ -3882,17 +3882,19 @@ static Model LoadGLTF(const char *fileName)
cgltf_accessor *acc = data->meshes[i].primitives[p].attributes[j].data;
model.meshes[primitiveIndex].vertexCount = (int)acc->count;
model.meshes[primitiveIndex].vertices = RL_MALLOC(model.meshes[primitiveIndex].vertexCount*3*sizeof(float));
model.meshes[primitiveIndex].animVertices = RL_MALLOC(sizeof(float)*model.meshes[primitiveIndex].vertexCount*3);
model.meshes[primitiveIndex].animVertices = RL_MALLOC(model.meshes[primitiveIndex].vertexCount*3*sizeof(float));
LOAD_ACCESSOR(float, 3, acc, model.meshes[primitiveIndex].vertices)
memcpy(model.meshes[primitiveIndex].animVertices, model.meshes[primitiveIndex].vertices, model.meshes[primitiveIndex].vertexCount*3*sizeof(float));
}
else if (data->meshes[i].primitives[p].attributes[j].type == cgltf_attribute_type_normal)
{
cgltf_accessor *acc = data->meshes[i].primitives[p].attributes[j].data;
model.meshes[primitiveIndex].normals = RL_MALLOC(acc->count*3*sizeof(float));
model.meshes[primitiveIndex].animNormals = RL_MALLOC(sizeof(float)*model.meshes[primitiveIndex].vertexCount*3);
model.meshes[primitiveIndex].animNormals = RL_MALLOC(acc->count*3*sizeof(float));
LOAD_ACCESSOR(float, 3, acc, model.meshes[primitiveIndex].normals)
memcpy(model.meshes[primitiveIndex].animNormals, model.meshes[primitiveIndex].normals, acc->count*3*sizeof(float));
}
else if (data->meshes[i].primitives[p].attributes[j].type == cgltf_attribute_type_texcoord)
{
@ -3914,14 +3916,12 @@ static Model LoadGLTF(const char *fileName)
cgltf_accessor *acc = data->meshes[i].primitives[p].attributes[j].data;
model.meshes[primitiveIndex].boneIds = RL_MALLOC(sizeof(int) * acc->count * 4);
float* bones = RL_MALLOC(sizeof(float) * acc->count * 4);
LOAD_ACCESSOR(float, 4, acc, bones);
short* bones = RL_MALLOC(sizeof(short) * acc->count * 4);
LOAD_ACCESSOR(short, 4, acc, bones);
for(int a = 0; a < acc->count * 4; a ++)
{
cgltf_node* ptr = (int)(bones[a] + 0.5f);
int index = ptr - data->nodes;
model.meshes[primitiveIndex].boneIds[a] = max(0, index);
model.meshes[primitiveIndex].boneIds[a] = (int)bones[a];
}
}
else if (data->meshes[i].primitives[p].attributes[j].type == cgltf_attribute_type_weights)
@ -4033,37 +4033,48 @@ static ModelAnimation* LoadGLTFModelAnimations(const char *fileName, int *animCo
output->bones[j].parent = j != 0 ? data->nodes[j].parent - data->nodes : 0;
}
for (unsigned int j = 0; j < output->frameCount; j++) output->framePoses[j] = RL_MALLOC(output->frameCount*sizeof(Transform));
int dcounter = animation->channels_count;
for (unsigned int j = 0; j < output->frameCount; j++)
output->framePoses[j] = RL_MALLOC(output->frameCount*sizeof(Transform));
for (unsigned int frame = 0; frame < output->frameCount; frame++)
{
cgltf_animation_channel *channel = animation->channels + frame;
cgltf_animation_sampler *sampler = channel->sampler;
int i = channel->target_node - data->nodes;
cgltf_animation_channel* channel = animation->channels + frame;
cgltf_animation_sampler* sampler = channel->sampler;
if(channel->target_path == cgltf_animation_path_type_translation)
for (unsigned int i = 0; i < data->nodes_count; i++)
{
cgltf_accessor_read_float(sampler->output, i, (float*)&output->framePoses[frame][i].translation,
3);
cgltf_node* node = channel->target_node + i;
if (node->has_translation)
{
memcpy(&output->framePoses[frame][i].translation, node->translation, sizeof(float) * 3);
}
else
{
output->framePoses[frame][i].translation = Vector3Zero();
}
if(channel->target_path == cgltf_animation_path_type_rotation)
if (node->has_rotation)
{
cgltf_accessor_read_float(sampler->output, i, (float*)&output->framePoses[frame][i].rotation,
4);
memcpy(&output->framePoses[frame][i].rotation, node->rotation, sizeof(float)*4);
}
else
{
output->framePoses[frame][i].rotation = QuaternionIdentity();
}
if(channel->target_path == cgltf_animation_path_type_scale)
if (node->has_scale)
{
cgltf_accessor_read_float(sampler->output, i, (float*)&output->framePoses[frame][i].scale,
3);
memcpy(&output->framePoses[frame][i].scale, node->scale, sizeof(float)*3);
}
else
{
output->framePoses[frame][i].scale = Vector3One();
}
output->framePoses[frame][i].rotation = QuaternionNormalize(output->framePoses[frame][i].rotation);
}
}
// Build frameposes
for (unsigned int frame = 0; frame < output->frameCount; frame++)