mirror of https://github.com/mosra/magnum.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
213 lines
8.4 KiB
213 lines
8.4 KiB
|
14 years ago
|
#ifndef Magnum_MeshTools_Interleave_h
|
||
|
|
#define Magnum_MeshTools_Interleave_h
|
||
|
|
/*
|
||
|
|
This file is part of Magnum.
|
||
|
|
|
||
|
13 years ago
|
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz>
|
||
|
|
|
||
|
|
Permission is hereby granted, free of charge, to any person obtaining a
|
||
|
|
copy of this software and associated documentation files (the "Software"),
|
||
|
|
to deal in the Software without restriction, including without limitation
|
||
|
|
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||
|
|
and/or sell copies of the Software, and to permit persons to whom the
|
||
|
|
Software is furnished to do so, subject to the following conditions:
|
||
|
|
|
||
|
|
The above copyright notice and this permission notice shall be included
|
||
|
|
in all copies or substantial portions of the Software.
|
||
|
14 years ago
|
|
||
|
13 years ago
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||
|
|
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||
|
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||
|
|
DEALINGS IN THE SOFTWARE.
|
||
|
14 years ago
|
*/
|
||
|
|
|
||
|
|
/** @file
|
||
|
13 years ago
|
* @brief Function @ref Magnum::MeshTools::interleave()
|
||
|
14 years ago
|
*/
|
||
|
|
|
||
|
|
#include <cstring>
|
||
|
|
#include <vector>
|
||
|
|
#include <limits>
|
||
|
14 years ago
|
#include <tuple>
|
||
|
14 years ago
|
|
||
|
|
#include "Mesh.h"
|
||
|
|
#include "Buffer.h"
|
||
|
|
|
||
|
|
namespace Magnum { namespace MeshTools {
|
||
|
|
|
||
|
14 years ago
|
namespace Implementation {
|
||
|
14 years ago
|
|
||
|
|
class Interleave {
|
||
|
|
public:
|
||
|
13 years ago
|
Interleave(): _attributeCount(0), _stride(0) {}
|
||
|
14 years ago
|
|
||
|
13 years ago
|
template<class ...T> std::tuple<std::size_t, std::size_t, Containers::Array<char>> operator()(const T&... attributes) {
|
||
|
14 years ago
|
/* Compute buffer size and stride */
|
||
|
14 years ago
|
_attributeCount = attributeCount(attributes...);
|
||
|
13 years ago
|
Containers::Array<char> data;
|
||
|
14 years ago
|
if(_attributeCount && _attributeCount != ~std::size_t(0)) {
|
||
|
14 years ago
|
_stride = stride(attributes...);
|
||
|
14 years ago
|
|
||
|
|
/* Create output buffer */
|
||
|
13 years ago
|
data = Containers::Array<char>(_attributeCount*_stride);
|
||
|
14 years ago
|
|
||
|
|
/* Save the data */
|
||
|
13 years ago
|
write(data.begin(), attributes...);
|
||
|
14 years ago
|
}
|
||
|
|
|
||
|
13 years ago
|
return std::make_tuple(_attributeCount, _stride, std::move(data));
|
||
|
14 years ago
|
}
|
||
|
|
|
||
|
13 years ago
|
template<class ...T> void operator()(Mesh& mesh, Buffer& buffer, BufferUsage usage, const T&... attributes) {
|
||
|
13 years ago
|
Containers::Array<char> data;
|
||
|
|
std::tie(std::ignore, std::ignore, data) = operator()(attributes...);
|
||
|
14 years ago
|
|
||
|
13 years ago
|
mesh.setVertexCount(_attributeCount);
|
||
|
13 years ago
|
buffer.setData(data, usage);
|
||
|
14 years ago
|
}
|
||
|
|
|
||
|
14 years ago
|
/* Specialization for only one attribute array */
|
||
|
13 years ago
|
template<class T> typename std::enable_if<!std::is_convertible<T, std::size_t>::value, void>::type operator()(Mesh& mesh, Buffer& buffer, BufferUsage usage, const T& attribute) {
|
||
|
13 years ago
|
mesh.setVertexCount(attribute.size());
|
||
|
|
buffer.setData(attribute, usage);
|
||
|
14 years ago
|
}
|
||
|
|
|
||
|
13 years ago
|
template<class T, class ...U> static typename std::enable_if<!std::is_convertible<T, std::size_t>::value, std::size_t>::type attributeCount(const T& first, const U&... next) {
|
||
|
13 years ago
|
CORRADE_ASSERT(sizeof...(next) == 0 || attributeCount(next...) == first.size() || attributeCount(next...) == ~std::size_t(0), "MeshTools::interleave(): attribute arrays don't have the same length, expected" << first.size() << "but got" << attributeCount(next...), 0);
|
||
|
14 years ago
|
|
||
|
14 years ago
|
return first.size();
|
||
|
14 years ago
|
}
|
||
|
|
|
||
|
13 years ago
|
template<class... T> static std::size_t attributeCount(std::size_t, const T&... next) {
|
||
|
14 years ago
|
return attributeCount(next...);
|
||
|
|
}
|
||
|
|
|
||
|
13 years ago
|
template<class ...T> static std::size_t attributeCount(std::size_t) {
|
||
|
14 years ago
|
return ~std::size_t(0);
|
||
|
|
}
|
||
|
|
|
||
|
13 years ago
|
template<class T, class ...U> static typename std::enable_if<!std::is_convertible<T, std::size_t>::value, std::size_t>::type stride(const T&, const U&... next) {
|
||
|
14 years ago
|
return sizeof(typename T::value_type) + stride(next...);
|
||
|
14 years ago
|
}
|
||
|
|
|
||
|
13 years ago
|
template<class... T> static std::size_t stride(std::size_t gap, const T&... next) {
|
||
|
14 years ago
|
return gap + stride(next...);
|
||
|
|
}
|
||
|
|
|
||
|
14 years ago
|
private:
|
||
|
13 years ago
|
template<class T, class ...U> void write(char* startingOffset, const T& first, const U&... next) {
|
||
|
14 years ago
|
write(startingOffset+writeOne(startingOffset, first), next...);
|
||
|
|
}
|
||
|
|
|
||
|
|
/* Copy data to the buffer */
|
||
|
|
template<class T> typename std::enable_if<!std::is_convertible<T, std::size_t>::value, std::size_t>::type writeOne(char* startingOffset, const T& attributeList) {
|
||
|
|
auto it = attributeList.begin();
|
||
|
14 years ago
|
for(std::size_t i = 0; i != _attributeCount; ++i, ++it)
|
||
|
13 years ago
|
std::memcpy(startingOffset+i*_stride, reinterpret_cast<const char*>(&*it), sizeof(typename T::value_type));
|
||
|
14 years ago
|
|
||
|
14 years ago
|
return sizeof(typename T::value_type);
|
||
|
|
}
|
||
|
|
|
||
|
|
/* Fill gap with zeros */
|
||
|
|
std::size_t writeOne(char* startingOffset, std::size_t gap) {
|
||
|
|
for(std::size_t i = 0; i != _attributeCount; ++i)
|
||
|
13 years ago
|
std::memset(startingOffset+i*_stride, 0, gap);
|
||
|
14 years ago
|
|
||
|
|
return gap;
|
||
|
14 years ago
|
}
|
||
|
|
|
||
|
|
/* Terminator functions for recursive calls */
|
||
|
13 years ago
|
static std::size_t attributeCount() { return 0; }
|
||
|
|
static std::size_t stride() { return 0; }
|
||
|
|
void write(char*) {}
|
||
|
14 years ago
|
|
||
|
14 years ago
|
std::size_t _attributeCount;
|
||
|
|
std::size_t _stride;
|
||
|
14 years ago
|
};
|
||
|
|
|
||
|
14 years ago
|
}
|
||
|
|
|
||
|
14 years ago
|
/**
|
||
|
|
@brief %Interleave vertex attributes
|
||
|
14 years ago
|
|
||
|
|
This function takes list of attribute arrays and returns them interleaved, so
|
||
|
|
data for each attribute are in continuous place in memory. Returned tuple
|
||
|
|
contains attribute count, stride and data array. Deleting the data array is up
|
||
|
|
to the user.
|
||
|
|
|
||
|
|
Size of the data buffer can be computed from attribute count and stride, as
|
||
|
|
shown below. Example usage:
|
||
|
14 years ago
|
@code
|
||
|
14 years ago
|
std::vector<Vector4> positions;
|
||
|
14 years ago
|
std::vector<Vector2> textureCoordinates;
|
||
|
14 years ago
|
std::size_t attributeCount;
|
||
|
|
std::size_t stride;
|
||
|
13 years ago
|
Containers::Array<char> data;
|
||
|
14 years ago
|
std::tie(attributeCount, stride, data) = MeshTools::interleave(positions, textureCoordinates);
|
||
|
14 years ago
|
// ...
|
||
|
14 years ago
|
@endcode
|
||
|
14 years ago
|
|
||
|
14 years ago
|
It's often desirable to align data for one vertex on 32bit boundaries. To
|
||
|
|
achieve that, you can specify gaps between the attributes:
|
||
|
|
@code
|
||
|
|
std::vector<Vector4> positions;
|
||
|
|
std::vector<GLushort> weights;
|
||
|
13 years ago
|
std::vector<BasicColor3<GLubyte>> vertexColors;
|
||
|
14 years ago
|
std::size_t attributeCount;
|
||
|
|
std::size_t stride;
|
||
|
13 years ago
|
Containers::Array<char> data;
|
||
|
14 years ago
|
std::tie(attributeCount, stride, data) = MeshTools::interleave(positions, weights, 2, textureCoordinates, 1);
|
||
|
|
@endcode
|
||
|
|
This way vertex stride is 24 bytes, without gaps it would be 21 bytes, causing
|
||
|
|
possible performance loss.
|
||
|
|
|
||
|
|
@attention The function expects that all arrays have the same size.
|
||
|
|
|
||
|
|
@note The only requirements to attribute array type is that it must have
|
||
|
|
typedef `T::value_type`, forward iterator (to be used with range-based
|
||
|
|
for) and function `size()` returning count of elements. In most cases it
|
||
|
|
will be `std::vector` or `std::array`.
|
||
|
14 years ago
|
|
||
|
13 years ago
|
See also @ref interleave(Mesh&, Buffer&, BufferUsage, const T&...),
|
||
|
14 years ago
|
which writes the interleaved array directly into buffer of given mesh.
|
||
|
14 years ago
|
*/
|
||
|
14 years ago
|
/* enable_if to avoid clash with overloaded function below */
|
||
|
13 years ago
|
template<class T, class ...U> inline typename std::enable_if<!std::is_same<T, Mesh>::value, std::tuple<std::size_t, std::size_t, Containers::Array<char>>>::type interleave(const T& first, const U&... next) {
|
||
|
14 years ago
|
return Implementation::Interleave()(first, next...);
|
||
|
14 years ago
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
@brief %Interleave vertex attributes and write them to array buffer
|
||
|
|
@param mesh Output mesh
|
||
|
14 years ago
|
@param buffer Output vertex buffer
|
||
|
|
@param usage Vertex buffer usage
|
||
|
|
@param attributes Attribute arrays and gaps
|
||
|
14 years ago
|
|
||
|
13 years ago
|
The same as @ref interleave(const T&, const U&...), but this function writes the
|
||
|
14 years ago
|
output to given array buffer and updates vertex count in the mesh accordingly,
|
||
|
13 years ago
|
so you don't have to call @ref Mesh::setVertexCount() on your own.
|
||
|
14 years ago
|
|
||
|
13 years ago
|
@attention You still must call @ref Mesh::setPrimitive() and
|
||
|
|
@ref Mesh::addVertexBuffer() on the mesh afterwards.
|
||
|
14 years ago
|
|
||
|
|
For only one attribute array this function is convenient equivalent to the
|
||
|
|
following, without any performance loss:
|
||
|
|
@code
|
||
|
13 years ago
|
buffer.setData(attribute, usage);
|
||
|
|
mesh.setVertexCount(attribute.size());
|
||
|
14 years ago
|
@endcode
|
||
|
14 years ago
|
|
||
|
13 years ago
|
@see @ref MeshTools::compressIndices()
|
||
|
13 years ago
|
@todo rework so Mesh & Buffer doesn't need to be included in header
|
||
|
14 years ago
|
*/
|
||
|
13 years ago
|
template<class ...T> inline void interleave(Mesh& mesh, Buffer& buffer, BufferUsage usage, const T&... attributes) {
|
||
|
14 years ago
|
return Implementation::Interleave()(mesh, buffer, usage, attributes...);
|
||
|
14 years ago
|
}
|
||
|
|
|
||
|
|
}}
|
||
|
|
|
||
|
|
#endif
|