basic vbo

main
Brett 2023-12-27 22:04:33 -05:00
parent 76bd471b9b
commit 2e1eb6707e
2 changed files with 117 additions and 3 deletions

View File

@ -22,6 +22,7 @@
#include <blt/math/vectors.h> #include <blt/math/vectors.h>
#include <vector> #include <vector>
#include <blt/gfx/gl_includes.h> #include <blt/gfx/gl_includes.h>
#include <variant>
namespace blt::gfx namespace blt::gfx
{ {
@ -34,6 +35,57 @@ namespace blt::gfx
std::vector<blt::vec3f> normals; std::vector<blt::vec3f> normals;
}; };
class static_dynamic_array
{
private:
static constexpr size_t DATA_SIZE = 8;
std::variant<GLuint[DATA_SIZE], GLuint*> data_;
size_t size_ = DATA_SIZE;
public:
static_dynamic_array() = default;
static_dynamic_array(const static_dynamic_array& copy) = delete;
static_dynamic_array(static_dynamic_array&& move) noexcept;
static_dynamic_array& operator=(const static_dynamic_array& copy) = delete;
static_dynamic_array& operator=(static_dynamic_array&& move) = delete;
~static_dynamic_array()
{
if (std::holds_alternative<GLuint*>(data_))
delete[] std::get<GLuint*>(data_);
}
};
/**
* basic VAO class.
*/
class basic_vertex_array
{
private:
struct vbo_t
{
GLuint bufferID_ = 0;
GLsizeiptr size_ = 0;
vbo_t();
void bind(GLint buffer_type = GL_ARRAY_BUFFER) const;
void allocate(GLsizeiptr size, GLint buffer_type = GL_ARRAY_BUFFER, GLint memory_type = GL_STATIC_DRAW, void* data = nullptr);
static void sub_update(GLsizeiptr offset, GLsizeiptr size, void* data, GLint buffer_type = GL_ARRAY_BUFFER);
void update(GLsizeiptr size, void* data, GLint buffer_type = GL_ARRAY_BUFFER, GLint memory_type = GL_STATIC_DRAW);
~vbo_t();
};
GLuint vaoID;
public:
basic_vertex_array();
};
} }

View File

@ -16,8 +16,70 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <blt/gfx/model.h> #include <blt/gfx/model.h>
#include <blt/std/utility.h>
blt::gfx::vertex_array::vertex_array() namespace blt::gfx
{
static_dynamic_array::static_dynamic_array(static_dynamic_array&& move) noexcept
{
if (std::holds_alternative<GLuint[DATA_SIZE]>(move.data_)){
data_ = std::move(std::get<GLuint[DATA_SIZE]>(move.data_));
} else {
data_ = std::get<GLuint*>(move.data_);
size_ = move.size_;
}
move.data_ = nullptr;
move.size_ = 0;
}
/*
* -----------------------------------
* basic_vertex_array::vbo_t
* -----------------------------------
*/
basic_vertex_array::vbo_t::vbo_t()
{
glGenBuffers(1, &bufferID_);
}
basic_vertex_array::vbo_t::~vbo_t()
{
glDeleteBuffers(1, &bufferID_);
}
void basic_vertex_array::vbo_t::allocate(GLsizeiptr size, GLint buffer_type, GLint memory_type, void* data)
{
size_ = size;
glBufferData(buffer_type, size, data, memory_type);
}
void blt::gfx::basic_vertex_array::vbo_t::update(GLsizeiptr size, void* data, GLint buffer_type, GLint memory_type)
{
if (size <= size_)
sub_update(0, size, data, buffer_type);
else
allocate(size, buffer_type, memory_type, data);
size_ = size;
}
void basic_vertex_array::vbo_t::sub_update(GLsizeiptr offset, GLsizeiptr size, void* data, GLint buffer_type)
{
glBufferSubData(buffer_type, offset, size, data);
}
void basic_vertex_array::vbo_t::bind(GLint buffer_type) const
{
glBindBuffer(buffer_type, bufferID_);
}
/*
* ----------------------------
* basic_vertex_array
* ----------------------------
*/
blt::gfx::basic_vertex_array::basic_vertex_array()
{ {
glGenVertexArrays(1, &vaoID); glGenVertexArrays(1, &vaoID);
} }
}