将 3D 位置转换为 2D 屏幕点.(XYZ => XY) [英] Convert a 3D location to a 2D on-screen point. (XYZ => XY)

查看:29
本文介绍了将 3D 位置转换为 2D 屏幕点.(XYZ => XY)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将位置 (X Y Z) 转换为屏幕上的点 (X Y).

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

我有一个玩家(你控制的玩家)在 (X Y Z) 坐标中,另一个玩家也在 (XY Z) 坐标中.

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.

我如何在屏幕上将其他玩家的 X Y Z 转换为 X Y,以便我可以使用 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 代码:

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 ]
", 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 ]
", 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;
    }
}

这篇关于将 3D 位置转换为 2D 屏幕点.(XYZ => XY)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆