#ifndef Magnum_MeshTools_Interleave_h #define Magnum_MeshTools_Interleave_h /* Copyright © 2010, 2011, 2012 Vladimír Vondruš This file is part of Magnum. Magnum is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 3 only, as published by the Free Software Foundation. Magnum is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License version 3 for more details. */ /** @file * @brief Class Magnum::MeshTools::Interleave */ #include #include #include #include #include "Mesh.h" #include "Buffer.h" namespace Magnum { namespace MeshTools { /** @brief Vertex attribute interleaver implementation See interleave() for full documentation. */ class Interleave { public: /** * @brief Interleaved attribute array * * Size of the data buffer can be computed as follows: * @code * Result result; * size_t size = result.attributeCount*result.stride; * @endcode */ struct Result { size_t attributeCount; /**< @brief Attribute count */ size_t stride; /**< @brief Distance between two attributes in resulting array */ char* data; /**< @brief Data buffer */ }; /** @brief Constructor */ inline Interleave(): result{0, 0, 0} {} /** * @brief Functor * * See interleave(const std::vector&...) for full documentation. */ template Result operator()(const std::vector&... attributes) { /* Compute buffer size and stride */ result.attributeCount = attributeCount(attributes...); if(result.attributeCount) { result.stride = stride(attributes...); /* Create output buffer */ result.data = new char[result.attributeCount*result.stride]; /* Save the data */ write(result.data, attributes...); } return result; } /** * @brief Functor * * See interleave(Mesh*, Buffer*, Buffer::Usage, const std::vector&...) for full documentation. */ template void operator()(Mesh* mesh, Buffer* buffer, Buffer::Usage usage, const std::vector&... attributes) { if(!mesh->isInterleaved(buffer)) { Corrade::Utility::Error() << "MeshTools::Interleave: the buffer is not interleaved, nothing done"; assert(0); return; } operator()(attributes...); mesh->setVertexCount(result.attributeCount); buffer->setData(result.attributeCount*result.stride, result.data, usage); delete[] result.data; } /** @brief Minimal count of passed attributes */ template inline static size_t attributeCount(const std::vector& attribute, const std::vector&... attributes) { size_t count = attributeCount(attributes...); if(sizeof...(attributes) != 0 && count != attribute.size()) { Corrade::Utility::Error() << "MeshTools::Interleave: attribute arrays don't have the same length, nothing done."; assert(0); return 0; } return attribute.size(); } /** @brief Distance between two attributes in resulting array */ template inline static size_t stride(const std::vector& attribute, const std::vector&... attributes) { return sizeof(T) + stride(attributes...); } private: template void write(char* startingOffset, const std::vector& attribute, const std::vector&... attributes) const { /* Copy the data to the buffer */ for(size_t i = 0; i != result.attributeCount; ++i) memcpy(startingOffset+i*result.stride, reinterpret_cast(&attribute[i]), sizeof(T)); write(startingOffset+sizeof(T), attributes...); } /* Terminator functions for recursive calls */ inline static size_t attributeCount() { return 0; } inline static size_t stride() { return 0; } inline void write(char*) const {} Result result; }; /** @brief %Interleave vertex attributes @param attributes Attribute arrays @return Interleaved attribute array. Deleting the buffer is user's responsibility. This function takes two or more attribute arrays and interleaves them, so data for each attribute are in continuous place in memory. @attention Each vector should have the same size, if not, resulting array has zero length. This is convenience function supplementing direct usage of Interleave class, instead of @code MeshTools::Interleave()(attributes...); @endcode you can just write @code MeshTools::interleave(attributes...); @endcode */ template inline Interleave::Result interleave(const std::vector&... attributes) { return Interleave()(attributes...); } /** @brief %Interleave vertex attributes and write them to array buffer @param attributes Attribute arrays @param mesh Output mesh @param buffer Output array buffer @param usage Array buffer usage The same as interleave(const std::vector&...), but this function writes the output to given array buffer and updates vertex count in the mesh accordingly. @attention The buffer must be set as interleaved (see Mesh::addBuffer()), otherwise this function does nothing. Binding the attributes to shader is left to user. This is convenience function supplementing direct usage of Interleave class, instead of @code MeshTools::Interleave()(mesh, buffer, usage, attributes...); @endcode you can just write @code MeshTools::interleave(mesh, buffer, usage, attributes...); @endcode */ template inline void interleave(Mesh* mesh, Buffer* buffer, Buffer::Usage usage, const std::vector&... attributes) { return Interleave()(mesh, buffer, usage, attributes...); } }} #endif