|
|
|
|
@ -25,11 +25,16 @@
|
|
|
|
|
|
|
|
|
|
#include "Compile.h" |
|
|
|
|
|
|
|
|
|
#include <Corrade/Containers/StridedArrayView.h> |
|
|
|
|
#include <Corrade/Containers/ArrayViewStl.h> /** @todo remove once MeshData is sane */ |
|
|
|
|
|
|
|
|
|
#include "Magnum/GL/Buffer.h" |
|
|
|
|
#include "Magnum/GL/Mesh.h" |
|
|
|
|
#include "Magnum/Math/Vector3.h" |
|
|
|
|
#include "Magnum/Math/Color.h" |
|
|
|
|
#include "Magnum/MeshTools/CompressIndices.h" |
|
|
|
|
#include "Magnum/MeshTools/GenerateNormals.h" |
|
|
|
|
#include "Magnum/MeshTools/Duplicate.h" |
|
|
|
|
#include "Magnum/MeshTools/Interleave.h" |
|
|
|
|
#include "Magnum/Trade/MeshData2D.h" |
|
|
|
|
#include "Magnum/Trade/MeshData3D.h" |
|
|
|
|
@ -121,16 +126,18 @@ std::tuple<GL::Mesh, std::unique_ptr<GL::Buffer>, std::unique_ptr<GL::Buffer>> c
|
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
GL::Mesh compile(const Trade::MeshData3D& meshData) { |
|
|
|
|
GL::Mesh compile(const Trade::MeshData3D& meshData, CompileFlags flags) { |
|
|
|
|
GL::Mesh mesh; |
|
|
|
|
mesh.setPrimitive(meshData.primitive()); |
|
|
|
|
|
|
|
|
|
const bool generateNormals = flags & CompileFlag::GenerateFlatNormals && meshData.primitive() == MeshPrimitive::Triangles; |
|
|
|
|
|
|
|
|
|
/* Decide about stride and offsets */ |
|
|
|
|
UnsignedInt stride = sizeof(Shaders::Generic3D::Position::Type); |
|
|
|
|
const UnsignedInt normalOffset = sizeof(Shaders::Generic3D::Position::Type); |
|
|
|
|
UnsignedInt textureCoordsOffset = sizeof(Shaders::Generic3D::Position::Type); |
|
|
|
|
UnsignedInt colorsOffset = sizeof(Shaders::Generic3D::Position::Type); |
|
|
|
|
if(meshData.hasNormals()) { |
|
|
|
|
if(meshData.hasNormals() || generateNormals) { |
|
|
|
|
stride += sizeof(Shaders::Generic3D::Normal::Type); |
|
|
|
|
textureCoordsOffset += sizeof(Shaders::Generic3D::Normal::Type); |
|
|
|
|
colorsOffset += sizeof(Shaders::Generic3D::Normal::Type); |
|
|
|
|
@ -146,20 +153,73 @@ GL::Mesh compile(const Trade::MeshData3D& meshData) {
|
|
|
|
|
GL::Buffer vertexBuffer{GL::Buffer::TargetHint::Array}; |
|
|
|
|
GL::Buffer vertexBufferRef = GL::Buffer::wrap(vertexBuffer.id(), GL::Buffer::TargetHint::Array); |
|
|
|
|
|
|
|
|
|
/* Indirect reference to the mesh data -- either directly the original mesh
|
|
|
|
|
data or processed ones */ |
|
|
|
|
Containers::StridedArrayView1D<const Vector3> positions; |
|
|
|
|
Containers::StridedArrayView1D<const Vector3> normals; |
|
|
|
|
Containers::StridedArrayView1D<const Vector2> textureCoords2D; |
|
|
|
|
Containers::StridedArrayView1D<const Color4> colors; |
|
|
|
|
bool useIndices; /**< @todo turn into a view once compressIndices() takes views */ |
|
|
|
|
|
|
|
|
|
/* If the mesh has no normals, we want to generate them and the mesh is an
|
|
|
|
|
indexed triangle mesh, duplicate all attributes, otherwise just |
|
|
|
|
reference the original data */ |
|
|
|
|
Containers::Array<Vector3> positionStorage; |
|
|
|
|
Containers::Array<Vector3> normalStorage; |
|
|
|
|
Containers::Array<Vector2> textureCoords2DStorage; |
|
|
|
|
Containers::Array<Color4> colorStorage; |
|
|
|
|
if(generateNormals) { |
|
|
|
|
/* If the mesh is indexed, duplicate all attributes */ |
|
|
|
|
if(meshData.isIndexed()) { |
|
|
|
|
positionStorage = duplicate( |
|
|
|
|
Containers::stridedArrayView(meshData.indices()), Containers::stridedArrayView(meshData.positions(0))); |
|
|
|
|
positions = Containers::arrayView(positionStorage); |
|
|
|
|
if(meshData.hasTextureCoords2D()) { |
|
|
|
|
textureCoords2DStorage = duplicate( |
|
|
|
|
Containers::stridedArrayView(meshData.indices()), |
|
|
|
|
Containers::stridedArrayView(meshData.textureCoords2D(0))); |
|
|
|
|
textureCoords2D = Containers::arrayView(textureCoords2DStorage); |
|
|
|
|
} |
|
|
|
|
if(meshData.hasColors()) { |
|
|
|
|
colorStorage = duplicate( |
|
|
|
|
Containers::stridedArrayView(meshData.indices()), |
|
|
|
|
Containers::stridedArrayView(meshData.colors(0))); |
|
|
|
|
colors = Containers::arrayView(colorStorage); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
positions = meshData.positions(0); |
|
|
|
|
if(meshData.hasTextureCoords2D()) |
|
|
|
|
textureCoords2D = meshData.textureCoords2D(0); |
|
|
|
|
if(meshData.hasColors()) |
|
|
|
|
colors = meshData.colors(0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
normalStorage = generateFlatNormals(positions); |
|
|
|
|
normals = Containers::arrayView(normalStorage); |
|
|
|
|
useIndices = false; |
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
positions = meshData.positions(0); |
|
|
|
|
if(meshData.hasNormals()) normals = meshData.normals(0); |
|
|
|
|
if(meshData.hasTextureCoords2D()) textureCoords2D = meshData.textureCoords2D(0); |
|
|
|
|
if(meshData.hasColors()) colors = meshData.colors(0); |
|
|
|
|
useIndices = meshData.isIndexed(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Interleave positions and put them in with ownership transfer, use the
|
|
|
|
|
ref for the rest */ |
|
|
|
|
Containers::Array<char> data = MeshTools::interleave( |
|
|
|
|
meshData.positions(0), |
|
|
|
|
positions, |
|
|
|
|
stride - sizeof(Shaders::Generic3D::Position::Type)); |
|
|
|
|
mesh.addVertexBuffer(std::move(vertexBuffer), 0, |
|
|
|
|
Shaders::Generic3D::Position(), |
|
|
|
|
stride - sizeof(Shaders::Generic3D::Position::Type)); |
|
|
|
|
|
|
|
|
|
/* Add also normals, if present */ |
|
|
|
|
if(meshData.hasNormals()) { |
|
|
|
|
if(normals) { |
|
|
|
|
MeshTools::interleaveInto(data, |
|
|
|
|
normalOffset, |
|
|
|
|
meshData.normals(0), |
|
|
|
|
normals, |
|
|
|
|
stride - normalOffset - sizeof(Shaders::Generic3D::Normal::Type)); |
|
|
|
|
mesh.addVertexBuffer(vertexBufferRef, 0, |
|
|
|
|
normalOffset, |
|
|
|
|
@ -168,10 +228,10 @@ GL::Mesh compile(const Trade::MeshData3D& meshData) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Add also texture coordinates, if present */ |
|
|
|
|
if(meshData.hasTextureCoords2D()) { |
|
|
|
|
if(textureCoords2D) { |
|
|
|
|
MeshTools::interleaveInto(data, |
|
|
|
|
textureCoordsOffset, |
|
|
|
|
meshData.textureCoords2D(0), |
|
|
|
|
textureCoords2D, |
|
|
|
|
stride - textureCoordsOffset - sizeof(Shaders::Generic3D::TextureCoordinates::Type)); |
|
|
|
|
mesh.addVertexBuffer(vertexBufferRef, 0, |
|
|
|
|
textureCoordsOffset, |
|
|
|
|
@ -180,10 +240,10 @@ GL::Mesh compile(const Trade::MeshData3D& meshData) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Add also colors, if present */ |
|
|
|
|
if(meshData.hasColors()) { |
|
|
|
|
if(colors) { |
|
|
|
|
MeshTools::interleaveInto(data, |
|
|
|
|
colorsOffset, |
|
|
|
|
meshData.colors(0), |
|
|
|
|
colors, |
|
|
|
|
stride - colorsOffset - sizeof(Shaders::Generic3D::Color4::Type)); |
|
|
|
|
mesh.addVertexBuffer(vertexBufferRef, 0, |
|
|
|
|
colorsOffset, |
|
|
|
|
@ -194,8 +254,9 @@ GL::Mesh compile(const Trade::MeshData3D& meshData) {
|
|
|
|
|
/* Fill vertex buffer with interleaved data */ |
|
|
|
|
vertexBufferRef.setData(data, GL::BufferUsage::StaticDraw); |
|
|
|
|
|
|
|
|
|
/* If indexed, fill index buffer and configure indexed mesh */ |
|
|
|
|
if(meshData.isIndexed()) { |
|
|
|
|
/* If indexed (and the mesh didn't have the vertex data duplicated for flat
|
|
|
|
|
normals), fill index buffer and configure indexed mesh */ |
|
|
|
|
if(useIndices) { |
|
|
|
|
Containers::Array<char> indexData; |
|
|
|
|
MeshIndexType indexType; |
|
|
|
|
UnsignedInt indexStart, indexEnd; |
|
|
|
|
@ -207,7 +268,7 @@ GL::Mesh compile(const Trade::MeshData3D& meshData) {
|
|
|
|
|
.setIndexBuffer(std::move(indexBuffer), 0, indexType, indexStart, indexEnd); |
|
|
|
|
|
|
|
|
|
/* Else set vertex count */ |
|
|
|
|
} else mesh.setCount(meshData.positions(0).size()); |
|
|
|
|
} else mesh.setCount(positions.size()); |
|
|
|
|
|
|
|
|
|
return mesh; |
|
|
|
|
} |
|
|
|
|
|