From 69e67e580260ec1fb9e7accd3cea6730f8f16213 Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Wed, 11 Dec 2024 13:07:53 +0300 Subject: [PATCH] Fixes --- programs/lab1/src/main.cpp | 2 +- programs/lab1/src/variants/variant1.cpp | 29 ++++++++++++--- .../qt_utilities/renderer_widget.hpp | 8 ++--- .../utilities/default_renderer_linear.hpp | 6 ++-- .../utilities/shapes/brezenham.hpp | 19 +++++----- .../utilities/shapes/polygon.hpp | 4 +-- .../utilities/shapes/rectangle.hpp | 3 +- .../utilities/zoomed_voxel_painter.hpp | 2 +- utilities/src/default_renderer_linear.cpp | 36 ++++++++++--------- 9 files changed, 68 insertions(+), 41 deletions(-) diff --git a/programs/lab1/src/main.cpp b/programs/lab1/src/main.cpp index c324533..e400070 100644 --- a/programs/lab1/src/main.cpp +++ b/programs/lab1/src/main.cpp @@ -34,7 +34,7 @@ namespace BGTU::ComputerGraphicsLabWork::Lab1 { }); - Lab1SpriteData::Provider sprites_data{1}; + Lab1SpriteData::Provider sprites_data{0.1}; sprites_data.set_pixel_size(16); sprites_data.set_sub_sprites(variant1.sprites, variant1.count); diff --git a/programs/lab1/src/variants/variant1.cpp b/programs/lab1/src/variants/variant1.cpp index 1e46557..44c0084 100644 --- a/programs/lab1/src/variants/variant1.cpp +++ b/programs/lab1/src/variants/variant1.cpp @@ -4,26 +4,45 @@ #include "../variants.hpp" #include "../sprite_data.hpp" -namespace BGTU::ComputerGraphicsLabWork::Lab1{ +namespace BGTU::ComputerGraphicsLabWork::Lab1 { namespace Variant1 { class S1 : public RendererApi::Sprite> { public: void draw( BGTU::ComputerGraphicsLabWork::Utilities::ZoomedVoxelPainter *frame, const BGTU::ComputerGraphicsLabWork::Lab1::Lab1SpriteData::ShapeData *data - ) const final { + ) const final { Utilities::Shapes::draw_polygon_edge( frame, {data->neg_rotated(data->radius / 2, 0), data->neg_rotated(data->radius / 2, 90), data->neg_rotated(data->radius / 2, 180), data->neg_rotated(data->radius / 2, 270),}, 1, {255, 0, 0} - ); + ); + } + }; + + class S2 : public RendererApi::Sprite> { + public: + void draw( + BGTU::ComputerGraphicsLabWork::Utilities::ZoomedVoxelPainter *frame, + const BGTU::ComputerGraphicsLabWork::Lab1::Lab1SpriteData::ShapeData *data + ) const final { + Utilities::Shapes::draw_polygon_edge( + frame, + { + {data->radius, 0}, + {0, data->radius}, + {-data->radius, 0}, + {0, -data->radius} + }, + 2, + {0, 255, 255} + ); } }; - } - variant_sprites variant1 = variant_sprites::make(Variant1::S1{}); + variant_sprites variant1 = variant_sprites::make(Variant1::S1{}, Variant1::S2{}); } \ No newline at end of file diff --git a/qt-utilities/include/bgtu/computer_graphics_lab_work/qt_utilities/renderer_widget.hpp b/qt-utilities/include/bgtu/computer_graphics_lab_work/qt_utilities/renderer_widget.hpp index 86957a0..8ee58b8 100644 --- a/qt-utilities/include/bgtu/computer_graphics_lab_work/qt_utilities/renderer_widget.hpp +++ b/qt-utilities/include/bgtu/computer_graphics_lab_work/qt_utilities/renderer_widget.hpp @@ -31,7 +31,7 @@ namespace BGTU::ComputerGraphicsLabWork::QtUtilities { void receive_image(BGTU::ComputerGraphicsLabWork::QtUtilities::OwnedQImage *img) final { this->sync.lock(); - if (this->next_image != nullptr) + if (this->next_image != nullptr && this->next_image != img) delete this->next_image; this->next_image = img; this->sync.unlock(); @@ -40,15 +40,15 @@ namespace BGTU::ComputerGraphicsLabWork::QtUtilities { public: - explicit RendererWidget(_SeparateThreadedRenderer_Signals *renderer, QWidget *owner = nullptr) : _RendererWidget_SignalSlots{owner}, sync{}, next_image{}, current_image{}, renderer{renderer} { - connect(this->renderer, &_SeparateThreadedRenderer_Signals::frame_rendered, this, &_RendererWidget_SignalSlots::receive_image); + explicit RendererWidget(_SeparateThreadedRenderer_Signals *renderer, QWidget *owner = nullptr) : _RendererWidget_SignalSlots{owner}, sync{}, next_image{nullptr}, current_image{nullptr}, renderer{renderer} { + QObject::connect(this->renderer, &_SeparateThreadedRenderer_Signals::frame_rendered, this, &_RendererWidget_SignalSlots::receive_image); } protected: void paintEvent(QPaintEvent *event) final { this->sync.lock(); if (this->next_image != nullptr) { - if (this->current_image != nullptr) + if (this->current_image != nullptr && this->next_image != this->current_image) delete this->current_image; this->current_image = this->next_image; this->next_image = nullptr; 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 4581613..aa92ea2 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 @@ -175,10 +175,12 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities { auto cell = this->next_unallocated; if (cell != nullptr) { this->next_unallocated = cell->empty.next_empty; - return &(cell->allocated); + return new(&(cell->allocated)) ZElement{args...}; } - for (; this->next_unfinished_page != nullptr && (cell = this->next_unfinished_page->try_allocate()) == nullptr; this->next_unfinished_page = this->next_unfinished_page->next) {} + while (this->next_unfinished_page != nullptr && (cell = this->next_unfinished_page->try_allocate()) == nullptr) { + this->next_unfinished_page = this->next_unfinished_page->next; + } if (cell == nullptr) { while ((cell = this->last_allocated_page->try_allocate()) == nullptr) diff --git a/utilities/include/bgtu/computer_graphics_lab_work/utilities/shapes/brezenham.hpp b/utilities/include/bgtu/computer_graphics_lab_work/utilities/shapes/brezenham.hpp index 62a5c81..aeb2435 100644 --- a/utilities/include/bgtu/computer_graphics_lab_work/utilities/shapes/brezenham.hpp +++ b/utilities/include/bgtu/computer_graphics_lab_work/utilities/shapes/brezenham.hpp @@ -7,7 +7,7 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities::Shapes { class brezenham_line_iterable { private: RendererApi::PointI<2>::component_t start_arg, start_ret, end_arg; - RendererApi::PointI<2>::component_t delta_ret; + RendererApi::PointI<2>::component_t delta_ret, delta_arg; RendererApi::PointI<2>::component_t delta_error, error_threshold; bool swap_flag; public: @@ -36,6 +36,7 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities::Shapes { this->delta_ret = (x2 > x1) ? 1 : -1; this->swap_flag = true; } + this->delta_arg = (this->end_arg > this->start_arg) ? 1 : -1; } constexpr brezenham_line_iterable( @@ -46,22 +47,24 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities::Shapes { class iterator { private: RendererApi::PointI<2>::component_t arg, ret, end_arg; - RendererApi::PointI<2>::component_t delta_ret; + RendererApi::PointI<2>::component_t delta_ret, delta_arg; RendererApi::PointI<2>::component_t error, delta_error, error_threshold; bool swap_flag; constexpr iterator( RendererApi::PointI<2>::component_t arg, RendererApi::PointI<2>::component_t ret, RendererApi::PointI<2>::component_t end_arg, - RendererApi::PointI<2>::component_t delta_ret, + RendererApi::PointI<2>::component_t delta_ret, RendererApi::PointI<2>::component_t delta_arg, RendererApi::PointI<2>::component_t delta_error, RendererApi::PointI<2>::component_t error_threshold, bool swap_flag - ) noexcept: arg{arg}, ret{ret}, end_arg{end_arg}, delta_ret{delta_ret}, error{0}, delta_error{delta_error}, swap_flag{swap_flag}, error_threshold{error_threshold} {} + ) noexcept: arg{arg}, ret{ret}, end_arg{end_arg}, delta_ret{delta_ret}, delta_arg{delta_arg}, error{0}, delta_error{delta_error}, swap_flag{swap_flag}, error_threshold{error_threshold} {} friend class brezenham_line_iterable; public: constexpr iterator &operator++() noexcept { - this->arg++; + if (*this == nullptr) + return *this; + this->arg += this->delta_arg; this->error += this->delta_error; if (this->error >= this->error_threshold) { this->ret += this->delta_ret; @@ -84,18 +87,18 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities::Shapes { } constexpr bool operator==(std::nullptr_t) const noexcept { - return this->arg > this->end_arg; + return (this->arg > this->end_arg) ^ (this->delta_arg < 0); } constexpr bool operator!=(std::nullptr_t) const noexcept { - return this->arg <= this->end_arg; + return !(*this == nullptr); } }; [[nodiscard]] constexpr iterator begin() const noexcept { return iterator{ this->start_arg, this->start_ret, this->end_arg, - this->delta_ret, + this->delta_ret, this->delta_arg, this->delta_error, this->error_threshold, this->swap_flag }; diff --git a/utilities/include/bgtu/computer_graphics_lab_work/utilities/shapes/polygon.hpp b/utilities/include/bgtu/computer_graphics_lab_work/utilities/shapes/polygon.hpp index df4e5b0..649afd8 100644 --- a/utilities/include/bgtu/computer_graphics_lab_work/utilities/shapes/polygon.hpp +++ b/utilities/include/bgtu/computer_graphics_lab_work/utilities/shapes/polygon.hpp @@ -25,14 +25,14 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities::Shapes { if (points[i - 1] == points[i]) continue; auto it = brezenham_line_iterable{points[i - 1], points[i]}.begin(); - it++; +// it++; for (; it != nullptr; it++) { receiver((*it).x, (*it).y); } } if (points[0] != points[points_count - 1]) { auto it = brezenham_line_iterable{points[points_count - 1], points[0]}.begin(); - it++; +// it++; for (; it != nullptr; it++) { receiver((*it).x, (*it).y); } diff --git a/utilities/include/bgtu/computer_graphics_lab_work/utilities/shapes/rectangle.hpp b/utilities/include/bgtu/computer_graphics_lab_work/utilities/shapes/rectangle.hpp index 761f184..4b61749 100644 --- a/utilities/include/bgtu/computer_graphics_lab_work/utilities/shapes/rectangle.hpp +++ b/utilities/include/bgtu/computer_graphics_lab_work/utilities/shapes/rectangle.hpp @@ -108,7 +108,7 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities::Shapes { right--; bottom--; - for (RendererApi::PointI<2>::component_t y = top; y < bottom; y++) { + for (RendererApi::PointI<2>::component_t y = top+1; y < bottom; y++) { RendererApi::PointI<2>::component_t x = left; receiver(true, x, y); for (x++; x < right; x++) { @@ -116,7 +116,6 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities::Shapes { } receiver(true, x, y); } - bottom++; for (RendererApi::PointI<2>::component_t x = left; x <= right; x++) { receiver(true, x, bottom); diff --git a/utilities/include/bgtu/computer_graphics_lab_work/utilities/zoomed_voxel_painter.hpp b/utilities/include/bgtu/computer_graphics_lab_work/utilities/zoomed_voxel_painter.hpp index 95ba4b5..dcf8537 100644 --- a/utilities/include/bgtu/computer_graphics_lab_work/utilities/zoomed_voxel_painter.hpp +++ b/utilities/include/bgtu/computer_graphics_lab_work/utilities/zoomed_voxel_painter.hpp @@ -48,7 +48,7 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities { this->pixel_size, this->pixel_size, z, - c, c, + c, {0, 255, 255}, owner ); } diff --git a/utilities/src/default_renderer_linear.cpp b/utilities/src/default_renderer_linear.cpp index 0b27b81..fa4661c 100644 --- a/utilities/src/default_renderer_linear.cpp +++ b/utilities/src/default_renderer_linear.cpp @@ -80,13 +80,13 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities { *union_ptr = left_ptr; left_ptr = left_ptr->next; union_ptr = &((*union_ptr)->next); - if (left_counter-- == 0) + if (--left_counter == 0) goto RIGHT_TAIL; } else { *union_ptr = right_ptr; right_ptr = right_ptr->next; union_ptr = &((*union_ptr)->next); - if (right_counter-- == 0) + if (--right_counter == 0) goto LEFT_TAIL; if (right_ptr == nullptr) { @@ -96,12 +96,12 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities { } if (right_counter == 0) { LEFT_TAIL: - while (right_counter-- > 1) + *union_ptr = left_ptr; + while (left_counter-- > 1) left_ptr = left_ptr->next; left_ptr->next = right_ptr; before_left = &(left_ptr->next); - } - if (left_counter == 0) { + } else if (left_counter == 0) { RIGHT_TAIL: *union_ptr = right_ptr; while (right_counter-- > 1) { @@ -138,24 +138,28 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities { void DefaultVoxelDrawerCache::VoxelPainterImpl::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::VoxelPainterImpl::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; auto p = this->at(x, y); +#if 0 + if (z <= p->nearest_z) { - p->nearest_z = z; -#if 1 - if (c.alpha == 255) { - ZElement *e; - while (p->voxels != nullptr) { - e = p->voxels; - p->voxels = e->next; - this->pixel_trace_elements_allocator->free_elem(e); - } - } + p->nearest_z = z; + + if (c.alpha == 255) { + ZElement *e; + while (p->voxels != nullptr) { + e = p->voxels; + p->voxels = e->next; + this->pixel_trace_elements_allocator->free_elem(e); + } + } + } #endif - } + p->voxels = this->pixel_trace_elements_allocator->alloc_elem(p->voxels, z, c, owner); } } \ No newline at end of file