spacebox/src/VBO.hpp

75 lines
2.6 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 |/
+--------------+
[VBO.hpp]
This class can be used to allocate space for vertex attributes on the GPU and transfer their
values into the allocated space.
First allocate space by passing a size in bytes to the allocate member function. This size
can be calculated by adding together the sizes of every Attributes object that is intended to
be added to the VBO. After allocating, pass each Attributes object to the add function one at
a time to fill the buffer.
This class doesn't support updating the attribute values once they've been added. To do that,
either reallocate the entire buffer and re-add the attribute values or keep track of the offset
returned by the add function and use glBufferSubData independently.
*/
#pragma once
#include <iostream>
#include <sstream>
#include "GLObject.hpp"
#include "Attributes.hpp"
#include "Log.hpp"
namespace sb
{
class VBO;
std::ostream& operator<<(std::ostream&, const VBO&);
class VBO : public Buffer
{
private:
GLintptr offset = 0;
public:
VBO();
void allocate(GLsizeiptr, GLenum);
/*!
* Set memory in the GPU buffer to the values of the attribute data using glBufferSubData. The memory
* is a contiguous area from the object's current byte offset value to the offset plus the size in
* bytes of the attributes. After the memory is set, the offset is updated to point to the end of the
* area.
*
* The offset is stored in the sb::Attributes object, so the object can associate the vertex data with
* the VAO by passing the offset along when it calls glVertexAttribPointer in its sb::Attributes::bind
* method.
*
* @param attributes vertices to be added to the vertex buffer, wrapped in a sb::Attributes object
* @return the new offset that indicates the end of the vertex buffer
*/
GLintptr add(sb::Attributes&);
friend std::ostream& operator<<(std::ostream&, const VBO&);
};
}