0

嘿,我正在尝试在我的程序中实现 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.

任何帮助将不胜感激!谢谢。

4

1 回答 1

0

我想知道您实现了哪种算法。这是解决问题的光线投射方法吗?

我并没有过多关注代码本身,但这看起来太简单了,无法成为一个完全可操作的光线投射解决方案。

根据我的拙见,我想建议您根据最终项目的复杂性(我不知道)采用颜色选择解决方案。

这种解决方案通常是最灵活和最容易实现的。

它包括将场景中的对象以独特的平面颜色(通常在着色器中禁用照明)渲染到后缓冲区...纹理,然后获取单击(触摸)的坐标并阅读该特定坐标中像素的颜色。

拥有像素的颜色和渲染的不同对象的颜色表,可以让您从逻辑角度了解用户单击的内容。

对象拾取问题还有其他方法,这可能是公认的最快的方法。

干杯毛里齐奥

于 2012-06-27T13:39:47.257 回答