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:
public:
ArrayBufferControl(void)
: mUsage(GL_STATIC_DRAW),
mMode(GL_TRIANGLES),
mElements(0),
mpData(NULL),
mAttributeDefines()
......@@ -53,7 +52,6 @@ public:
// ==================================================================================================== \/
public:
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)
{
......@@ -80,7 +78,6 @@ public:
// =================================================================================================== \/
protected:
GLenum mUsage;
GLenum mMode;
GLsizei mElements;
const GLvoid* mpData;
AttributeDefineVec mAttributeDefines;
......
......@@ -39,6 +39,8 @@
namespace ACGL{
namespace OpenGL{
/*
class ArrayBuffer
{
ACGL_NOT_COPYABLE(ArrayBuffer)
......@@ -223,6 +225,7 @@ protected:
ACGL_SHARED_TYPEDEF(ArrayBuffer)
*/
......@@ -230,8 +233,7 @@ ACGL_SHARED_TYPEDEF(ArrayBuffer)
class ArrayBufferX : Buffer
class ArrayBuffer : public Buffer
{
// ==================================================================================================== \/
// ============================================================================================ STRUCTS \/
......@@ -257,13 +259,13 @@ public:
// ============================================================================================ CONSTRUCTORS \/
// ========================================================================================================= \/
public:
ArrayBufferX()
ArrayBuffer()
: Buffer(GL_ARRAY_BUFFER),
mStride(0),
mAttributes()
{}
ArrayBufferX( SharedBufferObject _pBuffer )
ArrayBuffer( SharedBufferObject _pBuffer )
: Buffer(_pBuffer, GL_ARRAY_BUFFER),
mStride(0),
mAttributes()
......@@ -357,6 +359,9 @@ public:
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)
inline void setStride( GLsizei _stride ) {
mStride = _stride;
......@@ -369,6 +374,12 @@ public:
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)
inline void setTarget( GLenum ) {
ACGL::Utils::error() << "DON'T redefine the target binding point of an ArrayBuffer" << std::endl;
......@@ -383,7 +394,7 @@ protected:
AttributeVec mAttributes;
};
ACGL_SHARED_TYPEDEF(ArrayBufferX)
ACGL_SHARED_TYPEDEF(ArrayBuffer)
......
......@@ -78,9 +78,9 @@ public:
inline void setAttributePointer(AttributeVec::size_type _indexInArray, GLuint _indexInShader) const
{
mArrayBuffers[mAttributes[_indexInArray].bufferID]->setAttributePointer(
mAttributes[_indexInArray].attributeID,
_indexInShader);
// mArrayBuffers[mAttributes[_indexInArray].bufferID]->setAttributePointer(
// mAttributes[_indexInArray].attributeID,
// _indexInShader);
}
void validate (void) const;
......@@ -106,12 +106,12 @@ public:
const std::string& _attributeName,
GLint _indexInShader)
{
Attribute attribute = {
_name,
_bufferID,
mArrayBuffers[_bufferID]->getAttributeIndexByName(_attributeName),
_indexInShader};
mAttributes.push_back(attribute);
// Attribute attribute = {
// _name,
// _bufferID,
// mArrayBuffers[_bufferID]->getAttributeIndexByName(_attributeName),
// _indexInShader};
// mAttributes.push_back(attribute);
}
inline void removeAttributes(void)
......@@ -143,7 +143,7 @@ public:
{
//If no ElementArrayBuffer is specified we use the convention that
//the first ArrayBuffers determines the mode and the number of elements.
mArrayBuffers[0]->draw();
// mArrayBuffers[0]->draw();
}
void draw(void) const
......
......@@ -9,12 +9,10 @@ using namespace ACGL::OpenGL;
SharedArrayBuffer ArrayBufferControl::create(void)
{
SharedArrayBuffer arrayBuffer(new ArrayBuffer(
mUsage,
mMode));
SharedArrayBuffer arrayBuffer(new ArrayBuffer());
for(AttributeDefineVec::size_type i = 0; i < mAttributeDefines.size(); i++)
{
arrayBuffer->attachAttribute(
arrayBuffer->defineAttribute(
mAttributeDefines[i].name,
mAttributeDefines[i].type,
mAttributeDefines[i].dimension,
......@@ -23,8 +21,7 @@ SharedArrayBuffer ArrayBufferControl::create(void)
if(mpData != NULL)
{
arrayBuffer->bind();
arrayBuffer->setData(mpData, mElements);
//glBindBuffer(GL_ARRAY_BUFFER, 0);
arrayBuffer->setDataElements(mElements, mpData, mUsage);
}
return arrayBuffer;
}
......@@ -23,7 +23,6 @@ SharedArrayBuffer ArrayBufferControlFileATB::create(void)
updateFileModificationTime();
SharedArrayBuffer arrayBuffer = ArrayBufferControl().
mode(GL_TRIANGLES).
usage(GL_STATIC_DRAW).
create();
......@@ -162,9 +161,10 @@ bool ArrayBufferControlFileATB::loadATB(SharedArrayBuffer& _arrayBuffer)
fileStream.close();
_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->setData(&dataVector[0], index);
_arrayBuffer->setDataElements(index, &dataVector[0]);
return true;
}
......
......@@ -15,21 +15,3 @@ int_t ArrayBuffer::getAttributeIndexByName(const std::string& _nameInArray) cons
return i;
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
void RenderObject::enableVertexBufferObject (void) const
{
// TODO: deprecate
/*
if(mpVertexBufferObject->getElementArrayBuffer())
mpVertexBufferObject->getElementArrayBuffer()->bind();
......@@ -85,10 +87,13 @@ void RenderObject::enableVertexBufferObject (void) const
openGLRareError();
}
}
*/
}
void RenderObject::disableVertexBufferObject (void) const
{
// TODO: deprecate
/*
int currentArrayBufferID = -1;
for(AttributeMappingVec::size_type i = 0; i < mAttributeMappings.size(); i++)
{
......@@ -103,6 +108,7 @@ void RenderObject::disableVertexBufferObject (void) const
openGLRareError();
}
}
*/
}
#endif
......@@ -9,14 +9,14 @@
using namespace ACGL;
using namespace ACGL::OpenGL;
GLint VertexArrayObject::sPreviousVAOName = 0;
bool VertexArrayObject::isValid(void) const
{
GLenum mode;
GLsizei elements = 0;
if (mAttributes.size() > 0)
{
mode = mAttributes[0].arrayBuffer->getMode();
elements = mAttributes[0].arrayBuffer->getElements();
} else {
Utils::error() << "VertexArrayObject has no attributes attached" << std::endl;
......@@ -24,18 +24,13 @@ bool VertexArrayObject::isValid(void) const
}
if (mpElementArrayBuffer)
{
mode = mpElementArrayBuffer->getMode();
elements = mpElementArrayBuffer->getElements();
}
for (AttributeVec::size_type i = 0; i < mAttributes.size(); ++i)
{
if (mAttributes[0].arrayBuffer->getMode() != mode) {
Utils::error() << "VertexArrayObject validation failed: Attribute "<< i << " has different mode."<< std::endl;
return false;
}
if (mAttributes[0].arrayBuffer->getElements() != elements) {
if (mAttributes[0].arrayBuffer->getElements() != elements)
{
Utils::error() << "VertexArrayObject validation failed: Attribute "<< i << " has different number of elements."<< std::endl;
return false;
}
......
......@@ -3,6 +3,11 @@
// 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>
using namespace ACGL;
......@@ -18,65 +23,65 @@ int_t VertexBufferObject::getAttributeIndexByName(const std::string& _name) cons
void VertexBufferObject::validate(void) const
{
GLenum mode = -1;
GLsizei elements = -1;
// GLenum mode = -1;
// GLsizei elements = -1;
if(mpElementArrayBuffer)
{
mode = mpElementArrayBuffer->getMode();
elements = mpElementArrayBuffer->getElements();
}
else if(mArrayBuffers.size() > 0)
{
mode = mArrayBuffers[0]->getMode();
elements = mArrayBuffers[0]->getElements();
}
else
{
Utils::error() << "VertexBufferObject validation failed: No ArrayBuffer or ElementArrayBuffer specified."<< std::endl;
return;
}
// if(mpElementArrayBuffer)
// {
// mode = mpElementArrayBuffer->getMode();
// elements = mpElementArrayBuffer->getElements();
// }
// else if(mArrayBuffers.size() > 0)
// {
// mode = mArrayBuffers[0]->getMode();
// elements = mArrayBuffers[0]->getElements();
// }
// else
// {
// Utils::error() << "VertexBufferObject validation failed: No ArrayBuffer or ElementArrayBuffer specified."<< std::endl;
// return;
// }
for(ConstArrayBufferVec::size_type k = 0; k < mArrayBuffers.size(); k++)
{
if(mArrayBuffers[k]->getMode() != mode)
Utils::error() << "VertexBufferObject validation failed: ArrayBuffer "<< k << " has different mode."<< std::endl;
// for(ConstArrayBufferVec::size_type k = 0; k < mArrayBuffers.size(); k++)
// {
// if(mArrayBuffers[k]->getMode() != mode)
// Utils::error() << "VertexBufferObject validation failed: ArrayBuffer "<< k << " has different mode."<< std::endl;
if(mArrayBuffers[k]->getElements() != elements)
Utils::error() << "VertexBufferObject validation failed: ArrayBuffer "<< k << " has different number of elements."<< std::endl;
}
// if(mArrayBuffers[k]->getElements() != elements)
// Utils::error() << "VertexBufferObject validation failed: ArrayBuffer "<< k << " has different number of elements."<< std::endl;
// }
}
void VertexBufferObject::enable(void) const
{
if(mpElementArrayBuffer)
mpElementArrayBuffer->bind();
// if(mpElementArrayBuffer)
// mpElementArrayBuffer->bind();
int_t currentBufferID = -1;
for(AttributeVec::size_type i = 0; i < mAttributes.size(); ++i)
{
if(mAttributes[i].bufferID != currentBufferID)
{
currentBufferID = mAttributes[i].bufferID;
mArrayBuffers[currentBufferID]->bind();
}
mArrayBuffers[currentBufferID]->setAttributePointer(mAttributes[i].attributeID, mAttributes[i].indexInShader);
glEnableVertexAttribArray(mAttributes[i].indexInShader);
openGLRareError();
}
// int_t currentBufferID = -1;
// for(AttributeVec::size_type i = 0; i < mAttributes.size(); ++i)
// {
// if(mAttributes[i].bufferID != currentBufferID)
// {
// currentBufferID = mAttributes[i].bufferID;
// mArrayBuffers[currentBufferID]->bind();
// }
// mArrayBuffers[currentBufferID]->setAttributePointer(mAttributes[i].attributeID, mAttributes[i].indexInShader);
// glEnableVertexAttribArray(mAttributes[i].indexInShader);
// openGLRareError();
// }
}
void VertexBufferObject::disable(void) const
{
int_t currentBufferID = -1;
for(AttributeVec::size_type i = 0; i < mAttributes.size(); ++i)
{
if(mAttributes[i].bufferID != currentBufferID)
{
currentBufferID = mAttributes[i].bufferID;
mArrayBuffers[currentBufferID]->bind();
}
glDisableVertexAttribArray(mAttributes[i].indexInShader);
openGLRareError();
}
// int_t currentBufferID = -1;
// for(AttributeVec::size_type i = 0; i < mAttributes.size(); ++i)
// {
// if(mAttributes[i].bufferID != currentBufferID)
// {
// currentBufferID = mAttributes[i].bufferID;
// mArrayBuffers[currentBufferID]->bind();
// }
// glDisableVertexAttribArray(mAttributes[i].indexInShader);
// 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