]> git.sur5r.net Git - fstl/commitdiff
Zoom about mouse cursor
authorMatt Keeter <matt.j.keeter@gmail.com>
Mon, 24 Mar 2014 23:32:12 +0000 (19:32 -0400)
committerMatt Keeter <matt.j.keeter@gmail.com>
Mon, 24 Mar 2014 23:32:12 +0000 (19:32 -0400)
src/canvas.cpp

index 1a1e06077a69cea7ff3c9c2292de06f6a3807732..15624dac5eb786d85f1aefc1825a8e3ff0c2683b 100644 (file)
@@ -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();
 }