summaryrefslogtreecommitdiffhomepage
path: root/src/rmodels.c
diff options
context:
space:
mode:
authorRay <[email protected]>2024-04-20 23:23:30 +0200
committerRay <[email protected]>2024-04-20 23:23:30 +0200
commit5cfcf13d4bc4723055f1d6b329148abb92b6b383 (patch)
tree3cb336b26ba9d5d6c103cf29408c78373a27fc94 /src/rmodels.c
parente543b78bb78266e95c88b17e94a9ede55cc7be6a (diff)
downloadraylib-5cfcf13d4bc4723055f1d6b329148abb92b6b383.tar.gz
raylib-5cfcf13d4bc4723055f1d6b329148abb92b6b383.zip
REVIEWED: `LoadGLTF()`, support 2nd texture coordinates loading
Diffstat (limited to 'src/rmodels.c')
-rw-r--r--src/rmodels.c29
1 files changed, 19 insertions, 10 deletions
diff --git a/src/rmodels.c b/src/rmodels.c
index 207fb1af..cfe7f84c 100644
--- a/src/rmodels.c
+++ b/src/rmodels.c
@@ -5109,9 +5109,18 @@ static Model LoadGLTF(const char *fileName)
}
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, vec2, float/u8n/u16n
+ else if (data->meshes[i].primitives[p].attributes[j].type == cgltf_attribute_type_texcoord) // TEXCOORD_n, vec2, float/u8n/u16n
{
- // TODO: Support additional texture coordinates: TEXCOORD_1 -> mesh.texcoords2
+ // Support up to 2 texture coordinates attributes
+ float *texcoordPtr = NULL;
+ int index = data->meshes[i].primitives[p].attributes[j].index;
+ if (index == 0) texcoordPtr = model.meshes[meshIndex].texcoords;
+ else if (index == 1) texcoordPtr = model.meshes[meshIndex].texcoords2;
+ else
+ {
+ TRACELOG(LOG_WARNING, "MODEL: [%s] No more than 2 texture coordinates attributes supported", fileName);
+ continue;
+ }
cgltf_accessor *attribute = data->meshes[i].primitives[p].attributes[j].data;
@@ -5120,44 +5129,44 @@ static Model LoadGLTF(const char *fileName)
if (attribute->component_type == cgltf_component_type_r_32f) // vec2, float
{
// Init raylib mesh texcoords to copy glTF attribute data
- model.meshes[meshIndex].texcoords = RL_MALLOC(attribute->count*2*sizeof(float));
+ texcoordPtr = RL_MALLOC(attribute->count*2*sizeof(float));
// Load 3 components of float data type into mesh.texcoords
- LOAD_ATTRIBUTE(attribute, 2, float, model.meshes[meshIndex].texcoords)
+ LOAD_ATTRIBUTE(attribute, 2, float, texcoordPtr)
}
else if (attribute->component_type == cgltf_component_type_r_8u) // vec2, u8n
{
// Init raylib mesh texcoords to copy glTF attribute data
- model.meshes[meshIndex].texcoords = RL_MALLOC(attribute->count*2*sizeof(float));
+ texcoordPtr = RL_MALLOC(attribute->count*2*sizeof(float));
// Load data into a temp buffer to be converted to raylib data type
unsigned short *temp = RL_MALLOC(attribute->count*2*sizeof(unsigned char));
LOAD_ATTRIBUTE(attribute, 2, unsigned char, temp);
// Convert data to raylib texcoord data type (float)
- for (unsigned int t = 0; t < attribute->count*2; t++) model.meshes[meshIndex].texcoords[t] = (float)temp[t]/255.0f;
+ for (unsigned int t = 0; t < attribute->count*2; t++) texcoordPtr[t] = (float)temp[t]/255.0f;
RL_FREE(temp);
}
else if (attribute->component_type == cgltf_component_type_r_16u) // vec2, u16n
{
// Init raylib mesh texcoords to copy glTF attribute data
- model.meshes[meshIndex].texcoords = RL_MALLOC(attribute->count*2*sizeof(float));
+ texcoordPtr = RL_MALLOC(attribute->count*2*sizeof(float));
// Load data into a temp buffer to be converted to raylib data type
unsigned short *temp = RL_MALLOC(attribute->count*2*sizeof(unsigned short));
LOAD_ATTRIBUTE(attribute, 2, unsigned short, temp);
// Convert data to raylib texcoord data type (float)
- for (unsigned int t = 0; t < attribute->count*2; t++) model.meshes[meshIndex].texcoords[t] = (float)temp[t]/65535.0f;
+ for (unsigned int t = 0; t < attribute->count*2; t++) texcoordPtr[t] = (float)temp[t]/65535.0f;
RL_FREE(temp);
}
- else TRACELOG(LOG_WARNING, "MODEL: [%s] Texcoords attribute data format not supported, use vec2 float", fileName);
+ else TRACELOG(LOG_WARNING, "MODEL: [%s] Texcoords attribute data format not supported", fileName);
}
else TRACELOG(LOG_WARNING, "MODEL: [%s] Texcoords attribute data format not supported, use vec2 float", fileName);
}
- else if (data->meshes[i].primitives[p].attributes[j].type == cgltf_attribute_type_color) // COLOR_0, vec3/vec4, float/u8n/u16n
+ else if (data->meshes[i].primitives[p].attributes[j].type == cgltf_attribute_type_color) // COLOR_n, vec3/vec4, float/u8n/u16n
{
cgltf_accessor *attribute = data->meshes[i].primitives[p].attributes[j].data;