Commit a9cebf32 authored by Janis Born's avatar Janis Born

refactoring of ArrayBuffer and VertexArrayObject

* move mMode fom ArrayBuffer to VAO
* add attachAllAttributes method to VAO
* allow adding VAO attributes without binding them (location == -1)
* store state of previous VAO binding in static field
parent 8b934bde
...@@ -41,7 +41,6 @@ public: ...@@ -41,7 +41,6 @@ public:
public: public:
ArrayBufferControl(void) ArrayBufferControl(void)
: mUsage(GL_STATIC_DRAW), : mUsage(GL_STATIC_DRAW),
mMode(GL_TRIANGLES),
mElements(0), mElements(0),
mpData(NULL), mpData(NULL),
mAttributeDefines() mAttributeDefines()
...@@ -53,7 +52,6 @@ public: ...@@ -53,7 +52,6 @@ public:
// ==================================================================================================== \/ // ==================================================================================================== \/
public: public:
inline ArrayBufferControl& usage (GLenum _usage) { mUsage = _usage; return *this; } inline ArrayBufferControl& usage (GLenum _usage) { mUsage = _usage; return *this; }
inline ArrayBufferControl& mode (GLenum _mode) { mMode = _mode; return *this; }
inline ArrayBufferControl& data (const GLvoid* _pData, GLsizei _elements) inline ArrayBufferControl& data (const GLvoid* _pData, GLsizei _elements)
{ {
...@@ -80,7 +78,6 @@ public: ...@@ -80,7 +78,6 @@ public:
// =================================================================================================== \/ // =================================================================================================== \/
protected: protected:
GLenum mUsage; GLenum mUsage;
GLenum mMode;
GLsizei mElements; GLsizei mElements;
const GLvoid* mpData; const GLvoid* mpData;
AttributeDefineVec mAttributeDefines; AttributeDefineVec mAttributeDefines;
......
...@@ -39,6 +39,8 @@ ...@@ -39,6 +39,8 @@
namespace ACGL{ namespace ACGL{
namespace OpenGL{ namespace OpenGL{
/*
class ArrayBuffer class ArrayBuffer
{ {
ACGL_NOT_COPYABLE(ArrayBuffer) ACGL_NOT_COPYABLE(ArrayBuffer)
...@@ -223,6 +225,7 @@ protected: ...@@ -223,6 +225,7 @@ protected:
ACGL_SHARED_TYPEDEF(ArrayBuffer) ACGL_SHARED_TYPEDEF(ArrayBuffer)
*/
...@@ -230,8 +233,7 @@ ACGL_SHARED_TYPEDEF(ArrayBuffer) ...@@ -230,8 +233,7 @@ ACGL_SHARED_TYPEDEF(ArrayBuffer)
class ArrayBuffer : public Buffer
class ArrayBufferX : Buffer
{ {
// ==================================================================================================== \/ // ==================================================================================================== \/
// ============================================================================================ STRUCTS \/ // ============================================================================================ STRUCTS \/
...@@ -257,13 +259,13 @@ public: ...@@ -257,13 +259,13 @@ public:
// ============================================================================================ CONSTRUCTORS \/ // ============================================================================================ CONSTRUCTORS \/
// ========================================================================================================= \/ // ========================================================================================================= \/
public: public:
ArrayBufferX() ArrayBuffer()
: Buffer(GL_ARRAY_BUFFER), : Buffer(GL_ARRAY_BUFFER),
mStride(0), mStride(0),
mAttributes() mAttributes()
{} {}
ArrayBufferX( SharedBufferObject _pBuffer ) ArrayBuffer( SharedBufferObject _pBuffer )
: Buffer(_pBuffer, GL_ARRAY_BUFFER), : Buffer(_pBuffer, GL_ARRAY_BUFFER),
mStride(0), mStride(0),
mAttributes() mAttributes()
...@@ -357,6 +359,9 @@ public: ...@@ -357,6 +359,9 @@ public:
mAttributes.push_back( _attribute ); mAttributes.push_back( _attribute );
} }
//! Returns the index of a named attribute
int_t getAttributeIndexByName(const std::string& _nameInArray) const;
//! Setting of the stride size explicitly is not needed if the attributes are defined correctly (with padding) //! Setting of the stride size explicitly is not needed if the attributes are defined correctly (with padding)
inline void setStride( GLsizei _stride ) { inline void setStride( GLsizei _stride ) {
mStride = _stride; mStride = _stride;
...@@ -369,6 +374,12 @@ public: ...@@ -369,6 +374,12 @@ public:
mAttributes.clear(); mAttributes.clear();
} }
//! Set data for this buffer for a given number of elements
//! Use only after all attributes have been defined
inline void setDataElements( uint_t _elements, const GLvoid *_pData = NULL, GLenum _usage = GL_STATIC_DRAW ) {
setData( mTarget, _elements * mStride, _pData, _usage );
}
//! Overloaded from the base class to _prevent_ redefining of the binding target! (see Buffer) //! Overloaded from the base class to _prevent_ redefining of the binding target! (see Buffer)
inline void setTarget( GLenum ) { inline void setTarget( GLenum ) {
ACGL::Utils::error() << "DON'T redefine the target binding point of an ArrayBuffer" << std::endl; ACGL::Utils::error() << "DON'T redefine the target binding point of an ArrayBuffer" << std::endl;
...@@ -383,7 +394,7 @@ protected: ...@@ -383,7 +394,7 @@ protected:
AttributeVec mAttributes; AttributeVec mAttributes;
}; };
ACGL_SHARED_TYPEDEF(ArrayBufferX) ACGL_SHARED_TYPEDEF(ArrayBuffer)
......
...@@ -78,9 +78,9 @@ public: ...@@ -78,9 +78,9 @@ public:
inline void setAttributePointer(AttributeVec::size_type _indexInArray, GLuint _indexInShader) const inline void setAttributePointer(AttributeVec::size_type _indexInArray, GLuint _indexInShader) const
{ {
mArrayBuffers[mAttributes[_indexInArray].bufferID]->setAttributePointer( // mArrayBuffers[mAttributes[_indexInArray].bufferID]->setAttributePointer(
mAttributes[_indexInArray].attributeID, // mAttributes[_indexInArray].attributeID,
_indexInShader); // _indexInShader);
} }
void validate (void) const; void validate (void) const;
...@@ -106,12 +106,12 @@ public: ...@@ -106,12 +106,12 @@ public:
const std::string& _attributeName, const std::string& _attributeName,
GLint _indexInShader) GLint _indexInShader)
{ {
Attribute attribute = { // Attribute attribute = {
_name, // _name,
_bufferID, // _bufferID,
mArrayBuffers[_bufferID]->getAttributeIndexByName(_attributeName), // mArrayBuffers[_bufferID]->getAttributeIndexByName(_attributeName),
_indexInShader}; // _indexInShader};
mAttributes.push_back(attribute); // mAttributes.push_back(attribute);
} }
inline void removeAttributes(void) inline void removeAttributes(void)
...@@ -143,7 +143,7 @@ public: ...@@ -143,7 +143,7 @@ public:
{ {
//If no ElementArrayBuffer is specified we use the convention that //If no ElementArrayBuffer is specified we use the convention that
//the first ArrayBuffers determines the mode and the number of elements. //the first ArrayBuffers determines the mode and the number of elements.
mArrayBuffers[0]->draw(); // mArrayBuffers[0]->draw();
} }
void draw(void) const void draw(void) const
......
...@@ -9,12 +9,10 @@ using namespace ACGL::OpenGL; ...@@ -9,12 +9,10 @@ using namespace ACGL::OpenGL;
SharedArrayBuffer ArrayBufferControl::create(void) SharedArrayBuffer ArrayBufferControl::create(void)
{ {
SharedArrayBuffer arrayBuffer(new ArrayBuffer( SharedArrayBuffer arrayBuffer(new ArrayBuffer());
mUsage,
mMode));
for(AttributeDefineVec::size_type i = 0; i < mAttributeDefines.size(); i++) for(AttributeDefineVec::size_type i = 0; i < mAttributeDefines.size(); i++)
{ {
arrayBuffer->attachAttribute( arrayBuffer->defineAttribute(
mAttributeDefines[i].name, mAttributeDefines[i].name,
mAttributeDefines[i].type, mAttributeDefines[i].type,
mAttributeDefines[i].dimension, mAttributeDefines[i].dimension,
...@@ -23,8 +21,7 @@ SharedArrayBuffer ArrayBufferControl::create(void) ...@@ -23,8 +21,7 @@ SharedArrayBuffer ArrayBufferControl::create(void)
if(mpData != NULL) if(mpData != NULL)
{ {
arrayBuffer->bind(); arrayBuffer->bind();
arrayBuffer->setData(mpData, mElements); arrayBuffer->setDataElements(mElements, mpData, mUsage);
//glBindBuffer(GL_ARRAY_BUFFER, 0);
} }
return arrayBuffer; return arrayBuffer;
} }
...@@ -23,7 +23,6 @@ SharedArrayBuffer ArrayBufferControlFileATB::create(void) ...@@ -23,7 +23,6 @@ SharedArrayBuffer ArrayBufferControlFileATB::create(void)
updateFileModificationTime(); updateFileModificationTime();
SharedArrayBuffer arrayBuffer = ArrayBufferControl(). SharedArrayBuffer arrayBuffer = ArrayBufferControl().
mode(GL_TRIANGLES).
usage(GL_STATIC_DRAW). usage(GL_STATIC_DRAW).
create(); create();
...@@ -162,9 +161,10 @@ bool ArrayBufferControlFileATB::loadATB(SharedArrayBuffer& _arrayBuffer) ...@@ -162,9 +161,10 @@ bool ArrayBufferControlFileATB::loadATB(SharedArrayBuffer& _arrayBuffer)
fileStream.close(); fileStream.close();
_arrayBuffer->removeAttributes(); _arrayBuffer->removeAttributes();
_arrayBuffer->attachAttribute("aAttribute", GL_FLOAT, attributeDimension, GL_FALSE); //_arrayBuffer->attachAttribute("aAttribute", GL_FLOAT, attributeDimension, GL_FALSE);
_arrayBuffer->defineAttribute("aAttribute", GL_FLOAT, attributeDimension, GL_FALSE);
_arrayBuffer->bind(); _arrayBuffer->bind();
_arrayBuffer->setData(&dataVector[0], index); _arrayBuffer->setDataElements(index, &dataVector[0]);
return true; return true;
} }
......
...@@ -15,21 +15,3 @@ int_t ArrayBuffer::getAttributeIndexByName(const std::string& _nameInArray) cons ...@@ -15,21 +15,3 @@ int_t ArrayBuffer::getAttributeIndexByName(const std::string& _nameInArray) cons
return i; return i;
return -1; return -1;
} }
void ArrayBuffer::render(void) const
{
glBindBuffer(GL_ARRAY_BUFFER, mObjectName);
for(AttributeVec::size_type i = 0; i < mAttributes.size(); ++i)
{
setAttributePointer(i, i);
glEnableVertexAttribArray(i);
openGLRareError();
}
draw();
for(AttributeVec::size_type i = 0; i < mAttributes.size(); ++i)
{
glDisableVertexAttribArray(i);
openGLRareError();
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
...@@ -66,6 +66,8 @@ void RenderObject::useShaderProgramObject (void) const ...@@ -66,6 +66,8 @@ void RenderObject::useShaderProgramObject (void) const
void RenderObject::enableVertexBufferObject (void) const void RenderObject::enableVertexBufferObject (void) const
{ {
// TODO: deprecate
/*
if(mpVertexBufferObject->getElementArrayBuffer()) if(mpVertexBufferObject->getElementArrayBuffer())
mpVertexBufferObject->getElementArrayBuffer()->bind(); mpVertexBufferObject->getElementArrayBuffer()->bind();
...@@ -85,10 +87,13 @@ void RenderObject::enableVertexBufferObject (void) const ...@@ -85,10 +87,13 @@ void RenderObject::enableVertexBufferObject (void) const
openGLRareError(); openGLRareError();
} }
} }
*/
} }
void RenderObject::disableVertexBufferObject (void) const void RenderObject::disableVertexBufferObject (void) const
{ {
// TODO: deprecate
/*
int currentArrayBufferID = -1; int currentArrayBufferID = -1;
for(AttributeMappingVec::size_type i = 0; i < mAttributeMappings.size(); i++) for(AttributeMappingVec::size_type i = 0; i < mAttributeMappings.size(); i++)
{ {
...@@ -103,6 +108,7 @@ void RenderObject::disableVertexBufferObject (void) const ...@@ -103,6 +108,7 @@ void RenderObject::disableVertexBufferObject (void) const
openGLRareError(); openGLRareError();
} }
} }
*/
} }
#endif #endif
...@@ -9,14 +9,14 @@ ...@@ -9,14 +9,14 @@
using namespace ACGL; using namespace ACGL;
using namespace ACGL::OpenGL; using namespace ACGL::OpenGL;
GLint VertexArrayObject::sPreviousVAOName = 0;
bool VertexArrayObject::isValid(void) const bool VertexArrayObject::isValid(void) const
{ {
GLenum mode;
GLsizei elements = 0; GLsizei elements = 0;
if (mAttributes.size() > 0) if (mAttributes.size() > 0)
{ {
mode = mAttributes[0].arrayBuffer->getMode();
elements = mAttributes[0].arrayBuffer->getElements(); elements = mAttributes[0].arrayBuffer->getElements();
} else { } else {
Utils::error() << "VertexArrayObject has no attributes attached" << std::endl; Utils::error() << "VertexArrayObject has no attributes attached" << std::endl;
...@@ -24,18 +24,13 @@ bool VertexArrayObject::isValid(void) const ...@@ -24,18 +24,13 @@ bool VertexArrayObject::isValid(void) const
} }
if (mpElementArrayBuffer) if (mpElementArrayBuffer)
{ {
mode = mpElementArrayBuffer->getMode();
elements = mpElementArrayBuffer->getElements(); elements = mpElementArrayBuffer->getElements();
} }
for (AttributeVec::size_type i = 0; i < mAttributes.size(); ++i) for (AttributeVec::size_type i = 0; i < mAttributes.size(); ++i)
{ {
if (mAttributes[0].arrayBuffer->getMode() != mode) { if (mAttributes[0].arrayBuffer->getElements() != elements)
Utils::error() << "VertexArrayObject validation failed: Attribute "<< i << " has different mode."<< std::endl; {
return false;
}
if (mAttributes[0].arrayBuffer->getElements() != elements) {
Utils::error() << "VertexArrayObject validation failed: Attribute "<< i << " has different number of elements."<< std::endl; Utils::error() << "VertexArrayObject validation failed: Attribute "<< i << " has different number of elements."<< std::endl;
return false; return false;
} }
......
...@@ -3,6 +3,11 @@ ...@@ -3,6 +3,11 @@
// All rights reserved. // // All rights reserved. //
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// TODO: lots of stuff commented out here
// this code is deprecated and will shortly be removed completely
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#include <ACGL/OpenGL/Objects/VertexBufferObject.hh> #include <ACGL/OpenGL/Objects/VertexBufferObject.hh>
using namespace ACGL; using namespace ACGL;
...@@ -18,65 +23,65 @@ int_t VertexBufferObject::getAttributeIndexByName(const std::string& _name) cons ...@@ -18,65 +23,65 @@ int_t VertexBufferObject::getAttributeIndexByName(const std::string& _name) cons
void VertexBufferObject::validate(void) const void VertexBufferObject::validate(void) const
{ {
GLenum mode = -1; // GLenum mode = -1;
GLsizei elements = -1; // GLsizei elements = -1;
if(mpElementArrayBuffer) // if(mpElementArrayBuffer)
{ // {
mode = mpElementArrayBuffer->getMode(); // mode = mpElementArrayBuffer->getMode();
elements = mpElementArrayBuffer->getElements(); // elements = mpElementArrayBuffer->getElements();
} // }
else if(mArrayBuffers.size() > 0) // else if(mArrayBuffers.size() > 0)
{ // {
mode = mArrayBuffers[0]->getMode(); // mode = mArrayBuffers[0]->getMode();
elements = mArrayBuffers[0]->getElements(); // elements = mArrayBuffers[0]->getElements();
} // }
else // else
{ // {
Utils::error() << "VertexBufferObject validation failed: No ArrayBuffer or ElementArrayBuffer specified."<< std::endl; // Utils::error() << "VertexBufferObject validation failed: No ArrayBuffer or ElementArrayBuffer specified."<< std::endl;
return; // return;
} // }
for(ConstArrayBufferVec::size_type k = 0; k < mArrayBuffers.size(); k++) // for(ConstArrayBufferVec::size_type k = 0; k < mArrayBuffers.size(); k++)
{ // {
if(mArrayBuffers[k]->getMode() != mode) // if(mArrayBuffers[k]->getMode() != mode)
Utils::error() << "VertexBufferObject validation failed: ArrayBuffer "<< k << " has different mode."<< std::endl; // Utils::error() << "VertexBufferObject validation failed: ArrayBuffer "<< k << " has different mode."<< std::endl;
if(mArrayBuffers[k]->getElements() != elements) // if(mArrayBuffers[k]->getElements() != elements)
Utils::error() << "VertexBufferObject validation failed: ArrayBuffer "<< k << " has different number of elements."<< std::endl; // Utils::error() << "VertexBufferObject validation failed: ArrayBuffer "<< k << " has different number of elements."<< std::endl;
} // }
} }
void VertexBufferObject::enable(void) const void VertexBufferObject::enable(void) const
{ {
if(mpElementArrayBuffer) // if(mpElementArrayBuffer)
mpElementArrayBuffer->bind(); // mpElementArrayBuffer->bind();
int_t currentBufferID = -1; // int_t currentBufferID = -1;
for(AttributeVec::size_type i = 0; i < mAttributes.size(); ++i) // for(AttributeVec::size_type i = 0; i < mAttributes.size(); ++i)
{ // {
if(mAttributes[i].bufferID != currentBufferID) // if(mAttributes[i].bufferID != currentBufferID)
{ // {
currentBufferID = mAttributes[i].bufferID; // currentBufferID = mAttributes[i].bufferID;
mArrayBuffers[currentBufferID]->bind(); // mArrayBuffers[currentBufferID]->bind();
} // }
mArrayBuffers[currentBufferID]->setAttributePointer(mAttributes[i].attributeID, mAttributes[i].indexInShader); // mArrayBuffers[currentBufferID]->setAttributePointer(mAttributes[i].attributeID, mAttributes[i].indexInShader);
glEnableVertexAttribArray(mAttributes[i].indexInShader); // glEnableVertexAttribArray(mAttributes[i].indexInShader);
openGLRareError(); // openGLRareError();
} // }
} }
void VertexBufferObject::disable(void) const void VertexBufferObject::disable(void) const
{ {
int_t currentBufferID = -1; // int_t currentBufferID = -1;
for(AttributeVec::size_type i = 0; i < mAttributes.size(); ++i) // for(AttributeVec::size_type i = 0; i < mAttributes.size(); ++i)
{ // {
if(mAttributes[i].bufferID != currentBufferID) // if(mAttributes[i].bufferID != currentBufferID)
{ // {
currentBufferID = mAttributes[i].bufferID; // currentBufferID = mAttributes[i].bufferID;
mArrayBuffers[currentBufferID]->bind(); // mArrayBuffers[currentBufferID]->bind();
} // }
glDisableVertexAttribArray(mAttributes[i].indexInShader); // glDisableVertexAttribArray(mAttributes[i].indexInShader);
openGLRareError(); // openGLRareError();
} // }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment