【发布时间】:2014-02-22 05:21:03
【问题描述】:
我正在尝试将 OpenGL 中的 3d 点转换为屏幕上的 2d 点,以渲染我正在编写的小游戏的健康栏。但是,我在检索绘制健康条的位置的 x 坐标时遇到了一些麻烦。基本上,生命值条必须出现在玩家上方,但相对于屏幕必须始终具有相同的宽度/高度。
我调整了从Convert a 3D location to a 2D on-screen point. (XYZ => XY) 接受的答案中找到的代码的 sn-p,现在我有了这个
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);
// int[] screenCoords = new double[4];
// int[] viewport = new int[4];
// double[] modelView = new double[16];
// double[] projection = new double[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(3), (int) screenCoords.get(1) };
}
return null;
}
它似乎可以很好地处理 y 坐标,但是,无论角度如何,x 总是返回 0。
提前非常感谢!
【问题讨论】: