My apologies, I find pasting code here very painful, I guess I tried cutting corners in my explanation but my code
looks more like this:
void OpenGL::SetVertexBuffer(VertexBuffer *in_buffer)
{
UInt &l_id = in_buffer->GetArrayId();
if (l_id == 0)
{
glGenBuffers(1, &l_id);
}
glBindBuffer(GL_ARRAY_BUFFER, l_id);
if (in_buffer->IsUpdated())
{
glBufferData(GL_ARRAY_BUFFER, in_buffer->GetArraySize(), NULL, gl_BufferTypes[in_buffer->GetType()]);
}
}
template <class T> void OpenGL::SetVertexData(ArrayBuffer<T> &in_array)
{
if (in_array.IsValid())
{
glBufferSubData(GL_ARRAY_BUFFER, in_array.GetOffset(), in_array.GetSize(), in_array.GetData());
}
}
template <class T> void OpenGL::SetVertexAttribute(ArrayBuffer<T> &in_array)
{
if (in_array.IsValid())
{
e_VertexAttributes &l_index = in_array.GetIndex();
glVertexAttribPointer(l_index, gl_AttributeSizes[l_index], gl_AttributeTypes[l_index], gl_AttributeNormalized[l_index], gl_AttributeStrides[l_index], (void*) in_array.GetOffset());
glEnableVertexAttribArray(l_index);
}
else
glDisableVertexAttribArray(in_array.GetIndex());
}
template <class T> void OpenGL::Draw(VertexBuffer *in_buffer, ArrayBuffer<T> &in_indices, UInt in_offset, const e_RenderTypes &in_type)
{
UInt &l_id = in_buffer->GetElementId();
if (l_id == 0)
{
glGenBuffers(1, &l_id);
}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, l_id);
if (in_buffer->IsUpdated())
{
in_buffer->SetUpdated(false);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, in_indices.GetSize(), in_indices.GetData(), gl_BufferTypes[in_buffer->GetType()]); /// Only if reseted.
}
glDrawRangeElements(gl_RenderTypes[in_type], in_offset, in_offset + in_indices.GetSize(), in_indices.GetSize(), GL_UNSIGNED_INT, (void*) in_indices.GetOffset());
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
If my understanding is correct, unbinding any subData attached to the buffers was my missing link before deletion at run time.
void OpenGL::DeleteVertexBuffer(UInt &in_id)
{
if (in_id != 0)
{
glBindBuffer(GL_ARRAY_BUFFER, in_id);
for (UInt i = 0; i < 16; ++i)
{
glDisableVertexAttribArray(i);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDeleteBuffers(1, &in_id);
in_id = 0;
}
}