-
你需要
-
你们需要解方程组,从第一步得到的3条射线中找到3D中的真实点。
您可以找到的源代码
gluUnProject
here
. 我还在这里提供了我的代码:
public Vector4 Unproject(float x, float y, Matrix4 View)
{
var ndcX = x / Viewport.Width * 2 - 1.0f;
var ndcY = y / Viewport.Height * 2 - 1.0f;
var invVP = Matrix4.Invert(View * ProjectionMatrix);
// We don't z-coordinate of the point, so we choose 0.0f for it.
// We are going to find out it later.
var screenPos = new Vector4(ndcX, -ndcY, 0.0f, 1.0f);
var res = Vector4.Transform(screenPos, invVP);
return res / res.W;
}
Vector3 ComputeRay(Camera camera, Vector2 p)
{
var worldPos = Unproject(p.X, p.Y, camera.View);
var dir = new Vector3(worldPos) - camera.Eye;
return new Ray(camera.Eye, Vector3.Normalize(dir));
}
现在你需要找到三条这样的光线的交点。理论上,只使用两条光线就足够了。这取决于你相机的位置。
如果我们有无限精度浮点运算,并且输入没有噪声,那将是微不足道的。但实际上,您可能需要利用一些简单的数值格式来找到具有适当精度的点。