Added some comments on LoadGLTF()

This commit is contained in:
raysan5 2021-10-23 14:00:05 +02:00
parent c4f0e80895
commit 04a4efb591

View file

@ -4653,6 +4653,7 @@ static Model LoadGLTF(const char *fileName)
model.meshMaterial = RL_CALLOC(model.meshCount, sizeof(int)); model.meshMaterial = RL_CALLOC(model.meshCount, sizeof(int));
// Load materials data // Load materials data
//----------------------------------------------------------------------------------------------------
for (unsigned int i = 0, j = 1; i < data->materials_count; i++, j++) for (unsigned int i = 0, j = 1; i < data->materials_count; i++, j++)
{ {
model.materials[j] = LoadMaterialDefault(); model.materials[j] = LoadMaterialDefault();
@ -4742,9 +4743,11 @@ static Model LoadGLTF(const char *fileName)
} }
// Load meshes data // Load meshes data
// NOTE: meshIndex accumulates primitives //----------------------------------------------------------------------------------------------------
for (unsigned int i = 0, meshIndex = 0; i < data->meshes_count; i++) for (unsigned int i = 0, meshIndex = 0; i < data->meshes_count; i++)
{ {
// NOTE: meshIndex accumulates primitives
for (unsigned int p = 0; p < data->meshes[i].primitives_count; p++) for (unsigned int p = 0; p < data->meshes[i].primitives_count; p++)
{ {
// NOTE: We only support primitives defined by triangles // NOTE: We only support primitives defined by triangles
@ -4761,6 +4764,8 @@ static Model LoadGLTF(const char *fileName)
{ {
cgltf_accessor *attribute = data->meshes[i].primitives[p].attributes[j].data; cgltf_accessor *attribute = data->meshes[i].primitives[p].attributes[j].data;
// WARNING: SPECS: POSITION accessor MUST have its min and max properties defined.
if ((attribute->component_type == cgltf_component_type_r_32f) && (attribute->type == cgltf_type_vec3)) if ((attribute->component_type == cgltf_component_type_r_32f) && (attribute->type == cgltf_type_vec3))
{ {
// Init raylib mesh vertices to copy glTF attribute data // Init raylib mesh vertices to copy glTF attribute data
@ -4786,8 +4791,24 @@ static Model LoadGLTF(const char *fileName)
} }
else TRACELOG(LOG_WARNING, "MODEL: [%s] Normal attribute data format not supported, use vec3 float", fileName); else TRACELOG(LOG_WARNING, "MODEL: [%s] Normal attribute data format not supported, use vec3 float", fileName);
} }
else if (data->meshes[i].primitives[p].attributes[j].type == cgltf_attribute_type_tangent) // TANGENT
{
cgltf_accessor *attribute = data->meshes[i].primitives[p].attributes[j].data;
if ((attribute->component_type == cgltf_component_type_r_32f) && (attribute->type == cgltf_type_vec4))
{
// Init raylib mesh normals to copy glTF attribute data
model.meshes[meshIndex].tangents = RL_MALLOC(attribute->count*4*sizeof(float));
// Load 3 components of float data type into mesh.normals
LOAD_ATTRIBUTE(attribute, 4, float, model.meshes[meshIndex].tangents)
}
else TRACELOG(LOG_WARNING, "MODEL: [%s] Tangent attribute data format not supported, use vec4 float", fileName);
}
else if (data->meshes[i].primitives[p].attributes[j].type == cgltf_attribute_type_texcoord) // TEXCOORD_0 else if (data->meshes[i].primitives[p].attributes[j].type == cgltf_attribute_type_texcoord) // TEXCOORD_0
{ {
// TODO: Support additional texture coordinates: TEXCOORD_1 -> mesh.texcoords2
cgltf_accessor *attribute = data->meshes[i].primitives[p].attributes[j].data; cgltf_accessor *attribute = data->meshes[i].primitives[p].attributes[j].data;
if ((attribute->component_type == cgltf_component_type_r_32f) && (attribute->type == cgltf_type_vec2)) if ((attribute->component_type == cgltf_component_type_r_32f) && (attribute->type == cgltf_type_vec2))
@ -4804,6 +4825,8 @@ static Model LoadGLTF(const char *fileName)
{ {
cgltf_accessor *attribute = data->meshes[i].primitives[p].attributes[j].data; cgltf_accessor *attribute = data->meshes[i].primitives[p].attributes[j].data;
// WARNING: SPECS: All components of each COLOR_n accessor element MUST be clamped to [0.0, 1.0] range.
if ((attribute->component_type == cgltf_component_type_r_8u) && (attribute->type == cgltf_type_vec4)) if ((attribute->component_type == cgltf_component_type_r_8u) && (attribute->type == cgltf_type_vec4))
{ {
// Init raylib mesh color to copy glTF attribute data // Init raylib mesh color to copy glTF attribute data
@ -4843,15 +4866,14 @@ static Model LoadGLTF(const char *fileName)
else TRACELOG(LOG_WARNING, "MODEL: [%s] Color attribute data format not supported", fileName); else TRACELOG(LOG_WARNING, "MODEL: [%s] Color attribute data format not supported", fileName);
} }
// TODO: Additional attributes that could be supported (some related to animations): // NOTE: Attributes related to animations are processed separately
// cgltf_attribute_type_tangent, cgltf_attribute_type_joints, cgltf_attribute_type_weights
} }
// Load primitive indices data (if provided) // Load primitive indices data (if provided)
if (data->meshes[i].primitives[p].indices != NULL)
{
cgltf_accessor *attribute = data->meshes[i].primitives[p].indices; cgltf_accessor *attribute = data->meshes[i].primitives[p].indices;
if (attribute != NULL)
{
model.meshes[meshIndex].triangleCount = (int)attribute->count/3; model.meshes[meshIndex].triangleCount = (int)attribute->count/3;
if (attribute->component_type == cgltf_component_type_r_16u) if (attribute->component_type == cgltf_component_type_r_16u)