我试图在 2D 屏幕空间上用户选择的点的 3D 空间上放置一个球体。为此,我尝试使用以下技术从 2d 点计算 3d 点,但该技术没有给出正确的解决方案。
mousePosition.x = ((clickPos.clientX - window.left) / control.width) * 2 - 1;
mousePosition.y = -((clickPos.clientY - window.top) / control.height) * 2 + 1;
然后我乘以mousePosition
with 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(使用前将#替换为@)