Browse Source

Split the OpenGL layer out, pt 18: adapted MeshTools.

pull/233/head
Vladimír Vondruš 8 years ago
parent
commit
f279549044
  1. 27
      src/Magnum/MeshTools/Compile.cpp
  2. 6
      src/Magnum/MeshTools/Compile.h
  3. 10
      src/Magnum/MeshTools/CompressIndices.cpp
  4. 2
      src/Magnum/MeshTools/CompressIndices.h
  5. 32
      src/Magnum/MeshTools/FullScreenTriangle.cpp
  6. 6
      src/Magnum/MeshTools/FullScreenTriangle.h
  7. 12
      src/Magnum/MeshTools/Test/CompressIndicesTest.cpp

27
src/Magnum/MeshTools/Compile.cpp

@ -25,7 +25,8 @@
#include "Compile.h" #include "Compile.h"
#include "Magnum/Buffer.h" #include "Magnum/GL/Buffer.h"
#include "Magnum/GL/Mesh.h"
#include "Magnum/Math/Vector3.h" #include "Magnum/Math/Vector3.h"
#include "Magnum/MeshTools/CompressIndices.h" #include "Magnum/MeshTools/CompressIndices.h"
#include "Magnum/MeshTools/Interleave.h" #include "Magnum/MeshTools/Interleave.h"
@ -38,8 +39,8 @@
namespace Magnum { namespace MeshTools { namespace Magnum { namespace MeshTools {
std::tuple<Mesh, std::unique_ptr<Buffer>, std::unique_ptr<Buffer>> compile(const Trade::MeshData2D& meshData, const BufferUsage usage) { std::tuple<GL::Mesh, std::unique_ptr<GL::Buffer>, std::unique_ptr<GL::Buffer>> compile(const Trade::MeshData2D& meshData, const GL::BufferUsage usage) {
Mesh mesh; GL::Mesh mesh;
mesh.setPrimitive(meshData.primitive()); mesh.setPrimitive(meshData.primitive());
/* Decide about stride and offsets */ /* Decide about stride and offsets */
@ -49,7 +50,7 @@ std::tuple<Mesh, std::unique_ptr<Buffer>, std::unique_ptr<Buffer>> compile(const
stride += sizeof(Shaders::Generic2D::TextureCoordinates::Type); stride += sizeof(Shaders::Generic2D::TextureCoordinates::Type);
/* Create vertex buffer */ /* Create vertex buffer */
std::unique_ptr<Buffer> vertexBuffer{new Buffer{Buffer::TargetHint::Array}}; std::unique_ptr<GL::Buffer> vertexBuffer{new GL::Buffer{GL::Buffer::TargetHint::Array}};
/* Interleave positions */ /* Interleave positions */
Containers::Array<char> data = MeshTools::interleave( Containers::Array<char> data = MeshTools::interleave(
@ -75,14 +76,14 @@ std::tuple<Mesh, std::unique_ptr<Buffer>, std::unique_ptr<Buffer>> compile(const
vertexBuffer->setData(data, usage); vertexBuffer->setData(data, usage);
/* If indexed, fill index buffer and configure indexed mesh */ /* If indexed, fill index buffer and configure indexed mesh */
std::unique_ptr<Buffer> indexBuffer; std::unique_ptr<GL::Buffer> indexBuffer;
if(meshData.isIndexed()) { if(meshData.isIndexed()) {
Containers::Array<char> indexData; Containers::Array<char> indexData;
Mesh::IndexType indexType; MeshIndexType indexType;
UnsignedInt indexStart, indexEnd; UnsignedInt indexStart, indexEnd;
std::tie(indexData, indexType, indexStart, indexEnd) = MeshTools::compressIndices(meshData.indices()); 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); indexBuffer->setData(indexData, usage);
mesh.setCount(meshData.indices().size()) mesh.setCount(meshData.indices().size())
.setIndexBuffer(*indexBuffer, 0, indexType, indexStart, indexEnd); .setIndexBuffer(*indexBuffer, 0, indexType, indexStart, indexEnd);
@ -93,8 +94,8 @@ std::tuple<Mesh, std::unique_ptr<Buffer>, std::unique_ptr<Buffer>> compile(const
return std::make_tuple(std::move(mesh), std::move(vertexBuffer), std::move(indexBuffer)); return std::make_tuple(std::move(mesh), std::move(vertexBuffer), std::move(indexBuffer));
} }
std::tuple<Mesh, std::unique_ptr<Buffer>, std::unique_ptr<Buffer>> compile(const Trade::MeshData3D& meshData, const BufferUsage usage) { std::tuple<GL::Mesh, std::unique_ptr<GL::Buffer>, std::unique_ptr<GL::Buffer>> compile(const Trade::MeshData3D& meshData, const GL::BufferUsage usage) {
Mesh mesh; GL::Mesh mesh;
mesh.setPrimitive(meshData.primitive()); mesh.setPrimitive(meshData.primitive());
/* Decide about stride and offsets */ /* Decide about stride and offsets */
@ -109,7 +110,7 @@ std::tuple<Mesh, std::unique_ptr<Buffer>, std::unique_ptr<Buffer>> compile(const
stride += sizeof(Shaders::Generic3D::TextureCoordinates::Type); stride += sizeof(Shaders::Generic3D::TextureCoordinates::Type);
/* Create vertex buffer */ /* Create vertex buffer */
std::unique_ptr<Buffer> vertexBuffer{new Buffer{Buffer::TargetHint::Array}}; std::unique_ptr<GL::Buffer> vertexBuffer{new GL::Buffer{GL::Buffer::TargetHint::Array}};
/* Interleave positions */ /* Interleave positions */
Containers::Array<char> data = MeshTools::interleave( Containers::Array<char> data = MeshTools::interleave(
@ -147,14 +148,14 @@ std::tuple<Mesh, std::unique_ptr<Buffer>, std::unique_ptr<Buffer>> compile(const
vertexBuffer->setData(data, usage); vertexBuffer->setData(data, usage);
/* If indexed, fill index buffer and configure indexed mesh */ /* If indexed, fill index buffer and configure indexed mesh */
std::unique_ptr<Buffer> indexBuffer; std::unique_ptr<GL::Buffer> indexBuffer;
if(meshData.isIndexed()) { if(meshData.isIndexed()) {
Containers::Array<char> indexData; Containers::Array<char> indexData;
Mesh::IndexType indexType; MeshIndexType indexType;
UnsignedInt indexStart, indexEnd; UnsignedInt indexStart, indexEnd;
std::tie(indexData, indexType, indexStart, indexEnd) = MeshTools::compressIndices(meshData.indices()); 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); indexBuffer->setData(indexData, usage);
mesh.setCount(meshData.indices().size()) mesh.setCount(meshData.indices().size())
.setIndexBuffer(*indexBuffer, 0, indexType, indexStart, indexEnd); .setIndexBuffer(*indexBuffer, 0, indexType, indexStart, indexEnd);

6
src/Magnum/MeshTools/Compile.h

@ -32,7 +32,7 @@
#include <tuple> #include <tuple>
#include <memory> #include <memory>
#include "Magnum/Magnum.h" #include "Magnum/GL/GL.h"
#include "Magnum/Trade/Trade.h" #include "Magnum/Trade/Trade.h"
#include "Magnum/MeshTools/visibility.h" #include "Magnum/MeshTools/visibility.h"
@ -57,7 +57,7 @@ greater flexibility.
@see @ref shaders-generic @see @ref shaders-generic
*/ */
MAGNUM_MESHTOOLS_EXPORT std::tuple<Mesh, std::unique_ptr<Buffer>, std::unique_ptr<Buffer>> compile(const Trade::MeshData2D& meshData, BufferUsage usage); MAGNUM_MESHTOOLS_EXPORT std::tuple<GL::Mesh, std::unique_ptr<GL::Buffer>, std::unique_ptr<GL::Buffer>> compile(const Trade::MeshData2D& meshData, GL::BufferUsage usage);
/** /**
@brief Compile 3D mesh data @brief Compile 3D mesh data
@ -78,7 +78,7 @@ greater flexibility.
@see @ref shaders-generic @see @ref shaders-generic
*/ */
MAGNUM_MESHTOOLS_EXPORT std::tuple<Mesh, std::unique_ptr<Buffer>, std::unique_ptr<Buffer>> compile(const Trade::MeshData3D& meshData, BufferUsage usage); MAGNUM_MESHTOOLS_EXPORT std::tuple<GL::Mesh, std::unique_ptr<GL::Buffer>, std::unique_ptr<GL::Buffer>> compile(const Trade::MeshData3D& meshData, GL::BufferUsage usage);
}} }}

10
src/Magnum/MeshTools/CompressIndices.cpp

@ -47,24 +47,24 @@ template<class T> inline Containers::Array<char> compress(const std::vector<Unsi
} }
std::tuple<Containers::Array<char>, Mesh::IndexType, UnsignedInt, UnsignedInt> compressIndices(const std::vector<UnsignedInt>& indices) { std::tuple<Containers::Array<char>, MeshIndexType, UnsignedInt, UnsignedInt> compressIndices(const std::vector<UnsignedInt>& indices) {
/** @todo Performance hint when range can be represented by smaller value? */ /** @todo Performance hint when range can be represented by smaller value? */
const auto minmax = std::minmax_element(indices.begin(), indices.end()); const auto minmax = std::minmax_element(indices.begin(), indices.end());
Containers::Array<char> data; Containers::Array<char> data;
Mesh::IndexType type; MeshIndexType type;
switch(Math::log(256, *minmax.second)) { switch(Math::log(256, *minmax.second)) {
case 0: case 0:
data = compress<UnsignedByte>(indices); data = compress<UnsignedByte>(indices);
type = Mesh::IndexType::UnsignedByte; type = MeshIndexType::UnsignedByte;
break; break;
case 1: case 1:
data = compress<UnsignedShort>(indices); data = compress<UnsignedShort>(indices);
type = Mesh::IndexType::UnsignedShort; type = MeshIndexType::UnsignedShort;
break; break;
case 2: case 2:
case 3: case 3:
data = compress<UnsignedInt>(indices); data = compress<UnsignedInt>(indices);
type = Mesh::IndexType::UnsignedInt; type = MeshIndexType::UnsignedInt;
break; break;
default: default:

2
src/Magnum/MeshTools/CompressIndices.h

@ -53,7 +53,7 @@ Example usage:
@see @ref compressIndicesAs() @see @ref compressIndicesAs()
@todo Extract IndexType out of Mesh class @todo Extract IndexType out of Mesh class
*/ */
std::tuple<Containers::Array<char>, Mesh::IndexType, UnsignedInt, UnsignedInt> MAGNUM_MESHTOOLS_EXPORT compressIndices(const std::vector<UnsignedInt>& indices); std::tuple<Containers::Array<char>, MeshIndexType, UnsignedInt, UnsignedInt> MAGNUM_MESHTOOLS_EXPORT compressIndices(const std::vector<UnsignedInt>& indices);
/** /**
@brief Compress vertex indices as given type @brief Compress vertex indices as given type

32
src/Magnum/MeshTools/FullScreenTriangle.cpp

@ -25,44 +25,44 @@
#include "FullScreenTriangle.h" #include "FullScreenTriangle.h"
#include "Magnum/Attribute.h" #include "Magnum/GL/Attribute.h"
#include "Magnum/Buffer.h" #include "Magnum/GL/Buffer.h"
#include "Magnum/Context.h" #include "Magnum/GL/Context.h"
#include "Magnum/Mesh.h" #include "Magnum/GL/Mesh.h"
#include "Magnum/Version.h" #include "Magnum/GL/Version.h"
#include "Magnum/Math/Vector2.h" #include "Magnum/Math/Vector2.h"
namespace Magnum { namespace MeshTools { namespace Magnum { namespace MeshTools {
std::pair<std::unique_ptr<Buffer>, Mesh> fullScreenTriangle(Version version) { std::pair<std::unique_ptr<GL::Buffer>, GL::Mesh> fullScreenTriangle(GL::Version version) {
Mesh mesh; GL::Mesh mesh;
mesh.setPrimitive(MeshPrimitive::Triangles) mesh.setPrimitive(GL::MeshPrimitive::Triangles)
.setCount(3); .setCount(3);
std::unique_ptr<Buffer> buffer; std::unique_ptr<GL::Buffer> buffer;
#ifndef MAGNUM_TARGET_GLES #ifndef MAGNUM_TARGET_GLES
if(version < Version::GL300) if(version < GL::Version::GL300)
#else #else
if(version < Version::GLES300) if(version < GL::Version::GLES300)
#endif #endif
{ {
buffer.reset(new Buffer); buffer.reset(new GL::Buffer);
constexpr Vector2 triangle[] = { constexpr Vector2 triangle[] = {
{-1.0f, 1.0f}, {-1.0f, 1.0f},
{-1.0f, -3.0f}, {-1.0f, -3.0f},
{ 3.0f, 1.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 /** @todo Is it possible to attach moveable buffer here to avoid heap
allocation? OTOH this is more effective in most (modern) cases */ 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)}; return {std::move(buffer), std::move(mesh)};
} }
std::pair<std::unique_ptr<Buffer>, Mesh> fullScreenTriangle() { std::pair<std::unique_ptr<GL::Buffer>, GL::Mesh> fullScreenTriangle() {
return fullScreenTriangle(Context::current().version()); return fullScreenTriangle(GL::Context::current().version());
} }
}} }}

6
src/Magnum/MeshTools/FullScreenTriangle.h

@ -32,7 +32,7 @@
#include <memory> #include <memory>
#include <utility> #include <utility>
#include "Magnum/Magnum.h" #include "Magnum/GL/GL.h"
#include "Magnum/MeshTools/visibility.h" #include "Magnum/MeshTools/visibility.h"
namespace Magnum { namespace MeshTools { namespace Magnum { namespace MeshTools {
@ -80,14 +80,14 @@ void main() {
} }
@endcode @endcode
*/ */
std::pair<std::unique_ptr<Buffer>, Mesh> MAGNUM_MESHTOOLS_EXPORT fullScreenTriangle(Version version); std::pair<std::unique_ptr<GL::Buffer>, GL::Mesh> MAGNUM_MESHTOOLS_EXPORT fullScreenTriangle(GL::Version version);
/** /**
@overload @overload
This function implicitly uses current context version. This function implicitly uses current context version.
*/ */
std::pair<std::unique_ptr<Buffer>, Mesh> MAGNUM_MESHTOOLS_EXPORT fullScreenTriangle(); std::pair<std::unique_ptr<GL::Buffer>, GL::Mesh> MAGNUM_MESHTOOLS_EXPORT fullScreenTriangle();
}} }}

12
src/Magnum/MeshTools/Test/CompressIndicesTest.cpp

@ -53,28 +53,28 @@ CompressIndicesTest::CompressIndicesTest() {
void CompressIndicesTest::compressChar() { void CompressIndicesTest::compressChar() {
Containers::Array<char> data; Containers::Array<char> data;
Mesh::IndexType type; MeshIndexType type;
UnsignedInt start, end; UnsignedInt start, end;
std::tie(data, type, start, end) = MeshTools::compressIndices( std::tie(data, type, start, end) = MeshTools::compressIndices(
std::vector<UnsignedInt>{1, 2, 3, 0, 4}); std::vector<UnsignedInt>{1, 2, 3, 0, 4});
CORRADE_COMPARE(start, 0); CORRADE_COMPARE(start, 0);
CORRADE_COMPARE(end, 4); CORRADE_COMPARE(end, 4);
CORRADE_COMPARE(type, Mesh::IndexType::UnsignedByte); CORRADE_COMPARE(type, MeshIndexType::UnsignedByte);
CORRADE_COMPARE(std::vector<char>(data.begin(), data.end()), CORRADE_COMPARE(std::vector<char>(data.begin(), data.end()),
(std::vector<char>{ 0x01, 0x02, 0x03, 0x00, 0x04 })); (std::vector<char>{ 0x01, 0x02, 0x03, 0x00, 0x04 }));
} }
void CompressIndicesTest::compressShort() { void CompressIndicesTest::compressShort() {
Containers::Array<char> data; Containers::Array<char> data;
Mesh::IndexType type; MeshIndexType type;
UnsignedInt start, end; UnsignedInt start, end;
std::tie(data, type, start, end) = MeshTools::compressIndices( std::tie(data, type, start, end) = MeshTools::compressIndices(
std::vector<UnsignedInt>{1, 256, 0, 5}); std::vector<UnsignedInt>{1, 256, 0, 5});
CORRADE_COMPARE(start, 0); CORRADE_COMPARE(start, 0);
CORRADE_COMPARE(end, 256); CORRADE_COMPARE(end, 256);
CORRADE_COMPARE(type, Mesh::IndexType::UnsignedShort); CORRADE_COMPARE(type, MeshIndexType::UnsignedShort);
if(!Utility::Endianness::isBigEndian()) { if(!Utility::Endianness::isBigEndian()) {
CORRADE_COMPARE(std::vector<char>(data.begin(), data.end()), CORRADE_COMPARE(std::vector<char>(data.begin(), data.end()),
(std::vector<char>{ 0x01, 0x00, (std::vector<char>{ 0x01, 0x00,
@ -92,14 +92,14 @@ void CompressIndicesTest::compressShort() {
void CompressIndicesTest::compressInt() { void CompressIndicesTest::compressInt() {
Containers::Array<char> data; Containers::Array<char> data;
Mesh::IndexType type; MeshIndexType type;
UnsignedInt start, end; UnsignedInt start, end;
std::tie(data, type, start, end) = MeshTools::compressIndices( std::tie(data, type, start, end) = MeshTools::compressIndices(
std::vector<UnsignedInt>{65536, 3, 2}); std::vector<UnsignedInt>{65536, 3, 2});
CORRADE_COMPARE(start, 2); CORRADE_COMPARE(start, 2);
CORRADE_COMPARE(end, 65536); CORRADE_COMPARE(end, 65536);
CORRADE_COMPARE(type, Mesh::IndexType::UnsignedInt); CORRADE_COMPARE(type, MeshIndexType::UnsignedInt);
if(!Utility::Endianness::isBigEndian()) { if(!Utility::Endianness::isBigEndian()) {
CORRADE_COMPARE(std::vector<char>(data.begin(), data.end()), CORRADE_COMPARE(std::vector<char>(data.begin(), data.end()),

Loading…
Cancel
Save