嘿所有我正在尝试在我的程序中实现3D拾取,如果我不从原点移动它就会完美地工作。这完全准确。但是如果我将模型矩阵从原点移开(视图矩阵眼仍然是0,0,0),仍然会从原始位置绘制拾取矢量。它应该仍然是从视图矩阵眼(0,0,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.
任何帮助将不胜感激!感谢。
答案 0 :(得分:0)
我想知道你实施了哪种算法。它是解决这个问题的光线投射方法吗?
我没有太多关注代码本身,但这看起来太简单了,无法成为完全可操作的光线投射解决方案。
在我谦逊的经历中,我建议您,根据最终项目(我不知道)的复杂程度,采用颜色选择解决方案。
此解决方案通常是最灵活,最容易实施的。
它包括渲染场景中的对象,使用独特的平面颜色(通常在着色器中禁用光照)到后备缓冲区...纹理,然后获取点击(触摸)的坐标和你读取特定坐标中像素的颜色。
使用像素的颜色和渲染的不同对象的颜色表,可以让您了解用户从逻辑角度点击的内容。
对象拾取问题还有其他方法,这可能被普遍认为是最快的。
干杯 莫里吉奥