From 213c7d8a1e60508e4ba6574d40b760c92195c6a1 Mon Sep 17 00:00:00 2001 From: Lynix Date: Fri, 21 Jan 2022 14:57:07 +0100 Subject: [PATCH] Utility/OBJSaver: Handle case where normal and/or texcoords are not present --- src/Nazara/Utility/Formats/OBJParser.cpp | 56 +++++++++++++----------- src/Nazara/Utility/Formats/OBJSaver.cpp | 18 +++++--- 2 files changed, 44 insertions(+), 30 deletions(-) diff --git a/src/Nazara/Utility/Formats/OBJParser.cpp b/src/Nazara/Utility/Formats/OBJParser.cpp index 6727c7557..997d7220e 100644 --- a/src/Nazara/Utility/Formats/OBJParser.cpp +++ b/src/Nazara/Utility/Formats/OBJParser.cpp @@ -537,38 +537,44 @@ namespace Nz } EmitLine(); - Emit("# normal count: "); - EmitLine(m_normals.size()); - - for (const Nz::Vector3f& normal : m_normals) + if (!m_normals.empty()) { - Emit("vn "); - Emit(normal.x); - Emit(' '); - Emit(normal.y); - Emit(' '); - Emit(normal.y); - EmitLine(); - } - EmitLine(); + Emit("# normal count: "); + EmitLine(m_normals.size()); - Emit("# texcoords count: "); - EmitLine(m_texCoords.size()); - - for (const Nz::Vector3f& uvw : m_texCoords) - { - Emit("vt "); - Emit(uvw.x); - Emit(' '); - Emit(uvw.y); - if (NumberEquals(uvw.z, 0.f)) + for (const Nz::Vector3f& normal : m_normals) { + Emit("vn "); + Emit(normal.x); Emit(' '); - Emit(uvw.z); + Emit(normal.y); + Emit(' '); + Emit(normal.y); + EmitLine(); + } + EmitLine(); + } + + if (!m_texCoords.empty()) + { + Emit("# texcoords count: "); + EmitLine(m_texCoords.size()); + + for (const Nz::Vector3f& uvw : m_texCoords) + { + Emit("vt "); + Emit(uvw.x); + Emit(' '); + Emit(uvw.y); + if (NumberEquals(uvw.z, 0.f)) + { + Emit(' '); + Emit(uvw.z); + } + EmitLine(); } EmitLine(); } - EmitLine(); std::unordered_map /* meshes*/> meshesByMaterials; std::size_t meshIndex = 0; diff --git a/src/Nazara/Utility/Formats/OBJSaver.cpp b/src/Nazara/Utility/Formats/OBJSaver.cpp index 41474df17..f72a0bf0c 100644 --- a/src/Nazara/Utility/Formats/OBJSaver.cpp +++ b/src/Nazara/Utility/Formats/OBJSaver.cpp @@ -160,7 +160,7 @@ namespace Nz SparsePtr texCoordsPtr = vertexMapper.GetComponentPtr(VertexComponent::TexCoord); std::size_t faceIndex = 0; - TriangleIterator triangle(staticMesh); + TriangleIterator triangleIt(staticMesh); do { OBJParser::Face& face = meshes[i].faces[faceIndex]; @@ -171,15 +171,23 @@ namespace Nz { OBJParser::FaceVertex& vertexIndices = meshes[i].vertices[face.firstVertex + j]; - std::size_t index = triangle[j]; - vertexIndices.normal = normalCache.Insert(normalPtr[index]); + std::size_t index = triangleIt[j]; vertexIndices.position = positionCache.Insert(positionPtr[index]); - vertexIndices.texCoord = texCoordsCache.Insert(texCoordsPtr[index]); + + if (normalPtr) + vertexIndices.normal = normalCache.Insert(normalPtr[index]); + else + vertexIndices.normal = 0; + + if (texCoordsPtr) + vertexIndices.texCoord = texCoordsCache.Insert(texCoordsPtr[index]); + else + vertexIndices.texCoord = 0; } faceIndex++; } - while (triangle.Advance()); + while (triangleIt.Advance()); } }