|
|
@ -3738,7 +3738,7 @@ static Model LoadGLTF(const char *fileName) |
|
|
|
|
|
|
|
for (unsigned int i = 0; i < data->nodes_count; i++) |
|
|
|
{ |
|
|
|
if(data->nodes[i].has_translation) |
|
|
|
if (data->nodes[i].has_translation) |
|
|
|
{ |
|
|
|
memcpy(&model.bindPose[i].translation, data->nodes[i].translation, 3 * sizeof(float)); |
|
|
|
} |
|
|
@ -3747,7 +3747,7 @@ static Model LoadGLTF(const char *fileName) |
|
|
|
model.bindPose[i].translation = Vector3Zero(); |
|
|
|
} |
|
|
|
|
|
|
|
if(data->nodes[i].has_rotation) |
|
|
|
if (data->nodes[i].has_rotation) |
|
|
|
{ |
|
|
|
memcpy(&model.bindPose[i].rotation, data->nodes[i].rotation, 4 * sizeof(float)); |
|
|
|
} |
|
|
@ -3757,7 +3757,7 @@ static Model LoadGLTF(const char *fileName) |
|
|
|
} |
|
|
|
model.bindPose[i].rotation = QuaternionNormalize(model.bindPose[i].rotation); |
|
|
|
|
|
|
|
if(data->nodes[i].has_scale) |
|
|
|
if (data->nodes[i].has_scale) |
|
|
|
{ |
|
|
|
memcpy(&model.bindPose[i].scale, data->nodes[i].scale, 3 * sizeof(float)); |
|
|
|
} |
|
|
@ -3898,19 +3898,19 @@ static Model LoadGLTF(const char *fileName) |
|
|
|
{ |
|
|
|
cgltf_accessor *acc = data->meshes[i].primitives[p].attributes[j].data; |
|
|
|
|
|
|
|
if(acc->component_type == cgltf_component_type_r_16u) |
|
|
|
if (acc->component_type == cgltf_component_type_r_16u) |
|
|
|
{ |
|
|
|
model.meshes[primitiveIndex].boneIds = RL_MALLOC(sizeof(int) * acc->count * 4); |
|
|
|
short* bones = RL_MALLOC(sizeof(short) * acc->count * 4); |
|
|
|
|
|
|
|
LOAD_ACCESSOR(short, 4, acc, bones); |
|
|
|
for(int a = 0; a < acc->count * 4; a ++) |
|
|
|
for (unsigned int a = 0; a < acc->count * 4; a ++) |
|
|
|
{ |
|
|
|
cgltf_node* skinJoint = data->skins->joints[bones[a]]; |
|
|
|
|
|
|
|
for(int k = 0; k < data->nodes_count; k++) |
|
|
|
for (unsigned int k = 0; k < data->nodes_count; k++) |
|
|
|
{ |
|
|
|
if(&(data->nodes[k]) == skinJoint) |
|
|
|
if (&(data->nodes[k]) == skinJoint) |
|
|
|
{ |
|
|
|
model.meshes[primitiveIndex].boneIds[a] = k; |
|
|
|
break; |
|
|
@ -3967,7 +3967,7 @@ static Model LoadGLTF(const char *fileName) |
|
|
|
model.meshMaterial[primitiveIndex] = model.materialCount - 1;; |
|
|
|
} |
|
|
|
|
|
|
|
// if(data->meshes[i].) |
|
|
|
// if (data->meshes[i].) |
|
|
|
|
|
|
|
primitiveIndex++; |
|
|
|
} |
|
|
@ -3985,9 +3985,9 @@ static Model LoadGLTF(const char *fileName) |
|
|
|
|
|
|
|
static bool GltfReadFloat(cgltf_accessor* acc, unsigned int index, float* variable, unsigned int elements) |
|
|
|
{ |
|
|
|
if(acc->count == 2) |
|
|
|
if (acc->count == 2) |
|
|
|
{ |
|
|
|
if(index > 1) |
|
|
|
if (index > 1) |
|
|
|
{ |
|
|
|
return false; |
|
|
|
} |
|
|
@ -4058,18 +4058,18 @@ static ModelAnimation* LoadGLTFModelAnimations(const char *fileName, int *animCo |
|
|
|
float animationDuration = 0.0f; |
|
|
|
|
|
|
|
// Getting the max animation time to consider for animation duration |
|
|
|
for (int i = 0; i < animation->channels_count; i++) |
|
|
|
for (unsigned int i = 0; i < animation->channels_count; i++) |
|
|
|
{ |
|
|
|
cgltf_animation_channel* channel = animation->channels + i; |
|
|
|
int frameCounts = channel->sampler->input->count; |
|
|
|
float lastFrameTime = 0.0f; |
|
|
|
if(GltfReadFloat(channel->sampler->input, frameCounts - 1, &lastFrameTime, 1)) |
|
|
|
if (GltfReadFloat(channel->sampler->input, frameCounts - 1, &lastFrameTime, 1)) |
|
|
|
{ |
|
|
|
animationDuration = fmaxf(lastFrameTime, animationDuration); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
output->frameCount = animationDuration / TIMESTEP; |
|
|
|
output->frameCount = p">(int)(animationDuration / TIMESTEP); |
|
|
|
output->boneCount = data->nodes_count; |
|
|
|
output->bones = RL_MALLOC(output->boneCount*sizeof(BoneInfo)); |
|
|
|
output->framePoses = RL_MALLOC(output->frameCount*sizeof(Transform *)); |
|
|
@ -4084,7 +4084,7 @@ static ModelAnimation* LoadGLTFModelAnimations(const char *fileName, int *animCo |
|
|
|
|
|
|
|
// Allocate data for frames |
|
|
|
// Initiate with zero bone translations |
|
|
|
for (unsigned int frame = 0; frame < output->frameCount; frame++) |
|
|
|
for (int frame = 0; frame < output->frameCount; frame++) |
|
|
|
{ |
|
|
|
output->framePoses[frame] = RL_MALLOC(output->frameCount*data->nodes_count*sizeof(Transform)); |
|
|
|
|
|
|
@ -4098,14 +4098,14 @@ static ModelAnimation* LoadGLTFModelAnimations(const char *fileName, int *animCo |
|
|
|
} |
|
|
|
|
|
|
|
// for each single transformation type on single bone |
|
|
|
for(int channelId = 0; channelId < animation->channels_count; channelId++) |
|
|
|
for (unsigned int channelId = 0; channelId < animation->channels_count; channelId++) |
|
|
|
{ |
|
|
|
cgltf_animation_channel* channel = animation->channels + channelId; |
|
|
|
cgltf_animation_sampler* sampler = channel->sampler; |
|
|
|
|
|
|
|
int boneId = channel->target_node - data->nodes; |
|
|
|
|
|
|
|
for(int frame = 0; frame < output->frameCount; frame++) |
|
|
|
for (int frame = 0; frame < output->frameCount; frame++) |
|
|
|
{ |
|
|
|
bool shouldSkipFurtherTransformation = true; |
|
|
|
int outputMin = 0; |
|
|
@ -4116,19 +4116,19 @@ static ModelAnimation* LoadGLTFModelAnimations(const char *fileName, int *animCo |
|
|
|
// For this transformation: |
|
|
|
// getting between which input values the current frame time position |
|
|
|
// and also what is the percent to use in the linear interpolation later |
|
|
|
for(int j = 0; j < sampler->input->count; j++) |
|
|
|
for (unsigned int j = 0; j < sampler->input->count; j++) |
|
|
|
{ |
|
|
|
float inputFrameTime; |
|
|
|
if(GltfReadFloat(sampler->input, j, (float*)&inputFrameTime, 1)) |
|
|
|
if (GltfReadFloat(sampler->input, j, (float*)&inputFrameTime, 1)) |
|
|
|
{ |
|
|
|
if(frameTime < inputFrameTime) |
|
|
|
if (frameTime < inputFrameTime) |
|
|
|
{ |
|
|
|
shouldSkipFurtherTransformation = false; |
|
|
|
outputMin = j - 1; |
|
|
|
outputMax = j; |
|
|
|
|
|
|
|
float previousInputTime = 0.0f; |
|
|
|
if(GltfReadFloat(sampler->input, j - 1, (float*)&previousInputTime, 1)) |
|
|
|
if (GltfReadFloat(sampler->input, j - 1, (float*)&previousInputTime, 1)) |
|
|
|
{ |
|
|
|
lerpPercent = (frameTime - previousInputTime) / (inputFrameTime - previousInputTime); |
|
|
|
} |
|
|
@ -4140,44 +4140,44 @@ static ModelAnimation* LoadGLTFModelAnimations(const char *fileName, int *animCo |
|
|
|
} |
|
|
|
|
|
|
|
// If the current transformation has no information for the current frame time point |
|
|
|
if(shouldSkipFurtherTransformation) { |
|
|
|
if (shouldSkipFurtherTransformation) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
if(channel->target_path == cgltf_animation_path_type_translation) { |
|
|
|
if (channel->target_path == cgltf_animation_path_type_translation) { |
|
|
|
Vector3 translationStart; |
|
|
|
Vector3 translationEnd; |
|
|
|
|
|
|
|
bool success = GltfReadFloat(sampler->output, outputMin, (float*)&translationStart, 3); |
|
|
|
success = GltfReadFloat(sampler->output, outputMax, (float*)&translationEnd, 3) || success; |
|
|
|
|
|
|
|
if(success) |
|
|
|
if (success) |
|
|
|
{ |
|
|
|
output->framePoses[frame][boneId].translation = Vector3Lerp(translationStart, translationEnd, lerpPercent); |
|
|
|
} |
|
|
|
} |
|
|
|
if(channel->target_path == cgltf_animation_path_type_rotation) { |
|
|
|
if (channel->target_path == cgltf_animation_path_type_rotation) { |
|
|
|
Quaternion rotationStart; |
|
|
|
Quaternion rotationEnd; |
|
|
|
|
|
|
|
bool success = GltfReadFloat(sampler->output, outputMin, (float*)&rotationStart, 4); |
|
|
|
success = GltfReadFloat(sampler->output, outputMax, (float*)&rotationEnd, 4) || success; |
|
|
|
|
|
|
|
if(success) |
|
|
|
if (success) |
|
|
|
{ |
|
|
|
output->framePoses[frame][boneId].rotation = QuaternionLerp(rotationStart, rotationEnd, lerpPercent); |
|
|
|
output->framePoses[frame][boneId].rotation = QuaternionNormalize(output->framePoses[frame][boneId].rotation); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if(channel->target_path == cgltf_animation_path_type_scale) { |
|
|
|
if (channel->target_path == cgltf_animation_path_type_scale) { |
|
|
|
Vector3 scaleStart; |
|
|
|
Vector3 scaleEnd; |
|
|
|
|
|
|
|
bool success = GltfReadFloat(sampler->output, outputMin, (float*)&scaleStart, 3); |
|
|
|
success = GltfReadFloat(sampler->output, outputMax, (float*)&scaleEnd, 3) || success; |
|
|
|
|
|
|
|
if(success) |
|
|
|
if (success) |
|
|
|
{ |
|
|
|
output->framePoses[frame][boneId].scale = Vector3Lerp(scaleStart, scaleEnd, lerpPercent); |
|
|
|
} |
|
|
@ -4186,7 +4186,7 @@ static ModelAnimation* LoadGLTFModelAnimations(const char *fileName, int *animCo |
|
|
|
} |
|
|
|
|
|
|
|
// Build frameposes |
|
|
|
for (unsigned int frame = 0; frame < output->frameCount; frame++) |
|
|
|
for (int frame = 0; frame < output->frameCount; frame++) |
|
|
|
{ |
|
|
|
for (int i = 0; i < output->boneCount; i++) |
|
|
|
{ |
|
|
|