From b3d657d1dbb9ced4db3d3d408cefd1032f610dbd Mon Sep 17 00:00:00 2001 From: denizdiktas Date: Fri, 16 Jun 2023 10:40:53 +0300 Subject: [PATCH] intermediate camera fix (initial axes are not correct, this needs to be fixed!) --- .../demo/earth/Camera.cpp | 25 ++++++++++++++++--- Arrangement_on_surface_2/demo/earth/Camera.h | 6 ++++- .../demo/earth/mainwidget.cpp | 14 +++++++---- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/Arrangement_on_surface_2/demo/earth/Camera.cpp b/Arrangement_on_surface_2/demo/earth/Camera.cpp index 10c93125209..467d85dd5cd 100644 --- a/Arrangement_on_surface_2/demo/earth/Camera.cpp +++ b/Arrangement_on_surface_2/demo/earth/Camera.cpp @@ -57,10 +57,29 @@ void Camera::rotate_around_y(float theta) m_ux = ux.toVector3D(); m_uz = uz.toVector3D(); } -void Camera::rotate(float theta_around_x, float theta_around_y) +//void Camera::rotate(float theta_around_x, float theta_around_y) +//{ +// rotate_around_x(theta_around_x); +// rotate_around_y(theta_around_y); +//} +void Camera::rotate(float theta, float phi) { - rotate_around_x(theta_around_x); - rotate_around_y(theta_around_y); + QMatrix4x4 r1; + QVector3D ey(0, 1, 0); + r1.rotate(theta, ey); + + // rx = rotated x axis + auto rx = r1 * QVector3D(1,0,0); + QMatrix4x4 r2; + r2.rotate(phi, rx); + + // total rotation: + auto r = r2 * r1; + + m_pos = r * QVector3D(0, 0, 3); + m_ux = r * QVector3D(1, 0, 0); // should be the same as rx (sanity check?) + m_uy = r * QVector3D(0, 1, 0); + m_uz = r * QVector3D(0, 0, 1); } void Camera::move_forward(float distance) diff --git a/Arrangement_on_surface_2/demo/earth/Camera.h b/Arrangement_on_surface_2/demo/earth/Camera.h index 458798cde65..88f8a03480c 100644 --- a/Arrangement_on_surface_2/demo/earth/Camera.h +++ b/Arrangement_on_surface_2/demo/earth/Camera.h @@ -25,7 +25,11 @@ public: // rotate the camera around its own axes void rotate_around_x(float theta); void rotate_around_y(float theta); - void rotate(float theta_around_x, float theta_around_y); + //void rotate(float theta_around_x, float theta_around_y); + + // theta: angle around y-axis + // phi: angle from the xz-plane (= rotated x-axis after the above rotation) + void rotate(float theta, float phi); // move the camera forward around its own z-axis void move_forward(float distance); diff --git a/Arrangement_on_surface_2/demo/earth/mainwidget.cpp b/Arrangement_on_surface_2/demo/earth/mainwidget.cpp index 81887630c61..0f4c7f00317 100644 --- a/Arrangement_on_surface_2/demo/earth/mainwidget.cpp +++ b/Arrangement_on_surface_2/demo/earth/mainwidget.cpp @@ -16,6 +16,8 @@ MainWidget::~MainWidget() } +float theta = 0, phi = 0; + void MainWidget::set_mouse_button_pressed_flag(QMouseEvent* e, bool flag) { switch (e->button()) @@ -42,9 +44,12 @@ void MainWidget::mouseMoveEvent(QMouseEvent* e) if (m_left_mouse_button_down) { 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); + //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); + theta += rotation_scale_factor * diff.x(); + phi += rotation_scale_factor * diff.y(); + m_camera.rotate(-theta, -phi); } else if(m_middle_mouse_button_down) { @@ -92,7 +97,7 @@ void MainWidget::initializeGL() void MainWidget::init_camera() { m_camera.set_pos(0, 0, 3); - m_camera.rotate_around_x(-90); + //m_camera.rotate_around_x(-90); } void MainWidget::init_geometry() { @@ -118,7 +123,6 @@ void MainWidget::init_sp_smooth() const char* vs = "shaders/smooth_vs.glsl"; const char* fs = "shaders/smooth_fs.glsl"; m_sp_smooth.init(vs, "", fs); - } void MainWidget::init_sp_per_vertex_color() {