commit 54206d3f9caa44f91c51547cd1edbfc4091cc908
parent 9283aa4752f20951dbb82975d209ee147eea0ec1
Author: Matt Keeter <matt.j.keeter@gmail.com>
Date: Mon, 24 Mar 2014 19:32:12 -0400
Zoom about mouse cursor
Diffstat:
1 file changed, 13 insertions(+), 1 deletion(-)
diff --git 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();
}