2015-07-02 133 views
3

我试图在3D环境中渲染2D文本,但要做到这一点,我需要能够将一组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

+0

你可以试试[本教程](http://antongerdelan.net/opengl/raycasting.html)。这是关于将二维矢量转换为三维坐标,但这些步骤可以轻松颠倒。 – javac

+0

谢谢,我会试试看。 –

+0

嗯,也许我错过了一些明显的东西,但我无法弄清楚如何逆转过程,并且我没有在任何地方看到矩阵...... brain.exe已停止工作。大声笑 –

回答

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

此计算应至少设置你在正确的方向methamatically说。