Skip to content

Commit 561cc27

Browse files
authored
[rModels] Support 16 bit vec3 values in gltf reader (#5388)
* Support 16 bit vec3 values coming from gltf * Add support for 8 bit normals
1 parent 983efae commit 561cc27

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

src/rmodels.c

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5578,6 +5578,56 @@ static Model LoadGLTF(const char *fileName)
55785578
vertices[3*k+2] = vt.z;
55795579
}
55805580
}
5581+
else if ((attribute->type == cgltf_type_vec3) && (attribute->component_type == cgltf_component_type_r_16u))
5582+
{
5583+
// Init raylib mesh vertices to copy glTF attribute data
5584+
model.meshes[meshIndex].vertexCount = (int)attribute->count;
5585+
model.meshes[meshIndex].vertices = (float*)RL_MALLOC(attribute->count * 3 * sizeof(float));
5586+
5587+
// Load data into a temp buffer to be converted to raylib data type
5588+
unsigned short* temp = (unsigned short*)RL_MALLOC(attribute->count * 3 * sizeof(unsigned short));
5589+
LOAD_ATTRIBUTE(attribute, 3, unsigned short, temp);
5590+
5591+
// Convert data to raylib vertex data type (float) the matrix will scale it to the correct size as a float
5592+
for (unsigned int t = 0; t < attribute->count * 3; t++) model.meshes[meshIndex].vertices[t] = (float)temp[t];
5593+
5594+
RL_FREE(temp);
5595+
5596+
// Transform the vertices
5597+
float* vertices = model.meshes[meshIndex].vertices;
5598+
for (unsigned int k = 0; k < attribute->count; k++)
5599+
{
5600+
Vector3 vt = Vector3Transform((Vector3) { vertices[3 * k], vertices[3 * k + 1], vertices[3 * k + 2] }, worldMatrix);
5601+
vertices[3 * k] = vt.x;
5602+
vertices[3 * k + 1] = vt.y;
5603+
vertices[3 * k + 2] = vt.z;
5604+
}
5605+
}
5606+
else if ((attribute->type == cgltf_type_vec3) && (attribute->component_type == cgltf_component_type_r_16))
5607+
{
5608+
// Init raylib mesh vertices to copy glTF attribute data
5609+
model.meshes[meshIndex].vertexCount = (int)attribute->count;
5610+
model.meshes[meshIndex].vertices = (float*)RL_MALLOC(attribute->count * 3 * sizeof(float));
5611+
5612+
// Load data into a temp buffer to be converted to raylib data type
5613+
short* temp = (short*)RL_MALLOC(attribute->count * 3 * sizeof(short));
5614+
LOAD_ATTRIBUTE(attribute, 3, short, temp);
5615+
5616+
// Convert data to raylib vertex data type (float) the matrix will scale it to the correct size as a float
5617+
for (unsigned int t = 0; t < attribute->count * 3; t++) model.meshes[meshIndex].vertices[t] = (float)temp[t];
5618+
5619+
RL_FREE(temp);
5620+
5621+
// Transform the vertices
5622+
float* vertices = model.meshes[meshIndex].vertices;
5623+
for (unsigned int k = 0; k < attribute->count; k++)
5624+
{
5625+
Vector3 vt = Vector3Transform((Vector3) { vertices[3 * k], vertices[3 * k + 1], vertices[3 * k + 2] }, worldMatrix);
5626+
vertices[3 * k] = vt.x;
5627+
vertices[3 * k + 1] = vt.y;
5628+
vertices[3 * k + 2] = vt.z;
5629+
}
5630+
}
55815631
else TRACELOG(LOG_WARNING, "MODEL: [%s] Vertices attribute data format not supported, use vec3 float", fileName);
55825632
}
55835633
}
@@ -5606,6 +5656,78 @@ static Model LoadGLTF(const char *fileName)
56065656
normals[3*k+2] = nt.z;
56075657
}
56085658
}
5659+
else if ((attribute->type == cgltf_type_vec3) && (attribute->component_type == cgltf_component_type_r_16))
5660+
{
5661+
// Init raylib mesh normals to copy glTF attribute data
5662+
model.meshes[meshIndex].normals = (float*)RL_MALLOC(attribute->count * 3 * sizeof(float));
5663+
5664+
// Load data into a temp buffer to be converted to raylib data type
5665+
short* temp = (short*)RL_MALLOC(attribute->count * 3 * sizeof(short));
5666+
LOAD_ATTRIBUTE(attribute, 3, short, temp);
5667+
5668+
// Convert data to raylib normal data type (float)
5669+
for (unsigned int t = 0; t < attribute->count * 3; t++) model.meshes[meshIndex].normals[t] = (float)temp[t];
5670+
5671+
RL_FREE(temp);
5672+
5673+
// Transform the normals
5674+
float* normals = model.meshes[meshIndex].normals;
5675+
for (unsigned int k = 0; k < attribute->count; k++)
5676+
{
5677+
Vector3 nt = Vector3Normalize(Vector3Transform((Vector3) { normals[3 * k], normals[3 * k + 1], normals[3 * k + 2] }, worldMatrixNormals));
5678+
normals[3 * k] = nt.x;
5679+
normals[3 * k + 1] = nt.y;
5680+
normals[3 * k + 2] = nt.z;
5681+
}
5682+
}
5683+
else if ((attribute->type == cgltf_type_vec3) && (attribute->component_type == cgltf_component_type_r_8u))
5684+
{
5685+
// Init raylib mesh normals to copy glTF attribute data
5686+
model.meshes[meshIndex].normals = (float*)RL_MALLOC(attribute->count * 3 * sizeof(float));
5687+
5688+
// Load data into a temp buffer to be converted to raylib data type
5689+
unsigned char* temp = (unsigned char*)RL_MALLOC(attribute->count * 3 * sizeof(unsigned char));
5690+
LOAD_ATTRIBUTE(attribute, 3, unsigned char, temp);
5691+
5692+
// Convert data to raylib normal data type (float)
5693+
for (unsigned int t = 0; t < attribute->count * 3; t++) model.meshes[meshIndex].normals[t] = (float)temp[t];
5694+
5695+
RL_FREE(temp);
5696+
5697+
// Transform the normals
5698+
float* normals = model.meshes[meshIndex].normals;
5699+
for (unsigned int k = 0; k < attribute->count; k++)
5700+
{
5701+
Vector3 nt = Vector3Normalize(Vector3Transform((Vector3) { normals[3 * k], normals[3 * k + 1], normals[3 * k + 2] }, worldMatrixNormals));
5702+
normals[3 * k] = nt.x;
5703+
normals[3 * k + 1] = nt.y;
5704+
normals[3 * k + 2] = nt.z;
5705+
}
5706+
}
5707+
else if ((attribute->type == cgltf_type_vec3) && (attribute->component_type == cgltf_component_type_r_8))
5708+
{
5709+
// Init raylib mesh normals to copy glTF attribute data
5710+
model.meshes[meshIndex].normals = (float*)RL_MALLOC(attribute->count * 3 * sizeof(float));
5711+
5712+
// Load data into a temp buffer to be converted to raylib data type
5713+
char* temp = (char*)RL_MALLOC(attribute->count * 3 * sizeof(char));
5714+
LOAD_ATTRIBUTE(attribute, 3, char, temp);
5715+
5716+
// Convert data to raylib normal data type (float)
5717+
for (unsigned int t = 0; t < attribute->count * 3; t++) model.meshes[meshIndex].normals[t] = (float)temp[t];
5718+
5719+
RL_FREE(temp);
5720+
5721+
// Transform the normals
5722+
float* normals = model.meshes[meshIndex].normals;
5723+
for (unsigned int k = 0; k < attribute->count; k++)
5724+
{
5725+
Vector3 nt = Vector3Normalize(Vector3Transform((Vector3) { normals[3 * k], normals[3 * k + 1], normals[3 * k + 2] }, worldMatrixNormals));
5726+
normals[3 * k] = nt.x;
5727+
normals[3 * k + 1] = nt.y;
5728+
normals[3 * k + 2] = nt.z;
5729+
}
5730+
}
56095731
else TRACELOG(LOG_WARNING, "MODEL: [%s] Normals attribute data format not supported, use vec3 float", fileName);
56105732
}
56115733
}

0 commit comments

Comments
 (0)