diff --git a/src/Magnum/Trade/MeshData.cpp b/src/Magnum/Trade/MeshData.cpp index 9c149db99..f1e458bb9 100644 --- a/src/Magnum/Trade/MeshData.cpp +++ b/src/Magnum/Trade/MeshData.cpp @@ -261,6 +261,12 @@ UnsignedInt MeshData::attributeFor(const MeshAttribute name, UnsignedInt id) con #endif } +UnsignedInt MeshData::attributeId(const MeshAttribute name, UnsignedInt id) const { + const UnsignedInt attributeId = attributeFor(name, id); + CORRADE_ASSERT(attributeId != ~UnsignedInt{}, "Trade::MeshData::attributeId(): index" << id << "out of range for" << attributeCount(name) << name << "attributes", {}); + return attributeId; +} + VertexFormat MeshData::attributeFormat(MeshAttribute name, UnsignedInt id) const { const UnsignedInt attributeId = attributeFor(name, id); CORRADE_ASSERT(attributeId != ~UnsignedInt{}, "Trade::MeshData::attributeFormat(): index" << id << "out of range for" << attributeCount(name) << name << "attributes", {}); diff --git a/src/Magnum/Trade/MeshData.h b/src/Magnum/Trade/MeshData.h index bba3ce6ab..c710d4aa3 100644 --- a/src/Magnum/Trade/MeshData.h +++ b/src/Magnum/Trade/MeshData.h @@ -817,6 +817,14 @@ class MAGNUM_TRADE_EXPORT MeshData { */ UnsignedInt attributeCount(MeshAttribute name) const; + /** + * @brief Absolute ID of a named attribute + * + * The @p id is expected to be smaller than + * @ref attributeCount(MeshAttribute) const. + */ + UnsignedInt attributeId(MeshAttribute name, UnsignedInt id = 0) const; + /** * @brief Format of a named attribute * @@ -1129,6 +1137,7 @@ class MAGNUM_TRADE_EXPORT MeshData { implementations. */ friend AbstractImporter; + /* Internal helper that doesn't assert, unlike attributeId() */ UnsignedInt attributeFor(MeshAttribute name, UnsignedInt id) const; UnsignedInt _vertexCount; diff --git a/src/Magnum/Trade/Test/MeshDataTest.cpp b/src/Magnum/Trade/Test/MeshDataTest.cpp index 72f267860..8d42f8bb6 100644 --- a/src/Magnum/Trade/Test/MeshDataTest.cpp +++ b/src/Magnum/Trade/Test/MeshDataTest.cpp @@ -628,6 +628,11 @@ void MeshDataTest::construct() { CORRADE_COMPARE(data.attributeCount(meshAttributeCustom(13)), 1); CORRADE_COMPARE(data.attributeCount(MeshAttribute::Color), 0); CORRADE_COMPARE(data.attributeCount(meshAttributeCustom(23)), 0); + CORRADE_COMPARE(data.attributeId(MeshAttribute::Position), 0); + CORRADE_COMPARE(data.attributeId(MeshAttribute::Normal), 2); + CORRADE_COMPARE(data.attributeId(MeshAttribute::TextureCoordinates), 1); + CORRADE_COMPARE(data.attributeId(MeshAttribute::TextureCoordinates, 1), 3); + CORRADE_COMPARE(data.attributeId(meshAttributeCustom(13)), 4); CORRADE_COMPARE(data.attributeFormat(MeshAttribute::Position), VertexFormat::Vector3); CORRADE_COMPARE(data.attributeFormat(MeshAttribute::Normal), @@ -1533,6 +1538,8 @@ void MeshDataTest::attributeNotFound() { data.attributeStride(2); data.attribute(2); data.attribute(2); + data.attributeId(MeshAttribute::Position); + data.attributeId(MeshAttribute::Color, 2); data.attributeFormat(MeshAttribute::Position); data.attributeFormat(MeshAttribute::Color, 2); data.attributeOffset(MeshAttribute::Position); @@ -1555,6 +1562,8 @@ void MeshDataTest::attributeNotFound() { "Trade::MeshData::attributeStride(): index 2 out of range for 2 attributes\n" "Trade::MeshData::attribute(): index 2 out of range for 2 attributes\n" "Trade::MeshData::attribute(): index 2 out of range for 2 attributes\n" + "Trade::MeshData::attributeId(): index 0 out of range for 0 Trade::MeshAttribute::Position attributes\n" + "Trade::MeshData::attributeId(): index 2 out of range for 2 Trade::MeshAttribute::Color attributes\n" "Trade::MeshData::attributeFormat(): index 0 out of range for 0 Trade::MeshAttribute::Position attributes\n" "Trade::MeshData::attributeFormat(): index 2 out of range for 2 Trade::MeshAttribute::Color attributes\n" "Trade::MeshData::attributeOffset(): index 0 out of range for 0 Trade::MeshAttribute::Position attributes\n"