From 9a5bfa00bef5ea0149ef3e5456114e6e1b40de61 Mon Sep 17 00:00:00 2001 From: denizdiktas Date: Wed, 14 Jun 2023 10:24:08 +0300 Subject: [PATCH] Corrected initial camera position such that z-axis points to up, x-axis to right, y-axis into the screen --- .../demo/earth/Camera.cpp | 19 ++++++++++++------- Arrangement_on_surface_2/demo/earth/Camera.h | 2 ++ .../demo/earth/mainwidget.cpp | 12 ++++-------- .../demo/earth/mainwidget.h | 10 +++++++--- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/Arrangement_on_surface_2/demo/earth/Camera.cpp b/Arrangement_on_surface_2/demo/earth/Camera.cpp index d6fe8b8c552..e52004a3577 100644 --- a/Arrangement_on_surface_2/demo/earth/Camera.cpp +++ b/Arrangement_on_surface_2/demo/earth/Camera.cpp @@ -25,22 +25,27 @@ QMatrix4x4 Camera::get_view_matrix() const } -void Camera::rotate(float theta_around_x, float theta_around_y) +void Camera::rotate_around_x(float theta) { - // rotate the camera around its x-axis QMatrix4x4 rot; - rot.rotate(theta_around_x, m_ux); + rot.rotate(theta, m_ux); m_pos = m_pos * rot; m_uy = m_uy * rot; m_uz = m_uz * rot; - - // rotate the camera around its y-axis - rot.setToIdentity(); - rot.rotate(theta_around_y, m_uy); +} +void Camera::rotate_around_y(float theta) +{ + QMatrix4x4 rot; + rot.rotate(theta, m_uy); m_pos = m_pos * rot; m_ux = m_ux * rot; m_uz = m_uz * rot; } +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::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 4deb0b9a64a..1daedcea3ba 100644 --- a/Arrangement_on_surface_2/demo/earth/Camera.h +++ b/Arrangement_on_surface_2/demo/earth/Camera.h @@ -22,6 +22,8 @@ public: QMatrix4x4 get_projection_matrix() const { return m_projection; } // 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); // move the camera forward around its own z-axis diff --git a/Arrangement_on_surface_2/demo/earth/mainwidget.cpp b/Arrangement_on_surface_2/demo/earth/mainwidget.cpp index d2a15a0605e..7b0fb64fbb9 100644 --- a/Arrangement_on_surface_2/demo/earth/mainwidget.cpp +++ b/Arrangement_on_surface_2/demo/earth/mainwidget.cpp @@ -84,26 +84,22 @@ void MainWidget::initializeGL() - -#include "World_coordinate_axes.h" -std::unique_ptr m_world_coord_axes; - - void MainWidget::init_camera() { - m_camera.set_pos(0, 0, 10); + m_camera.set_pos(0, 0, 3); + m_camera.rotate_around_x(-90); } void MainWidget::init_geometry() { int num_slices, num_stacks; num_slices = num_stacks = 64; - float r = 3; + float r = 1; m_sphere = std::make_unique(num_slices, num_stacks, r); const float c = 0.8; m_sphere->set_color(c, c, c, 1); - m_world_coord_axes = std::make_unique(5); + m_world_coord_axes = std::make_unique(2); } void MainWidget::init_shader_programs() { diff --git a/Arrangement_on_surface_2/demo/earth/mainwidget.h b/Arrangement_on_surface_2/demo/earth/mainwidget.h index 98c57db7a27..0354a34365a 100644 --- a/Arrangement_on_surface_2/demo/earth/mainwidget.h +++ b/Arrangement_on_surface_2/demo/earth/mainwidget.h @@ -18,6 +18,7 @@ #include "Common_defs.h" #include "Shader_program.h" #include "Sphere.h" +#include "World_coordinate_axes.h" class MainWidget : public QOpenGLWidget, protected OpenGLFunctionsBase @@ -50,18 +51,21 @@ protected: void init_shader_programs(); private: - std::unique_ptr m_sphere; + // Objects in the scene + std::unique_ptr m_sphere; + std::unique_ptr m_world_coord_axes; + // Shaders Shader_program m_sp_smooth; Shader_program m_sp_color_only; - // camera & controls + // Camera & controls Camera m_camera; bool m_left_mouse_button_down = false; bool m_middle_mouse_button_down = false; QVector2D m_last_mouse_pos; - + // Timer for continuous screen-updates QBasicTimer m_timer; };