2015-07-02 129 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說。