4

我目前正在尝试研究如何从 JOGL 获取世界坐标 - 目前无论我点击哪里,它都只返回 x = 0.0、y= 0.0 和 z = 0.0。我究竟做错了什么?

 public double[] getMousePosition(int x, int y){

    int viewport[] = new int[4];
    double modelview[] = new double[16];
    double projection[] = new double[16];
    float winX, winY, winZ;
    float posX, posY, posZ;
    double wcoord[] = new double[4];

    gl.glGetDoublev( GL2.GL_MODELVIEW_MATRIX, modelview, 0 );
    gl.glGetDoublev( GL2.GL_PROJECTION_MATRIX, projection, 0 );
    gl.glGetIntegerv( GL2.GL_VIEWPORT, viewport, 0 );

    winX = (float)x;
    winY = (float)viewport[3] - (float)y;

    float[] depth = new float[1];
    // gl.glReadPixels(winX, winY, 1, 1, gl.GL_DEPTH_COMPONENT, GL2.GL_FLOAT, depth);

    boolean test =  glu.gluUnProject( winX, winY, 0.0, modelview, 0, projection, 0, viewport, 0, wcoord, 0);

    System.out.println("x: " + wcoord[0] +"y: "+wcoord[1]+" worked? "+test);
    System.out.println(modelview[0]);
    return wcoord;
}

编辑 :: 忘了提到我注意到 glu.gluUnproject 返回一个布尔值,所以我将它分配给一个名为 test 的布尔值,它返回 false。

EDIT2 :: 我添加了另一个调试语句 - System.out.println(modelview[0]); 并且还返回 0.0

感谢您的帮助

詹姆士

4

3 回答 3

0

一个可能的原因:您同时获取模型视图矩阵和投影矩阵?

gluUnProject() 在我这边按预期工作,这就是我正在做的事情:

1)仅当视口大小和/或“相机”更新时:获取视口和投影矩阵。
IE 你应该在一个gl.glMatrixMode(GL.GL_PROJECTION)

2)每个“帧”:抓取模型视图矩阵。
IE 你应该在一个 gl.glMatrixMode(GL.GL_MODELVIEW)

3) 然后只:将 3 数组传递给 gluUnProject()...

于 2009-11-16T09:21:56.927 回答
0

如果您尝试查找的 3D 点位于近剪裁平面之后(通常也在 camara 位置之后),则 gluUnproject() 将始终为窗口坐标返回 TRUE 和 (0,0,0)。在查询矩阵和视口信息后,您可能必须手动进行计算。

于 2009-11-18T00:46:06.873 回答
0

I've finally found the reason why this was happening, it turns out that I was pulling gl.glGetIntegerv( GL2.GL_VIEWPORT, viewport, 0 ); in a method before this one. It seems that if you do this the next method to call it will be returned nothing but a set of zeros!

Thanks for your suggestions

于 2009-11-18T07:12:40.153 回答