Browse Source

Using enum instead of bool parameter antipattern.

vectorfields
Vladimír Vondruš 14 years ago
parent
commit
e527cfb226
  1. 21
      src/Mesh.cpp
  2. 25
      src/Mesh.h

21
src/Mesh.cpp

@ -21,18 +21,15 @@ using namespace std;
namespace Magnum {
Mesh::~Mesh() {
for(map<Buffer*, pair<bool, vector<Attribute> > >::iterator it = _buffers.begin(); it != _buffers.end(); ++it)
delete it->first;
for(auto it: _buffers)
delete it.first;
glDeleteVertexArrays(1, &vao);
}
Buffer* Mesh::addBuffer(bool interleaved) {
Buffer* Mesh::addBuffer(BufferType interleaved) {
Buffer* buffer = new Buffer(Buffer::Target::Array);
_buffers.insert(pair<Buffer*, pair<bool, vector<Attribute> > >(
buffer,
pair<bool, vector<Attribute> >(interleaved, vector<Attribute>())
));
_buffers.insert(make_pair(buffer, make_pair(interleaved, vector<Attribute>())));
return buffer;
}
@ -60,10 +57,10 @@ void Mesh::finalize() {
glEnableVertexAttribArray(*it);
/* Finalize attribute positions for every buffer */
for(map<Buffer*, pair<bool, vector<Attribute> > >::iterator it = _buffers.begin(); it != _buffers.end(); ++it) {
for(auto it: _buffers) {
/* Avoid confustion */
bool interleaved = it->second.first;
vector<Attribute>& attributes = it->second.second;
bool interleaved = it.second.first == BufferType::Interleaved;
vector<Attribute>& attributes = it.second.second;
/* Interleaved buffer, set stride and position of first attribute */
if(interleaved) {
@ -95,7 +92,7 @@ void Mesh::finalize() {
}
/* Bind buffer */
it->first->bind();
it.first->bind();
/* Bind all attributes to this buffer */
for(vector<Attribute>::const_iterator ait = attributes.begin(); ait != attributes.end(); ++ait)
@ -113,7 +110,7 @@ void Mesh::bindAttribute(Buffer* buffer, GLuint attribute, GLint size, Type type
if(finalized || _attributes.find(attribute) != _attributes.end()) return;
/* If buffer is not managed by this mesh, nothing to do */
map<Buffer*, pair<bool, vector<Attribute> > >::iterator found = _buffers.find(buffer);
auto found = _buffers.find(buffer);
if(found == _buffers.end()) return;
Attribute a;

25
src/Mesh.h

@ -120,6 +120,20 @@ class MAGNUM_EXPORT Mesh {
TriangleFan = GL_TRIANGLE_FAN
};
/**
* @brief Buffer type
*
* If storing more than one attribute data in the buffer, the data of
* one attribute can be either kept together or interleaved with data
* for another attributes, so data for every vertex will be in one
* continuous place.
* @see addBuffer()
*/
enum class BufferType: bool {
Interleaved, /**< Interleaved buffer */
NonInterleaved /**< Non-interleaved buffer */
};
/**
* @brief Set polygon drawing mode
*
@ -214,17 +228,14 @@ class MAGNUM_EXPORT Mesh {
/**
* @brief Add buffer
* @param interleaved If storing more than one attribute data in the
* buffer, the data of one attribute can be either kept together
* or interleaved with data for another attributes, so data for
* every vertex will be in one continuous place.
* @param interleaved Whether the buffer is interleaved
*
* Adds new buffer to the mesh. The buffer can be then filled with
* Buffer::setData(). See also isInterleaved().
*
* @todo Move interleaveability to Buffer itself?
*/
Buffer* addBuffer(bool interleaved);
Buffer* addBuffer(BufferType interleaved);
/**
* @brief Whether given buffer is interleaved
@ -235,7 +246,7 @@ class MAGNUM_EXPORT Mesh {
*/
inline bool isInterleaved(Buffer* buffer) const {
auto found = _buffers.find(buffer);
return found != _buffers.end() && found->second.first;
return found != _buffers.end() && found->second.first == BufferType::Interleaved;
}
/**
@ -298,7 +309,7 @@ class MAGNUM_EXPORT Mesh {
* - boolean value which signalizes whether the buffer is interleaved
* - list of bound attributes
*/
std::map<Buffer*, std::pair<bool, std::vector<Attribute> > > _buffers;
std::map<Buffer*, std::pair<BufferType, std::vector<Attribute> > > _buffers;
/**
* @brief List of all bound attributes

Loading…
Cancel
Save