OSG QT: get the coordinates of the mouse world

Most of the online code like this:

1         auto matViewMatrix = camera->getViewMatrix();
2         auto matProjectionMatrix = camera->getProjectionMatrix();
3         auto wndMatrix = camera->getViewport()->computeWindowMatrix();
4         osg::Matrix MVPW = matViewMatrix * matProjectionMatrix * wndMatrix;
5         osg::Matrix inverseMVPW = osg::Matrix::inverse(MVPW);
6         osg::Vec3 mouseWorld = osg::Vec3(x,y, 0) * inverseMVPW;

During development and QT combination and found that the location is wrong, because it is the origin of the coordinates of the mouse get the top left corner, and the OSG origin of the world coordinate system is the bottom left corner, so should the value of Y mouse do the conversion.

Something like this:

 1 void GraphicsWinQt::mouseMoveEvent( QMouseEvent *event )
 2 {
 3     setKeyboardModifiers( event );
 4     this->getEventQueue()->mouseMotion(event->x(), event->y());
 5     if(nullptr != m_pMainWnd)
 6     {
 7         auto x = event->x();
 8         auto y = event->y();
 9         int iWindowHeight = this->height();
10         //坐标值转换
11         y = iWindowHeight - y;
12         int z = 0;
13         osg::Camera* camera = m_pViewer->getCamera();
14 
15         auto matViewMatrix = camera->getViewMatrix();
16         auto matProjectionMatrix = camera->getProjectionMatrix();
17         auto wndMatrix = camera->getViewport()->computeWindowMatrix();
18         osg::Matrix MVPW = matViewMatrix * matProjectionMatrix * wndMatrix;
19         osg::Matrix inverseMVPW = osg::Matrix::inverse(MVPW);
20         osg::Vec3 mouseWorld = osg::Vec3(x,y, 0) * inverseMVPW;
21 
22         auto s = QStringLiteral("当前位置:");
23         s += QString("%1,%2,%3").arg(mouseWorld.x()).arg(mouseWorld.y()).arg(mouseWorld.z());
24         auto pEvent = MessageEvent::CreateInstance(1);
25         pEvent->m_sMsg = s;
26         qApp->postEvent(m_pMainWnd, pEvent);
27     }
28 }

OSG is another vector so that the left row vector multiply

Guess you like

Origin www.cnblogs.com/bingbingzhe/p/12185372.html