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));
}
出于某种原因,此代码始终返回所有位置的NaN值。我已经检查了,模型,投影和视图矩阵不是空的,也不是WinZ。该函数也返回true。
立即行动,阅读评论!