Browse Source

Make Mesh usable for subclassing, also document it a bit more.

pull/279/head
Vladimír Vondruš 16 years ago
parent
commit
8bc5fa67da
  1. 22
      src/Mesh.cpp
  2. 52
      src/Mesh.h

22
src/Mesh.cpp

@ -23,13 +23,13 @@ using namespace std;
namespace Magnum { namespace Magnum {
Mesh::~Mesh() { Mesh::~Mesh() {
for(map<Buffer*, pair<bool, vector<Attribute> > >::iterator it = buffers.begin(); it != buffers.end(); ++it) for(map<Buffer*, pair<bool, vector<Attribute> > >::iterator it = _buffers.begin(); it != _buffers.end(); ++it)
delete it->first; delete it->first;
} }
Buffer* Mesh::addBuffer(bool interleaved) { Buffer* Mesh::addBuffer(bool interleaved) {
Buffer* buffer = new Buffer(Buffer::ArrayBuffer); Buffer* buffer = new Buffer(Buffer::ArrayBuffer);
buffers.insert(pair<Buffer*, pair<bool, vector<Attribute> > >( _buffers.insert(pair<Buffer*, pair<bool, vector<Attribute> > >(
buffer, buffer,
pair<bool, vector<Attribute> >(interleaved, vector<Attribute>()) pair<bool, vector<Attribute> >(interleaved, vector<Attribute>())
)); ));
@ -42,11 +42,11 @@ void Mesh::draw() {
finalize(); finalize();
/* Enable vertex arrays for all attributes */ /* Enable vertex arrays for all attributes */
for(set<GLuint>::const_iterator it = attributes.begin(); it != attributes.end(); ++it) for(set<GLuint>::const_iterator it = _attributes.begin(); it != _attributes.end(); ++it)
glEnableVertexAttribArray(*it); glEnableVertexAttribArray(*it);
/* Bind attributes to vertex buffers */ /* Bind attributes to vertex buffers */
for(map<Buffer*, pair<bool, vector<Attribute> > >::iterator it = buffers.begin(); it != buffers.end(); ++it) { for(map<Buffer*, pair<bool, vector<Attribute> > >::const_iterator it = _buffers.begin(); it != _buffers.end(); ++it) {
/* Bind buffer */ /* Bind buffer */
it->first->bind(); it->first->bind();
@ -69,10 +69,10 @@ void Mesh::draw() {
it->first->unbind(); it->first->unbind();
} }
glDrawArrays(primitive, 0, count); glDrawArrays(_primitive, 0, count);
/* Disable vertex arrays for all attributes */ /* Disable vertex arrays for all attributes */
for(set<GLuint>::const_iterator it = attributes.begin(); it != attributes.end(); ++it) for(set<GLuint>::const_iterator it = _attributes.begin(); it != _attributes.end(); ++it)
glDisableVertexAttribArray(*it); glDisableVertexAttribArray(*it);
} }
@ -81,7 +81,7 @@ void Mesh::finalize() {
if(finalized) return; if(finalized) return;
/* Finalize attribute positions for every buffer */ /* Finalize attribute positions for every buffer */
for(map<Buffer*, pair<bool, vector<Attribute> > >::iterator it = buffers.begin(); it != buffers.end(); ++it) { for(map<Buffer*, pair<bool, vector<Attribute> > >::iterator it = _buffers.begin(); it != _buffers.end(); ++it) {
/* Avoid confustion */ /* Avoid confustion */
bool interleaved = it->second.first; bool interleaved = it->second.first;
vector<Attribute>& attributes = it->second.second; vector<Attribute>& attributes = it->second.second;
@ -122,11 +122,11 @@ void Mesh::finalize() {
void Mesh::bindAttribute(Buffer* buffer, GLuint attribute, GLint size, GLenum type) { void Mesh::bindAttribute(Buffer* buffer, GLuint attribute, GLint size, GLenum type) {
/* The mesh is finalized or attribute is already bound, nothing to do */ /* 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 */ /* If buffer is not managed by this mesh, nothing to do */
map<Buffer*, pair<bool, vector<Attribute> > >::iterator found = buffers.find(buffer); map<Buffer*, pair<bool, vector<Attribute> > >::iterator found = _buffers.find(buffer);
if(found == buffers.end()) return; if(found == _buffers.end()) return;
Attribute a; Attribute a;
a.location = attribute; a.location = attribute;
@ -136,7 +136,7 @@ void Mesh::bindAttribute(Buffer* buffer, GLuint attribute, GLint size, GLenum ty
a.pointer = 0; a.pointer = 0;
found->second.second.push_back(a); found->second.second.push_back(a);
attributes.insert(attribute); _attributes.insert(attribute);
} }
GLsizei Mesh::sizeOf(GLenum type) { GLsizei Mesh::sizeOf(GLenum type) {

52
src/Mesh.h

@ -80,17 +80,20 @@ class Mesh {
/** /**
* @brief Constructor * @brief Constructor
* @param _primitive Primitive type * @param primitive Primitive type
* @param _count Vertex count * @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 * @brief Destructor
* *
* Deletes all associated buffers. * Deletes all associated buffers.
*/ */
~Mesh(); virtual ~Mesh();
/** @brief Primitive type */
inline Primitive primitive() const { return _primitive; }
/** /**
* @brief Add buffer * @brief Add buffer
@ -118,23 +121,14 @@ class Mesh {
template<class T> void bindAttribute(Buffer* buffer, GLuint attribute); template<class T> 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 * Binds attributes to buffers and draws the mesh. Expects an active
* shader with all uniforms set. * shader with all uniforms set.
*/ */
void draw(); virtual void draw();
protected: 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 { struct Attribute {
GLuint location; GLuint location;
GLint size; GLint size;
@ -143,13 +137,37 @@ class Mesh {
const GLvoid* pointer; const GLvoid* pointer;
}; };
std::map<Buffer*, std::pair<bool, std::vector<Attribute> > > buffers; /**
std::set<GLuint> 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<Buffer*, std::pair<bool, std::vector<Attribute> > >& buffers() { return _buffers; }
GLenum primitive; /**
* @brief List of all bound attributes
*
* List of all bound attributes bound with bindAttribute().
*/
inline const std::set<GLuint>& 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; GLsizei count;
bool finalized; bool finalized;
std::map<Buffer*, std::pair<bool, std::vector<Attribute> > > _buffers;
std::set<GLuint> _attributes;
void bindAttribute(Buffer* buffer, GLuint attribute, GLint size, GLenum type); void bindAttribute(Buffer* buffer, GLuint attribute, GLint size, GLenum type);
GLsizei sizeOf(GLenum type); GLsizei sizeOf(GLenum type);

Loading…
Cancel
Save