0
public Vector3f getClickPos(int x, int y)
{
    IntBuffer viewport = BufferUtils.createIntBuffer(16);
    FloatBuffer modelview = BufferUtils.createFloatBuffer(16);
    FloatBuffer projection = BufferUtils.createFloatBuffer(16);
    float winX, winY, winZ;

    GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelview);
    GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projection);
    GL11.glGetInteger(GL11.GL_PROJECTION_MATRIX, viewport);

    winX = (float)x;
    winY = (float)y;

    FloatBuffer winZBuffer = BufferUtils.createFloatBuffer(1);
    GL11.glReadPixels( (int)winX, (int)winY, 1, 1, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, winZBuffer);
    winZ = winZBuffer.get(0);

    FloatBuffer position = BufferUtils.createFloatBuffer(3);
    GLU.gluUnProject( winX, winY, winZ, modelview, projection, viewport, position);

    return new Vector3f(position.get(0), position.get(1), position.get(2));
}

For some reason, this code always returns NaN values for all of the positions. I've checked, and the model, projection and view matrixes are not empty, nor is WinZ. The function also returns true.

Working now, read comment!

Inqy
  • 125
  • 1
  • 8
  • 1
    You are using the wrong token to query your viewport. It should be `GL11.glGetInteger (GL11.GL_VIEWPORT, viewport);` – Andon M. Coleman Apr 19 '14 at 01:49
  • @AndonM.Coleman Oh my god, can't remember the last moment i felt so stupid, THANK YOU. Time to go to sleep. – Inqy Apr 19 '14 at 01:58

0 Answers0