From 47e5eca45644a632867b0306384a2f0fe8b6176b Mon Sep 17 00:00:00 2001 From: Dawid Pietrykowski Date: Tue, 13 Jun 2023 21:00:54 +0200 Subject: [PATCH] Finished 3D-1 --- QT/3d-cube/mywindow.cpp | 44 +++++++++++++++----------- QT/3d-cube/mywindow.h | 34 ++++++++++++++------ QT/3d-cube/mywindow.ui | 70 ++++++++++++++++++----------------------- 3 files changed, 82 insertions(+), 66 deletions(-) diff --git a/QT/3d-cube/mywindow.cpp b/QT/3d-cube/mywindow.cpp index 39ee21a..9f18eb0 100644 --- a/QT/3d-cube/mywindow.cpp +++ b/QT/3d-cube/mywindow.cpp @@ -38,10 +38,15 @@ std::vector matrixMul(std::vector p, std::vector m) std::vector MyWindow::GetXY(Point p, std::vector m) { std::vector res(3); - res[0] = ((p.x + VIEW_SIZE / 2.0f) / VIEW_SIZE * szer ); - res[1] = ((p.y + VIEW_SIZE / 2.0f) / VIEW_SIZE * wys); + float d = 5; + float x = (p.x * d) / (p.z + d); + float y = (p.y * d) / (p.z + d); + float z = 0; - return res; + x = ((x + VIEW_SIZE / 2.0f) / VIEW_SIZE * szer ); + y = ((y + VIEW_SIZE / 2.0f) / VIEW_SIZE * wys); + + return std::vector{x, y, z}; } std::vector matrixMul3x3(std::vector m1, std::vector m2) @@ -154,15 +159,15 @@ void MyWindow::on_exitButton_clicked() } void MyWindow::on_slider_tx_valueChanged(int val){ - translation_vec[0] = val / 100.0f - 1.0f; + translation_vec[0] = 4.0f * (val / 100.0f - 1.0f); } void MyWindow::on_slider_ty_valueChanged(int val){ - translation_vec[1] = val / 100.0f - 1.0f; + translation_vec[1] = 4.0f * (val / 100.0f - 1.0f); } void MyWindow::on_slider_tz_valueChanged(int val){ - translation_vec[2] = val / 100.0f - 1.0f; + translation_vec[2] = 4.0f * (val / 100.0f - 1.0f); } void MyWindow::on_slider_rx_valueChanged(int val){ @@ -178,15 +183,15 @@ void MyWindow::on_slider_rz_valueChanged(int val){ } void MyWindow::on_slider_sx_valueChanged(int val){ - scale_vec[0] = 1.0f / ( val / 100.0f ); + scale_vec[0] = 1.0f + (4.0f * (( val / 100.0f ) - 1.0f)); } void MyWindow::on_slider_sy_valueChanged(int val){ - scale_vec[1] = 1.0f / ( val / 100.0f ); + scale_vec[1] = 1.0f + (4.0f * (( val / 100.0f ) - 1.0f)); } void MyWindow::on_slider_sz_valueChanged(int val){ - scale_vec[2] = 1.0f / ( val / 100.0f ); + scale_vec[2] = 1.0f + (4.0f * (( val / 100.0f ) - 1.0f)); } void MyWindow::on_slider_shx_valueChanged(int val){ @@ -256,17 +261,19 @@ void MyWindow::UpdateImage(){ matrixMul4x4( matrixMul4x4( scale_mat, + translate_mat), rotation_mat_x), rotation_mat_y), rotation_mat_z), - sh_mat), - translate_mat); + sh_mat); ClearImage(img); - for(int i = 1; i < 8; i++){ - Point P1 = points[i-1]; - Point P2 = points[i]; + int lines = (sizeof(indices)/sizeof(*indices)) / 2; + + for(int i = 0; i < lines; i++){ + Point P1 = vertices[indices[2*i]]; + Point P2 = vertices[indices[2*i + 1]]; Point P1M = Point(matrixMul(P1.GetVector(), tansform_mat)); Point P2M = Point(matrixMul(P2.GetVector(), tansform_mat)); std::vector p1_vec = GetXY(P1M, std::vector{}); @@ -367,7 +374,8 @@ void MyWindow::DrawLine(int x1, int y1, int x2, int y2, QImage *img){ float diff = x2 - x1; float a = diff != 0 ? (y2 - y1) / diff : FLT_MAX; - QColor color(255, 255, 255, 255); +// QColor color(255, 255, 255, 255); + QColor color(255, 0, 80, 255); if(abs(a) < 1.0f){ for(int x = x1; x <= x2; x++){ @@ -409,9 +417,9 @@ void MyWindow::DrawPixel(QImage* img, int x, int y, QColor color){ void MyWindow::ClearImage(QImage *img){ unsigned char* empty_val = (unsigned char*)malloc(4); - empty_val[0] = 0; - empty_val[1] = 0; - empty_val[2] = 0; + empty_val[0] = 15; + empty_val[1] = 15; + empty_val[2] = 15; empty_val[3] = 255; unsigned char* ptr = img->bits(); for(int i = 0; i < img->width(); i++){ diff --git a/QT/3d-cube/mywindow.h b/QT/3d-cube/mywindow.h index b15fc2c..8dfdf0c 100644 --- a/QT/3d-cube/mywindow.h +++ b/QT/3d-cube/mywindow.h @@ -125,15 +125,31 @@ private: int segment_count; float rotation_angle; - Point points[8] = { - Point(-1.0f, -1.0f, 0.0f), - Point(1.0f, -1.0f, 0.0f), - Point(1.0f, 1.0f, 0.0f), - Point(-1.0f, 1.0f, 0.0f), - Point(-1.0f, -1.0f, 0.0f), - Point(1.0f, -1.0f, 0.0f), - Point(1.0f, 1.0f, 0.0f), - Point(-1.0f, 1.0f, 0.0f), + Point vertices[8] = { + Point(-1.0f, -1.0f, -1.0f), + Point(1.0f, -1.0f, -1.0f), + Point(1.0f, 1.0f, -1.0f), + Point(-1.0f, 1.0f, -1.0f), + Point(-1.0f, -1.0f, 1.0f), + Point(1.0f, -1.0f, 1.0f), + Point(1.0f, 1.0f, 1.0f), + Point(-1.0f, 1.0f, 1.0f), + }; + int indices[24] = { + 0, 1, + 1, 2, + 2, 3, + 3, 0, + + 4, 5, + 5, 6, + 6, 7, + 7, 4, + + 0, 4, + 1, 5, + 2, 6, + 3, 7, }; std::vector scale_vec; std::vector translation_vec; diff --git a/QT/3d-cube/mywindow.ui b/QT/3d-cube/mywindow.ui index 6db85f6..068061d 100644 --- a/QT/3d-cube/mywindow.ui +++ b/QT/3d-cube/mywindow.ui @@ -6,8 +6,8 @@ 0 0 - 888 - 868 + 1084 + 820 @@ -22,8 +22,8 @@ 10 10 - 600 - 600 + 800 + 800 @@ -39,20 +39,20 @@ - 630 + 820 10 241 - 781 + 751 - Opcje + 90 - 30 + 10 75 23 @@ -65,7 +65,7 @@ 30 - 120 + 90 31 121 @@ -84,7 +84,7 @@ 110 - 120 + 90 31 121 @@ -103,7 +103,7 @@ 30 - 640 + 610 31 121 @@ -122,7 +122,7 @@ 30 - 290 + 260 31 121 @@ -141,7 +141,7 @@ 110 - 290 + 260 31 121 @@ -160,7 +160,7 @@ 110 - 640 + 610 31 121 @@ -179,7 +179,7 @@ 30 - 80 + 50 41 31 @@ -200,7 +200,7 @@ 110 - 80 + 50 41 31 @@ -221,7 +221,7 @@ 30 - 250 + 220 41 31 @@ -242,7 +242,7 @@ 110 - 250 + 220 41 31 @@ -263,7 +263,7 @@ 20 - 600 + 570 61 31 @@ -284,7 +284,7 @@ 100 - 600 + 570 61 31 @@ -305,7 +305,7 @@ 30 - 460 + 430 31 121 @@ -321,7 +321,7 @@ 110 - 460 + 430 31 121 @@ -337,7 +337,7 @@ 190 - 460 + 430 31 121 @@ -353,7 +353,7 @@ 30 - 420 + 390 41 31 @@ -374,7 +374,7 @@ 100 - 420 + 390 41 31 @@ -395,7 +395,7 @@ 180 - 420 + 390 41 31 @@ -416,7 +416,7 @@ 190 - 250 + 220 41 31 @@ -437,7 +437,7 @@ 190 - 290 + 260 31 121 @@ -456,7 +456,7 @@ 190 - 120 + 90 31 121 @@ -475,7 +475,7 @@ 190 - 79 + 49 41 31 @@ -499,19 +499,11 @@ 0 0 - 888 + 1084 22 - - - TopToolBarArea - - - false - -