【问题标题】:How do you convert a 3D position to a 2D point on screen?如何将 3D 位置转换为屏幕上的 2D 点?
【发布时间】:2015-07-02 16:41:04
【问题描述】:

我正在尝试在 3D 环境中渲染 2D 文本,但为此我需要能够将一组 3D 坐标转换为屏幕上的 2D 点。我的相机有一个视图投影矩阵、一个视野和一个 3D 空间中的位置。我尝试渲染的文本在屏幕上有一个 2D 点。

这是我目前所拥有的:

public final Vector2f get2DPointFrom3DPosition(Vector3f position) {//position is the point in 3D space that I'm trying to render the text at
    final Vector3f camPos = this.getTransform().getPos();//where Vector3f == (x, y, z), and is a 3D position in space
    final Matrix4f viewProjection = this.getViewProjection();//where Matrix4f == float[][]
    final double fov = this.getFieldOfView();//usually 70.0f
    float X;
    float Y;
    //complicated math that I can't find with google or figure out
    return new Vector2f(X, Y);//where vector2f is a pixel position on the screen
}

如果我错过了类似的问题或者我有任何不清楚的地方,我提前道歉。我确实找到了这个问题,但它不在 java 中,我找不到正在进行的基础数学:Projecting a 3D point to a 2D screen position issue

【问题讨论】:

  • 你可以试试this tutorial。这是关于将 2D 矢量转换为 3D 坐标,但步骤可以很容易地反转。
  • 谢谢,我试试看。
  • 嗯,也许我遗漏了一些明显的东西,但我不知道如何扭转这个过程,而且我在任何地方都看不到矩阵...brain.exe 已停止工作。大声笑
  • 你用的是哪个数学库?
  • 顶部的图表显示了如何从一个空间到另一个空间 - 你必须按照逆时针方向的箭头。您不需要模型矩阵(您已经有了 3D 位置),viewProjection 可能包含视图和投影矩阵。是否必须进行透视除法取决于您使用的数学库。到视口空间的转换应该很简单。

标签: java 3d 2d lwjgl


【解决方案1】:
2D_X = 3D_X / Z
2D_Y = 3D_Y / Z

从数学上讲,这个计算至少应该让你朝着正确的方向前进。

【讨论】:

    猜你喜欢
    • 2013-11-22
    • 2015-02-09
    • 2012-07-17
    • 2020-02-16
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多