From d1e97b5955623a1c54d7e4eae420e95e50c89e4b Mon Sep 17 00:00:00 2001 From: MrDiver Date: Sat, 28 Aug 2021 18:50:27 +0200 Subject: [PATCH] Fixed gltf missing transforms on load mend --- src/models.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/models.c b/src/models.c index b233cf2af..0eb1179ea 100644 --- a/src/models.c +++ b/src/models.c @@ -5448,15 +5448,31 @@ void LoadGLTFMesh(cgltf_data *data, cgltf_mesh *mesh, Model *outModel, Matrix cu void LoadGLTFNode(cgltf_data *data, cgltf_node *node, Model *outModel, Matrix currentTransform, int *primitiveIndex, const char *fileName) { - Matrix nodeTransform = { + // Apply the transforms if they exist (Will still be applied even if no mesh is present to support emptys and bone structures) + if(node->has_matrix){ + Matrix nodeTransform = { node->matrix[0], node->matrix[4], node->matrix[8], node->matrix[12], node->matrix[1], node->matrix[5], node->matrix[9], node->matrix[13], node->matrix[2], node->matrix[6], node->matrix[10], node->matrix[14], node->matrix[3], node->matrix[7], node->matrix[11], node->matrix[15] }; - - currentTransform = MatrixMultiply(nodeTransform, currentTransform); - - if (node->mesh != NULL) LoadGLTFMesh(data, node->mesh, outModel, currentTransform, primitiveIndex, fileName); + currentTransform = MatrixMultiply(nodeTransform, currentTransform); + } + if(node->has_translation){ + Matrix tl = MatrixTranslate(node->translation[0],node->translation[1],node->translation[2]); + currentTransform = MatrixMultiply(tl, currentTransform); + } + if(node->has_rotation){ + Matrix rot = QuaternionToMatrix((Quaternion){node->rotation[0],node->rotation[1],node->rotation[2],node->rotation[3]}); + currentTransform = MatrixMultiply(rot, currentTransform); + } + if(node->has_scale){ + Matrix scale = MatrixScale(node->scale[0],node->scale[1],node->scale[2]); + currentTransform = MatrixMultiply(scale, currentTransform); + } + // Load mesh if it exists + if (node->mesh != NULL){ + LoadGLTFMesh(data, node->mesh, outModel, currentTransform, primitiveIndex, fileName); + } for (unsigned int i = 0; i < node->children_count; i++) LoadGLTFNode(data, node->children[i], outModel, currentTransform, primitiveIndex, fileName); }