From 87d16bc627960ff541482d653b78f50e6b76effd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 20 Feb 2020 14:34:59 +0100 Subject: [PATCH] Trade: add a convenience MeshData::indexOffset() getter. --- src/Magnum/Trade/MeshData.cpp | 6 ++++++ src/Magnum/Trade/MeshData.h | 12 ++++++++++++ src/Magnum/Trade/Test/MeshDataTest.cpp | 11 +++++++---- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/Magnum/Trade/MeshData.cpp b/src/Magnum/Trade/MeshData.cpp index 37b48e11e..c08ae6b2c 100644 --- a/src/Magnum/Trade/MeshData.cpp +++ b/src/Magnum/Trade/MeshData.cpp @@ -191,6 +191,12 @@ MeshIndexType MeshData::indexType() const { return _indexType; } +std::size_t MeshData::indexOffset() const { + CORRADE_ASSERT(isIndexed(), + "Trade::MeshData::indexOffset(): the mesh is not indexed", {}); + return _indices.data() - _indexData.data(); +} + Containers::StridedArrayView2D MeshData::indices() const { CORRADE_ASSERT(isIndexed(), "Trade::MeshData::indices(): the mesh is not indexed", {}); diff --git a/src/Magnum/Trade/MeshData.h b/src/Magnum/Trade/MeshData.h index e7ab2cafc..4ef07f9bb 100644 --- a/src/Magnum/Trade/MeshData.h +++ b/src/Magnum/Trade/MeshData.h @@ -681,6 +681,17 @@ class MAGNUM_TRADE_EXPORT MeshData { */ MeshIndexType indexType() const; + /** + * @brief Index offset + * + * Byte offset of the first index from the beginning of the + * @ref indexData(), or a byte difference between pointers returned + * from @ref indexData() and @ref indices(). Expects that the mesh is + * indexed. + * @see @ref attributeOffset() + */ + std::size_t indexOffset() const; + /** * @brief Mesh indices * @@ -775,6 +786,7 @@ class MAGNUM_TRADE_EXPORT MeshData { * @ref attributeCount() const. You can also use * @ref attributeOffset(MeshAttribute, UnsignedInt) const to * directly get an offset of given named attribute. + * @see @ref indexOffset() */ std::size_t attributeOffset(UnsignedInt id) const; diff --git a/src/Magnum/Trade/Test/MeshDataTest.cpp b/src/Magnum/Trade/Test/MeshDataTest.cpp index 1b1ffd097..8d3b04fea 100644 --- a/src/Magnum/Trade/Test/MeshDataTest.cpp +++ b/src/Magnum/Trade/Test/MeshDataTest.cpp @@ -497,8 +497,8 @@ void MeshDataTest::construct() { Short id; }; - Containers::Array indexData{6*sizeof(UnsignedShort)}; - auto indexView = Containers::arrayCast(indexData); + Containers::Array indexData{8*sizeof(UnsignedShort)}; + auto indexView = Containers::arrayCast(indexData).slice(1, 7); indexView[0] = 0; indexView[1] = 1; indexView[2] = 2; @@ -541,9 +541,9 @@ void MeshDataTest::construct() { CORRADE_COMPARE(data.vertexDataFlags(), DataFlag::Owned|DataFlag::Mutable); CORRADE_COMPARE(data.primitive(), MeshPrimitive::Triangles); CORRADE_VERIFY(!data.attributeData().empty()); - CORRADE_COMPARE(static_cast(data.indexData()), indexView.data()); + CORRADE_COMPARE(static_cast(data.indexData() + 2), indexView.data()); CORRADE_COMPARE(static_cast(data.vertexData()), vertexView.data()); - CORRADE_COMPARE(static_cast(data.mutableIndexData()), indexView.data()); + CORRADE_COMPARE(static_cast(data.mutableIndexData() + 2), indexView.data()); CORRADE_COMPARE(static_cast(data.mutableVertexData()), vertexView.data()); CORRADE_COMPARE(data.importerState(), &importerState); @@ -551,6 +551,7 @@ void MeshDataTest::construct() { CORRADE_VERIFY(data.isIndexed()); CORRADE_COMPARE(data.indexCount(), 6); CORRADE_COMPARE(data.indexType(), MeshIndexType::UnsignedShort); + CORRADE_COMPARE(data.indexOffset(), 2); /* Typeless index access with a cast later */ CORRADE_COMPARE((Containers::arrayCast<1, const UnsignedShort>(data.indices())[1]), 1); @@ -1456,6 +1457,7 @@ void MeshDataTest::indicesNotIndexed() { Error redirectError{&out}; data.indexCount(); data.indexType(); + data.indexOffset(); data.indices(); data.indices(); data.indicesAsArray(); @@ -1464,6 +1466,7 @@ void MeshDataTest::indicesNotIndexed() { CORRADE_COMPARE(out.str(), "Trade::MeshData::indexCount(): the mesh is not indexed\n" "Trade::MeshData::indexType(): the mesh is not indexed\n" + "Trade::MeshData::indexOffset(): the mesh is not indexed\n" "Trade::MeshData::indices(): the mesh is not indexed\n" "Trade::MeshData::indices(): the mesh is not indexed\n" "Trade::MeshData::indicesAsArray(): the mesh is not indexed\n"