mirror of
https://git.mirrors.martin98.com/https://github.com/syoyo/tinygltf.git
synced 2025-08-14 02:15:53 +08:00
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:
parent
55bff342c2
commit
0da2b35085
@ -129,13 +129,14 @@ bool LoadGLTF(const std::string &filename, float scale,
|
|||||||
}
|
}
|
||||||
const auto &indices = *indicesArrayPtr;
|
const auto &indices = *indicesArrayPtr;
|
||||||
|
|
||||||
if (indicesArrayPtr)
|
if (indicesArrayPtr) {
|
||||||
|
std::cout << "indices: ";
|
||||||
for (size_t i(0); i < indicesArrayPtr->size(); ++i) {
|
for (size_t i(0); i < indicesArrayPtr->size(); ++i) {
|
||||||
// std::cout << indices[i] << " ";
|
std::cout << indices[i] << " ";
|
||||||
loadedMesh.faces.push_back(indices[i]);
|
loadedMesh.faces.push_back(indices[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << '\n';
|
std::cout << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
switch (meshPrimitive.mode) {
|
switch (meshPrimitive.mode) {
|
||||||
case TINYGLTF_MODE_TRIANGLES: // this is the simpliest case to handle
|
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 byte_stride = attribAccessor.ByteStride(bufferView);
|
||||||
const auto count = attribAccessor.count;
|
const auto count = attribAccessor.count;
|
||||||
|
|
||||||
|
std::cout << "current attribute has count " << count
|
||||||
|
<< " and stride " << byte_stride << " bytes\n";
|
||||||
|
|
||||||
if (attribute.first == "POSITION") {
|
if (attribute.first == "POSITION") {
|
||||||
std::cout << "found position attribute\n";
|
std::cout << "found position attribute\n";
|
||||||
|
|
||||||
@ -166,15 +170,20 @@ bool LoadGLTF(const std::string &filename, float scale,
|
|||||||
|
|
||||||
switch (attribAccessor.componentType) {
|
switch (attribAccessor.componentType) {
|
||||||
case TINYGLTF_COMPONENT_TYPE_FLOAT:
|
case TINYGLTF_COMPONENT_TYPE_FLOAT:
|
||||||
|
std::cout << "Type is FLOAT\n";
|
||||||
switch (attribAccessor.type) {
|
switch (attribAccessor.type) {
|
||||||
case TINYGLTF_TYPE_VEC3: {
|
case TINYGLTF_TYPE_VEC3: {
|
||||||
// 3D vector of float
|
// 3D vector of float
|
||||||
v3fArray positions(
|
v3fArray positions(
|
||||||
arrayAdapter<v3f>(dataPtr, count, byte_stride));
|
arrayAdapter<v3f>(dataPtr, count, byte_stride));
|
||||||
|
|
||||||
|
std::cout << "positions's size : " << positions.size()
|
||||||
|
<< '\n';
|
||||||
|
|
||||||
for (size_t i{0}; i < positions.size(); ++i) {
|
for (size_t i{0}; i < positions.size(); ++i) {
|
||||||
const auto v = positions[i];
|
const auto v = positions[i];
|
||||||
std::cout << '(' << v.x << ", " << v.y << ", " << v.z
|
std::cout << "positions[" << i << "]: (" << v.x << ", "
|
||||||
<< ")\n";
|
<< v.y << ", " << v.z << ")\n";
|
||||||
|
|
||||||
loadedMesh.vertices.push_back(v.x * scale);
|
loadedMesh.vertices.push_back(v.x * scale);
|
||||||
loadedMesh.vertices.push_back(v.y * scale);
|
loadedMesh.vertices.push_back(v.y * scale);
|
||||||
@ -185,15 +194,17 @@ bool LoadGLTF(const std::string &filename, float scale,
|
|||||||
// TODO Handle error
|
// TODO Handle error
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
case TINYGLTF_COMPONENT_TYPE_DOUBLE: {
|
case TINYGLTF_COMPONENT_TYPE_DOUBLE: {
|
||||||
|
std::cout << "Type is DOUBLE\n";
|
||||||
switch (attribAccessor.type) {
|
switch (attribAccessor.type) {
|
||||||
case TINYGLTF_TYPE_VEC3: {
|
case TINYGLTF_TYPE_VEC3: {
|
||||||
v3dArray positions(
|
v3dArray positions(
|
||||||
arrayAdapter<v3d>(dataPtr, count, byte_stride));
|
arrayAdapter<v3d>(dataPtr, count, byte_stride));
|
||||||
for (size_t i{0}; i < positions.size(); ++i) {
|
for (size_t i{0}; i < positions.size(); ++i) {
|
||||||
const auto v = positions[i];
|
const auto v = positions[i];
|
||||||
std::cout << '(' << v.x << ", " << v.y << ", " << v.z
|
std::cout << "positions[" << i << "]: (" << v.x << ", "
|
||||||
<< ")\n";
|
<< v.y << ", " << v.z << ")\n";
|
||||||
|
|
||||||
loadedMesh.vertices.push_back(v.x * scale);
|
loadedMesh.vertices.push_back(v.x * scale);
|
||||||
loadedMesh.vertices.push_back(v.y * 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
|
// Other trigangle based modes
|
||||||
case TINYGLTF_MODE_TRIANGLE_FAN:
|
case TINYGLTF_MODE_TRIANGLE_FAN:
|
||||||
@ -303,8 +312,6 @@ bool LoadGLTF(const std::string &filename, float scale,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO compute pivot point
|
|
||||||
|
|
||||||
// bbox :
|
// bbox :
|
||||||
v3f bCenter;
|
v3f bCenter;
|
||||||
bCenter.x = 0.5f * (pMax.x - pMin.x) + pMin.x;
|
bCenter.x = 0.5f * (pMax.x - pMin.x) + pMin.x;
|
||||||
|
@ -20,7 +20,6 @@ struct arrayAdapter {
|
|||||||
const size_t elemCount;
|
const size_t elemCount;
|
||||||
/// Stride in bytes between two elements
|
/// Stride in bytes between two elements
|
||||||
const size_t stride;
|
const size_t stride;
|
||||||
/// Constructor.
|
|
||||||
|
|
||||||
/// Construct an array adapter.
|
/// Construct an array adapter.
|
||||||
/// \param ptr Pointer to the start of the data, with offset applied
|
/// \param ptr Pointer to the start of the data, with offset applied
|
||||||
|
@ -773,6 +773,14 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
for (size_t n = 0; n < gAsset.meshes.size(); n++) {
|
for (size_t n = 0; n < gAsset.meshes.size(); n++) {
|
||||||
nanosg::Node<float, example::Mesh<float> > node(&gAsset.meshes[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.SetName(meshes[n].name);
|
||||||
node.SetLocalXform(meshes[n].pivot_xform); // Use mesh's pivot transform
|
node.SetLocalXform(meshes[n].pivot_xform); // Use mesh's pivot transform
|
||||||
// as node's local transform.
|
// as node's local transform.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user