经过两个小时的谷歌搜索(here, here, here, here, and here,以及大量其他我懒得去找的),我以为我终于学会了将 3D 坐标转换为 2D 坐标的理论。但它不起作用。这个想法是将船舶的 3D 坐标转换为屏幕上的 2D 坐标,以呈现控制该船舶的玩家的用户名。
However, the text is rendering in the wrong location:
文本为“测试 || 2DXCooperative || 2DZCooperative”。
这是我的getScreenCoords()
- 将 3D 坐标转换为 2D。
public static int[] getScreenCoords(double x, double y, double z) {
FloatBuffer screenCoords = BufferUtils.createFloatBuffer(4);
IntBuffer viewport = BufferUtils.createIntBuffer(16);
FloatBuffer modelView = BufferUtils.createFloatBuffer(16);
FloatBuffer projection = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelView);
GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projection);
GL11.glGetInteger(GL11.GL_VIEWPORT, viewport);
boolean result = GLU.gluProject((float) x, (float) y, (float) z, modelView, projection, viewport, screenCoords);
if (result) {
return new int[] { (int) screenCoords.get(0), (int) screenCoords.get(1) };
}
return null;
}
screenCoords.get(0) 返回完美的 X 坐标。但是,screenCoords.get(1) 会升高或降低,具体取决于我离船的距离。经过几个小时的调试,我将范围缩小到这一行不正确:
GLU.gluProject((float) x, (float) y, (float) z, modelView, projection, viewport, screenCoords);
但是,我不知道出了什么问题。船的 X 坐标没问题……为什么 Y 坐标不行呢?
根据 BDL 的回答,我向 gluProject() 提供了“错误的矩阵”。但我不明白这是怎么可能的,因为我在渲染我的船后立即调用该方法(这显然是在绘制船的任何矩阵中)。
我就是不明白出了什么问题。
注意:BDL 的答案完全足够,只是它没有解释为什么 Y 坐标不正确。
注意:这个问题过去更长、更模糊。经过几个小时的调试后,我在上面发布了我缩小的问题。