From 2ee383241c08a1dadc0f22b6152b441a39775d45 Mon Sep 17 00:00:00 2001 From: denizdiktas Date: Tue, 13 Jun 2023 12:06:20 +0300 Subject: [PATCH] Added: camera-zoom --- .../demo/earth/Camera.cpp | 7 ++++ Arrangement_on_surface_2/demo/earth/Camera.h | 3 ++ .../demo/earth/mainwidget.cpp | 42 +++++++++++++++---- .../demo/earth/mainwidget.h | 4 +- 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/Arrangement_on_surface_2/demo/earth/Camera.cpp b/Arrangement_on_surface_2/demo/earth/Camera.cpp index e6112160fd5..d6fe8b8c552 100644 --- a/Arrangement_on_surface_2/demo/earth/Camera.cpp +++ b/Arrangement_on_surface_2/demo/earth/Camera.cpp @@ -41,3 +41,10 @@ void Camera::rotate(float theta_around_x, float theta_around_y) m_ux = m_ux * rot; m_uz = m_uz * rot; } + +void Camera::move_forward(float distance) +{ + // recall that in OpenGL camera model, camera's z-axis points always + // out of the screen (towards the user). + m_pos -= distance * m_uz; +} diff --git a/Arrangement_on_surface_2/demo/earth/Camera.h b/Arrangement_on_surface_2/demo/earth/Camera.h index 532d8b83a22..4deb0b9a64a 100644 --- a/Arrangement_on_surface_2/demo/earth/Camera.h +++ b/Arrangement_on_surface_2/demo/earth/Camera.h @@ -24,6 +24,9 @@ class Camera // rotate the camera around its own axes void rotate(float theta_around_x, float theta_around_y); + // move the camera forward around its own z-axis + void move_forward(float distance); + private: QVector3D m_pos; QVector3D m_ux; diff --git a/Arrangement_on_surface_2/demo/earth/mainwidget.cpp b/Arrangement_on_surface_2/demo/earth/mainwidget.cpp index 6da96304121..9b789e5f800 100644 --- a/Arrangement_on_surface_2/demo/earth/mainwidget.cpp +++ b/Arrangement_on_surface_2/demo/earth/mainwidget.cpp @@ -19,29 +19,57 @@ MainWidget::~MainWidget() void MainWidget::mousePressEvent(QMouseEvent *e) { - m_mouse_pressed = true; + switch (e->button()) + { + case Qt::LeftButton: + m_left_mouse_button_down = true; + break; + + case Qt::MiddleButton: + m_middle_mouse_button_down = true; + break; + } m_last_mouse_pos = QVector2D(e->position()); } void MainWidget::mouseMoveEvent(QMouseEvent* e) { auto current_mouse_pos = QVector2D(e->position()); + const auto diff = current_mouse_pos - m_last_mouse_pos; - if (m_mouse_pressed) + if (m_left_mouse_button_down) { - const auto diff = current_mouse_pos - m_last_mouse_pos; - const float scale_factor = 0.1f; - const float theta_around_x = scale_factor * diff.y(); - const float theta_around_y = scale_factor * diff.x(); + const float rotation_scale_factor = 0.1f; + const float theta_around_x = rotation_scale_factor * diff.y(); + const float theta_around_y = rotation_scale_factor * diff.x(); m_camera.rotate(theta_around_x, theta_around_y); } + else + { + const float zoom_scale_factor = 0.01f; + const auto distance = zoom_scale_factor * diff.y(); + m_camera.move_forward(distance); + } m_last_mouse_pos = current_mouse_pos; } +void MainWidget::wheelEvent(QWheelEvent* e) +{ + auto distance = e->angleDelta(); + m_camera.move_forward(distance.x()); +} void MainWidget::mouseReleaseEvent(QMouseEvent *e) { - m_mouse_pressed = false; + switch (e->button()) + { + case Qt::LeftButton: + m_left_mouse_button_down = false; + break; + case Qt::MiddleButton: + m_middle_mouse_button_down = false; + break; + } } void MainWidget::timerEvent(QTimerEvent *) { diff --git a/Arrangement_on_surface_2/demo/earth/mainwidget.h b/Arrangement_on_surface_2/demo/earth/mainwidget.h index 6cb8b23d1c4..86c3d7334b3 100644 --- a/Arrangement_on_surface_2/demo/earth/mainwidget.h +++ b/Arrangement_on_surface_2/demo/earth/mainwidget.h @@ -31,6 +31,7 @@ class MainWidget : public QOpenGLWidget, protected OpenGLFunctionsBase protected: void mousePressEvent(QMouseEvent *e) override; void mouseMoveEvent(QMouseEvent* e) override; + void wheelEvent(QWheelEvent* event) override; void mouseReleaseEvent(QMouseEvent *e) override; void timerEvent(QTimerEvent *e) override; @@ -54,7 +55,8 @@ class MainWidget : public QOpenGLWidget, protected OpenGLFunctionsBase // camera & controls Camera m_camera; - bool m_mouse_pressed = false; + bool m_left_mouse_button_down = false; + bool m_middle_mouse_button_down = false; QVector2D m_last_mouse_pos;