3d射线拾取精度

时间:2012-07-23 14:35:44

标签: android opengl-es glu picking

我实施了3D射线拾取并且在准确性方面存在一些问题。当在屏幕的底部或中心进行拾取时 - 一切都很完美,但是在屏幕的顶部,坐标似乎移动了一点。因此,当我选择靠近屏幕顶部的物体时,我经常选择的不是我正在敲击的物体而是较低的物体。

以下是屏幕截图http://img838.imageshack.us/img838/9192/device20120723171612.png

我认为问题可能存在于矩阵中,所以我的定义方式如下:

模型视图

GLU.gluLookAt(gl, 
        camera.position.x, camera.position.y, camera.position.z,
        camera.target.x,   camera.target.y,   camera.target.z,
        camera.upAxis.x,   camera.upAxis.y,   camera.upAxis.z);

投影

horizontalCenter = 0f;
verticalCenter = 0f;
shortSideLength = 1.0f;
zNear = 1f;
zFar = 200.0f;
surfaceAspectRatio = (float) w / (float) h;
float n = shortSideLength / 2f;
gl.glFrustumf(horizontalCenter - surfaceAspectRatio*n, 
              horizontalCenter + surfaceAspectRatio*n, 
              verticalCenter - n, verticalCenter + n, zNear, zFar);

我的选择代码:

public static Point3d pickSquare(int size, float step, float tapX, float tapY, float[] modelView, float[] projection)
{

    float[] near = new float[4];
    GLU.gluUnProject(tapX, raypickingViewport[3] - tapY, 0f, modelView, 0, projection, 0, raypickingViewport, 0, near, 0);
    if (near[3] != 0)
    {
        near[0] = near[0] / near[3];
        near[1] = near[1] / near[3];
        near[2] = near[2] / near[3];
    }
    Point3d near3 = new Point3d(near[0], near[1], near[2]);

    float[] far = new float[4];
    GLU.gluUnProject(tapX, raypickingViewport[3] - tapY, 1f, modelView, 0, projection, 0, raypickingViewport, 0, far, 0);
    if (far[3] != 0)
    {
        far[0] = far[0] / far[3];
        far[1] = far[1] / far[3];
        far[2] = far[2] / far[3];
    }
    Point3d far3 = new Point3d(far[0], far[1], far[2]);

    // here I'm searching for intersection with x0z plane using equation 
    // (x-x1)/(x2-x1) = (y-y1)/(y2-y1) = (z-z1)/(z2-z1)
    // My y is 0 so I can find x and y coords.

    float intersectY = (-near3.y) / (far3.y - near3.y);
    float pickX = intersectY * (far3.x - near3.x) + near3.x;
    float pickZ = intersectY * (far3.z - near3.z) + near3.z;
    return  new Point3d(pickX, 0f, pickZ);   
}

感谢您的回复。

1 个答案:

答案 0 :(得分:0)

问题出在android状态栏中。在480x800屏幕上,它的高度是38像素,所以我在这一行中添加了拾取方法:

tapY-=38;

现在一切都很好