Browse Source

Trade: add a convenience MeshData::indexOffset() getter.

pull/371/head
Vladimír Vondruš 6 years ago
parent
commit
87d16bc627
  1. 6
      src/Magnum/Trade/MeshData.cpp
  2. 12
      src/Magnum/Trade/MeshData.h
  3. 11
      src/Magnum/Trade/Test/MeshDataTest.cpp

6
src/Magnum/Trade/MeshData.cpp

@ -191,6 +191,12 @@ MeshIndexType MeshData::indexType() const {
return _indexType; 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<const char> MeshData::indices() const { Containers::StridedArrayView2D<const char> MeshData::indices() const {
CORRADE_ASSERT(isIndexed(), CORRADE_ASSERT(isIndexed(),
"Trade::MeshData::indices(): the mesh is not indexed", {}); "Trade::MeshData::indices(): the mesh is not indexed", {});

12
src/Magnum/Trade/MeshData.h

@ -681,6 +681,17 @@ class MAGNUM_TRADE_EXPORT MeshData {
*/ */
MeshIndexType indexType() const; 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 * @brief Mesh indices
* *
@ -775,6 +786,7 @@ class MAGNUM_TRADE_EXPORT MeshData {
* @ref attributeCount() const. You can also use * @ref attributeCount() const. You can also use
* @ref attributeOffset(MeshAttribute, UnsignedInt) const to * @ref attributeOffset(MeshAttribute, UnsignedInt) const to
* directly get an offset of given named attribute. * directly get an offset of given named attribute.
* @see @ref indexOffset()
*/ */
std::size_t attributeOffset(UnsignedInt id) const; std::size_t attributeOffset(UnsignedInt id) const;

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

@ -497,8 +497,8 @@ void MeshDataTest::construct() {
Short id; Short id;
}; };
Containers::Array<char> indexData{6*sizeof(UnsignedShort)}; Containers::Array<char> indexData{8*sizeof(UnsignedShort)};
auto indexView = Containers::arrayCast<UnsignedShort>(indexData); auto indexView = Containers::arrayCast<UnsignedShort>(indexData).slice(1, 7);
indexView[0] = 0; indexView[0] = 0;
indexView[1] = 1; indexView[1] = 1;
indexView[2] = 2; indexView[2] = 2;
@ -541,9 +541,9 @@ void MeshDataTest::construct() {
CORRADE_COMPARE(data.vertexDataFlags(), DataFlag::Owned|DataFlag::Mutable); CORRADE_COMPARE(data.vertexDataFlags(), DataFlag::Owned|DataFlag::Mutable);
CORRADE_COMPARE(data.primitive(), MeshPrimitive::Triangles); CORRADE_COMPARE(data.primitive(), MeshPrimitive::Triangles);
CORRADE_VERIFY(!data.attributeData().empty()); CORRADE_VERIFY(!data.attributeData().empty());
CORRADE_COMPARE(static_cast<const void*>(data.indexData()), indexView.data()); CORRADE_COMPARE(static_cast<const void*>(data.indexData() + 2), indexView.data());
CORRADE_COMPARE(static_cast<const void*>(data.vertexData()), vertexView.data()); CORRADE_COMPARE(static_cast<const void*>(data.vertexData()), vertexView.data());
CORRADE_COMPARE(static_cast<void*>(data.mutableIndexData()), indexView.data()); CORRADE_COMPARE(static_cast<void*>(data.mutableIndexData() + 2), indexView.data());
CORRADE_COMPARE(static_cast<void*>(data.mutableVertexData()), vertexView.data()); CORRADE_COMPARE(static_cast<void*>(data.mutableVertexData()), vertexView.data());
CORRADE_COMPARE(data.importerState(), &importerState); CORRADE_COMPARE(data.importerState(), &importerState);
@ -551,6 +551,7 @@ void MeshDataTest::construct() {
CORRADE_VERIFY(data.isIndexed()); CORRADE_VERIFY(data.isIndexed());
CORRADE_COMPARE(data.indexCount(), 6); CORRADE_COMPARE(data.indexCount(), 6);
CORRADE_COMPARE(data.indexType(), MeshIndexType::UnsignedShort); CORRADE_COMPARE(data.indexType(), MeshIndexType::UnsignedShort);
CORRADE_COMPARE(data.indexOffset(), 2);
/* Typeless index access with a cast later */ /* Typeless index access with a cast later */
CORRADE_COMPARE((Containers::arrayCast<1, const UnsignedShort>(data.indices())[1]), 1); CORRADE_COMPARE((Containers::arrayCast<1, const UnsignedShort>(data.indices())[1]), 1);
@ -1456,6 +1457,7 @@ void MeshDataTest::indicesNotIndexed() {
Error redirectError{&out}; Error redirectError{&out};
data.indexCount(); data.indexCount();
data.indexType(); data.indexType();
data.indexOffset();
data.indices(); data.indices();
data.indices<UnsignedInt>(); data.indices<UnsignedInt>();
data.indicesAsArray(); data.indicesAsArray();
@ -1464,6 +1466,7 @@ void MeshDataTest::indicesNotIndexed() {
CORRADE_COMPARE(out.str(), CORRADE_COMPARE(out.str(),
"Trade::MeshData::indexCount(): the mesh is not indexed\n" "Trade::MeshData::indexCount(): the mesh is not indexed\n"
"Trade::MeshData::indexType(): 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::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" "Trade::MeshData::indicesAsArray(): the mesh is not indexed\n"

Loading…
Cancel
Save