How do I convert a 3D position to a 2D point on the screen?

I'm trying to render 2D text in a 3D environment, but for that I need to be able to convert a set of 3D coordinates to a 2D point on the screen. My camera has a projection projection matrix, field of view and position in 3D space. The text I am trying to make has a 2D dot on the screen.

Here's what I have so far:

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
}

      

I apologize in advance if I missed a similar question or I didn't understand anything. I found this question, but it is not in java and I cannot find basic math: Projecting a 3D point onto a 2D screen problem

+3


source to share


1 answer


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

      



This calculation should at least put you in the right direction, metamatically speaking.

0


source







All Articles