我正在尝试将OpenGL中的3d点转换为屏幕上的2d点,以便为我正在编写的小游戏提供一个健康栏。但是,我在检索绘制健康条的位置的x坐标时遇到了一些问题。基本上,健康栏必须显示在播放器上方,但相对于屏幕而言,必须始终具有相同的宽度/高度。
我调整了从Convert a 3D location to a 2D on-screen point. (XYZ => XY)接受的答案中找到的代码片段,现在我有了以下内容
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。
事先非常感谢!
发布于 2014-02-22 05:35:29
screenCoords.get(3)应该是screenCoords.get(0),因为x位置存储在索引0。实际上,您也只需要screenCoords的容量就可以成为3个浮点数,而不是4个。
https://stackoverflow.com/questions/21950105
复制相似问题