转换一个三维位置到2D屏幕上的点。 (XYZ => XY) [英] Convert a 3D location to a 2D on-screen point. (XYZ => XY)
本文介绍了转换一个三维位置到2D屏幕上的点。 (XYZ => XY)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道究竟如何,我可以转换的位置(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;
}
}
这篇关于转换一个三维位置到2D屏幕上的点。 (XYZ => XY)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文