diff --git a/QT/3d-cube/mywindow.cpp b/QT/3d-cube/mywindow.cpp index 6a27aab..39ee21a 100644 --- a/QT/3d-cube/mywindow.cpp +++ b/QT/3d-cube/mywindow.cpp @@ -22,13 +22,13 @@ const int VIEW_SIZE = 4.0f; std::vector matrixMul(std::vector p, std::vector m) { - std::vector res(3); - for(int i = 0; i < 3; i++) + std::vector res(4); + for(int i = 0; i < 4; i++) { res.push_back(0); - for(int j = 0; j < 3; j++) + for(int j = 0; j < 4; j++) { - res[i] += m[i * 3 + j] * p[j]; + res[i] += m[i * 4 + j] * p[j]; } } return res; @@ -107,6 +107,11 @@ MyWindow::MyWindow(QWidget *parent) : translation_vec.push_back(0); translation_vec.push_back(0); + translation_vec.push_back(0); + + rotation_vec.push_back(0); + rotation_vec.push_back(0); + rotation_vec.push_back(0); // points[0] = Point(-1.0f, -1.0f, 0.0f); // points[1] = Point(1.0f, -1.0f, 0.0f); @@ -119,6 +124,7 @@ MyWindow::MyWindow(QWidget *parent) : // points[7] = Point(-1.0f, 1.0f, 0.0f); + scale_vec.push_back(1.0f); scale_vec.push_back(1.0f); scale_vec.push_back(1.0f); @@ -155,8 +161,20 @@ void MyWindow::on_slider_ty_valueChanged(int val){ translation_vec[1] = val / 100.0f - 1.0f; } -void MyWindow::on_slider_a_valueChanged(int val){ - rotation_angle = val; +void MyWindow::on_slider_tz_valueChanged(int val){ + translation_vec[2] = val / 100.0f - 1.0f; +} + +void MyWindow::on_slider_rx_valueChanged(int val){ + rotation_vec[0] = val; +} + +void MyWindow::on_slider_ry_valueChanged(int val){ + rotation_vec[1] = val; +} + +void MyWindow::on_slider_rz_valueChanged(int val){ + rotation_vec[2] = val; } void MyWindow::on_slider_sx_valueChanged(int val){ @@ -167,6 +185,10 @@ void MyWindow::on_slider_sy_valueChanged(int val){ scale_vec[1] = 1.0f / ( val / 100.0f ); } +void MyWindow::on_slider_sz_valueChanged(int val){ + scale_vec[2] = 1.0f / ( val / 100.0f ); +} + void MyWindow::on_slider_shx_valueChanged(int val){ sh_vec[0] = val / 100.0f - 1.0f; } @@ -176,23 +198,25 @@ void MyWindow::on_slider_shy_valueChanged(int val){ } void MyWindow::UpdateImage(){ - float sina = sin(rotation_angle * 0.01745329252f); - float cosa = cos(rotation_angle * 0.01745329252f); + float sina; + float cosa; std::vector translate_mat = { 1, 0, 0, translation_vec[0], 0, 1, 0, translation_vec[1], - 0, 0, 1, 0, + 0, 0, 1, translation_vec[2], 0, 0, 0, 1, }; std::vector scale_mat = { scale_vec[0], 0, 0, 0, 0, scale_vec[1], 0, 0, - 0, 0, scale_vec[1], 0, + 0, 0, scale_vec[2], 0, 0, 0, 0, 1, }; + sina = sin(rotation_vec[0] * 0.01745329252f); + cosa = cos(rotation_vec[0] * 0.01745329252f); std::vector rotation_mat_x = { 1, 0, 0, 0, 0, cosa, -sina, 0, @@ -200,6 +224,8 @@ void MyWindow::UpdateImage(){ 0, 0, 0, 1 }; + sina = sin(rotation_vec[1] * 0.01745329252f); + cosa = cos(rotation_vec[1] * 0.01745329252f); std::vector rotation_mat_y = { cosa, 0, sina, 0, 0, 1, 0, 0, @@ -207,6 +233,8 @@ void MyWindow::UpdateImage(){ 0, 0, 0, 1 }; + sina = sin(rotation_vec[2] * 0.01745329252f); + cosa = cos(rotation_vec[2] * 0.01745329252f); std::vector rotation_mat_z = { cosa, -sina, 0, 0, sina, cosa, 0, 0, @@ -234,11 +262,15 @@ void MyWindow::UpdateImage(){ sh_mat), translate_mat); + ClearImage(img); + for(int i = 1; i < 8; i++){ Point P1 = points[i-1]; Point P2 = points[i]; - std::vector p1_vec = GetXY(P1, std::vector{}); - std::vector p2_vec = GetXY(P2, std::vector{}); + Point P1M = Point(matrixMul(P1.GetVector(), tansform_mat)); + Point P2M = Point(matrixMul(P2.GetVector(), tansform_mat)); + std::vector p1_vec = GetXY(P1M, std::vector{}); + std::vector p2_vec = GetXY(P2M, std::vector{}); DrawLine(p1_vec[0], p1_vec[1], p2_vec[0], p2_vec[1], img); } diff --git a/QT/3d-cube/mywindow.h b/QT/3d-cube/mywindow.h index 1221b0d..b15fc2c 100644 --- a/QT/3d-cube/mywindow.h +++ b/QT/3d-cube/mywindow.h @@ -40,6 +40,16 @@ struct Point{ this->z=z; } + Point(std::vector vec){ + this->x=vec[0]; + this->y=vec[1]; + this->z=vec[2]; + } + + std::vector GetVector(){ + return std::vector{x, y, z, 1}; + } + float length(){ float mag = std::sqrt(std::pow(x, 2) + std::pow(y, 2) + std::pow(z, 2)); return mag; @@ -127,6 +137,7 @@ private: }; std::vector scale_vec; std::vector translation_vec; + std::vector rotation_vec; std::vector sh_vec; int tmp_point_id = 0; @@ -164,11 +175,15 @@ private slots: void paintEvent(QPaintEvent*); void on_slider_tx_valueChanged(int val); void on_slider_ty_valueChanged(int val); + void on_slider_tz_valueChanged(int val); void on_slider_sx_valueChanged(int val); void on_slider_sy_valueChanged(int val); + void on_slider_sz_valueChanged(int val); void on_slider_shx_valueChanged(int val); void on_slider_shy_valueChanged(int val); - void on_slider_a_valueChanged(int val); + void on_slider_rx_valueChanged(int val); + void on_slider_ry_valueChanged(int val); + void on_slider_rz_valueChanged(int val); }; #endif // MYWINDOW_H diff --git a/QT/3d-cube/mywindow.ui b/QT/3d-cube/mywindow.ui index 5f5d511..6db85f6 100644 --- a/QT/3d-cube/mywindow.ui +++ b/QT/3d-cube/mywindow.ui @@ -6,8 +6,8 @@ 0 0 - 883 - 583 + 888 + 868 @@ -42,7 +42,7 @@ 630 10 241 - 421 + 781 @@ -102,8 +102,8 @@ - 190 - 120 + 30 + 640 31 121 @@ -159,8 +159,8 @@ - 190 - 290 + 110 + 640 31 121 @@ -217,27 +217,6 @@ Qt::AlignCenter - - - - 190 - 80 - 41 - 31 - - - - - 25 - - - - SHX - - - Qt::AlignCenter - - @@ -280,12 +259,33 @@ Qt::AlignCenter - + - 190 - 250 - 41 + 20 + 600 + 61 + 31 + + + + + 25 + + + + SHX + + + Qt::AlignCenter + + + + + + 100 + 600 + 61 31 @@ -301,22 +301,197 @@ Qt::AlignCenter - - - - - 660 - 450 - 191 - 31 - - - - 100 - - - Qt::Horizontal - + + + + 30 + 460 + 31 + 121 + + + + 100 + + + Qt::Vertical + + + + + + 110 + 460 + 31 + 121 + + + + 100 + + + Qt::Vertical + + + + + + 190 + 460 + 31 + 121 + + + + 100 + + + Qt::Vertical + + + + + + 30 + 420 + 41 + 31 + + + + + 25 + + + + RX + + + Qt::AlignCenter + + + + + + 100 + 420 + 41 + 31 + + + + + 25 + + + + RY + + + Qt::AlignCenter + + + + + + 180 + 420 + 41 + 31 + + + + + 25 + + + + RZ + + + Qt::AlignCenter + + + + + + 190 + 250 + 41 + 31 + + + + + 25 + + + + SZ + + + Qt::AlignCenter + + + + + + 190 + 290 + 31 + 121 + + + + 200 + + + 100 + + + Qt::Vertical + + + + + + 190 + 120 + 31 + 121 + + + + 200 + + + 100 + + + Qt::Vertical + + + + + + 190 + 79 + 41 + 31 + + + + + 25 + + + + TZ + + + Qt::AlignCenter + + @@ -324,8 +499,8 @@ 0 0 - 883 - 24 + 888 + 22