std::tuple<Mesh, std::unique_ptr<Buffer>, std::unique_ptr<Buffer>> compile(const Trade::MeshData2D& meshData, const BufferUsage usage) { Mesh mesh; mesh.setPrimitive(meshData.primitive()); /* Decide about stride and offsets */ UnsignedInt stride = sizeof(Shaders::Generic2D::Position::Type); const UnsignedInt normalOffset = sizeof(Shaders::Generic2D::Position::Type); if(meshData.hasTextureCoords2D()) stride += sizeof(Shaders::Generic2D::TextureCoordinates::Type); /* Create vertex buffer */ std::unique_ptr<Buffer> vertexBuffer{new Buffer{Buffer::TargetHint::Array}}; /* Interleave positions */ Containers::Array<char> data = MeshTools::interleave( meshData.positions(0), stride - sizeof(Shaders::Generic2D::Position::Type)); mesh.addVertexBuffer(*vertexBuffer, 0, Shaders::Generic2D::Position(), stride - sizeof(Shaders::Generic2D::Position::Type)); /* Add also texture coordinates, if present */ if(meshData.hasTextureCoords2D()) { MeshTools::interleaveInto(data, normalOffset, meshData.textureCoords2D(0), stride - normalOffset - sizeof(Shaders::Generic2D::TextureCoordinates::Type)); mesh.addVertexBuffer(*vertexBuffer, 0, normalOffset, Shaders::Generic2D::TextureCoordinates(), stride - normalOffset - sizeof(Shaders::Generic2D::TextureCoordinates::Type)); } /* Fill vertex buffer with interleaved data */ vertexBuffer->setData(data, usage); /* If indexed, fill index buffer and configure indexed mesh */ std::unique_ptr<Buffer> indexBuffer; if(meshData.isIndexed()) { Containers::Array<char> indexData; Mesh::IndexType indexType; UnsignedInt indexStart, indexEnd; std::tie(indexData, indexType, indexStart, indexEnd) = MeshTools::compressIndices(meshData.indices()); indexBuffer.reset(new Buffer{Buffer::TargetHint::ElementArray}); indexBuffer->setData(indexData, usage); mesh.setCount(meshData.indices().size()) .setIndexBuffer(*indexBuffer, 0, indexType, indexStart, indexEnd); /* Else set vertex count */ } else mesh.setCount(meshData.positions(0).size()); return std::make_tuple(std::move(mesh), std::move(vertexBuffer), std::move(indexBuffer)); }
Billboard::Billboard(const Trade::ImageData2D& image, Buffer* colorCorrectionBuffer, Object2D* parent, SceneGraph::DrawableGroup2D* group): Object2D(parent), SceneGraph::Drawable2D(*this, group) { Trade::MeshData2D square = Primitives::Square::solid(); buffer.setData(square.positions(0), BufferUsage::StaticDraw); mesh.setPrimitive(square.primitive()) .setVertexCount(square.positions(0).size()) .addVertexBuffer(buffer, 0, ColorCorrectionShader::Position()); texture.setWrapping(Sampler::Wrapping::ClampToBorder) .setMagnificationFilter(Sampler::Filter::Linear) .setMinificationFilter(Sampler::Filter::Linear) .setImage(0, TextureFormat::RGBA8, image); colorCorrectionTexture.setBuffer(BufferTextureFormat::R32F, *colorCorrectionBuffer); scale(Vector2::yScale(Float(image.size()[1])/image.size()[0])); }