【问题标题】:Java LWJGL OpenGL convert 3d point to 2d pointJava LWJGL OpenGL 将 3d 点转换为 2d 点
【发布时间】: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。

提前非常感谢!

【问题讨论】:

    标签: java opengl lwjgl


    【解决方案1】:

    screenCoords.get(3) 应该是 screenCoords.get(0),因为 x 位置存储在索引 0 处。您实际上也只需要 screenCoords 的容量为 3 个浮点数,而不是 4 个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-16
      • 2014-02-05
      • 1970-01-01
      相关资源
      最近更新 更多