From e6fd70fe0083a9c3d3e0c255b102a59a87ef0df3 Mon Sep 17 00:00:00 2001 From: Squareys Date: Wed, 12 Sep 2018 17:44:45 +0200 Subject: [PATCH] MeshTools: compile mesh colors to Color4 attribute Signed-off-by: Squareys --- src/Magnum/MeshTools/Compile.cpp | 50 +++++++++++++++++++++++++++----- src/Magnum/MeshTools/Compile.h | 10 ++++--- 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/Magnum/MeshTools/Compile.cpp b/src/Magnum/MeshTools/Compile.cpp index 05bc5e7e9..a65cfb354 100644 --- a/src/Magnum/MeshTools/Compile.cpp +++ b/src/Magnum/MeshTools/Compile.cpp @@ -28,6 +28,7 @@ #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/Interleave.h" #include "Magnum/Trade/MeshData2D.h" @@ -45,9 +46,14 @@ GL::Mesh compile(const Trade::MeshData2D& meshData) { /* Decide about stride and offsets */ UnsignedInt stride = sizeof(Shaders::Generic2D::Position::Type); - const UnsignedInt normalOffset = sizeof(Shaders::Generic2D::Position::Type); - if(meshData.hasTextureCoords2D()) + UnsignedInt textureCoordsOffset = sizeof(Shaders::Generic2D::Position::Type); + UnsignedInt colorsOffset = sizeof(Shaders::Generic2D::Position::Type); + if(meshData.hasTextureCoords2D()) { stride += sizeof(Shaders::Generic2D::TextureCoordinates::Type); + colorsOffset += sizeof(Shaders::Generic2D::TextureCoordinates::Type); + } + if(meshData.hasColors()) + stride += sizeof(Shaders::Generic2D::Color4::Type); /* Create vertex buffer */ GL::Buffer vertexBuffer{GL::Buffer::TargetHint::Array}; @@ -65,13 +71,25 @@ GL::Mesh compile(const Trade::MeshData2D& meshData) { /* Add also texture coordinates, if present */ if(meshData.hasTextureCoords2D()) { MeshTools::interleaveInto(data, - normalOffset, + textureCoordsOffset, meshData.textureCoords2D(0), - stride - normalOffset - sizeof(Shaders::Generic2D::TextureCoordinates::Type)); + stride - textureCoordsOffset - sizeof(Shaders::Generic2D::TextureCoordinates::Type)); mesh.addVertexBuffer(vertexBufferRef, 0, - normalOffset, + textureCoordsOffset, Shaders::Generic2D::TextureCoordinates(), - stride - normalOffset - sizeof(Shaders::Generic2D::TextureCoordinates::Type)); + stride - textureCoordsOffset - sizeof(Shaders::Generic2D::TextureCoordinates::Type)); + } + + /* Add also colors, if present */ + if(meshData.hasColors()) { + MeshTools::interleaveInto(data, + colorsOffset, + meshData.colors(0), + stride - colorsOffset - sizeof(Shaders::Generic3D::Color4::Type)); + mesh.addVertexBuffer(vertexBufferRef, 0, + colorsOffset, + Shaders::Generic3D::Color4(), + stride - colorsOffset - sizeof(Shaders::Generic3D::Color4::Type)); } /* Fill vertex buffer with interleaved data */ @@ -111,12 +129,18 @@ GL::Mesh compile(const Trade::MeshData3D& meshData) { 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()) { stride += sizeof(Shaders::Generic3D::Normal::Type); textureCoordsOffset += sizeof(Shaders::Generic3D::Normal::Type); + colorsOffset += sizeof(Shaders::Generic3D::Normal::Type); } - if(meshData.hasTextureCoords2D()) + if(meshData.hasTextureCoords2D()) { stride += sizeof(Shaders::Generic3D::TextureCoordinates::Type); + colorsOffset += sizeof(Shaders::Generic3D::Normal::Type); + } + if(meshData.hasColors()) + stride += sizeof(Shaders::Generic3D::Color4::Type); /* Create vertex buffer */ GL::Buffer vertexBuffer{GL::Buffer::TargetHint::Array}; @@ -155,6 +179,18 @@ GL::Mesh compile(const Trade::MeshData3D& meshData) { stride - textureCoordsOffset - sizeof(Shaders::Generic3D::TextureCoordinates::Type)); } + /* Add also colors, if present */ + if(meshData.hasColors()) { + MeshTools::interleaveInto(data, + colorsOffset, + meshData.colors(0), + stride - colorsOffset - sizeof(Shaders::Generic3D::Color4::Type)); + mesh.addVertexBuffer(vertexBufferRef, 0, + colorsOffset, + Shaders::Generic3D::Color4(), + stride - colorsOffset - sizeof(Shaders::Generic3D::Color4::Type)); + } + /* Fill vertex buffer with interleaved data */ vertexBufferRef.setData(data, GL::BufferUsage::StaticDraw); diff --git a/src/Magnum/MeshTools/Compile.h b/src/Magnum/MeshTools/Compile.h index 4e338a1e6..ec9121e0e 100644 --- a/src/Magnum/MeshTools/Compile.h +++ b/src/Magnum/MeshTools/Compile.h @@ -51,6 +51,7 @@ Configures a mesh for @ref Shaders::Generic2D shader with vertex buffer and possibly also an index buffer, if the mesh is indexed. Positions are bound to @ref Shaders::Generic2D::Position attribute. If the mesh contains texture coordinates, these are bound to @ref Shaders::Generic2D::TextureCoordinates +attribute. If the mesh contains colors, these are bound to @ref Shaders::Generic3D::Color4 attribute. No data compression or index optimization (except for index buffer packing) is done, both the vertex buffer and the index buffer (if any) is owned by the mesh, both created with @ref GL::BufferUsage::StaticDraw. @@ -84,10 +85,11 @@ Configures mesh for @ref Shaders::Generic3D shader with vertex buffer and possibly also index buffer, if the mesh is indexed. Positions are bound to @ref Shaders::Generic3D::Position attribute. If the mesh contains normals, they are bound to @ref Shaders::Generic3D::Normal attribute, texture coordinates are -bound to @ref Shaders::Generic2D::TextureCoordinates attribute. No data -compression or index optimization (except for index buffer packing) is done, -both the vertex buffer and the index buffer (if any) is owned by the mesh, both -created with @ref GL::BufferUsage::StaticDraw. +bound to @ref Shaders::Generic3D::TextureCoordinates attribute. If the mesh +contains colors, they are bound to @ref Shaders::Generic3D::Color4 attribute. +No data compression or index optimization (except for index buffer packing) is +done, both the vertex buffer and the index buffer (if any) is owned by the mesh, +both created with @ref GL::BufferUsage::StaticDraw. This is just a convenience function for creating generic meshes, you might want to use @ref interleave() and @ref compressIndices() functions together with