fix view controls

This commit is contained in:
papush! 2021-11-27 14:11:18 +01:00
parent 61082c8331
commit 64da1084a0
2 changed files with 13 additions and 11 deletions

View File

@ -9,11 +9,12 @@
MeshViewer::MeshViewer(QWidget *parent) : QOpenGLWidget(parent) { MeshViewer::MeshViewer(QWidget *parent) : QOpenGLWidget(parent) {
setMouseTracking(true); setMouseTracking(true);
setFocus(); setFocus();
updateViewMatrix();
} }
void MeshViewer::updateViewMatrix() { void MeshViewer::updateViewMatrix() {
view = trans * zoom * rot; view = zoom * rot * trans;
} }
@ -121,7 +122,6 @@ void MeshViewer::updateForReal() {
void MeshViewer::mousePressEvent(QMouseEvent *e) { void MeshViewer::mousePressEvent(QMouseEvent *e) {
if (e->button() & Qt::MiddleButton || e->button() & Qt::RightButton) { if (e->button() & Qt::MiddleButton || e->button() & Qt::RightButton) {
mouse_pos = e->pos(); mouse_pos = e->pos();
trans_start = trans;
} else if (e->button() == Qt::LeftButton) { } else if (e->button() == Qt::LeftButton) {
float x = e->x(); float x = e->x();
float y = height() - e->y(); float y = height() - e->y();
@ -141,23 +141,26 @@ void MeshViewer::mousePressEvent(QMouseEvent *e) {
void MeshViewer::mouseReleaseEvent(QMouseEvent *e) { void MeshViewer::mouseReleaseEvent(QMouseEvent *e) {
(void) e; (void) e;
rot_start = rot;
} }
void MeshViewer::mouseMoveEvent(QMouseEvent *e) { void MeshViewer::mouseMoveEvent(QMouseEvent *e) {
if (e->buttons() & Qt::MiddleButton) {
QPoint delta = e->pos() - mouse_pos; QPoint delta = e->pos() - mouse_pos;
rot = rot_start; mouse_pos = e->pos();
if (e->buttons() & Qt::MiddleButton) {
rot.rotate(delta.x() / 5., 0, 1, 0); rot.rotate(delta.x() / 5., 0, 1, 0);
rot.rotate(delta.y() / 5., QVector3D(1, 0, 0) * rot); rot.rotate(delta.y() / 5., QVector3D(1, 0, 0) * rot);
updateViewMatrix(); updateViewMatrix();
update(); update();
} }
if (e->buttons() & Qt::RightButton) { if (e->buttons() & Qt::RightButton) {
trans = trans_start; QMatrix4x4 screen_trans;
trans(0, 3) += (e->pos() - mouse_pos).x() / 1000.; screen_trans.translate(delta.x() / 1000., -delta.y() / 1000., 0);
trans(1, 3) -= (e->pos() - mouse_pos).y() / 1000.; trans = rot.inverted() * screen_trans * rot * trans;
// trans(0, 0) = 1;
// trans(0, 1) = 0;
// trans(1, 0) = 0;
// trans(1, 1) = 1;
updateViewMatrix(); updateViewMatrix();
update(); update();
} }

View File

@ -31,9 +31,8 @@ class MeshViewer : public QOpenGLWidget {
0, 1, 0, 0, 0, 1, 0, 0,
0, 0, 1, -1, 0, 0, 1, -1,
0, 0, 0, 1); 0, 0, 0, 1);
QMatrix4x4 trans, trans_start; QMatrix4x4 rot, trans;
QMatrix4x4 rot, rot_start; QMatrix4x4 view;
QMatrix4x4 view = trans * zoom * rot;
QPoint mouse_pos; QPoint mouse_pos;
void updateViewMatrix(); void updateViewMatrix();
bool is_initialized = false; bool is_initialized = false;