Browse Source

Trade: add MeshData::attributeId() for inverse ID mapping.

A helper for knowing e.g. which texture coordinate set is given
texture coordinate attribute.
pull/554/head
Vladimír Vondruš 4 years ago
parent
commit
4d5c53dbc6
  1. 2
      doc/changelog.dox
  2. 10
      src/Magnum/Trade/MeshData.cpp
  3. 13
      src/Magnum/Trade/MeshData.h
  4. 7
      src/Magnum/Trade/Test/MeshDataTest.cpp

2
doc/changelog.dox

@ -525,6 +525,8 @@ See also:
lookup compared to @relativeref{Trade::MeshData,hasAttribute()} +
@relativeref{Trade::MeshData,attributeId()}. This also makes the API
consistent with @ref Trade::SceneData::findFieldId().
- Added @ref Trade::MeshData::attributeId() that returns ID of an attribute
in a set of attributes of the same name
- Added @ref Trade::TextureType::Texture1DArray,
@relativeref{Trade::TextureType,Texture2DArray} and
@relativeref{Trade::TextureType,CubeMapArray} in order to be able to

10
src/Magnum/Trade/MeshData.cpp

@ -346,6 +346,16 @@ MeshAttribute MeshData::attributeName(const UnsignedInt id) const {
return _attributes[id]._name;
}
UnsignedInt MeshData::attributeId(const UnsignedInt id) const {
CORRADE_ASSERT(id < _attributes.size(),
"Trade::MeshData::attributeId(): index" << id << "out of range for" << _attributes.size() << "attributes", {});
const MeshAttribute name = _attributes[id]._name;
UnsignedInt count = 0;
for(UnsignedInt i = 0; i != id; ++i)
if(_attributes[i]._name == name) ++count;
return count;
}
VertexFormat MeshData::attributeFormat(const UnsignedInt id) const {
CORRADE_ASSERT(id < _attributes.size(),
"Trade::MeshData::attributeFormat(): index" << id << "out of range for" << _attributes.size() << "attributes", {});

13
src/Magnum/Trade/MeshData.h

@ -1322,6 +1322,18 @@ class MAGNUM_TRADE_EXPORT MeshData {
*/
MeshAttribute attributeName(UnsignedInt id) const;
/**
* @brief Attribute ID in a set of attributes of the same name
* @m_since_latest
*
* The @p id is expected to be smaller than @ref attributeCount() const.
* Returns the number of attributes of the same @ref attributeName()
* preceeding @p id, or @cpp 0 @ce if it's the first attribute of
* given name.
* @see @ref attributeId(MeshAttribute, UnsignedInt) const
*/
UnsignedInt attributeId(UnsignedInt id) const;
/**
* @brief Attribute format
*
@ -1417,6 +1429,7 @@ class MAGNUM_TRADE_EXPORT MeshData {
*
* Like @ref findAttributeId(), but the @p id is expected to be smaller
* than @ref attributeCount(MeshAttribute) const.
* @see @ref attributeId(UnsignedInt) const
*/
UnsignedInt attributeId(MeshAttribute name, UnsignedInt id = 0) const;

7
src/Magnum/Trade/Test/MeshDataTest.cpp

@ -1295,6 +1295,11 @@ void MeshDataTest::construct() {
CORRADE_COMPARE(data.attributeName(2), MeshAttribute::Normal);
CORRADE_COMPARE(data.attributeName(3), MeshAttribute::TextureCoordinates);
CORRADE_COMPARE(data.attributeName(4), meshAttributeCustom(13));
CORRADE_COMPARE(data.attributeId(0), 0);
CORRADE_COMPARE(data.attributeId(1), 0);
CORRADE_COMPARE(data.attributeId(2), 0);
CORRADE_COMPARE(data.attributeId(3), 1);
CORRADE_COMPARE(data.attributeId(4), 0);
CORRADE_COMPARE(data.attributeFormat(0), VertexFormat::Vector3);
CORRADE_COMPARE(data.attributeFormat(1), VertexFormat::Vector2);
CORRADE_COMPARE(data.attributeFormat(2), VertexFormat::Vector3);
@ -3474,6 +3479,7 @@ void MeshDataTest::attributeNotFound() {
Error redirectError{&out};
data.attributeData(2);
data.attributeName(2);
data.attributeId(2);
data.attributeFormat(2);
data.attributeOffset(2);
data.attributeStride(2);
@ -3520,6 +3526,7 @@ void MeshDataTest::attributeNotFound() {
CORRADE_COMPARE(out.str(),
"Trade::MeshData::attributeData(): index 2 out of range for 2 attributes\n"
"Trade::MeshData::attributeName(): index 2 out of range for 2 attributes\n"
"Trade::MeshData::attributeId(): index 2 out of range for 2 attributes\n"
"Trade::MeshData::attributeFormat(): index 2 out of range for 2 attributes\n"
"Trade::MeshData::attributeOffset(): index 2 out of range for 2 attributes\n"
"Trade::MeshData::attributeStride(): index 2 out of range for 2 attributes\n"

Loading…
Cancel
Save