diff --git a/vis/include/cgl/render.hpp b/vis/include/cgl/render.hpp index a0c1f0a..e608419 100644 --- a/vis/include/cgl/render.hpp +++ b/vis/include/cgl/render.hpp @@ -7,12 +7,21 @@ namespace cgl { template class buffer { - GLuint id; + GLuint id{}; public: - buffer() : id(0) { + buffer() { 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 &data, GLenum usage = GL_STATIC_DRAW) + : buffer() { + put(data, usage); } buffer(buffer &) = delete; @@ -29,15 +38,23 @@ namespace cgl { operator GLuint() const { return id; } + + void put(const T &data, GLenum usage = GL_STATIC_DRAW) { + glNamedBufferData(id, sizeof(T), &data, usage); + } + + void put(const std::vector &data, GLenum usage = GL_STATIC_DRAW) { + glNamedBufferData(id, sizeof(T) * data.size(), &data[0], usage); + } }; class shader { protected: - GLuint id; + GLuint id{}; GLenum mode; public: - shader(GLenum mode) : mode(mode), id(0) { + shader(GLenum mode) : mode(mode) { id = glCreateShader(mode); } @@ -86,10 +103,10 @@ namespace cgl { class program { protected: - GLuint id; + GLuint id{}; public: - program() : id(0) { + program() { id = glCreateProgram(); } @@ -154,10 +171,10 @@ namespace cgl { class pipeline { protected: - GLuint id; + GLuint id{}; public: - pipeline() : id(0) { + pipeline() { glCreateProgramPipelines(1, &id); } diff --git a/vis/src/main.cpp b/vis/src/main.cpp index 09af24f..a52b4a8 100644 --- a/vis/src/main.cpp +++ b/vis/src/main.cpp @@ -128,14 +128,9 @@ void run(GLFWwindow *window) { //endregion - GLuint vbo; - 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); + cgl::buffer vbo(points); - GLuint ubo; - glGenBuffers(1, &ubo); + cgl::buffer ubo; glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, vbo); glBindBufferBase(GL_UNIFORM_BUFFER, 1, ubo); @@ -151,9 +146,7 @@ void run(GLFWwindow *window) { auto st = (float) glfwGetTime() / 8; Matrices mats = build(window, st); - glBindBuffer(GL_UNIFORM_BUFFER, ubo); - glBufferData(GL_UNIFORM_BUFFER, sizeof(mats), &mats, GL_STATIC_DRAW); - glBindBuffer(GL_UNIFORM_BUFFER, 0); + ubo.put(mats); glLineWidth(1.5); const auto wires_dark = glm::vec3(.3, .3, .3);