0
嘿,我試圖在我的程序中實現3D拾取,並且如果我不從原點移動,它會很好地工作。這是完全準確的。但是如果我將模型矩陣從原點移開(視圖矩陣仍然在0,0,0),拾取矢量仍然從原始位置繪製。它應該仍然從視圖矩陣眼(0,0,0)繪製,但事實並非如此。這裏是我的一些代碼,看看你是否可以找出原因..3D模型矩陣翻譯之後挑選OpenGL ES 2.0
Vector3d near = unProject(x, y, 0, mMVPMatrix, this.width, this.height);
Vector3d far = unProject(x, y, 1, mMVPMatrix, this.width, this.height);
Vector3d pickingRay = far.subtract(near);
//pickingRay.z *= -1;
Vector3d normal = new Vector3d(0,0,1);
if (normal.dot(pickingRay) != 0 && pickingRay.z < 0)
{
float t = (-5f-normal.dot(mCamera.eye))/(normal.dot(pickingRay));
pickingRay = mCamera.eye.add(pickingRay.scale(t));
addObject(pickingRay.x, pickingRay.y, pickingRay.z+.5f, Shape.BOX);
//a line for the picking vector for debugging
PrimProperties a = new PrimProperties(); //new prim properties for size and center
Prim result = null;
result = new Line(a, mCamera.eye, far);//new line object for seeing look at vector
result.createVertices();
objects.add(result);
}
public static Vector3d unProject(
float winx, float winy, float winz,
float[] resultantMatrix,
float width, float height)
{
winy = height-winy;
float[] m = new float[16],
in = new float[4],
out = new float[4];
Matrix.invertM(m, 0, resultantMatrix, 0);
in[0] = (winx/width) * 2 - 1;
in[1] = (winy/height) * 2 - 1;
in[2] = 2 * winz - 1;
in[3] = 1;
Matrix.multiplyMV(out, 0, m, 0, in, 0);
if (out[3]==0)
return null;
out[3] = 1/out[3];
return new Vector3d(out[0] * out[3], out[1] * out[3], out[2] * out[3]);
}
Matrix.translateM(mModelMatrix, 0, this.diffX, this.diffY, 0); //i use this to move the model matrix based on pinch zooming stuff.
任何幫助將不勝感激!謝謝。
我其實已經想通了。這只是投射射線並檢查與設置平面的交叉點。後來我實現了檢查物體中心與光線的距離有多近,如果足夠接近,它會選擇該物體。不過謝謝。 – scssquatch