From 9b8c655c13b17c7a9d75f49e0854cd9e773cf44a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 5 Jul 2022 09:33:51 +0200 Subject: [PATCH] ObjImporter: reorder texture coordinate handling to be before normals. To have it consistent, as it was a mess right now. OBJ indices are position/texcoord/normal, so this should match that order. --- src/MagnumPlugins/ObjImporter/ObjImporter.cpp | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/MagnumPlugins/ObjImporter/ObjImporter.cpp b/src/MagnumPlugins/ObjImporter/ObjImporter.cpp index 49d4e4f13..e474b655b 100644 --- a/src/MagnumPlugins/ObjImporter/ObjImporter.cpp +++ b/src/MagnumPlugins/ObjImporter/ObjImporter.cpp @@ -284,8 +284,8 @@ Containers::Optional ObjImporter::doMesh(const UnsignedInt id, Unsigne Containers::Optional primitive; Containers::Array positions; - Containers::Array normals; Containers::Array textureCoordinates; + Containers::Array normals; /* Taking a shortcut as there's fortunately nothing else than just 3 types of data. First positions, then normals, then texture coordinates. */ Containers::Array indices; @@ -409,18 +409,18 @@ Containers::Optional ObjImporter::doMesh(const UnsignedInt id, Unsigne indexTuple = indexTuple.suffix(foundSlash1.end()); const Containers::StringView foundSlash2 = indexTuple.findOr('/', indexTuple.end()); if(!foundSlash2 || foundSlash2.begin() != indexTuple.begin()) { - if(!parseUnsignedInt("Trade::ObjImporter::mesh():", indexTuple.prefix(foundSlash2.begin()), data[i][2])) + if(!parseUnsignedInt("Trade::ObjImporter::mesh():", indexTuple.prefix(foundSlash2.begin()), data[i][1])) return {}; - data[i][2] -= mesh.textureCoordinateIndexOffset; + data[i][1] -= mesh.textureCoordinateIndexOffset; ++textureCoordinateIndexCount; } /* If there was a second slash, last is a normal */ if(foundSlash2) { indexTuple = indexTuple.suffix(foundSlash2.end()); - if(!parseUnsignedInt("Trade::ObjImporter::mesh():", indexTuple, data[i][1])) + if(!parseUnsignedInt("Trade::ObjImporter::mesh():", indexTuple, data[i][2])) return {}; - data[i][1] -= mesh.normalIndexOffset; + data[i][2] -= mesh.normalIndexOffset; ++normalIndexCount; } } @@ -486,26 +486,26 @@ Containers::Optional ObjImporter::doMesh(const UnsignedInt id, Unsigne } /* If there are index data, there should be also vertex data (and also the other way) */ - if(normals.isEmpty() != (normalIndexCount == 0)) { - Error() << "Trade::ObjImporter::mesh(): incomplete normal data"; - return Containers::NullOpt; - } if(textureCoordinates.isEmpty() != (textureCoordinateIndexCount == 0)) { Error() << "Trade::ObjImporter::mesh(): incomplete texture coordinate data"; return Containers::NullOpt; } - - /* All index arrays should have the same length */ - if(normalIndexCount && normalIndexCount != indices.size()) { - CORRADE_INTERNAL_ASSERT(normalIndexCount < indices.size()); - Error() << "Trade::ObjImporter::mesh(): some normal indices are missing"; + if(normals.isEmpty() != (normalIndexCount == 0)) { + Error() << "Trade::ObjImporter::mesh(): incomplete normal data"; return Containers::NullOpt; } + + /* All index arrays should have the same length */ if(textureCoordinateIndexCount && textureCoordinateIndexCount != indices.size()) { CORRADE_INTERNAL_ASSERT(textureCoordinateIndexCount < indices.size()); Error() << "Trade::ObjImporter::mesh(): some texture coordinate indices are missing"; return Containers::NullOpt; } + if(normalIndexCount && normalIndexCount != indices.size()) { + CORRADE_INTERNAL_ASSERT(normalIndexCount < indices.size()); + Error() << "Trade::ObjImporter::mesh(): some normal indices are missing"; + return Containers::NullOpt; + } /* Merge index arrays. If any of the attributes was not there, the whole index array has zeros, not affecting the uniqueness in any way. */ @@ -517,14 +517,14 @@ Containers::Optional ObjImporter::doMesh(const UnsignedInt id, Unsigne /* Allocate attribute and vertex data */ std::size_t attributeCount = 1; UnsignedInt stride = sizeof(Vector3); - if(normalIndexCount) { - ++attributeCount; - stride += sizeof(Vector3); - } if(textureCoordinateIndexCount) { ++attributeCount; stride += sizeof(Vector2); } + if(normalIndexCount) { + ++attributeCount; + stride += sizeof(Vector3); + } Containers::Array attributeData{attributeCount}; Containers::Array vertexData{NoInit, vertexCount*stride}; @@ -540,22 +540,22 @@ Containers::Optional ObjImporter::doMesh(const UnsignedInt id, Unsigne attributeData[attributeIndex++] = MeshAttributeData{MeshAttribute::Position, view}; offset += sizeof(Vector3); } - if(normalIndexCount) { - Containers::StridedArrayView1D view{vertexData, - reinterpret_cast(vertexData.data() + offset), vertexCount, stride}; - if(!checkAndDuplicateInto(indicesPerAttribute[1].prefix(vertexCount), normals, view, mesh.normalIndexOffset)) - return Containers::NullOpt; - attributeData[attributeIndex++] = MeshAttributeData{MeshAttribute::Normal, view}; - offset += sizeof(Vector3); - } if(textureCoordinateIndexCount) { Containers::StridedArrayView1D view{vertexData, reinterpret_cast(vertexData.data() + offset), vertexCount, stride}; - if(!checkAndDuplicateInto(indicesPerAttribute[2].prefix(vertexCount), textureCoordinates, view, mesh.textureCoordinateIndexOffset)) + if(!checkAndDuplicateInto(indicesPerAttribute[1].prefix(vertexCount), textureCoordinates, view, mesh.textureCoordinateIndexOffset)) return Containers::NullOpt; attributeData[attributeIndex++] = MeshAttributeData{MeshAttribute::TextureCoordinates, view}; offset += sizeof(Vector2); } + if(normalIndexCount) { + Containers::StridedArrayView1D view{vertexData, + reinterpret_cast(vertexData.data() + offset), vertexCount, stride}; + if(!checkAndDuplicateInto(indicesPerAttribute[2].prefix(vertexCount), normals, view, mesh.normalIndexOffset)) + return Containers::NullOpt; + attributeData[attributeIndex++] = MeshAttributeData{MeshAttribute::Normal, view}; + offset += sizeof(Vector3); + } CORRADE_INTERNAL_ASSERT(offset == stride && attributeIndex == attributeCount); return MeshData{*primitive,