spacebox/src/VBO.cpp

54 lines
1.9 KiB
C++

/* /\ +--------------------------------------------------------------+
____/ \____ /| - zlib/MIT/Unlicenced game framework licensed to freely use, |
\ / / | copy, modify and sell without restriction |
+--\ ^__^ /--+ | |
| ~/ \~ | | - originally created at [http://nugget.fun] |
| ~~~~~~~~~~~~ | +--------------------------------------------------------------+
| SPACE ~~~~~ | /
| ~~~~~~~ BOX |/
+--------------+
*/
#include "VBO.hpp"
using namespace sb;
/* Initialize a Vertex Buffer Object. The base class will be initalized with a target of
* GL_ARRAY_BUFFER. */
VBO::VBO() : Buffer(GL_ARRAY_BUFFER) {}
/* Allocate size bytes of vertex attribute memory on the GPU. Usage should be one of GL_STREAM_DRAW,
* GL_STREAM_READ, GL_STREAM_COPY, GL_STATIC_DRAW, GL_STATIC_READ, GL_STATIC_COPY, GL_DYNAMIC_DRAW,
* GL_DYNAMIC_READ, or GL_DYNAMIC_COPY. The memory will be uninitialized. */
void VBO::allocate(GLsizeiptr size, GLenum usage)
{
glBufferData(target(), size, nullptr, usage);
/* Debug */
std::ostringstream message;
message << "After allocating memory for " << *this;
sb::Log::gl_errors(message.str());
}
GLintptr VBO::add(sb::Attributes& attributes)
{
/* Set memory */
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;
}
/* Overload the stream operator to support the VBO string representation. */
std::ostream& sb::operator<<(std::ostream& out, const VBO& vbo)
{
out << "<Vertex Buffer Object (id: " << vbo.id() << ")>";
return out;
}