Skip to content

Commit

Permalink
Trade: a reworked MeshData class.
Browse files Browse the repository at this point in the history
With API analogous to the (relatively) new AnimationData -- with one
buffer containing all index data and one buffer containing all vertex
data, both meant to be uploaded as-is to the GPU.

This will eventually replace MeshData2D and MeshData3D, backwards
compatibility and wiring up to other APIs will be done in follow-up
commits.
  • Loading branch information
mosra committed Nov 9, 2019
1 parent 79b3dad commit f2a8d48
Show file tree
Hide file tree
Showing 7 changed files with 1,768 additions and 0 deletions.
64 changes: 64 additions & 0 deletions doc/snippets/MagnumTrade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,24 @@

#include "Magnum/FileCallback.h"
#include "Magnum/ImageView.h"
#include "Magnum/Mesh.h"
#include "Magnum/PixelFormat.h"
#include "Magnum/MeshTools/Interleave.h"
#include "Magnum/Animation/Player.h"
#include "Magnum/MeshTools/Transform.h"
#include "Magnum/Trade/AbstractImporter.h"
#include "Magnum/Trade/AnimationData.h"
#include "Magnum/Trade/ImageData.h"
#include "Magnum/Trade/MeshData.h"
#include "Magnum/Trade/MeshData2D.h"
#include "Magnum/Trade/MeshData3D.h"
#include "Magnum/Trade/ObjectData2D.h"
#include "Magnum/Trade/ObjectData3D.h"
#include "Magnum/Trade/PhongMaterialData.h"
#ifdef MAGNUM_TARGET_GL
#include "Magnum/GL/Texture.h"
#include "Magnum/GL/Mesh.h"
#include "Magnum/Shaders/Phong.h"
#endif

using namespace Magnum;
Expand Down Expand Up @@ -201,6 +206,65 @@ else
}
#endif

#ifdef MAGNUM_TARGET_GL
{
Trade::MeshData data{MeshPrimitive::Points, 0};
/* [MeshData-usage] */
/* Check that we have at least positions and normals */
GL::Mesh mesh{data.primitive()};
if(!data.hasAttribute(Trade::MeshAttributeName::Positions) ||
!data.hasAttribute(Trade::MeshAttributeName::Normals))
Fatal{} << "Oh well";

/* Interleave vertex data */
GL::Buffer vertices;
vertices.setData(MeshTools::interleave(data.positions3D(), data.normals()));
mesh.addVertexBuffer(std::move(vertices), 0,
Shaders::Phong::Position{}, Shaders::Phong::Normal{});

/* Set up an index buffer, if the mesh is indexed*/
if(data.isIndexed()) {
GL::Buffer indices;
indices.setData(data.indices());
mesh.setIndexBuffer(std::move(indices), 0, MeshIndexType::UnsignedInt)
.setCount(data.indexCount());
} else mesh.setCount(data.vertexCount());
/* [MeshData-usage] */
}

{
Trade::MeshData data{MeshPrimitive::Points, 0};
GL::Mesh mesh{data.primitive()};
/* [MeshData-usage-advanced] */
/* Upload the original packed vertex data */
GL::Buffer vertices;
vertices.setData(data.vertexData());

/* Set up the position attribute */
Shaders::Phong::Position position;
auto positionType = data.attributeType(Trade::MeshAttributeName::Positions);
if(positionType == MeshAttributeType::Vector2)
position = {Shaders::Phong::Position::Components::Two};
else if(positionType == MeshAttributeType::Vector3)
position = {Shaders::Phong::Position::Components::Three};
else Fatal{} << "Huh?";
mesh.addVertexBuffer(vertices,
data.attributeOffset(Trade::MeshAttributeName::Positions),
data.attributeStride(Trade::MeshAttributeName::Positions), position);

// Set up other attributes ...

/* Upload the original packed index data */
if(data.isIndexed()) {
GL::Buffer indices;
indices.setData(data.indexData());
mesh.setIndexBuffer(std::move(indices), 0, data.indexType())
.setCount(data.indexCount());
} else mesh.setCount(data.vertexCount());
/* [MeshData-usage-advanced] */
}
#endif

{
Trade::MeshData2D& foo();
Trade::MeshData2D& data = foo();
Expand Down
2 changes: 2 additions & 0 deletions src/Magnum/Trade/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ set(MagnumTrade_GracefulAssert_SRCS
AnimationData.cpp
CameraData.cpp
ImageData.cpp
MeshData.cpp
ObjectData2D.cpp
ObjectData3D.cpp
PhongMaterialData.cpp)
Expand All @@ -53,6 +54,7 @@ set(MagnumTrade_HEADERS
CameraData.h
ImageData.h
LightData.h
MeshData.h
MeshData2D.h
MeshData3D.h
MeshObjectData2D.h
Expand Down
Loading

0 comments on commit f2a8d48

Please sign in to comment.