如何在屏幕上将三维位置转换为二维点



我试图在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中,我找不到正在进行的底层数学:将3D点投影到2D屏幕位置问题

2D_X = 3D_X / Z
2D_Y = 3D_Y / Z

这个计算至少应该让你在美语方面朝着正确的方向前进。

最新更新