From e43c1fae33eb93bb62d16f7d0175dc84e3ae5522 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 20 Feb 2012 18:03:45 +0100 Subject: [PATCH] Strongly-typed Primitive enum in Mesh. Also updated "default" constructor to include primitive type, as in many cases only the primitive is specified and not the counts and types of data. --- src/IndexedMesh.cpp | 2 +- src/IndexedMesh.h | 8 ++++---- src/Mesh.cpp | 2 +- src/Mesh.h | 11 ++++++----- src/MeshBuilder.h | 4 ++-- src/Primitives/Cube.h | 2 +- src/Primitives/Icosphere.h | 2 +- 7 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/IndexedMesh.cpp b/src/IndexedMesh.cpp index 3c06f94a6..4674427a1 100644 --- a/src/IndexedMesh.cpp +++ b/src/IndexedMesh.cpp @@ -41,7 +41,7 @@ void IndexedMesh::draw() { /* Bind index array, draw the elements and unbind */ _indexBuffer.bind(); - glDrawElements(primitive(), _indexCount, static_cast(_indexType), nullptr); + glDrawElements(static_cast(primitive()), _indexCount, static_cast(_indexType), nullptr); /* Disable vertex arrays for all attributes */ for(set::const_iterator it = attributes().begin(); it != attributes().end(); ++it) diff --git a/src/IndexedMesh.h b/src/IndexedMesh.h index ab1442ac9..9eb984fdc 100644 --- a/src/IndexedMesh.h +++ b/src/IndexedMesh.h @@ -31,13 +31,13 @@ class MAGNUM_EXPORT IndexedMesh: public Mesh { public: /** * @brief Implicit constructor + * @param primitive Primitive type * * Allows creating the object without knowing anything about mesh data. - * Note that you have to call setPrimitive(), setVertexCount(), - * setIndexCount() and setIndexType() manually for mesh to draw - * properly. + * Note that you have to call setVertexCount(), setIndexCount() and + * setIndexType() manually for mesh to draw properly. */ - inline IndexedMesh(): _indexBuffer(Buffer::Target::ElementArray), _indexCount(0), _indexType(Type::UnsignedShort) {} + inline IndexedMesh(Primitive primitive = Primitive::Triangles): Mesh(primitive), _indexBuffer(Buffer::Target::ElementArray), _indexCount(0), _indexType(Type::UnsignedShort) {} /** * @brief Constructor diff --git a/src/Mesh.cpp b/src/Mesh.cpp index 7492b5ba1..432372492 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -57,7 +57,7 @@ void Mesh::draw() { else glVertexAttribPointer(ait->attribute, ait->size, static_cast(ait->type), GL_FALSE, ait->stride, ait->pointer); } - glDrawArrays(_primitive, 0, _vertexCount); + glDrawArrays(static_cast(_primitive), 0, _vertexCount); /* Disable vertex arrays for all attributes */ for(set::const_iterator it = _attributes.begin(); it != _attributes.end(); ++it) diff --git a/src/Mesh.h b/src/Mesh.h index 7f23c0b78..14abf26b9 100644 --- a/src/Mesh.h +++ b/src/Mesh.h @@ -44,7 +44,7 @@ class MAGNUM_EXPORT Mesh { public: /** @brief Primitive type */ - enum Primitive { + enum class Primitive: GLenum { /** * Single points */ @@ -86,12 +86,13 @@ class MAGNUM_EXPORT Mesh { /** * @brief Implicit constructor + * @param primitive Primitive type * - * Allows creating the object without knowing anything about mesh data. - * Note that you have to call setPrimitive() and setVertexCount() - * manually for mesh to draw properly. + * Allows creating the object without knowing anything about mesh + * data. Note that you have to call setVertexCount() manually for mesh + * to draw properly. */ - inline Mesh(): _primitive(Triangles), _vertexCount(0), finalized(false) {} + inline Mesh(Primitive primitive = Primitive::Triangles): _primitive(primitive), _vertexCount(0), finalized(false) {} /** * @brief Constructor diff --git a/src/MeshBuilder.h b/src/MeshBuilder.h index b74919c49..41c526b47 100644 --- a/src/MeshBuilder.h +++ b/src/MeshBuilder.h @@ -136,7 +136,7 @@ template class MeshBuilder { * @note The mesh is @b not cleaned before building. */ void build(IndexedMesh* mesh, Buffer* vertexBuffer, Buffer::Usage vertexBufferUsage, Buffer::Usage indexBufferUsage) { - mesh->setPrimitive(Mesh::Triangles); + mesh->setPrimitive(Mesh::Primitive::Triangles); mesh->setVertexCount(_vertices.size()); vertexBuffer->setData(sizeof(Vertex)*_vertices.size(), _vertices.data(), vertexBufferUsage); SizeBasedCall(_vertices.size())(mesh, _indices, indexBufferUsage); @@ -150,7 +150,7 @@ template class MeshBuilder { * @see build(IndexedMesh*, Buffer*, Buffer::Usage, Buffer::Usage) */ IndexedMesh* build(Buffer::Usage vertexBufferUsage, Buffer::Usage indexBufferUsage) { - IndexedMesh mesh = new IndexedMesh(Mesh::Triangles, 0, 0, Type::UnsignedByte); + IndexedMesh mesh = new IndexedMesh; Buffer* vertexBuffer = mesh.addBuffer(true); build(mesh, vertexBuffer, vertexBufferUsage, indexBufferUsage); diff --git a/src/Primitives/Cube.h b/src/Primitives/Cube.h index 5c9690c40..5dcc99986 100644 --- a/src/Primitives/Cube.h +++ b/src/Primitives/Cube.h @@ -26,7 +26,7 @@ namespace Magnum { namespace Primitives { /** @brief %Cube primitive */ class Cube: public AbstractPrimitive { public: - inline Mesh::Primitive primitive() const { return Mesh::Triangles; } + inline Mesh::Primitive primitive() const { return Mesh::Primitive::Triangles; } inline size_t vertexCount() const { return 8; } inline size_t indexCount() const { return 36; } diff --git a/src/Primitives/Icosphere.h b/src/Primitives/Icosphere.h index c4adcfda7..9d32de9f9 100644 --- a/src/Primitives/Icosphere.h +++ b/src/Primitives/Icosphere.h @@ -48,7 +48,7 @@ template class Icosphere: public AbstractPrimitivevertexCount(); } inline size_t indexCount() const { return builder()->indexCount(); }