spacebox/src/VBO.cpp

74 lines
2.2 KiB
C++

/* +------------------------------------------------------+
____/ \____ /| - Open source game framework licensed to freely use, |
\ / / | copy, modify and sell without restriction |
+--\ ^__^ /--+ | |
| ~/ \~ | | - created for <https://foam.shampoo.ooo> |
| ~~~~~~~~~~~~ | +------------------------------------------------------+
| SPACE ~~~~~ | /
| ~~~~~~~ BOX |/
+-------------*/
#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, making sure the buffer currently bound matches this object's ID */
GLint bound_id;
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &bound_id);
if (static_cast<GLuint>(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;
message << "After allocating memory for " << *this;
sb::Log::gl_errors(message.str());
}
GLintptr VBO::add(sb::Attributes& attributes)
{
/* Set memory and update offset value in attributes object */
glBufferSubData(target(), offset, attributes.size(), attributes);
attributes.offset(offset);
/* Debug */
std::ostringstream initialization_message;
initialization_message << "After setting " << attributes.size() << " bytes in " << *this;
sb::Log::gl_errors(initialization_message.str());
/* Increase offset and return */
offset += attributes.size();
return offset;
}
std::ostream& sb::operator<<(std::ostream& out, const VBO& vbo)
{
out << "<Vertex Buffer Object (id: " << vbo.id() << ")>";
return out;
}