如何从Openscenegraph中的2D鼠标单击屏幕坐标点计算3D点(世界坐标)? [英] How to calculate 3D point (World Coordinate) from 2D mouse clicked screen coordinate point in Openscenegraph?

查看:167
本文介绍了如何从Openscenegraph中的2D鼠标单击屏幕坐标点计算3D点(世界坐标)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从2D屏幕空间上用户选择的点开始在3D空间上放置一个球体.为此,我尝试使用以下技术从2d点计算3d点,而该技术未提供正确的解决方案.

I was trying to place a sphere on the 3D space from the user selected point on 2d screen space. For this iam trying to calculate 3d point from 2d point using the below technique and this technique not giving the correct solution.

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

然后Iam将 mousePosition MVP矩阵的逆数相乘.但是会得到随机数.

then Iam multiplying the mousePositionwith Inverse of MVP matrix. But getting random number at result.

用于计算MVP矩阵:

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

我该如何进行?谢谢.

推荐答案

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

Under the assumption that the mouse position is normalized in the range [-1, 1] for x and y, the following code will give you 2 points in world coordinates projected from your mouse coords: nearPoint is the point in 3D lying on the camera frustum near plane, farPointon the frustum far plane.
Than you can compute a line passing by these points and intersecting that with your plane.

  // 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;

这篇关于如何从Openscenegraph中的2D鼠标单击屏幕坐标点计算3D点(世界坐标)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆