From 64da1084a0c9605b10dfe19d4e174c1dec92172c Mon Sep 17 00:00:00 2001 From: papush! Date: Sat, 27 Nov 2021 14:11:18 +0100 Subject: [PATCH] fix view controls --- src/mesh_viewer.cpp | 19 +++++++++++-------- src/mesh_viewer.h | 5 ++--- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/mesh_viewer.cpp b/src/mesh_viewer.cpp index eaebd80..45bb717 100644 --- a/src/mesh_viewer.cpp +++ b/src/mesh_viewer.cpp @@ -9,11 +9,12 @@ MeshViewer::MeshViewer(QWidget *parent) : QOpenGLWidget(parent) { setMouseTracking(true); setFocus(); + updateViewMatrix(); } void MeshViewer::updateViewMatrix() { - view = trans * zoom * rot; + view = zoom * rot * trans; } @@ -121,7 +122,6 @@ void MeshViewer::updateForReal() { void MeshViewer::mousePressEvent(QMouseEvent *e) { if (e->button() & Qt::MiddleButton || e->button() & Qt::RightButton) { mouse_pos = e->pos(); - trans_start = trans; } else if (e->button() == Qt::LeftButton) { float x = e->x(); float y = height() - e->y(); @@ -141,23 +141,26 @@ void MeshViewer::mousePressEvent(QMouseEvent *e) { void MeshViewer::mouseReleaseEvent(QMouseEvent *e) { (void) e; - rot_start = rot; } void MeshViewer::mouseMoveEvent(QMouseEvent *e) { + QPoint delta = e->pos() - mouse_pos; + mouse_pos = e->pos(); if (e->buttons() & Qt::MiddleButton) { - QPoint delta = e->pos() - mouse_pos; - rot = rot_start; rot.rotate(delta.x() / 5., 0, 1, 0); rot.rotate(delta.y() / 5., QVector3D(1, 0, 0) * rot); updateViewMatrix(); update(); } if (e->buttons() & Qt::RightButton) { - trans = trans_start; - trans(0, 3) += (e->pos() - mouse_pos).x() / 1000.; - trans(1, 3) -= (e->pos() - mouse_pos).y() / 1000.; + QMatrix4x4 screen_trans; + screen_trans.translate(delta.x() / 1000., -delta.y() / 1000., 0); + trans = rot.inverted() * screen_trans * rot * trans; + // trans(0, 0) = 1; + // trans(0, 1) = 0; + // trans(1, 0) = 0; + // trans(1, 1) = 1; updateViewMatrix(); update(); } diff --git a/src/mesh_viewer.h b/src/mesh_viewer.h index e626b78..47e044e 100644 --- a/src/mesh_viewer.h +++ b/src/mesh_viewer.h @@ -31,9 +31,8 @@ class MeshViewer : public QOpenGLWidget { 0, 1, 0, 0, 0, 0, 1, -1, 0, 0, 0, 1); - QMatrix4x4 trans, trans_start; - QMatrix4x4 rot, rot_start; - QMatrix4x4 view = trans * zoom * rot; + QMatrix4x4 rot, trans; + QMatrix4x4 view; QPoint mouse_pos; void updateViewMatrix(); bool is_initialized = false;