From f2795490443b7521ed5c97590172f8e35fee47e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 24 Apr 2018 20:07:28 +0200 Subject: [PATCH] Split the OpenGL layer out, pt 18: adapted MeshTools. --- src/Magnum/MeshTools/Compile.cpp | 27 ++++++++-------- src/Magnum/MeshTools/Compile.h | 6 ++-- src/Magnum/MeshTools/CompressIndices.cpp | 10 +++--- src/Magnum/MeshTools/CompressIndices.h | 2 +- src/Magnum/MeshTools/FullScreenTriangle.cpp | 32 +++++++++---------- src/Magnum/MeshTools/FullScreenTriangle.h | 6 ++-- .../MeshTools/Test/CompressIndicesTest.cpp | 12 +++---- 7 files changed, 48 insertions(+), 47 deletions(-) diff --git a/src/Magnum/MeshTools/Compile.cpp b/src/Magnum/MeshTools/Compile.cpp index 4b95a025a..024090632 100644 --- a/src/Magnum/MeshTools/Compile.cpp +++ b/src/Magnum/MeshTools/Compile.cpp @@ -25,7 +25,8 @@ #include "Compile.h" -#include "Magnum/Buffer.h" +#include "Magnum/GL/Buffer.h" +#include "Magnum/GL/Mesh.h" #include "Magnum/Math/Vector3.h" #include "Magnum/MeshTools/CompressIndices.h" #include "Magnum/MeshTools/Interleave.h" @@ -38,8 +39,8 @@ namespace Magnum { namespace MeshTools { -std::tuple, std::unique_ptr> compile(const Trade::MeshData2D& meshData, const BufferUsage usage) { - Mesh mesh; +std::tuple, std::unique_ptr> compile(const Trade::MeshData2D& meshData, const GL::BufferUsage usage) { + GL::Mesh mesh; mesh.setPrimitive(meshData.primitive()); /* Decide about stride and offsets */ @@ -49,7 +50,7 @@ std::tuple, std::unique_ptr> compile(const stride += sizeof(Shaders::Generic2D::TextureCoordinates::Type); /* Create vertex buffer */ - std::unique_ptr vertexBuffer{new Buffer{Buffer::TargetHint::Array}}; + std::unique_ptr vertexBuffer{new GL::Buffer{GL::Buffer::TargetHint::Array}}; /* Interleave positions */ Containers::Array data = MeshTools::interleave( @@ -75,14 +76,14 @@ std::tuple, std::unique_ptr> compile(const vertexBuffer->setData(data, usage); /* If indexed, fill index buffer and configure indexed mesh */ - std::unique_ptr indexBuffer; + std::unique_ptr indexBuffer; if(meshData.isIndexed()) { Containers::Array indexData; - Mesh::IndexType indexType; + MeshIndexType indexType; UnsignedInt indexStart, indexEnd; std::tie(indexData, indexType, indexStart, indexEnd) = MeshTools::compressIndices(meshData.indices()); - indexBuffer.reset(new Buffer{Buffer::TargetHint::ElementArray}); + indexBuffer.reset(new GL::Buffer{GL::Buffer::TargetHint::ElementArray}); indexBuffer->setData(indexData, usage); mesh.setCount(meshData.indices().size()) .setIndexBuffer(*indexBuffer, 0, indexType, indexStart, indexEnd); @@ -93,8 +94,8 @@ std::tuple, std::unique_ptr> compile(const return std::make_tuple(std::move(mesh), std::move(vertexBuffer), std::move(indexBuffer)); } -std::tuple, std::unique_ptr> compile(const Trade::MeshData3D& meshData, const BufferUsage usage) { - Mesh mesh; +std::tuple, std::unique_ptr> compile(const Trade::MeshData3D& meshData, const GL::BufferUsage usage) { + GL::Mesh mesh; mesh.setPrimitive(meshData.primitive()); /* Decide about stride and offsets */ @@ -109,7 +110,7 @@ std::tuple, std::unique_ptr> compile(const stride += sizeof(Shaders::Generic3D::TextureCoordinates::Type); /* Create vertex buffer */ - std::unique_ptr vertexBuffer{new Buffer{Buffer::TargetHint::Array}}; + std::unique_ptr vertexBuffer{new GL::Buffer{GL::Buffer::TargetHint::Array}}; /* Interleave positions */ Containers::Array data = MeshTools::interleave( @@ -147,14 +148,14 @@ std::tuple, std::unique_ptr> compile(const vertexBuffer->setData(data, usage); /* If indexed, fill index buffer and configure indexed mesh */ - std::unique_ptr indexBuffer; + std::unique_ptr indexBuffer; if(meshData.isIndexed()) { Containers::Array indexData; - Mesh::IndexType indexType; + MeshIndexType indexType; UnsignedInt indexStart, indexEnd; std::tie(indexData, indexType, indexStart, indexEnd) = MeshTools::compressIndices(meshData.indices()); - indexBuffer.reset(new Buffer{Buffer::TargetHint::ElementArray}); + indexBuffer.reset(new GL::Buffer{GL::Buffer::TargetHint::ElementArray}); indexBuffer->setData(indexData, usage); mesh.setCount(meshData.indices().size()) .setIndexBuffer(*indexBuffer, 0, indexType, indexStart, indexEnd); diff --git a/src/Magnum/MeshTools/Compile.h b/src/Magnum/MeshTools/Compile.h index b1d3e0e68..c53163e27 100644 --- a/src/Magnum/MeshTools/Compile.h +++ b/src/Magnum/MeshTools/Compile.h @@ -32,7 +32,7 @@ #include #include -#include "Magnum/Magnum.h" +#include "Magnum/GL/GL.h" #include "Magnum/Trade/Trade.h" #include "Magnum/MeshTools/visibility.h" @@ -57,7 +57,7 @@ greater flexibility. @see @ref shaders-generic */ -MAGNUM_MESHTOOLS_EXPORT std::tuple, std::unique_ptr> compile(const Trade::MeshData2D& meshData, BufferUsage usage); +MAGNUM_MESHTOOLS_EXPORT std::tuple, std::unique_ptr> compile(const Trade::MeshData2D& meshData, GL::BufferUsage usage); /** @brief Compile 3D mesh data @@ -78,7 +78,7 @@ greater flexibility. @see @ref shaders-generic */ -MAGNUM_MESHTOOLS_EXPORT std::tuple, std::unique_ptr> compile(const Trade::MeshData3D& meshData, BufferUsage usage); +MAGNUM_MESHTOOLS_EXPORT std::tuple, std::unique_ptr> compile(const Trade::MeshData3D& meshData, GL::BufferUsage usage); }} diff --git a/src/Magnum/MeshTools/CompressIndices.cpp b/src/Magnum/MeshTools/CompressIndices.cpp index 6c3bf22ad..2cfe85531 100644 --- a/src/Magnum/MeshTools/CompressIndices.cpp +++ b/src/Magnum/MeshTools/CompressIndices.cpp @@ -47,24 +47,24 @@ template inline Containers::Array compress(const std::vector, Mesh::IndexType, UnsignedInt, UnsignedInt> compressIndices(const std::vector& indices) { +std::tuple, MeshIndexType, UnsignedInt, UnsignedInt> compressIndices(const std::vector& indices) { /** @todo Performance hint when range can be represented by smaller value? */ const auto minmax = std::minmax_element(indices.begin(), indices.end()); Containers::Array data; - Mesh::IndexType type; + MeshIndexType type; switch(Math::log(256, *minmax.second)) { case 0: data = compress(indices); - type = Mesh::IndexType::UnsignedByte; + type = MeshIndexType::UnsignedByte; break; case 1: data = compress(indices); - type = Mesh::IndexType::UnsignedShort; + type = MeshIndexType::UnsignedShort; break; case 2: case 3: data = compress(indices); - type = Mesh::IndexType::UnsignedInt; + type = MeshIndexType::UnsignedInt; break; default: diff --git a/src/Magnum/MeshTools/CompressIndices.h b/src/Magnum/MeshTools/CompressIndices.h index 9e144de2d..e9015cb3f 100644 --- a/src/Magnum/MeshTools/CompressIndices.h +++ b/src/Magnum/MeshTools/CompressIndices.h @@ -53,7 +53,7 @@ Example usage: @see @ref compressIndicesAs() @todo Extract IndexType out of Mesh class */ -std::tuple, Mesh::IndexType, UnsignedInt, UnsignedInt> MAGNUM_MESHTOOLS_EXPORT compressIndices(const std::vector& indices); +std::tuple, MeshIndexType, UnsignedInt, UnsignedInt> MAGNUM_MESHTOOLS_EXPORT compressIndices(const std::vector& indices); /** @brief Compress vertex indices as given type diff --git a/src/Magnum/MeshTools/FullScreenTriangle.cpp b/src/Magnum/MeshTools/FullScreenTriangle.cpp index f7fcf3be1..743c850a3 100644 --- a/src/Magnum/MeshTools/FullScreenTriangle.cpp +++ b/src/Magnum/MeshTools/FullScreenTriangle.cpp @@ -25,44 +25,44 @@ #include "FullScreenTriangle.h" -#include "Magnum/Attribute.h" -#include "Magnum/Buffer.h" -#include "Magnum/Context.h" -#include "Magnum/Mesh.h" -#include "Magnum/Version.h" +#include "Magnum/GL/Attribute.h" +#include "Magnum/GL/Buffer.h" +#include "Magnum/GL/Context.h" +#include "Magnum/GL/Mesh.h" +#include "Magnum/GL/Version.h" #include "Magnum/Math/Vector2.h" namespace Magnum { namespace MeshTools { -std::pair, Mesh> fullScreenTriangle(Version version) { - Mesh mesh; - mesh.setPrimitive(MeshPrimitive::Triangles) +std::pair, GL::Mesh> fullScreenTriangle(GL::Version version) { + GL::Mesh mesh; + mesh.setPrimitive(GL::MeshPrimitive::Triangles) .setCount(3); - std::unique_ptr buffer; + std::unique_ptr buffer; #ifndef MAGNUM_TARGET_GLES - if(version < Version::GL300) + if(version < GL::Version::GL300) #else - if(version < Version::GLES300) + if(version < GL::Version::GLES300) #endif { - buffer.reset(new Buffer); + buffer.reset(new GL::Buffer); constexpr Vector2 triangle[] = { {-1.0f, 1.0f}, {-1.0f, -3.0f}, { 3.0f, 1.0f} }; - buffer->setData(triangle, BufferUsage::StaticDraw); + buffer->setData(triangle, GL::BufferUsage::StaticDraw); /** @todo Is it possible to attach moveable buffer here to avoid heap allocation? OTOH this is more effective in most (modern) cases */ - mesh.addVertexBuffer(*buffer, 0, Attribute<0, Vector2>{}); + mesh.addVertexBuffer(*buffer, 0, GL::Attribute<0, Vector2>{}); } return {std::move(buffer), std::move(mesh)}; } -std::pair, Mesh> fullScreenTriangle() { - return fullScreenTriangle(Context::current().version()); +std::pair, GL::Mesh> fullScreenTriangle() { + return fullScreenTriangle(GL::Context::current().version()); } }} diff --git a/src/Magnum/MeshTools/FullScreenTriangle.h b/src/Magnum/MeshTools/FullScreenTriangle.h index e80d4b403..eefe5986b 100644 --- a/src/Magnum/MeshTools/FullScreenTriangle.h +++ b/src/Magnum/MeshTools/FullScreenTriangle.h @@ -32,7 +32,7 @@ #include #include -#include "Magnum/Magnum.h" +#include "Magnum/GL/GL.h" #include "Magnum/MeshTools/visibility.h" namespace Magnum { namespace MeshTools { @@ -80,14 +80,14 @@ void main() { } @endcode */ -std::pair, Mesh> MAGNUM_MESHTOOLS_EXPORT fullScreenTriangle(Version version); +std::pair, GL::Mesh> MAGNUM_MESHTOOLS_EXPORT fullScreenTriangle(GL::Version version); /** @overload This function implicitly uses current context version. */ -std::pair, Mesh> MAGNUM_MESHTOOLS_EXPORT fullScreenTriangle(); +std::pair, GL::Mesh> MAGNUM_MESHTOOLS_EXPORT fullScreenTriangle(); }} diff --git a/src/Magnum/MeshTools/Test/CompressIndicesTest.cpp b/src/Magnum/MeshTools/Test/CompressIndicesTest.cpp index 027c488a0..1df64db4a 100644 --- a/src/Magnum/MeshTools/Test/CompressIndicesTest.cpp +++ b/src/Magnum/MeshTools/Test/CompressIndicesTest.cpp @@ -53,28 +53,28 @@ CompressIndicesTest::CompressIndicesTest() { void CompressIndicesTest::compressChar() { Containers::Array data; - Mesh::IndexType type; + MeshIndexType type; UnsignedInt start, end; std::tie(data, type, start, end) = MeshTools::compressIndices( std::vector{1, 2, 3, 0, 4}); CORRADE_COMPARE(start, 0); CORRADE_COMPARE(end, 4); - CORRADE_COMPARE(type, Mesh::IndexType::UnsignedByte); + CORRADE_COMPARE(type, MeshIndexType::UnsignedByte); CORRADE_COMPARE(std::vector(data.begin(), data.end()), (std::vector{ 0x01, 0x02, 0x03, 0x00, 0x04 })); } void CompressIndicesTest::compressShort() { Containers::Array data; - Mesh::IndexType type; + MeshIndexType type; UnsignedInt start, end; std::tie(data, type, start, end) = MeshTools::compressIndices( std::vector{1, 256, 0, 5}); CORRADE_COMPARE(start, 0); CORRADE_COMPARE(end, 256); - CORRADE_COMPARE(type, Mesh::IndexType::UnsignedShort); + CORRADE_COMPARE(type, MeshIndexType::UnsignedShort); if(!Utility::Endianness::isBigEndian()) { CORRADE_COMPARE(std::vector(data.begin(), data.end()), (std::vector{ 0x01, 0x00, @@ -92,14 +92,14 @@ void CompressIndicesTest::compressShort() { void CompressIndicesTest::compressInt() { Containers::Array data; - Mesh::IndexType type; + MeshIndexType type; UnsignedInt start, end; std::tie(data, type, start, end) = MeshTools::compressIndices( std::vector{65536, 3, 2}); CORRADE_COMPARE(start, 2); CORRADE_COMPARE(end, 65536); - CORRADE_COMPARE(type, Mesh::IndexType::UnsignedInt); + CORRADE_COMPARE(type, MeshIndexType::UnsignedInt); if(!Utility::Endianness::isBigEndian()) { CORRADE_COMPARE(std::vector(data.begin(), data.end()),