diff --git a/src/Model.cpp b/src/Model.cpp index 618ce4e..a2f9ff3 100644 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -138,3 +138,18 @@ Model::operator glm::mat4() const { return model_transformation; } + +/* Increment offset that indicates which tile texture is active. If offset reaches the end of the container, + * start over from the beginning. */ +void Background::next() +{ + offset = ++offset % model_texture.size(); +} + +/* Return the currently active tile texture. */ +const sb::Texture& Background::tile() const +{ + auto position = model_texture.begin(); + std::advance(position, offset); + return position->second; +} diff --git a/src/Model.hpp b/src/Model.hpp index 25b224f..0c3d2f6 100644 --- a/src/Model.hpp +++ b/src/Model.hpp @@ -21,6 +21,7 @@ #include #include #include +#include #include "glm/glm.hpp" #include "Attributes.hpp" #include "Texture.hpp" @@ -28,7 +29,7 @@ class Model { -private: +protected: std::map> model_attributes; std::map model_texture; @@ -69,10 +70,24 @@ public: {1.0f, 1.0f}, {1.0f, 0.0f}, {0.0f, 0.0f} }); -public: - Plane() : Model(std::map>({{"position", position}, {"uv", uv}})) {} }; +/* Plane that stores multiple textures which can be cycled through and looped. Only one texture is + * active at a time, returned by Background::current. Cycle only goes forward, using Background::next. */ +class Background : public Plane +{ + +private: + + std::uint8_t offset = 0; + +public: + + void next(); + const sb::Texture& tile() const; + +}; + #endif diff --git a/src/Pudding.cpp b/src/Pudding.cpp index a730048..2ad5f07 100644 --- a/src/Pudding.cpp +++ b/src/Pudding.cpp @@ -38,11 +38,21 @@ Pudding::Pudding() /* use gl context so we can draw 3D */ load_gl_context(); load_tiles(); - Model boom; + Model boom, kaboom; boom["hello world"] = std::make_shared(sb::Attributes{1, 2, 3, 4, 5}); std::cout << *boom["hello world"] << std::endl; boom.attributes("bam", {{4.2f, 42.0f, 420.0f}, {.69f, 6.9f, 69.0f}}); std::cout << *boom["bam"] << std::endl; + kaboom = boom; + std::cout << *kaboom.attributes("hello world") << std::endl; + background_copy = background; + background_copy.next(); + background = background_copy; + background_copy_2 = background_copy; + background_copy_2.next(); + background = background_copy_2; + background_copy = background_copy_2; + background = background_copy; } /* Assign vertices, colors and texture UV coordinates to the pudding model */ @@ -225,7 +235,7 @@ void Pudding::load_tiles() { sb::Texture texture = sb::Texture(path); texture.load(); - tiles.push_back(texture); + background.texture(path, texture); } } @@ -307,7 +317,7 @@ void Pudding::respond(SDL_Event& event) } else if (get_delegate().compare(event, "tile")) { - current_tile_index = ++current_tile_index % tiles.size(); + background_copy.next(); } } @@ -751,7 +761,7 @@ void Pudding::update() plane.enable(); glUniform1i(uniform["flat"]["texture"], 0); glActiveTexture(GL_TEXTURE0); - tiles[current_tile_index].bind(); + background_copy.tile().bind(); /* set blend to modify white part of background, color passed is in HSV format */ glUniform3f(uniform["flat"]["blend"], 0.0f, 0.0f, 1.0f); glUniform1i(uniform["flat"]["scroll"], true); diff --git a/src/Pudding.hpp b/src/Pudding.hpp index dc621ed..db24c03 100644 --- a/src/Pudding.hpp +++ b/src/Pudding.hpp @@ -78,8 +78,7 @@ private: const glm::vec3 PUDDING_YELLOW = glm::vec3(0.878f, 0.859f, 0.122f); std::string current_barcode, previous_barcode, current_config_barcode, current_camera_barcode; std::vector items; - int current_item_index = 0, effect_id = EFFECT_NONE, pudding_triangle_vertex_count = 0, pudding_fan_vertex_count = 0, - current_tile_index = 0; + int current_item_index = 0, effect_id = EFFECT_NONE, pudding_triangle_vertex_count = 0, pudding_fan_vertex_count = 0; cv::VideoCapture capture; zbar::ImageScanner image_scanner; std::map> uniform; @@ -87,10 +86,10 @@ private: glm::mat4 projection, model = glm::mat4(1.0f), mvp; Model pudding_model; Plane plane; + Background background, background_copy, background_copy_2; // Plane background, camera_frame; bool show_item = false, reading_capture_frame = false; SDL_GLContext capture_frame_thread_context = nullptr; - std::vector tiles; sb::Texture capture_texture_front_buffer, capture_texture_back_buffer; sb::Texture& capture_texture = capture_texture_front_buffer; sb::VAO vao;