From de4346d6d428ac902ebe081f57319ecb220bc343 Mon Sep 17 00:00:00 2001 From: Liangliang Nan Date: Sun, 20 Feb 2022 11:42:30 +0100 Subject: [PATCH] mouse position relative to the current view --- easy3d/viewer/comp_viewer.cpp | 58 ++++++++++++++++++++++++++++++----- easy3d/viewer/comp_viewer.h | 6 ++++ 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/easy3d/viewer/comp_viewer.cpp b/easy3d/viewer/comp_viewer.cpp index 8e03fb3b2..203adedab 100644 --- a/easy3d/viewer/comp_viewer.cpp +++ b/easy3d/viewer/comp_viewer.cpp @@ -36,6 +36,17 @@ #include #include #include +#include + +#include <3rd_party/glfw/include/GLFW/glfw3.h> // Include glfw3.h after our OpenGL definitions + +// To have the same shortcut behavior on macOS and other platforms (i.e., Windows, Linux) +#ifdef __APPLE__ +#define EASY3D_MOD_CONTROL GLFW_MOD_SUPER +#else +#define EASY3D_MOD_CONTROL GLFW_MOD_CONTROL +#endif + namespace easy3d { @@ -53,6 +64,10 @@ namespace easy3d { views_.resize(num_rows_); for (auto &row: views_) row.resize(num_cols_); + + // initialized to window size + view_width_ = width_; + view_height_ = height_; } @@ -216,16 +231,16 @@ namespace easy3d { glGetIntegerv(GL_VIEWPORT, viewport); const int w = viewport[2]; const int h = viewport[3]; - const int size_x = int(w / float(num_cols_)); - const int size_y = int(h / float(num_rows_)); + view_width_ = int(w / float(num_cols_)); + view_height_ = int(h / float(num_rows_)); // This is required to ensure a correct aspect ratio (thus the correct projection matrix) - camera()->setScreenWidthAndHeight(size_x, size_y); + camera()->setScreenWidthAndHeight(view_width_, view_height_); for (std::size_t i = 0; i < num_rows_; ++i) { auto &row = views_[i]; - const float y = h - (i + 1) * size_y; + const float y = h - (i + 1) * view_height_; for (std::size_t j = 0; j < num_cols_; ++j) - row[j].viewport = ivec4(j * size_x, y, size_x, size_y); + row[j].viewport = ivec4(j * view_width_, y, view_width_, view_height_); } // ------------------------------------------------------------ @@ -233,12 +248,12 @@ namespace easy3d { // Note: we need NDC std::vector points; for (std::size_t i = 1; i < num_rows_; ++i) { - const float y = 2.0f * i * size_y / h - 1.0f; + const float y = 2.0f * i * view_height_ / h - 1.0f; points.emplace_back(vec2(-1.0f, y)); points.emplace_back(vec2(1.0f, y)); } for (std::size_t i = 1; i < num_cols_; ++i) { - const float x = 2.0f * i * size_x / w - 1.0f; + const float x = 2.0f * i * view_width_ / w - 1.0f; points.emplace_back(vec2(x, -1.0f)); points.emplace_back(vec2(x, 1.0f)); } @@ -247,4 +262,33 @@ namespace easy3d { easy3d_debug_log_gl_error; } + + bool CompViewer::mouse_drag_event(int x, int y, int dx, int dy, int button, int modifiers) { + // make the mouse position relative to the current view + x %= view_width_; + y %= view_height_; + + // control modifier is reserved for zooming on region + if (modifiers != EASY3D_MOD_CONTROL) { + auto axis = ManipulatedFrame::NONE; + if (pressed_key_ == GLFW_KEY_X) axis = ManipulatedFrame::HORIZONTAL; + else if (pressed_key_ == GLFW_KEY_Y) axis = ManipulatedFrame::VERTICAL; + else if (pressed_key_ == GLFW_KEY_O) axis = ManipulatedFrame::ORTHOGONAL; + switch (button) { + case GLFW_MOUSE_BUTTON_LEFT: + camera_->frame()->action_rotate(x, y, dx, dy, camera_, axis); + break; + case GLFW_MOUSE_BUTTON_RIGHT: + camera_->frame()->action_translate(x, y, dx, dy, camera_, axis); + break; + case GLFW_MOUSE_BUTTON_MIDDLE: + if (std::abs(dy) >= 1) + camera_->frame()->action_zoom(dy > 0 ? 1 : -1, camera_); + break; + } + } + + return false; + } + } \ No newline at end of file diff --git a/easy3d/viewer/comp_viewer.h b/easy3d/viewer/comp_viewer.h index 16e06e328..7d646bfbf 100644 --- a/easy3d/viewer/comp_viewer.h +++ b/easy3d/viewer/comp_viewer.h @@ -79,6 +79,9 @@ namespace easy3d { void cleanup() override; void draw() const override; + // overloaded, so mouse positions are relative to the current view + bool mouse_drag_event(int x, int y, int dx, int dy, int button, int modifiers) override; + void draw_division() const; void update_division(); @@ -96,6 +99,9 @@ namespace easy3d { ShaderProgram *lines_program_; unsigned int division_vertex_buffer_; bool division_visible_; + + int view_width_; // the width of the views + int view_height_; // the height of the views }; }