Convert a 3D location to a 2D on-screen point. (XYZ => XY)

家住魔仙堡 提交于 2019-12-24 02:08:08

问题


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

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.

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...

Edit:

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;

Thanks.


回答1:


You should be able to deal with that:

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;
    }
}



回答2:


To find the screen coordinate corresponding to a point in the world, apply the camera transform to the world point. You don't say what 3D framework you are using, but almost certainly there is an API call you can make to perform this transformation. For example, in the Unity3D framework, you call Camera.WorldToScreenPoint, and in OpenGL, you call gluProject.

However, this isn't necessarily the best way to render "head up" displays like character names. Another technique is to create billboards, which are objects in the world that always face the camera. Again, your 3D framework probably has support for this: for example, in Unity3D you might call object.transform.LookAt(camera.transform).




回答3:


a method to plot 3d points in 2d exists. you will have to define a focal length parameter. the following code does it correctly

int 3D_2Dx(int pt[3]){
int focallength = ;//your focal length parameter i.e. 30,45,60,....
float multiplier =  focallength/(focallength+pt[2]) ;// pt[2] is its z co-ordinate
return (pt[0]*multiplier); //got the x co-ordinate
}
int 3D_2Dy(int pt[3]){
int focallength = ;//your focal length parameter i.e. 30,45,60,....
float multiplier =  focallength/(focallength+pt[2]) ;// pt[2] is its z co-ordinate
return (pt[1]*multiplier); //got the y co-ordinate
}

this code will do it perfectly. even I had made 3d graphics in flash 6 which is based totally on 2d.




回答4:


this is a maths, not programming question. the answer depends on where you're looking from and where your 'screen' is sitting in relation to your viewpoint and the player.

Essentially you need to draw a line from the viewpoint to the position in 3D, then calculate the x,y coordinates on a 2D plane (the screen) which intersects the line. - i think!



来源:https://stackoverflow.com/questions/10009771/convert-a-3d-location-to-a-2d-on-screen-point-xyz-xy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!