Zoom about mouse cursor
This commit is contained in:
parent
9283aa4752
commit
54206d3f9c
1 changed files with 13 additions and 1 deletions
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue