From: Matt Keeter Date: Mon, 24 Mar 2014 23:32:12 +0000 (-0400) Subject: Zoom about mouse cursor X-Git-Tag: v0.9.0~4^2~1 X-Git-Url: https://git.sur5r.net/?p=fstl;a=commitdiff_plain;h=54206d3f9caa44f91c51547cd1edbfc4091cc908 Zoom about mouse cursor --- diff --git a/src/canvas.cpp b/src/canvas.cpp index 1a1e060..15624da 100644 --- a/src/canvas.cpp +++ b/src/canvas.cpp @@ -161,7 +161,6 @@ void Canvas::mouseMoveEvent(QMouseEvent* event) } else if (event->buttons() & Qt::RightButton) { - qDebug() << d; center = transform_matrix().inverted() * view_matrix().inverted() * QVector3D(-d.x() / (0.5*width()), @@ -173,6 +172,14 @@ void Canvas::mouseMoveEvent(QMouseEvent* event) void Canvas::wheelEvent(QWheelEvent *event) { + // Find GL position before the zoom operation + // (to zoom about mouse cursor) + auto p = event->pos(); + QVector3D v(1 - p.x() / (0.5*width()), + p.y() / (0.5*height()) - 1, 0); + QVector3D a = transform_matrix().inverted() * + view_matrix().inverted() * v; + if (event->delta() < 0) { for (int i=0; i > event->delta(); --i) @@ -183,5 +190,10 @@ void Canvas::wheelEvent(QWheelEvent *event) for (int i=0; i < event->delta(); ++i) zoom /= 1.001; } + + // Then find the cursor's GL position post-zoom and adjust center. + QVector3D b = transform_matrix().inverted() * + view_matrix().inverted() * v; + center += b - a; update(); }