如何将3D坐标映射到2D平面

时间:2016-12-04 20:10:37

标签: java c++ opengl math 3d

我正在编写一个OpenGL应用程序,其中有一个矩形放在3D对象上。 3D对象可以四处移动并旋转,矩形跟随这些移动。

我想要做的是将鼠标指向矩形,以便在矩形上的那个点上出现一个点,我希望该点跟随它作为"持有的3D对象&# 34;矩形移动。

我知道如何在平面上找到交叉点,我知道如何找到接触点的世界坐标。我需要的是一种世界坐标转换为矩形的2D局部坐标系的方法。

例如,假设我在3D世界中有这样的平面,具有给定的方向(抱歉,我无法正确绘制):

enter image description here

中心的黑点是飞机的原点,而蓝点是我想要的。点附近的数字是它们的世界坐标;在这个例子中,Z轴"出现"屏幕。

我想在平面的局部坐标系中映射蓝点的坐标,如下所示: enter image description here

我知道这不应该变得困难,但我根本找不到办法。任何提示都将不胜感激!

2 个答案:

答案 0 :(得分:0)

您可能已经使用变换矩阵来渲染矩形。如果您有点的3D位置,只需使用逆变换矩阵对其进行变换,然后在矩形的空间中获得3D位置。如果本地系统与矩形对齐,则其中一个坐标应始终为零,您可以将其删除。

答案 1 :(得分:0)

我在这篇文章中找到了我需要的内容here

基本上我的解决方案是将点投影到矩形局部坐标系的x和y轴上。

在伪代码中,它是这样的:

// I compute the direction that go from the origin of the rectangle to its x axis, in world coordinates, and take the norm; same thing for the y axis
var xDir = Norm(mRectangle.LocalToWorld([1, 0, 0]) - mRectangle.LocalToWorld([0, 0, 0]));
var yDir = Norm(mRectangle.LocalToWorld([0, 1, 0]) - mRectangle.LocalToWorld([0, 0, 0]));

// I compute the dot product
mLocalPosition.x = Dot(xDir, (contactPoint - mRectangle.LocalToWorld([0, 0, 0])));
mLocalPosition.y = Dot(yDir, (contactPoint - mRectangle.LocalToWorld([0, 0, 0])));

// I can now set the position of the point converting the local coordinates found to world coordinates
mPoint.SetPosition(mRectangle.LocalToWorld([mLocalPosition.x, mLocalPosition.y, 0]));