#pragma once
#include <vector>
#include <stdexcept>
#include <glm/glm.hpp>
#include <QOpenGLFunctions_3_3_Core>
namespace GL
{
class VAO
{
private:
QOpenGLFunctions_3_3_Core* _functions;
GLuint _vao = 0;
std::vector<GLuint> _buffers;
unsigned int _indices_buffer = 0;
size_t _indices_size = 0;
public:
VAO(QOpenGLFunctions_3_3_Core* functions);
VAO(const VAO &) = delete;
~VAO();
void bind();
void draw(unsigned int type);
void add_VBO(const std::vector<float> &data);
void add_VBO(const std::vector<glm::vec2> &data);
void add_VBO(const std::vector<glm::vec3> &data);
void add_VBO(const std::vector<glm::vec4> &data);
void add_vertices(const std::vector<unsigned int> &data);
};
}
#include "VAO.h"
GL::VAO::VAO(QOpenGLFunctions_3_3_Core* functions)
{
_functions = functions;
_functions->glGenVertexArrays(1, &_vao);
bind();
}
GL::VAO::~VAO()
{
_functions->glDeleteBuffers((GLsizei)_buffers.size(), _buffers.data());
_functions->glDeleteVertexArrays(1, &_vao);
_functions->glDeleteBuffers(1, &_indices_buffer);
}
void GL::VAO::bind()
{
_functions->glBindVertexArray(_vao);
}
void GL::VAO::draw(unsigned int type)
{
assert(_indices_buffer != 0);
bind();
for (GLuint i = 0; i < _buffers.size(); ++i)
{
_functions->glEnableVertexAttribArray(i);
}
_functions->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indices_buffer);
_functions->glDrawElements(type, _indices_size, GL_UNSIGNED_INT, nullptr);
for (GLuint i = 0; i < _buffers.size(); ++i)
{
_functions->glDisableVertexAttribArray(i);
}
}
void GL::VAO::add_VBO(const std::vector<float> &data)
{
GLuint vbo;
_functions->glGenBuffers(1, &vbo);
_functions->glBindBuffer(GL_ARRAY_BUFFER, vbo);
_functions->glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), data.data(), GL_STATIC_DRAW);
_functions->glVertexAttribPointer((GLuint)_buffers.size(), 1, GL_FLOAT, GL_FALSE, 0, nullptr);
_buffers.push_back(vbo);
}
void GL::VAO::add_VBO(const std::vector<glm::vec2> &data)
{
GLuint vbo;
_functions->glGenBuffers(1, &vbo);
_functions->glBindBuffer(GL_ARRAY_BUFFER, vbo);
_functions->glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(glm::vec2), data.data(), GL_STATIC_DRAW);
_functions->glVertexAttribPointer((GLuint)_buffers.size(), 2, GL_FLOAT, GL_FALSE, 0, nullptr);
_buffers.push_back(vbo);
}
void GL::VAO::add_VBO(const std::vector<glm::vec3> &data)
{
GLuint vbo;
_functions->glGenBuffers(1, &vbo);
_functions->glBindBuffer(GL_ARRAY_BUFFER, vbo);
_functions->glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(glm::vec3), data.data(), GL_STATIC_DRAW);
_functions->glVertexAttribPointer((GLuint)_buffers.size(), 3, GL_FLOAT, GL_FALSE, 0, nullptr);
_buffers.push_back(vbo);
}
void GL::VAO::add_VBO(const std::vector<glm::vec4> &data)
{
GLuint vbo;
_functions->glGenBuffers(1, &vbo);
_functions->glBindBuffer(GL_ARRAY_BUFFER, vbo);
_functions->glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(glm::vec4), data.data(), GL_STATIC_DRAW);
_functions->glVertexAttribPointer((GLuint)_buffers.size(), 4, GL_FLOAT, GL_FALSE, 0, nullptr);
_buffers.push_back(vbo);
}
void GL::VAO::add_vertices(const std::vector<unsigned int> &data)
{
assert(_indices_buffer == 0);
_indices_size = data.size();
_functions->glGenBuffers(1, &_indices_buffer);
_functions->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indices_buffer);
_functions->glBufferData(GL_ELEMENT_ARRAY_BUFFER, data.size() * sizeof(unsigned int), data.data(), GL_STATIC_DRAW);
}
В процессе написания другого своего проекта(значительно позже, моего вопроса здесь) сделал почти также, как вы указали))) Спасибо)