use raii buffer

This commit is contained in:
2020-03-09 00:10:14 -04:00
parent 5d030ffe65
commit 0c4a6f5eff
2 changed files with 29 additions and 19 deletions

View File

@@ -7,12 +7,21 @@
namespace cgl { namespace cgl {
template<class T> template<class T>
class buffer { class buffer {
GLuint id; GLuint id{};
public: public:
buffer() : id(0) { buffer() {
glCreateBuffers(1, &id); glCreateBuffers(1, &id);
std::cout << "create " << id << std::endl; }
buffer(const T &data, GLenum usage = GL_STATIC_DRAW)
: buffer() {
put(data, usage);
}
buffer(const std::vector<T> &data, GLenum usage = GL_STATIC_DRAW)
: buffer() {
put(data, usage);
} }
buffer(buffer &) = delete; buffer(buffer &) = delete;
@@ -29,15 +38,23 @@ namespace cgl {
operator GLuint() const { operator GLuint() const {
return id; return id;
} }
void put(const T &data, GLenum usage = GL_STATIC_DRAW) {
glNamedBufferData(id, sizeof(T), &data, usage);
}
void put(const std::vector<T> &data, GLenum usage = GL_STATIC_DRAW) {
glNamedBufferData(id, sizeof(T) * data.size(), &data[0], usage);
}
}; };
class shader { class shader {
protected: protected:
GLuint id; GLuint id{};
GLenum mode; GLenum mode;
public: public:
shader(GLenum mode) : mode(mode), id(0) { shader(GLenum mode) : mode(mode) {
id = glCreateShader(mode); id = glCreateShader(mode);
} }
@@ -86,10 +103,10 @@ namespace cgl {
class program { class program {
protected: protected:
GLuint id; GLuint id{};
public: public:
program() : id(0) { program() {
id = glCreateProgram(); id = glCreateProgram();
} }
@@ -154,10 +171,10 @@ namespace cgl {
class pipeline { class pipeline {
protected: protected:
GLuint id; GLuint id{};
public: public:
pipeline() : id(0) { pipeline() {
glCreateProgramPipelines(1, &id); glCreateProgramPipelines(1, &id);
} }

View File

@@ -128,14 +128,9 @@ void run(GLFWwindow *window) {
//endregion //endregion
GLuint vbo; cgl::buffer<glm::vec4> vbo(points);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec4) * points.size(), &points[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
GLuint ubo; cgl::buffer<Matrices> ubo;
glGenBuffers(1, &ubo);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, vbo); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, vbo);
glBindBufferBase(GL_UNIFORM_BUFFER, 1, ubo); glBindBufferBase(GL_UNIFORM_BUFFER, 1, ubo);
@@ -151,9 +146,7 @@ void run(GLFWwindow *window) {
auto st = (float) glfwGetTime() / 8; auto st = (float) glfwGetTime() / 8;
Matrices mats = build(window, st); Matrices mats = build(window, st);
glBindBuffer(GL_UNIFORM_BUFFER, ubo); ubo.put(mats);
glBufferData(GL_UNIFORM_BUFFER, sizeof(mats), &mats, GL_STATIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
glLineWidth(1.5); glLineWidth(1.5);
const auto wires_dark = glm::vec3(.3, .3, .3); const auto wires_dark = glm::vec3(.3, .3, .3);