LWJGL Ray Picking(gluUnProject)

时间:2014-01-25 01:43:38

标签: java lwjgl ray-picking

所以我一直在浏览教程(以及本网站上的问题)并找不到解决我问题的方法。这是我想要实现的当前代码:

private void pick() {
    float[] matModelView = new float[16], matProjView = new float[16];
    int[] view = new int[16];
    float mouseX = width / 2;
    float mouseY = height / 2;
    Vector3f start = new Vector3f();
    Vector3f end = new Vector3f();

    FloatBuffer modelBuffer = compileBuffer(matModelView);
    FloatBuffer projBuffer = compileBuffer(matProjView);
    FloatBuffer startBuffer = compileBuffer(new float[]{start.x, start.y, start.z, 1});
    FloatBuffer endBuffer = compileBuffer(new float[]{end.x, end.y, end.z, 1});
    IntBuffer viewBuffer = compileBuffer(view);

    glGetFloat(GL_MODELVIEW_MATRIX, modelBuffer);
    glGetFloat(GL_PROJECTION_MATRIX, projBuffer);
    glGetInteger(GL_VIEWPORT, viewBuffer);

    gluUnProject(mouseX, mouseY, 0.0f, modelBuffer, projBuffer, viewBuffer, startBuffer);
    gluUnProject(mouseX, mouseX, 1.0f, modelBuffer, projBuffer, viewBuffer, endBuffer);

    start = new Vector3f(startBuffer.get(0), startBuffer.get(1), startBuffer.get(2));
    end = new Vector3f(endBuffer.get(0), endBuffer.get(1), endBuffer.get(2));

    picks.add(new Vector3f[]{start, end});

    System.out.println("Mouse Coords: " + mouseX + ", " + mouseY);
    System.out.println("Position: " + position.x + ", " + position.y + ", " + position.z);
    System.out.println("Rotation: " + rotation.x + ", " + rotation.y + ", " + rotation.z);
    System.out.println("Near Plane: " + start.x + ", " + start.y + ", " + start.z);
    System.out.println("Far Plane: " + end.x + ", " + end.y + ", " + end.z);
}

我有鼠标X和Y设置我的方式,因为我的鼠标被抓住了。这是一张图片和一些输出。 (我无法解释这个问题)

Mouse Coords: 400.0, 350.0

位置:0.0,0.0,0.0 旋转:0.0,0.0,0.0 近平面:0.0,0.0,-0.1 远平面:0.0,4.2315264,-99.99771

因此,对于X:0 Y:0 Z:0的旋转,Y坐标的预期输出应与输入相同。它更高。这是该输出的图片。 Odd Values

任何人都可以给我一些暗示或解释为什么会发生这种情况吗?

编辑: Facepalms暴力:我在第二个gluUnProject的Y参数中推送mouseX

0 个答案:

没有答案