diff --git a/src/Mesh.cpp b/src/Mesh.cpp index 6fd8148d7..de77e9175 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -23,13 +23,13 @@ using namespace std; namespace Magnum { Mesh::~Mesh() { - for(map > >::iterator it = buffers.begin(); it != buffers.end(); ++it) + for(map > >::iterator it = _buffers.begin(); it != _buffers.end(); ++it) delete it->first; } Buffer* Mesh::addBuffer(bool interleaved) { Buffer* buffer = new Buffer(Buffer::ArrayBuffer); - buffers.insert(pair > >( + _buffers.insert(pair > >( buffer, pair >(interleaved, vector()) )); @@ -42,11 +42,11 @@ void Mesh::draw() { finalize(); /* Enable vertex arrays for all attributes */ - for(set::const_iterator it = attributes.begin(); it != attributes.end(); ++it) + for(set::const_iterator it = _attributes.begin(); it != _attributes.end(); ++it) glEnableVertexAttribArray(*it); /* Bind attributes to vertex buffers */ - for(map > >::iterator it = buffers.begin(); it != buffers.end(); ++it) { + for(map > >::const_iterator it = _buffers.begin(); it != _buffers.end(); ++it) { /* Bind buffer */ it->first->bind(); @@ -69,10 +69,10 @@ void Mesh::draw() { it->first->unbind(); } - glDrawArrays(primitive, 0, count); + glDrawArrays(_primitive, 0, count); /* Disable vertex arrays for all attributes */ - for(set::const_iterator it = attributes.begin(); it != attributes.end(); ++it) + for(set::const_iterator it = _attributes.begin(); it != _attributes.end(); ++it) glDisableVertexAttribArray(*it); } @@ -81,7 +81,7 @@ void Mesh::finalize() { if(finalized) return; /* Finalize attribute positions for every buffer */ - for(map > >::iterator it = buffers.begin(); it != buffers.end(); ++it) { + for(map > >::iterator it = _buffers.begin(); it != _buffers.end(); ++it) { /* Avoid confustion */ bool interleaved = it->second.first; vector& attributes = it->second.second; @@ -122,11 +122,11 @@ void Mesh::finalize() { void Mesh::bindAttribute(Buffer* buffer, GLuint attribute, GLint size, GLenum type) { /* The mesh is finalized or attribute is already bound, nothing to do */ - if(finalized || attributes.find(attribute) != attributes.end()) return; + if(finalized || _attributes.find(attribute) != _attributes.end()) return; /* If buffer is not managed by this mesh, nothing to do */ - map > >::iterator found = buffers.find(buffer); - if(found == buffers.end()) return; + map > >::iterator found = _buffers.find(buffer); + if(found == _buffers.end()) return; Attribute a; a.location = attribute; @@ -136,7 +136,7 @@ void Mesh::bindAttribute(Buffer* buffer, GLuint attribute, GLint size, GLenum ty a.pointer = 0; found->second.second.push_back(a); - attributes.insert(attribute); + _attributes.insert(attribute); } GLsizei Mesh::sizeOf(GLenum type) { diff --git a/src/Mesh.h b/src/Mesh.h index 0d60012ae..699a66b40 100644 --- a/src/Mesh.h +++ b/src/Mesh.h @@ -80,17 +80,20 @@ class Mesh { /** * @brief Constructor - * @param _primitive Primitive type + * @param primitive Primitive type * @param _count Vertex count */ - inline Mesh(Primitive _primitive, GLsizei _count): primitive(_primitive), count(_count), finalized(false) {} + inline Mesh(Primitive primitive, GLsizei _count): _primitive(primitive), count(_count), finalized(false) {} /** * @brief Destructor * * Deletes all associated buffers. */ - ~Mesh(); + virtual ~Mesh(); + + /** @brief Primitive type */ + inline Primitive primitive() const { return _primitive; } /** * @brief Add buffer @@ -118,23 +121,14 @@ class Mesh { template void bindAttribute(Buffer* buffer, GLuint attribute); /** - * @brief Draw a mesh + * @brief Draw the mesh * * Binds attributes to buffers and draws the mesh. Expects an active * shader with all uniforms set. */ - void draw(); + virtual void draw(); protected: - /** - * @brief Finalize the mesh - * - * Computes location and stride of each attribute in its buffer. After - * this function is called, no new attribute can be bound. - */ - void finalize(); - - private: struct Attribute { GLuint location; GLint size; @@ -143,13 +137,37 @@ class Mesh { const GLvoid* pointer; }; - std::map > > buffers; - std::set attributes; + /** + * @brief Buffers with their attributes + * @return Map of associated buffers, evey buffer has: + * - boolean value which signalizes whether the buffer is interleaved + * - list of bound attributes + */ + inline const std::map > >& buffers() { return _buffers; } - GLenum primitive; + /** + * @brief List of all bound attributes + * + * List of all bound attributes bound with bindAttribute(). + */ + inline const std::set& attributes() { return _attributes; } + + /** + * @brief Finalize the mesh + * + * Computes location and stride of each attribute in its buffer. After + * this function is called, no new attribute can be bound. + */ + void finalize(); + + private: + Primitive _primitive; GLsizei count; bool finalized; + std::map > > _buffers; + std::set _attributes; + void bindAttribute(Buffer* buffer, GLuint attribute, GLint size, GLenum type); GLsizei sizeOf(GLenum type);