我已经实现了 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);
}
感谢您的答复。