Fixes problem when mesh in glTF file doesn't have a name

a "unnamed_X" name will be genrated.

This fixes the loading of Trinagle.gltf from khronos

Signed-off by: Arthur Brainville (Ybalrid) <ybalrid@ybalrid.info>
This commit is contained in:
Arthur Brainville (Ybalrid) 2018-02-23 15:15:33 +01:00
parent 55bff342c2
commit 0da2b35085
No known key found for this signature in database
GPG Key ID: BC05C4812A06BCF3
3 changed files with 28 additions and 14 deletions

View File

@ -129,13 +129,14 @@ bool LoadGLTF(const std::string &filename, float scale,
}
const auto &indices = *indicesArrayPtr;
if (indicesArrayPtr)
if (indicesArrayPtr) {
std::cout << "indices: ";
for (size_t i(0); i < indicesArrayPtr->size(); ++i) {
// std::cout << indices[i] << " ";
std::cout << indices[i] << " ";
loadedMesh.faces.push_back(indices[i]);
}
std::cout << '\n';
std::cout << '\n';
}
switch (meshPrimitive.mode) {
case TINYGLTF_MODE_TRIANGLES: // this is the simpliest case to handle
@ -153,6 +154,9 @@ bool LoadGLTF(const std::string &filename, float scale,
const auto byte_stride = attribAccessor.ByteStride(bufferView);
const auto count = attribAccessor.count;
std::cout << "current attribute has count " << count
<< " and stride " << byte_stride << " bytes\n";
if (attribute.first == "POSITION") {
std::cout << "found position attribute\n";
@ -166,15 +170,20 @@ bool LoadGLTF(const std::string &filename, float scale,
switch (attribAccessor.componentType) {
case TINYGLTF_COMPONENT_TYPE_FLOAT:
std::cout << "Type is FLOAT\n";
switch (attribAccessor.type) {
case TINYGLTF_TYPE_VEC3: {
// 3D vector of float
v3fArray positions(
arrayAdapter<v3f>(dataPtr, count, byte_stride));
std::cout << "positions's size : " << positions.size()
<< '\n';
for (size_t i{0}; i < positions.size(); ++i) {
const auto v = positions[i];
std::cout << '(' << v.x << ", " << v.y << ", " << v.z
<< ")\n";
std::cout << "positions[" << i << "]: (" << v.x << ", "
<< v.y << ", " << v.z << ")\n";
loadedMesh.vertices.push_back(v.x * scale);
loadedMesh.vertices.push_back(v.y * scale);
@ -185,15 +194,17 @@ bool LoadGLTF(const std::string &filename, float scale,
// TODO Handle error
break;
}
break;
case TINYGLTF_COMPONENT_TYPE_DOUBLE: {
std::cout << "Type is DOUBLE\n";
switch (attribAccessor.type) {
case TINYGLTF_TYPE_VEC3: {
v3dArray positions(
arrayAdapter<v3d>(dataPtr, count, byte_stride));
for (size_t i{0}; i < positions.size(); ++i) {
const auto v = positions[i];
std::cout << '(' << v.x << ", " << v.y << ", " << v.z
<< ")\n";
std::cout << "positions[" << i << "]: (" << v.x << ", "
<< v.y << ", " << v.z << ")\n";
loadedMesh.vertices.push_back(v.x * scale);
loadedMesh.vertices.push_back(v.y * scale);
@ -280,9 +291,7 @@ bool LoadGLTF(const std::string &filename, float scale,
}
}
}
}
break;
} break;
// Other trigangle based modes
case TINYGLTF_MODE_TRIANGLE_FAN:
@ -303,8 +312,6 @@ bool LoadGLTF(const std::string &filename, float scale,
}
}
// TODO compute pivot point
// bbox :
v3f bCenter;
bCenter.x = 0.5f * (pMax.x - pMin.x) + pMin.x;

View File

@ -20,7 +20,6 @@ struct arrayAdapter {
const size_t elemCount;
/// Stride in bytes between two elements
const size_t stride;
/// Constructor.
/// Construct an array adapter.
/// \param ptr Pointer to the start of the data, with offset applied

View File

@ -773,6 +773,14 @@ int main(int argc, char **argv) {
for (size_t n = 0; n < gAsset.meshes.size(); n++) {
nanosg::Node<float, example::Mesh<float> > node(&gAsset.meshes[n]);
// case where the name of a mesh isn't defined in the loaded file
if (gAsset.meshes[n].name.empty()) {
std::string generatedName = "unnamed_" + std::to_string(n);
gAsset.meshes[n].name = generatedName;
meshes[n].name = generatedName;
}
node.SetName(meshes[n].name);
node.SetLocalXform(meshes[n].pivot_xform); // Use mesh's pivot transform
// as node's local transform.