diff --git a/source/Irrlicht/CGLTFMeshFileLoader.cpp b/source/Irrlicht/CGLTFMeshFileLoader.cpp index 510ae76f5..eb94f2e9f 100644 --- a/source/Irrlicht/CGLTFMeshFileLoader.cpp +++ b/source/Irrlicht/CGLTFMeshFileLoader.cpp @@ -86,44 +86,26 @@ IAnimatedMesh* CGLTFMeshFileLoader::createMesh(io::IReadFile* file) { tinygltf::Model model {}; - if (file->getSize() == 0 || !tryParseGLTF(file, model)) { + if (file->getSize() <= 0 || !tryParseGLTF(file, model)) { return nullptr; } ModelParser parser(std::move(model)); SMesh* baseMesh(new SMesh {}); - // Iterate models - for (std::size_t meshIndex = 0; - meshIndex < parser.getMeshCount(); meshIndex++) { - // Iterate primitives - for (std::size_t primitiveIndex = 0; primitiveIndex < parser.getPrimitiveCount(meshIndex); primitiveIndex++) { - const auto positionAccessorId = model.meshes[meshIndex] - .primitives[primitiveIndex].attributes["POSITION"]; - - // Creates counts for preallocation - std::size_t vertexCount = model.accessors[positionAccessorId].count; - - // We must count to create containers for the data - // Create new buffer for vertices, positions, and normals - auto* vertexBuffer = new video::S3DVertex[vertexCount](); - // This is used to copy data into the vertexBuffer - Span verticesBuffer{vertexBuffer,vertexCount}; - auto indices = parser.getIndices(meshIndex, primitiveIndex); - parser.getVertices(positionAccessorId, - verticesBuffer, - meshIndex, - primitiveIndex); + for (std::size_t i = 0; i < parser.getMeshCount(); ++i) { + for (std::size_t j = 0; j < parser.getPrimitiveCount(i); ++j) { + auto indices = parser.getIndices(i, j); + auto vertices = parser.getVertices(i, j); SMeshBuffer* meshbuf(new SMeshBuffer {}); - meshbuf->append(vertexBuffer, vertexCount, + meshbuf->append(vertices.data(), vertices.size(), indices.data(), indices.size()); baseMesh->addMeshBuffer(meshbuf); } } - // Create the mesh animations - SAnimatedMesh* animatedMesh { new SAnimatedMesh {} }; + SAnimatedMesh* animatedMesh(new SAnimatedMesh {}); animatedMesh->addMesh(baseMesh); return animatedMesh; @@ -159,31 +141,36 @@ std::vector CGLTFMeshFileLoader::ModelParser::getIndices( return indices; } -//Returns a tuple of the current counts (current_vertex_index, -// current_normals_index, current_tcoords_index) -void CGLTFMeshFileLoader::ModelParser::getVertices( - const std::size_t accessorId, - Span& outVertices, - std::size_t meshIndex, - std::size_t primitiveIndex) const +std::vector CGLTFMeshFileLoader::ModelParser::getVertices( + std::size_t meshIdx, + std::size_t primitiveIdx) const { - copyPositions(outVertices, accessorId); + auto positionAccessorIdx = getPositionAccessorIdx(meshIdx, primitiveIdx); + auto vertexCount = getElemCount(positionAccessorIdx); + std::vector vertices{}; + vertices.resize(vertexCount); - const auto normalsField = m_model.meshes[meshIndex] - .primitives[primitiveIndex].attributes.find("NORMAL"); + Span outVertices {vertices.data(), vertexCount}; - if (normalsField != m_model.meshes[meshIndex] - .primitives[primitiveIndex].attributes.end()) { + copyPositions(outVertices, positionAccessorIdx); + + const auto normalsField = m_model.meshes[meshIdx] + .primitives[primitiveIdx].attributes.find("NORMAL"); + + if (normalsField != m_model.meshes[meshIdx] + .primitives[primitiveIdx].attributes.end()) { copyNormals(outVertices, normalsField->second); } - const auto tCoordsField = m_model.meshes[meshIndex] - .primitives[primitiveIndex].attributes.find("TEXCOORD_0"); + const auto tCoordsField = m_model.meshes[meshIdx] + .primitives[primitiveIdx].attributes.find("TEXCOORD_0"); - if (tCoordsField != m_model.meshes[meshIndex] - .primitives[primitiveIndex].attributes.end()) { + if (tCoordsField != m_model.meshes[meshIdx] + .primitives[primitiveIdx].attributes.end()) { copyTCoords(outVertices, tCoordsField->second); } + + return vertices; } std::size_t CGLTFMeshFileLoader::ModelParser::getMeshCount() const @@ -315,6 +302,14 @@ std::size_t CGLTFMeshFileLoader::ModelParser::getIndicesAccessorIdx( return m_model.meshes[meshIdx].primitives[primitiveIdx].indices; } +std::size_t CGLTFMeshFileLoader::ModelParser::getPositionAccessorIdx( + std::size_t meshIdx, + std::size_t primitiveIdx) const +{ + return m_model.meshes[meshIdx].primitives[primitiveIdx] + .attributes.find("POSITION")->second; +} + bool CGLTFMeshFileLoader::tryParseGLTF(io::IReadFile* file, tinygltf::Model& model) { diff --git a/source/Irrlicht/CGLTFMeshFileLoader.h b/source/Irrlicht/CGLTFMeshFileLoader.h index e071f9b66..d2bf56ea8 100644 --- a/source/Irrlicht/CGLTFMeshFileLoader.h +++ b/source/Irrlicht/CGLTFMeshFileLoader.h @@ -67,12 +67,11 @@ private: std::vector getIndices(std::size_t meshIdx, std::size_t primitiveIdx) const; - void getVertices(const std::size_t accessorId, - Span& outVertices, - std::size_t meshIndex, - std::size_t primitiveIndex) const; + std::vector getVertices(std::size_t meshIdx, + std::size_t primitiveIdx) const; std::size_t getMeshCount() const; + std::size_t getPrimitiveCount(std::size_t meshIdx) const; private: @@ -107,6 +106,9 @@ private: std::size_t getIndicesAccessorIdx(std::size_t meshIdx, std::size_t primitiveIdx) const; + + std::size_t getPositionAccessorIdx(std::size_t meshIdx, + std::size_t primitiveIdx) const; }; static bool tryParseGLTF(io::IReadFile* file,