diff --git a/src/MeshTools/CompressIndices.cpp b/src/MeshTools/CompressIndices.cpp index 60ce595be..b76c5af3e 100644 --- a/src/MeshTools/CompressIndices.cpp +++ b/src/MeshTools/CompressIndices.cpp @@ -40,13 +40,8 @@ template inline std::tuple compres return std::make_tuple(indices.size(), indexType(), buffer); } -} -#endif - -std::tuple compressIndices(const std::vector& indices) { - std::size_t size = *std::max_element(indices.begin(), indices.end()); - - switch(Math::log(256, size)) { +std::tuple compressIndicesInternal(const std::vector& indices, std::uint32_t max) { + switch(Math::log(256, max)) { case 0: return compress(indices); case 1: @@ -56,18 +51,29 @@ std::tuple compressIndices(const std::vecto return compress(indices); default: - CORRADE_ASSERT(false, "MeshTools::compressIndices(): no type able to index" << size << "elements.", {}); + CORRADE_ASSERT(false, "MeshTools::compressIndices(): no type able to index" << max << "elements.", {}); } } +} +#endif + +std::tuple compressIndices(const std::vector& indices) { + return compressIndicesInternal(indices, *std::max_element(indices.begin(), indices.end())); +} + void compressIndices(Mesh* mesh, Buffer* buffer, Buffer::Usage usage, const std::vector& indices) { + auto minmax = std::minmax_element(indices.begin(), indices.end()); + + /** @todo Performance hint when range can be represented by smaller value? */ + std::size_t indexCount; Mesh::IndexType indexType; char* data; - std::tie(indexCount, indexType, data) = compressIndices(indices); + std::tie(indexCount, indexType, data) = compressIndicesInternal(indices, *minmax.second); mesh->setIndexCount(indices.size()) - ->setIndexBuffer(buffer, 0, indexType); + ->setIndexBuffer(buffer, 0, indexType, *minmax.first, *minmax.second); buffer->setData(indexCount*Mesh::indexSize(indexType), data, usage); delete[] data; diff --git a/src/MeshTools/CompressIndices.h b/src/MeshTools/CompressIndices.h index a0ee59b1e..ac67d5849 100644 --- a/src/MeshTools/CompressIndices.h +++ b/src/MeshTools/CompressIndices.h @@ -63,8 +63,8 @@ std::tuple MAGNUM_MESHTOOLS_EXPORT compress The same as compressIndices(const std::vector&), but this function writes the output to given buffer, updates index count and specifies -index buffer in the mesh, so you don't have to call Mesh::setIndexCount() -and Mesh::setIndexBuffer() on your own. +index buffer with proper index range in the mesh, so you don't have to call +Mesh::setIndexCount() and Mesh::setIndexBuffer() on your own. @see MeshTools::interleave() */