diff --git a/src/MeshBuilder.h b/src/MeshBuilder.h index b5caca1ac..8a1ab7d9e 100644 --- a/src/MeshBuilder.h +++ b/src/MeshBuilder.h @@ -94,7 +94,7 @@ template class MeshBuilder { /** * @brief Set mesh data - * @param vertices Vertices + * @param vertexData Vertex data * @param indices Vertex indices * @param vertexCount Vertex count * @param indexCount Index count @@ -102,18 +102,20 @@ template class MeshBuilder { * Replaces mesh builder data with given data. Type of indices is * detected from given pointer. */ - inline void setData(const Vertex* vertices, const GLubyte* indices, GLsizei vertexCount, GLsizei indexCount) { - setData(vertices, indices, vertexCount, indexCount, GL_UNSIGNED_BYTE); - } + template void setData(const Vertex* vertexData, const IndexType* indices, GLsizei vertexCount, GLsizei indexCount) { + clear(); - /** @copydoc setData() */ - inline void setData(const Vertex* vertices, const GLushort* indices, GLsizei vertexCount, GLsizei indexCount) { - setData(vertices, indices, vertexCount, indexCount, GL_UNSIGNED_SHORT); - } + /* Map vertex indices to vertex pointers */ + std::vector vertices; + vertices.reserve(vertexCount); + /* Using TypeTraits::IndexType to ensure we have allowed type for indexing */ + for(typename TypeTraits::IndexType i = 0; i != vertexCount; ++i) + addVertex(Vertex(vertexData[i])); - /** @copydoc setData() */ - inline void setData(const Vertex* vertices, const GLuint* indices, GLsizei vertexCount, GLsizei indexCount) { - setData(vertices, indices, vertexCount, indexCount, GL_UNSIGNED_INT); + /* Faces array */ + _faces.reserve(indexCount/3); + for(size_t i = 0; i < indexCount; i += 3) + addFace(indices[i], indices[i+1], indices[i+2]); } /** @brief Add vertex */ @@ -266,11 +268,11 @@ template class MeshBuilder { */ void build(IndexedMesh* mesh, Buffer* vertexBuffer, Buffer::Usage vertexBufferUsage, Buffer::Usage indexBufferUsage) { if(_faces.size()*3 <= 0xFF) - buildInternal(mesh, vertexBuffer, vertexBufferUsage, indexBufferUsage, GL_UNSIGNED_BYTE); + buildInternal(mesh, vertexBuffer, vertexBufferUsage, indexBufferUsage); else if(_faces.size()*3 <= 0xFFFF) - buildInternal(mesh, vertexBuffer, vertexBufferUsage, indexBufferUsage, GL_UNSIGNED_SHORT); + buildInternal(mesh, vertexBuffer, vertexBufferUsage, indexBufferUsage); else - buildInternal(mesh, vertexBuffer, vertexBufferUsage, indexBufferUsage, GL_UNSIGNED_INT); + buildInternal(mesh, vertexBuffer, vertexBufferUsage, indexBufferUsage); } /** @@ -310,24 +312,10 @@ template class MeshBuilder { HashedVertex(VertexPointer oldVertexPointer, VertexPointer newVertexPointer): oldVertexPointer(oldVertexPointer), newVertexPointer(newVertexPointer) {} }; - template void setData(const Vertex* vertexData, const IndexType* indices, GLsizei vertexCount, GLsizei indexCount, GLenum indexType) { - clear(); - - /* Map vertex indices to vertex pointers */ - std::vector vertices; - vertices.reserve(vertexCount); - for(IndexType i = 0; i != vertexCount; ++i) - addVertex(Vertex(vertexData[i])); - - /* Faces array */ - _faces.reserve(indexCount/3); - for(IndexType i = 0; i < indexCount; i += 3) - addFace(indices[i], indices[i+1], indices[i+2]); - } - - template void buildInternal(IndexedMesh* mesh, Buffer* vertexBuffer, Buffer::Usage vertexBufferUsage, Buffer::Usage indexBufferUsage, GLenum indexType) { - /* Compress face array to index array */ - std::vector indices; + template void buildInternal(IndexedMesh* mesh, Buffer* vertexBuffer, Buffer::Usage vertexBufferUsage, Buffer::Usage indexBufferUsage) { + /* Compress face array to index array. Using TypeTraits::IndexType + to ensure we have allowed type for indexing */ + std::vector::IndexType> indices; indices.reserve(_faces.size()*3); for(auto it = _faces.cbegin(); it != _faces.cend(); ++it) { indices.push_back(it->vertices[0]); @@ -339,7 +327,7 @@ template class MeshBuilder { mesh->setPrimitive(Mesh::Triangles); mesh->setVertexCount(_vertices.size()); mesh->setIndexCount(indices.size()); - mesh->setIndexType(indexType); + mesh->setIndexType(TypeTraits::glType()); vertexBuffer->setData(sizeof(Vertex)*_vertices.size(), _vertices.data(), vertexBufferUsage); mesh->indexBuffer()->setData(sizeof(IndexType)*indices.size(), indices.data(), indexBufferUsage);