/
Tias
/
UIToolkit
Обзор
Документация
Войти
/
Tias
/
UIToolkit
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
include/core/render/VBO.hpp
148 строк
4 KB
Tias
debugging
24 июн 2025, 19:51
24 июн 2025, 19:51
66846bc
Код
Авторство
О чём код?
#ifndef VBO_HPP_ #define VBO_HPP_ // ----------------------------------------------------------- // | Vertex buffer object used to store vertex data in GPU | // ----------------------------------------------------------- #include "IBindable.hpp" #include "core/OpenGLHeaders.hpp" #include "core/render/Attribute.hpp" namespace core { namespace render { class IVertexBufferObject : public IBindable<GLuint> { public: virtual IVertexBufferObject &setData(GLsizeiptr memorySize, const void *memory) = 0; virtual IVertexBufferObject &removeData() = 0; virtual IVertexBufferObject &setVertexAttribute(IAttribute &attrib, const void *offset) = 0; virtual IVertexBufferObject & copySubData(GLintptr offset, GLsizeiptr memorySize, const void *memory) = 0; virtual void *memoryMapToRead(GLintptr offset, GLsizeiptr length) = 0; virtual void *memoryMapToWrite(GLintptr offset, GLsizeiptr length) = 0; virtual IVertexBufferObject &memoryUnmap() = 0; virtual ~IVertexBufferObject() = default; }; template <GLenum I> concept VBOType = (I == GL_ARRAY_BUFFER) || (I == GL_ELEMENT_ARRAY_BUFFER); template <GLenum I> concept VBOUsage = (I == GL_STATIC_DRAW) || (I == GL_DYNAMIC_DRAW) || (I == GL_STATIC_READ) || (I == GL_DYNAMIC_READ); template <GLenum BufferType> struct buffer_binding_map {}; template <> struct buffer_binding_map<GL_ARRAY_BUFFER> { static constexpr GLenum type = GL_ARRAY_BUFFER_BINDING; }; template <> struct buffer_binding_map<GL_ELEMENT_ARRAY_BUFFER> { static constexpr GLenum type = GL_ELEMENT_ARRAY_BUFFER_BINDING; }; template <GLenum BufferType, GLenum BufferUsage> requires VBOType<BufferType> && VBOUsage<BufferUsage> class VertexBufferObject : public IVertexBufferObject { private: GLuint id_; void *memoryMap(GLsizeiptr length, GLintptr offset, GLbitfield access) { bind(); return glMapBufferRange(BufferType, offset, length, access); } public: VertexBufferObject() { glGenBuffers(1, &id_); }; VertexBufferObject(VertexBufferObject &) = delete; VertexBufferObject &operator=(VertexBufferObject &) = delete; VertexBufferObject(VertexBufferObject &&other) { id_ = other.id_; other.id_ = 0; } VertexBufferObject &operator=(VertexBufferObject &&other) { id_ = other.id_; other.id_ = 0; return *this; } IBindable &bind() override { glBindBuffer(BufferType, id_); return *this; } IBindable &unbind(GLuint oldBind = 0) override { glBindBuffer(BufferType, oldBind); return *this; } GLuint getCurrentBind() override { GLint result; glGetIntegerv(buffer_binding_map<BufferType>::type, &result); return result; } IVertexBufferObject &setData(GLsizeiptr memorySize, const void *memory) override { bind(); glBufferData(BufferType, memorySize, memory, BufferUsage); return *this; } IVertexBufferObject &removeData() override { bind(); glBufferData(BufferType, 0, NULL, BufferUsage); return *this; } IVertexBufferObject &setVertexAttribute(IAttribute &attrib, const void *offset) override { bind(); glEnableVertexAttribArray(attrib.location()); glVertexAttribPointer(attrib.location(), attrib.size(), attrib.type(), attrib.normalized(), attrib.stride(), offset); return *this; } IVertexBufferObject ©SubData(GLintptr offset, GLsizeiptr memorySize, const void *memory) override { bind(); glBufferSubData(BufferType, offset, memorySize, memory); return *this; } void *memoryMapToRead(GLintptr offset, GLsizeiptr length) override { bind(); return memoryMap(length, offset, GL_MAP_READ_BIT); } void *memoryMapToWrite(GLintptr offset, GLsizeiptr length) override { bind(); return memoryMap(length, offset, GL_MAP_WRITE_BIT); } IVertexBufferObject &memoryUnmap() override { bind(); glUnmapBuffer(BufferType); return *this; } ~VertexBufferObject() { bind(); glDeleteBuffers(1, &id_); } }; } // namespace render } // namespace core #endif // !VBO_HPP_