简体   繁体   English

模型矩阵转换后的3D选择OpenGL ES 2.0

[英]3D Picking OpenGL ES 2.0 after model matrix translation

Hey all I'm trying to implement 3D picking into my program, and it works perfectly if I don't move from the origin. 嘿,我正在尝试在程序中实现3D拾取,如果我不从原点移动,它会完美地工作。 It is perfectly accurate. 这是完全准确的。 But if I move the model matrix away from the origin (the viewmatrix eye is still at 0,0,0) the picking vectors are still drawn from the original location. 但是,如果我将模型矩阵移离原点(视矩阵眼仍在0,0,0处),则拾取矢量仍将从原始位置绘制。 It should still be drawing from the view matrix eye (0,0,0) but it isn't. 它仍然应该从视图矩阵眼睛(0,0,0)绘制,但不是。 Here's some of my code to see if you can find out why.. 这是我的一些代码,以查看是否可以找到原因。

        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.

Any help would be greatly appreciated! 任何帮助将不胜感激! Thanks. 谢谢。

I wonder which algorithm you have implemented. 我想知道您实现了哪种算法。 Is it a ray casting approach to the problem? 这是射线投射的方法吗?

I didn't focus much on the code itself but this looks a way too simple implementation to be a fully operational ray casting solution. 我并没有太在意代码本身,但是这看起来太简单了,无法实现完全可操作的光线投射解决方案。

In my humble experience, i would like to suggest you, depending on the complexity of your final project (which I don't know), to adopt a color picking solution. 以我不起眼的经验,我建议您根据最终项目的复杂性(我不知道),建议您采用颜色选择解决方案。

This solution is usually the most flexible and the easiest to be implemented. 该解决方案通常是最灵活,最容易实现的。

It consist in the rendering of the objects in your scene with unique flat colors (usually you disable lighting as well in your shaders) to a backbuffer...a texture, then you acquire the coordinates of the click (touch) and you read the color of the pixel in that specific coordinates. 它包括将场景中的对象以唯一的平面颜色(通常在着色器中也禁用照明)渲染到后缓冲区...纹理,然后获取单击(触摸)的坐标并读取该特定坐标中像素的颜色。

Having the color of the pixel and the tables of the colors of the different objects you rendered, makes possible for you to understand what the user clicked from a logical perspective. 有了像素的颜色以及所渲染的不同对象的颜色的表格,就可以从逻辑的角度了解用户单击的内容。

There are other approaches to the object picking problem, this is probably universally recognized as the fastest one. 还有其他方法可以解决对象拾取问题,这可能是公认的最快方法。

Cheers Maurizio 干杯毛里齐奥

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM