From e7152838d41f7400fd54b01bd3beb07159870840 Mon Sep 17 00:00:00 2001 From: frank <420@shampoo.ooo> Date: Mon, 18 Oct 2021 17:34:17 -0400 Subject: [PATCH] move gl buffer objects into framework project --- Makefile | 3 +- config.json | 2 +- lib/sb | 2 +- src/Item.cpp | 23 ++++---- src/Item.hpp | 29 ++++----- src/Pudding.cpp | 154 +++--------------------------------------------- src/Pudding.hpp | 77 +++--------------------- 7 files changed, 47 insertions(+), 243 deletions(-) diff --git a/Makefile b/Makefile index 5fa637d..16aeef5 100644 --- a/Makefile +++ b/Makefile @@ -69,8 +69,9 @@ $(SB_SRC_DIR)Box.o : $(addprefix $(SB_SRC_DIR),extension.hpp Segment.hpp) $(SB_SRC_DIR)Segment.o : $(addprefix $(SB_SRC_DIR),extension.hpp Box.hpp) $(SB_SRC_DIR)Pixels.o : $(addprefix $(SB_SRC_DIR),Box.hpp extension.hpp Log.hpp) $(SB_SRC_DIR)Audio.o : $(addprefix $(SB_SRC_DIR),Node.hpp Display.hpp Configuration.hpp Box.hpp filesystem.hpp extension.hpp) -$(SB_SRC_DIR)Texture.o : $(addprefix $(SB_SRC_DIR),GLObject.hpp filesystem.hpp Log.hpp) $(SB_SRC_DIR)GLObject.o : $(addprefix $(SB_SRC_DIR),Log.hpp) +$(SB_SRC_DIR)Texture.o : $(addprefix $(SB_SRC_DIR),GLObject.hpp filesystem.hpp Log.hpp) +$(SB_SRC_DIR)VBO.o : $(addprefix $(SB_SRC_DIR),Log.hpp GLObject.hpp Attributes.hpp extension.hpp) $(SB_SRC_DIR)Attributes.o : $(addprefix $(SB_SRC_DIR),Log.hpp extension.hpp) $(SRC_DIR)Item.o : $(addprefix $(SB_SRC_DIR),extension.hpp Node.hpp Texture.hpp Log.hpp) $(SRC_DIR)Pudding.o : $(SRC_H_FILES) $(SB_H_FILES) diff --git a/config.json b/config.json index 51a3329..fe596c0 100644 --- a/config.json +++ b/config.json @@ -47,7 +47,7 @@ "enabled": true, "json-save": true, "json-save-directory": "local/scans", - "barcode": "037600110754", + "barcode": "", "capture-device": "/dev/video0" }, "api": diff --git a/lib/sb b/lib/sb index 3e5e0fc..3212dc1 160000 --- a/lib/sb +++ b/lib/sb @@ -1 +1 @@ -Subproject commit 3e5e0fcbb8aedd5c4ce15aa850e88511af17ad5e +Subproject commit 3212dc15cf15ecb9639f13b9076780ffd676ffa6 diff --git a/src/Item.cpp b/src/Item.cpp index f1f823b..a01b1f9 100644 --- a/src/Item.cpp +++ b/src/Item.cpp @@ -1,11 +1,12 @@ -/* _______________ +---------------------------------------------------------------------------------------+ - //~~~~~~~~~~~~~\\ | a game by @ohsqueezy & @sleepin | - //```````````````\\ | [ohsqueezy.itch.io] [instagram.com/sleepin] | - //_0_0_0_0_0_0_0_0_\\ | | - //_/_/_/_/___\_\_\_\_\\ | with code licensed for copy, modification and redistribution [git.nugget.fun/pudding] | - //GGGUUUNNNKKKIIISSSSSS\\ | | -//_/__/__/__/_\__\__\__\_\\ | 😀 Thank you for choosing Puddendo for your business 😀 | - +---------------------------------------------------------------------------------------+ */ +/* _______________ ,----------------------------------------------------------------. + //`````````````\\ \ \ + //~~~~~~~~~~~~~~~\\ \ by @ohsqueezy & @sleepin \ + //=================\\ \ [ohsqueezy.itch.io] [sleepin.itch.io] \ + // \\ \ \ + // \\ \ code released under the zlib license [git.nugget.fun/pudding] \ + // ☆ GUNKISS ☆ \\ \ \ +//_________________________\\ `---------------------------------------------------------------*/ + #include "Item.hpp" Item::Item(Node* parent) : Node(parent) {}; @@ -30,17 +31,17 @@ void Item::set_text_property(const std::string& value, std::string& property, co } } -void Item::add_image_texture(Texture texture) +void Item::add_image_texture(sb::Texture texture) { image_textures.push_back(texture); } -const std::vector& Item::get_image_textures() const +const std::vector& Item::get_image_textures() const { return image_textures; } -const Texture& Item::get_active_image_texture() const +const sb::Texture& Item::get_active_image_texture() const { return get_image_textures()[current_image_index]; } diff --git a/src/Item.hpp b/src/Item.hpp index 2bd4856..4df1d00 100644 --- a/src/Item.hpp +++ b/src/Item.hpp @@ -1,13 +1,14 @@ -/* _______________ +---------------------------------------------------------------------------------------+ - //~~~~~~~~~~~~~\\ | a game by @ohsqueezy & @sleepin | - //```````````````\\ | [ohsqueezy.itch.io] [instagram.com/sleepin] | - //_0_0_0_0_0_0_0_0_\\ | | - //_/_/_/_/___\_\_\_\_\\ | with code licensed for copy, modification and redistribution [git.nugget.fun/pudding] | - //GGGUUUNNNKKKIIISSSSSS\\ | | -//_/__/__/__/_\__\__\__\_\\ | 😀 Thank you for choosing Puddendo for your business 😀 | - +---------------------------------------------------------------------------------------+ */ -#ifndef Item_h_ -#define Item_h_ +/* _______________ ,----------------------------------------------------------------. + //`````````````\\ \ \ + //~~~~~~~~~~~~~~~\\ \ by @ohsqueezy & @sleepin \ + //=================\\ \ [ohsqueezy.itch.io] [sleepin.itch.io] \ + // \\ \ \ + // \\ \ code released under the zlib license [git.nugget.fun/pudding] \ + // ☆ GUNKISS ☆ \\ \ \ +//_________________________\\ `---------------------------------------------------------------*/ + +#ifndef ITEM_H_ +#define ITEM_H_ /* including so we can use GLuint type */ #if defined(__EMSCRIPTEN__) @@ -34,7 +35,7 @@ class Item : public Node private: nlohmann::json json = {}; - std::vector image_textures; + std::vector image_textures; std::string brand_name = "", product_name = "", upc = ""; int current_image_index = 0; void set_text_property(const std::string&, std::string&, const std::string&); @@ -47,9 +48,9 @@ private: public: Item(Node*); - void add_image_texture(Texture); - const std::vector& get_image_textures() const; - const Texture& get_active_image_texture() const; + void add_image_texture(sb::Texture); + const std::vector& get_image_textures() const; + const sb::Texture& get_active_image_texture() const; void set_brand_name(const std::string&); const std::string& get_brand_name() const; void set_product_name(const std::string&); diff --git a/src/Pudding.cpp b/src/Pudding.cpp index ed616e0..ca366ea 100644 --- a/src/Pudding.cpp +++ b/src/Pudding.cpp @@ -237,7 +237,7 @@ void Pudding::load_tiles() { for (fs::path path : sb::glob(get_configuration()["resource"]["tile-path"].get() / ".*.jpg")) { - Texture texture = Texture(path); + sb::Texture texture = sb::Texture(path); texture.load(); tiles.push_back(texture); } @@ -260,7 +260,7 @@ void Pudding::initialize_camera() "fps video capture device ID #" << device_id << " using " << capture.getBackendName(); /* generate two textures that will store the video frames with the intention of double buffering them * for threaded texture loading */ - for (Texture* texture : {&capture_texture_front_buffer, &capture_texture_back_buffer}) + for (sb::Texture* texture : {&capture_texture_front_buffer, &capture_texture_back_buffer}) { texture->generate({capture.get(cv::CAP_PROP_FRAME_WIDTH), capture.get(cv::CAP_PROP_FRAME_HEIGHT)}); } @@ -369,7 +369,7 @@ void Pudding::incorporate_open_food_api(Item& item) if (json["product"].value("image_url", "") != "") { std::string url = json["product"]["image_url"]; - Texture texture = texture_from_image_url(url); + sb::Texture texture = texture_from_image_url(url); if (texture.generated()) { item.add_image_texture(texture); @@ -404,7 +404,7 @@ void Pudding::incorporate_nutronix_api(Item& item) { std::string url = food["photo"]["thumb"]; sb::Log::log("adding image listed in Nutronix API at " + url); - Texture texture = texture_from_image_url(url); + sb::Texture texture = texture_from_image_url(url); if (texture.generated()) { item.add_image_texture(texture); @@ -438,7 +438,7 @@ void Pudding::incorporate_edamam_api(Item& item) if (food.value("image", "") != "") { std::string url = food["image"]; - Texture texture = texture_from_image_url(url); + sb::Texture texture = texture_from_image_url(url); if (texture.generated()) { item.add_image_texture(texture); @@ -473,7 +473,7 @@ void Pudding::incorporate_best_buy_api(Item& item) if (product.value(key, "") != "") { std::string url = product[key]; - Texture texture = texture_from_image_url(url); + sb::Texture texture = texture_from_image_url(url); if (texture.generated()) { item.add_image_texture(texture); @@ -593,10 +593,10 @@ size_t Pudding::curl_write_response(std::uint8_t* buffer, size_t size, size_t co /* Allocate storage for a texture, copy the cURL response data into the storage, and return the ID that corresponds to the GL texture */ -Texture Pudding::texture_from_image_url(const std::string& url) const +sb::Texture Pudding::texture_from_image_url(const std::string& url) const { /* this texture will be returned whether we load pixels into it or not */ - Texture texture; + sb::Texture texture; sb::Log::log("looking up image at " + url); std::vector storage; curl_get_bytes(url, storage); @@ -668,7 +668,7 @@ int Pudding::capture_frame(void* game) /* rotate the opencv matrix 180 to work with opengl coords */ cv::flip(frame, frame, -1); /* use whichever texture ID is not being used by the main rendering thread */ - Texture& texture = pudding->capture_texture == pudding->capture_texture_front_buffer ? + sb::Texture& texture = pudding->capture_texture == pudding->capture_texture_front_buffer ? pudding->capture_texture_back_buffer : pudding->capture_texture_front_buffer; /* bind texture for accepting pixel data */ texture.bind(); @@ -857,139 +857,3 @@ void Pudding::update() previous_barcode = current_barcode; } } - -VAO::VAO() : GLObject(vao_deleter) {} - -/* Bind this VAO to the current GL context */ -void VAO::bind() const -{ - glBindVertexArray(id()); -} - -/* Forward the GL VAO generate function to the base class */ -void VAO::generate() -{ - GLObject::generate(glGenVertexArrays); -} - -/* Increment the count of attributes with properties stored in this VAO. This will be called if you - * pass this object to the Buffer when adding attribute data. */ -void VAO::increment() -{ - attributes_counted++; -} - -/* Return the number of attributes with properties stored in this VAO counted so far */ -std::uint32_t VAO::counted() const -{ - return attributes_counted; -} - -/* This function gets passed to the abstract base class for deleting the VAO data when the ID - * pointer goes out of scope (when all instances of this VAO and its copies are out of scope) */ -void vao_deleter(GLuint* id) -{ - /* not sure why SDL_Log works here on program exit but SDL_LogDebug and SDL_LogInfo don't */ - SDL_Log("destroying VAO ID %i", *id); - glDeleteVertexArrays(1, id); -} - -Buffer::Buffer() : Buffer(GL_INVALID_ENUM) {} - -Buffer::Buffer(GLenum target) : GLObject(buffer_deleter), buffer_target(target) {} - -/* Forward the GL generate function to the base class. */ -void Buffer::generate() -{ - GLObject::generate(glGenBuffers); -} - -/* Set the target for this buffer. */ -void Buffer::target(GLenum target) -{ - buffer_target = target; -} - -/* Return the GL enum for target */ -GLenum Buffer::target() const -{ - return buffer_target; -} - -/* Bind this Buffer to the current GL context. An exception will be thrown if the target has not - * been set. */ -void Buffer::bind() const -{ - if (target() == GL_INVALID_ENUM) - { - throw std::invalid_argument("target must be set before binding buffer"); - } - glBindBuffer(target(), id()); -} - -/* Set the target and bind the buffer. */ -void Buffer::bind(GLenum target) -{ - this->target(target); - bind(); -} - -/* This function gets passed to the abstract base class for deleting the Buffer data when the ID - * pointer goes out of scope (when all instances of this Buffer and its copies are out of scope) */ -void buffer_deleter(GLuint* id) -{ - /* not sure why SDL_Log works here on program exit but SDL_LogDebug and SDL_LogInfo don't */ - SDL_Log("destroying buffer ID %i", *id); - glDeleteBuffers(1, id); -} - -/* Initialize a Vertex Buffer Object. The base class will be initalized with a target of - * GL_ARRAY_BUFFER. */ -VBO::VBO() : Buffer(GL_ARRAY_BUFFER) {} - -/* Allocate size bytes of vertex attribute memory on the GPU. Usage should be one of GL_STREAM_DRAW, - * GL_STREAM_READ, GL_STREAM_COPY, GL_STATIC_DRAW, GL_STATIC_READ, GL_STATIC_COPY, GL_DYNAMIC_DRAW, - * GL_DYNAMIC_READ, or GL_DYNAMIC_COPY. The memory will be uninitialized. */ -void VBO::allocate(GLsizeiptr size, GLenum usage) -{ - glBufferData(target(), size, nullptr, usage); - /* Debug */ - std::ostringstream message; - message << "After allocating memory for " << *this; - sb::Log::gl_errors(message.str()); -} - -/* Set memory in the GPU buffer to the values of the attribute data using glBufferSubData. The memory - * set is a contiguous area from the object's current byte offset value to the offset plus the size in - * bytes of the attributes. After the memory is set, the offset is set to the end of the area. - * - * Define an array of vertex attribute data on the GPU by passing the attributes's index, dimensions, - * normalization state, and type to glVertexAttribPointer, along with the buffer object's current - * offset. */ -void VBO::add(sb::Attributes& attributes) -{ - /* Set memory */ - glBufferSubData(target(), offset, attributes.size(), attributes); - /* Debug */ - std::ostringstream initialization_message; - initialization_message << "After setting " << attributes.size() << "bytes in " << *this; - sb::Log::gl_errors(initialization_message.str()); - /* Define array */ - glVertexAttribPointer( - attributes.index(), attributes.dimensions(), attributes.type(), attributes.normalized(), 0, reinterpret_cast(offset)); - /* Debug */ - std::ostringstream pointer_message; - pointer_message << "After defining attribute pointer for "; - sb::Log::gl_errors(pointer_message.str()); - /* Increase offset */ - offset += attributes.size(); -} - -/* Overload the stream operator to support adding the attributes in a VBO. Add each attributes object to - * the output stream. */ -std::ostream& operator<<(std::ostream& out, const VBO& vbo) -{ - out << ""; - return out; -} diff --git a/src/Pudding.hpp b/src/Pudding.hpp index b5fc200..595125b 100644 --- a/src/Pudding.hpp +++ b/src/Pudding.hpp @@ -17,7 +17,6 @@ #include #include #include -#include #include #include "SDL.h" #include "SDL_image.h" @@ -39,69 +38,7 @@ #include "GLObject.hpp" #include "Log.hpp" #include "Attributes.hpp" - -class VAO : public GLObject -{ - -private: - - std::uint32_t attributes_counted = 0; - -public: - - VAO(); - void generate(); - void bind() const; - void increment(); - std::uint32_t counted() const; - -}; - -void vao_deleter(GLuint*); - -class Buffer : public GLObject -{ - -private: - - GLenum buffer_target = GL_INVALID_ENUM; - -protected: - - GLenum target() const; - -public: - - Buffer(); - Buffer(GLenum); - void target(GLenum); - void generate(); - void bind() const; - void bind(GLenum); - -}; - -void buffer_deleter(GLuint*); - -class VBO; - -std::ostream& operator<<(std::ostream&, const VBO&); - -class VBO : public Buffer -{ - -private: - - GLintptr offset = 0; - -public: - - VBO(); - void allocate(GLsizeiptr, GLenum); - void add(sb::Attributes&); - friend std::ostream& operator<<(std::ostream&, const VBO&); - -}; +#include "VBO.hpp" class Pudding : public Game { @@ -155,11 +92,11 @@ private: }; bool show_item = false, reading_capture_frame = false; SDL_GLContext capture_frame_thread_context = nullptr; - std::vector tiles; - Texture capture_texture_front_buffer, capture_texture_back_buffer; - Texture& capture_texture = capture_texture_front_buffer; - VAO vao; - VBO vbo, other_vbo; + std::vector tiles; + sb::Texture capture_texture_front_buffer, capture_texture_back_buffer; + sb::Texture& capture_texture = capture_texture_front_buffer; + sb::VAO vao; + sb::VBO vbo; void set_pudding_model(float, float, int, int = 1, float = -1, float = 1, float = 0.3f); void load_gl_context(); @@ -173,7 +110,7 @@ private: nlohmann::json json_from_url(const std::string&, const std::vector& = {}); void curl_get_bytes(const std::string& url, std::vector&, const std::vector& = {}) const; static size_t curl_write_response(std::uint8_t*, size_t, size_t, std::vector*); - Texture texture_from_image_url(const std::string&) const; + sb::Texture texture_from_image_url(const std::string&) const; static void destroy_texture(GLuint*); bool item_display_active() const; static int capture_frame(void*);