From ff43f035b152b522dbafd673bdb0367521505128 Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Thu, 20 Mar 2025 18:00:27 +0300 Subject: [PATCH] Applying distance to perspective projection --- programs/lab5/src/main.cpp | 6 +++--- programs/lab5/src/sprite_data.hpp | 18 ++++++++++-------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/programs/lab5/src/main.cpp b/programs/lab5/src/main.cpp index 34f7a34..a9c7f6f 100644 --- a/programs/lab5/src/main.cpp +++ b/programs/lab5/src/main.cpp @@ -22,7 +22,7 @@ namespace BGTU::ComputerGraphicsLabWork::Impl { #if 1 QApplication qApplication{argc, argv}; - SpriteData::Provider sprites_data_custom{0.1, 0.1, 10}; + SpriteData::Provider sprites_data_custom{0.1, 0.1, 0.5}; QMainWindow w{}; @@ -37,8 +37,8 @@ namespace BGTU::ComputerGraphicsLabWork::Impl { QObject::connect(&kbd, &QtUtilities::KeyboardCatcherWidget::key_pressed_D, &sprites_data_custom, &SpriteData::Provider::inc_y_angle); QObject::connect(&kbd, &QtUtilities::KeyboardCatcherWidget::key_pressed_S, &sprites_data_custom, &SpriteData::Provider::dec_x_angle); QObject::connect(&kbd, &QtUtilities::KeyboardCatcherWidget::key_pressed_W, &sprites_data_custom, &SpriteData::Provider::inc_x_angle); - QObject::connect(&kbd, &QtUtilities::KeyboardCatcherWidget::key_pressed_PageDown, &sprites_data_custom, &SpriteData::Provider::dec_scale); - QObject::connect(&kbd, &QtUtilities::KeyboardCatcherWidget::key_pressed_PageUp, &sprites_data_custom, &SpriteData::Provider::inc_scale); + QObject::connect(&kbd, &QtUtilities::KeyboardCatcherWidget::key_pressed_PageDown, &sprites_data_custom, &SpriteData::Provider::dec_distance); + QObject::connect(&kbd, &QtUtilities::KeyboardCatcherWidget::key_pressed_PageUp, &sprites_data_custom, &SpriteData::Provider::inc_distance); QObject::connect(&kbd, &QtUtilities::KeyboardCatcherWidget::key_pressed_P, &sprites_data_custom, &SpriteData::Provider::switch_projection); SortSprite sprites_sort{sprites, sprites_count}; diff --git a/programs/lab5/src/sprite_data.hpp b/programs/lab5/src/sprite_data.hpp index 2a8523a..32b2ae5 100644 --- a/programs/lab5/src/sprite_data.hpp +++ b/programs/lab5/src/sprite_data.hpp @@ -36,11 +36,11 @@ namespace BGTU::ComputerGraphicsLabWork::Impl { QMutex sync; double x_angle; double y_angle; - double scale; + double distance; double x_angle_step; double y_angle_step; - double scale_step; + double distance_step; bool is_projection_enabled; RendererApi::VirtualVoxelPainter::visible_pixel_coordinate_fast_t w, h; @@ -50,14 +50,14 @@ namespace BGTU::ComputerGraphicsLabWork::Impl { double x_angle_step, double y_angle_step, double scale_step - ) : x_angle{0}, y_angle{0}, scale{1}, x_angle_step{x_angle_step}, y_angle_step{y_angle_step}, scale_step{scale_step}, + ) : x_angle{0}, y_angle{0}, distance{0}, x_angle_step{x_angle_step}, y_angle_step{y_angle_step}, distance_step{scale_step}, w{0}, h{0}, is_projection_enabled{false} {} SpriteData get_sprite_data() override { this->sync.lock(); double radius = (((this->w < this->h) ? this->w : this->h) * 7 / 16.0); auto projection = this->is_projection_enabled - ? Utilities::Matrix4d::projection_central(-2, 2, -2, 2, 1, 9) * Utilities::Matrix4d::scale(1, 1, -1) * Utilities::Matrix4d::shift(0, 0, 7) + ? Utilities::Matrix4d::projection_central(-1.5, 1.5, -1.5, 1.5, 1, 9) * Utilities::Matrix4d::scale(5, 5, -1) * Utilities::Matrix4d::shift(0, 0, 7 + this->distance) : Utilities::Matrix4d::projection_orto(-2, 2, -2, 2, -6, 2); SpriteData cached{ Utilities::Matrix4d::shift(this->w / 2, this->h / 2, 0) * Utilities::Matrix4d::scale(radius) * projection * Utilities::Matrix4d::rotate_x(this->x_angle) * Utilities::Matrix4d::rotate_y(this->y_angle), @@ -99,15 +99,17 @@ namespace BGTU::ComputerGraphicsLabWork::Impl { this->sync.unlock(); } - void inc_scale() { + void inc_distance() { this->sync.lock(); - this->scale += this->scale_step; + this->distance += this->distance_step; this->sync.unlock(); } - void dec_scale() { + void dec_distance() { this->sync.lock(); - this->scale -= this->scale_step; + this->distance -= this->distance_step; + if (this->distance < 0) + this->distance = 0; this->sync.unlock(); }