move gl buffer objects into framework project

This commit is contained in:
frank 2021-10-18 17:34:17 -04:00
parent cdf4b8ce0e
commit e7152838d4
7 changed files with 47 additions and 243 deletions

View File

@ -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)

View File

@ -47,7 +47,7 @@
"enabled": true,
"json-save": true,
"json-save-directory": "local/scans",
"barcode": "037600110754",
"barcode": "",
"capture-device": "/dev/video0"
},
"api":

2
lib/sb

@ -1 +1 @@
Subproject commit 3e5e0fcbb8aedd5c4ce15aa850e88511af17ad5e
Subproject commit 3212dc15cf15ecb9639f13b9076780ffd676ffa6

View File

@ -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<Texture>& Item::get_image_textures() const
const std::vector<sb::Texture>& 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];
}

View File

@ -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<Texture> image_textures;
std::vector<sb::Texture> 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<Texture>& get_image_textures() const;
const Texture& get_active_image_texture() const;
void add_image_texture(sb::Texture);
const std::vector<sb::Texture>& 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&);

View File

@ -237,7 +237,7 @@ void Pudding::load_tiles()
{
for (fs::path path : sb::glob(get_configuration()["resource"]["tile-path"].get<fs::path>() / ".*.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<std::uint8_t> 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<GLvoid*>(offset));
/* Debug */
std::ostringstream pointer_message;
pointer_message << "After defining attribute pointer for <Attributes (" << attributes.dimensions() << "D, id: " <<
attributes.index() << ", size:" << attributes.size() << ")>";
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 << "<Vertex Buffer Object (id: " << vbo.id() << ")>";
return out;
}

View File

@ -17,7 +17,6 @@
#include <thread>
#include <memory>
#include <stdexcept>
#include <functional>
#include <curl/curl.h>
#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<Texture> 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<sb::Texture> 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<std::string>& = {});
void curl_get_bytes(const std::string& url, std::vector<std::uint8_t>&, const std::vector<std::string>& = {}) const;
static size_t curl_write_response(std::uint8_t*, size_t, size_t, std::vector<std::uint8_t>*);
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*);