From b171ea84c23ae52c0ad834cf50f338f505d60501 Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Wed, 11 Dec 2024 02:26:45 +0300 Subject: [PATCH] Extending VoxelDrawer api to support wrapper sprites --- programs/lab1/src/pixel_grid_sprite.hpp | 4 +-- programs/lab1/src/sprite_data.hpp | 26 ++++++++++++--- .../qt_utilities/owned_qimage.hpp | 2 +- .../separate_threaded_renderer.hpp | 4 +-- .../renderer_api/point.hpp | 33 +++++++++++++++++++ .../renderer_api/voxel_drawer.hpp | 8 ++++- .../utilities/default_renderer_linear.hpp | 2 ++ utilities/src/default_renderer_linear.cpp | 5 ++- 8 files changed, 72 insertions(+), 12 deletions(-) diff --git a/programs/lab1/src/pixel_grid_sprite.hpp b/programs/lab1/src/pixel_grid_sprite.hpp index e00c9e5..6f0bba8 100644 --- a/programs/lab1/src/pixel_grid_sprite.hpp +++ b/programs/lab1/src/pixel_grid_sprite.hpp @@ -29,8 +29,8 @@ namespace BGTU::ComputerGraphicsLabWork::Lab1 { for (long long x = start_x; x < w; x += data->pixel_size, x_flag = !x_flag) { auto c = x_flag ? this->bg : this->fg; - for (long long yy = y + data->pixel_size - 1; yy-- > y;) { - for (long long xx = x + data->pixel_size - 1; xx-- > x;) { + for (long long yy = y + data->pixel_size; yy-- > y;) { + for (long long xx = x + data->pixel_size; xx-- > x;) { frame->add_voxel(xx, yy, std::numeric_limits::max(), c); } } diff --git a/programs/lab1/src/sprite_data.hpp b/programs/lab1/src/sprite_data.hpp index 0a4124d..6c835fb 100644 --- a/programs/lab1/src/sprite_data.hpp +++ b/programs/lab1/src/sprite_data.hpp @@ -2,8 +2,11 @@ #include +#include +#include #include #include +#include #include #include @@ -14,9 +17,16 @@ namespace BGTU::ComputerGraphicsLabWork::Lab1 { struct Lab1SpriteData { public: - Utilities::Matrix3d transform; + struct ShapeData { + double rotation_radians; + }; + + RendererApi::PointI<2> central_pixel_tl; std::size_t pixel_size; bool show_grid; + ShapeData shape_data; + RendererApi::Sprite **sub_sprites; + std::size_t sub_sprites_count; using Provider = _Lab1SpriteData_Provider; }; @@ -31,17 +41,23 @@ namespace BGTU::ComputerGraphicsLabWork::Lab1 { double radians_per_second; RendererApi::VoxelDrawer::visible_pixel_coordinate_fast_t w, h; bool show_grid; + RendererApi::Sprite **sub_sprites; + std::size_t sub_sprites_count; + public: explicit _Lab1SpriteData_Provider(double radians_per_second) : sync{}, time{}, pixel_size{1}, radians_per_second{radians_per_second}, show_grid{false} {} Lab1SpriteData get_sprite_data() override { this->sync.lock(); Lab1SpriteData cached{ - .transform = Utilities::Matrix3d::shift(this->pixel_size / -2.0, this->pixel_size / 2.0) * - Utilities::Matrix3d::shift(this->w / 2.0, this->h / 2.0) * - Utilities::Matrix3d::rotate(this->time.elapsed() / 1000.0 * this->radians_per_second), + .central_pixel_tl = static_cast>(RendererApi::PointF<2>(this->w, this->h) / 2.0 - RendererApi::PointF<2>(this->pixel_size, this->pixel_size) / 2.0), .pixel_size = this->pixel_size, - .show_grid = this->show_grid + .show_grid = this->show_grid, + .shape_data = { + .rotation_radians = std::fmod(this->time.elapsed() / 1000.0 * radians_per_second, std::numbers::pi) + }, + .sub_sprites = nullptr, + .sub_sprites_count = 0 }; this->sync.unlock(); return cached; diff --git a/qt-utilities/include/bgtu/computer_graphics_lab_work/qt_utilities/owned_qimage.hpp b/qt-utilities/include/bgtu/computer_graphics_lab_work/qt_utilities/owned_qimage.hpp index 2136d30..322b5df 100644 --- a/qt-utilities/include/bgtu/computer_graphics_lab_work/qt_utilities/owned_qimage.hpp +++ b/qt-utilities/include/bgtu/computer_graphics_lab_work/qt_utilities/owned_qimage.hpp @@ -86,7 +86,7 @@ namespace BGTU::ComputerGraphicsLabWork::QtUtilities { return this->owners[y * this->width() + x]; } - void set_pixel(RendererApi::PointI<2>::component_t x, RendererApi::PointI<2>::component_t y, RendererApi::Color c, RendererApi::SpriteMetadata const *owner) final { + void set_pixel(RendererApi::PointI<2>::component_t x, RendererApi::PointI<2>::component_t y, RendererApi::Color c, RendererApi::SpriteMetadata *owner) final { this->setPixelColor(x, y, QColor{c.red, c.green, c.blue}); this->setPixelOwner(x, y, owner); } diff --git a/qt-utilities/include/bgtu/computer_graphics_lab_work/qt_utilities/separate_threaded_renderer.hpp b/qt-utilities/include/bgtu/computer_graphics_lab_work/qt_utilities/separate_threaded_renderer.hpp index 6a58830..bf4b017 100644 --- a/qt-utilities/include/bgtu/computer_graphics_lab_work/qt_utilities/separate_threaded_renderer.hpp +++ b/qt-utilities/include/bgtu/computer_graphics_lab_work/qt_utilities/separate_threaded_renderer.hpp @@ -168,7 +168,7 @@ namespace BGTU::ComputerGraphicsLabWork::QtUtilities { pixels_argb{new std::uint_least8_t[w * h * 4]}, owners{new RendererApi::SpriteMetadata const *[w * h]} {} - void set_pixel(long long x, long long y, BGTU::ComputerGraphicsLabWork::RendererApi::Color c, const BGTU::ComputerGraphicsLabWork::RendererApi::SpriteMetadata *owner) final { + void set_pixel(long long x, long long y, BGTU::ComputerGraphicsLabWork::RendererApi::Color c, BGTU::ComputerGraphicsLabWork::RendererApi::SpriteMetadata *owner) final { if (x < 0 || this->w <= x) return; if (y < 0 || this->h <= y) return; this->pixels_argb[(y * this->w + x) * 4] = c.red; @@ -220,6 +220,6 @@ namespace BGTU::ComputerGraphicsLabWork::QtUtilities { private: Utilities::DefaultVoxelDrawerCache context; public: - explicit SeparateThreadedDefaultRendererLinear(QObject *owner = nullptr) : SeparateThreadedRenderer>{&this->context, owner} {}; + explicit SeparateThreadedDefaultRendererLinear(QObject *owner = nullptr) : SeparateThreadedRenderer>{&this->context, owner}, context{} {}; }; } \ No newline at end of file diff --git a/renderer-api/include/bgtu/computer_graphics_lab_work/renderer_api/point.hpp b/renderer-api/include/bgtu/computer_graphics_lab_work/renderer_api/point.hpp index 851f032..9f1aa80 100644 --- a/renderer-api/include/bgtu/computer_graphics_lab_work/renderer_api/point.hpp +++ b/renderer-api/include/bgtu/computer_graphics_lab_work/renderer_api/point.hpp @@ -22,6 +22,22 @@ namespace BGTU::ComputerGraphicsLabWork::RendererApi { explicit operator Point<2, new_component_t>() const { return Point<2, new_component_t>{converter(this->x), converter(this->y)}; } + + Point<2, _component_t> operator+(Point<2, _component_t> const &other) const { + return Point<2, _component_t>{this->x + other.x, this->y + other.y}; + } + + Point<2, _component_t> operator-(Point<2, _component_t> const &other) const { + return Point<2, _component_t>{this->x - other.x, this->y - other.y}; + } + + Point<2, _component_t> operator*(_component_t const &other) const { + return Point<2, _component_t>{this->x * other, this->y * other}; + } + + Point<2, _component_t> operator/(_component_t const &other) const { + return Point<2, _component_t>{this->x / other, this->y / other}; + } }; template @@ -40,6 +56,23 @@ namespace BGTU::ComputerGraphicsLabWork::RendererApi { explicit operator Point<3, new_component_t>() const { return Point<3, new_component_t>{new_component_t{this->x}, new_component_t{this->y}, new_component_t{this->z}}; } + + + Point<3, _component_t> operator+(Point<2, _component_t> const &other) const { + return Point<3, _component_t>{this->x + other.x, this->y + other.y, this->z + other.z}; + } + + Point<3, _component_t> operator-(Point<2, _component_t> const &other) const { + return Point<3, _component_t>{this->x - other.x, this->y - other.y, this->z - other.z}; + } + + Point<3, _component_t> operator*(_component_t const &other) const { + return Point<3, _component_t>{this->x * other, this->y * other, this->z * other}; + } + + Point<3, _component_t> operator/(_component_t const &other) const { + return Point<3, _component_t>{this->x / other, this->y / other, this->z / other}; + } }; template diff --git a/renderer-api/include/bgtu/computer_graphics_lab_work/renderer_api/voxel_drawer.hpp b/renderer-api/include/bgtu/computer_graphics_lab_work/renderer_api/voxel_drawer.hpp index c98ab59..367bc71 100644 --- a/renderer-api/include/bgtu/computer_graphics_lab_work/renderer_api/voxel_drawer.hpp +++ b/renderer-api/include/bgtu/computer_graphics_lab_work/renderer_api/voxel_drawer.hpp @@ -26,13 +26,19 @@ namespace BGTU::ComputerGraphicsLabWork::RendererApi { virtual void add_voxel(PointI<2>::component_t x, PointI<2>::component_t y, double z, Color::Transparent c) = 0; + virtual void add_voxel(PointI<2>::component_t x, PointI<2>::component_t y, double z, Color::Transparent c, SpriteMetadata *owner) = 0; + inline void add_voxel(PointI<2> p, double z, Color::Transparent c) { return this->add_voxel(p.x, p.y, z, c); } + inline void add_voxel(PointI<2> p, double z, Color::Transparent c, SpriteMetadata *owner) { + return this->add_voxel(p.x, p.y, z, c, owner); + } + class Exporter { public: - virtual void set_pixel(PointI<2>::component_t x, PointI<2>::component_t y, Color c, SpriteMetadata const* owner) = 0; + virtual void set_pixel(PointI<2>::component_t x, PointI<2>::component_t y, Color c, SpriteMetadata *owner) = 0; }; }; } \ No newline at end of file diff --git a/utilities/include/bgtu/computer_graphics_lab_work/utilities/default_renderer_linear.hpp b/utilities/include/bgtu/computer_graphics_lab_work/utilities/default_renderer_linear.hpp index 24b2711..7c0d3ae 100644 --- a/utilities/include/bgtu/computer_graphics_lab_work/utilities/default_renderer_linear.hpp +++ b/utilities/include/bgtu/computer_graphics_lab_work/utilities/default_renderer_linear.hpp @@ -131,6 +131,8 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities { } void add_voxel(long long x, long long y, double z, RendererApi::Color::Transparent c) final; + + void add_voxel(long long x, long long y, double z, RendererApi::Color::Transparent c, RendererApi::SpriteMetadata *owner) final; }; diff --git a/utilities/src/default_renderer_linear.cpp b/utilities/src/default_renderer_linear.cpp index 52e95fa..757da96 100644 --- a/utilities/src/default_renderer_linear.cpp +++ b/utilities/src/default_renderer_linear.cpp @@ -136,6 +136,9 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities { void DefaultVoxelDrawerCache::VoxelDrawerImpl::add_voxel(RendererApi::PointI<2>::component_t x, RendererApi::PointI<2>::component_t y, double z, RendererApi::Color::Transparent c) { + return this->add_voxel(x, y, z, c, this->current_artist); + } + void DefaultVoxelDrawerCache::VoxelDrawerImpl::add_voxel(RendererApi::PointI<2>::component_t x, RendererApi::PointI<2>::component_t y, double z, RendererApi::Color::Transparent c, RendererApi::SpriteMetadata *owner) { if (x < 0 || this->_width <= x) return; if (y < 0 || this->_height <= y) return; if (z < 0) return; @@ -153,6 +156,6 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities { } #endif } - p->voxels = this->pixel_trace_elements_allocator->alloc_elem(p->voxels, z, c, this->current_artist); + p->voxels = this->pixel_trace_elements_allocator->alloc_elem(p->voxels, z, c, owner); } } \ No newline at end of file