转换一个三维位置到2D屏幕上的点。 (XYZ => XY)位置、屏幕上、XYZ、GT

2023-09-08 10:38:30 作者:忘掉过去从心开始

我想知道究竟如何,我可以转换的位置(XYZ)在屏幕上的一个点(XY)。

I am wondering how exactly I can convert a location (X Y Z) to a point on screen (X Y).

我有一个播放器(可以控制播放器),是谁在(XYZ)坐标和另一名球员是谁也(XYZ)坐标。

I have a player (the player you control) who is in (X Y Z) co-ordinates and another player who is also in (X Y Z) co-ordinates.

我究竟如何可以将其他玩家的XYZ到屏幕上的xy,这样我可以/它使用X Y上面画了他的名字。

How exactly can I convert the other player's X Y Z to X Y on screen so that I can draw a name above him/it using the X Y.

希望是有道理的......

Hope that makes sense...

编辑:

下面是我的gluProject code:

Here is my gluProject code:

IntBuffer viewport = GLAllocation.createDirectIntBuffer(16);
FloatBuffer modelview = GLAllocation.createDirectFloatBuffer(16);
FloatBuffer projection = GLAllocation.createDirectFloatBuffer(16);
FloatBuffer objectCoords = GLAllocation.createDirectFloatBuffer(3);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelview);
GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projection);
GL11.glGetInteger(GL11.GL_VIEWPORT, viewport);
GLU.gluProject(x, y, z, modelview, projection, viewport, objectCoords);
eturn objectCoords;

感谢。

推荐答案

您应该能够对付的是:

public get2DFrom3D(float x, float y, float z)
{
    /*
    FloatBuffer screen_coords = GLAllocation.createDirectFloatBuffer(4);
    IntBuffer viewport = GLAllocation.createDirectIntBuffer(16);
    FloatBuffer modelview = GLAllocation.createDirectFloatBuffer(16);
    FloatBuffer projection = GLAllocation.createDirectFloatBuffer(16);
    */

    double[] screen_coords = new double[4];
    int[] viewport = new int[4];
    double[] modelview = new double[16];
    double[] projection = new double[16];


    GL11.glGetFloat(2982 /*GL_MODELVIEW_MATRIX*/, modelview);
    GL11.glGetFloat(2983 /*GL_PROJECTION_MATRIX*/, projection);
    GL11.glGetInteger(2978 /*GL_VIEWPORT*/, viewport);

    boolean result = GLU.gluProject(x, y, z, modelview, projection, viewport, screen_coords);
    if (result)
    {
        //System.out.printf("Convert [ %6.2f %6.2f %6.2f ] -> Screen [ %4d %4d ]\n", x, y, z, (int)screen_coords[0], (int)(screen_coords[3] - screen_coords[1]));
        System.out.printf("Convert [ %6.2f %6.2f %6.2f ] -> Screen [ %4d %4d ]\n", x, y, z, (int)screen_coords.get(0), (int)(screen_coords.get(3) - screen_coords.get(1)));
        return new Vector2(screen_coords[0], screen_coords[3] - screen_coords[1]);
    }
    else
    {
        System.out.printf("Failed to convert 3D coords to 2D screen coords");
        return null;
    }
}