如何在 Openscenegraph 中从 2D 鼠标单击屏幕坐标点计算 3D 点(世界坐标)?

2024-01-06

我试图在 2D 屏幕空间上用户选择的点的 3D 空间上放置一个球体。为此,我尝试使用以下技术从 2d 点计算 3d 点,但该技术没有给出正确的解决方案。

mousePosition.x = ((clickPos.clientX - window.left) / control.width) * 2 - 1;
    mousePosition.y = -((clickPos.clientY - window.top) / control.height) * 2 + 1;

然后我乘以mousePositionwith Inverse of MVP matrix。但在结果中得到随机数。

用于计算 MVP 矩阵:

 osg::Matrix mvp =   _camera->getViewMatrix() * _camera->getProjectionMatrix();

我该如何继续?谢谢。


假设鼠标位置在 x 和 y 的 [-1, 1] 范围内标准化,以下代码将为您提供从鼠标坐标投影的世界坐标中的 2 个点:nearPoint是 3D 中位于相机视锥体近平面上的点,farPoint在视锥体远平面上。
您可以计算一条经过这些点并与您的平面相交的线。

  // compute the matrix to unproject the mouse coords (in homogeneous space)    
  osg::Matrix VP = _camera->getViewMatrix() * _camera->getProjectionMatrix();

  osg::Matrix inverseVP;
  inverseVP.invert(VP);

  // compute world near far
  osg::Vec3 nearPoint(mousePosition.x, mousePosition.x, -1.0f);
  osg::Vec3 farPoint(mousePosition.x, mousePosition.x, 1.0f);
  osg::Vec3 nearPointWorld = nearPoint * inverseVP;
  osg::Vec3 farPointWorld = farPoint * inverseVP;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Openscenegraph 中从 2D 鼠标单击屏幕坐标点计算 3D 点(世界坐标)? 的相关文章

随机推荐