0
我已经实现了三维射线拾取并且有一些准确性问题。当在屏幕的底部或中心进行拾取时,一切都很完美,但屏幕顶部的坐标看起来稍微偏离了一点。所以当我在屏幕顶部附近选择物体时,我经常选择的不是我正在敲击的物体,而是更低的物体。三维射线拾取精度
下面是截图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);
}
感谢您的答复。