From ba8f4753bfa1c34bdff4bba1cd50dfd044afa63d Mon Sep 17 00:00:00 2001 From: frank Date: Fri, 14 Jul 2023 00:30:30 -0400 Subject: [PATCH] throw exception if VBO is not bound when allocate is called, reset VBO offset on ID generation --- src/GLObject.cpp | 2 +- src/VBO.cpp | 30 ++++++++++++++++++++++++++++-- src/VBO.hpp | 6 ++++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/GLObject.cpp b/src/GLObject.cpp index 309eb38..c7a3938 100644 --- a/src/GLObject.cpp +++ b/src/GLObject.cpp @@ -65,7 +65,7 @@ GLuint GLObject::id() const } else { - throw std::runtime_error("Cannot get ID for GL object that has no ID set yet"); + throw std::runtime_error("Cannot get ID for GL object that has no ID generated yet"); } } diff --git a/src/VBO.cpp b/src/VBO.cpp index 70aa1ff..b5a4c48 100644 --- a/src/VBO.cpp +++ b/src/VBO.cpp @@ -9,14 +9,40 @@ +-------------*/ #include "VBO.hpp" + using namespace sb; VBO::VBO() : Buffer(GL_ARRAY_BUFFER) {} +void VBO::generate() +{ + Buffer::generate(); + offset = 0; +} + void VBO::allocate(GLsizeiptr size, GLenum usage) { - /* Create data store */ - glBufferData(target(), size, nullptr, usage); + /* Create data store, making sure the buffer currently bound matches this object's ID */ + GLint bound_id; + glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &bound_id); + if (static_cast(bound_id) == id()) + { + glBufferData(target(), size, nullptr, usage); + } + else + { + std::ostringstream message; + message << "Error allocating vertex memory for " << *this << ": "; + if (bound_id == 0) + { + message << "no buffer is currently bound"; + } + else + { + message << "currently bound buffer ID " << bound_id << " does not match " << *this; + } + throw std::runtime_error(message.str()); + } /* Debug */ std::ostringstream message; diff --git a/src/VBO.hpp b/src/VBO.hpp index 86825ae..c68cce9 100644 --- a/src/VBO.hpp +++ b/src/VBO.hpp @@ -54,6 +54,12 @@ namespace sb */ VBO(); + /*! + * Generate an ID for VBO using the base class's generate function. Reset the offset to 0 because any data previously stored will be + * automatically freed once a new ID is generated. + */ + void generate(); + /*! * Allocate size bytes of unintialized vertex buffer memory on the GPU. *